Merge pull request #3505 from Shopify/watch-pod-lua

Update lua configuration_data when number of controller pod change
This commit is contained in:
Kubernetes Prow Robot 2018-12-17 00:10:30 -08:00 committed by GitHub
commit ee3a8fe581
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 113 additions and 19 deletions

View file

@ -163,6 +163,7 @@ func (n *NGINXController) syncIngress(interface{}) error {
UDPEndpoints: n.getStreamServices(n.cfg.UDPConfigMapName, apiv1.ProtocolUDP),
PassthroughBackends: passUpstreams,
BackendConfigChecksum: n.store.GetBackendConfiguration().Checksum,
ControllerPodsCount: n.store.GetRunningControllerPodsCount(),
}
if n.runningConfig.Equal(pcfg) {

View file

@ -754,6 +754,8 @@ func (n *NGINXController) IsDynamicConfigurationEnough(pcfg *ingress.Configurati
copyOfRunningConfig.Backends = []*ingress.Backend{}
copyOfPcfg.Backends = []*ingress.Backend{}
copyOfRunningConfig.ControllerPodsCount = 0
copyOfPcfg.ControllerPodsCount = 0
if n.cfg.DynamicCertificatesEnabled {
clearCertificates(&copyOfRunningConfig)
@ -827,6 +829,14 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif
return err
}
url = fmt.Sprintf("http://localhost:%d/configuration/general", port)
err = post(url, &ingress.GeneralConfig{
ControllerPodsCount: pcfg.ControllerPodsCount,
})
if err != nil {
return err
}
if isDynamicCertificatesEnabled {
err = configureCertificates(pcfg, port)
if err != nil {

View file

@ -205,8 +205,9 @@ func TestConfigureDynamically(t *testing.T) {
}}
commonConfig := &ingress.Configuration{
Backends: backends,
Servers: servers,
Backends: backends,
Servers: servers,
ControllerPodsCount: 2,
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -221,12 +222,26 @@ func TestConfigureDynamically(t *testing.T) {
t.Fatal(err)
}
body := string(b)
if strings.Contains(body, "target") {
t.Errorf("unexpected target reference in JSON content: %v", body)
}
if !strings.Contains(body, "service") {
t.Errorf("service reference should be present in JSON content: %v", body)
switch r.URL.Path {
case "/configuration/backends":
{
if strings.Contains(body, "target") {
t.Errorf("unexpected target reference in JSON content: %v", body)
}
if !strings.Contains(body, "service") {
t.Errorf("service reference should be present in JSON content: %v", body)
}
}
case "/configuration/general":
{
if !strings.Contains(body, "controllerPodsCount") {
t.Errorf("controllerPodsCount should be present in JSON content: %v", body)
}
}
default:
t.Errorf("unknown request to %s", r.URL.Path)
}
}))

View file

@ -76,8 +76,8 @@ type Storer interface {
// ListIngresses returns a list of all Ingresses in the store.
ListIngresses() []*ingress.Ingress
// ListControllerPods returns a list of ingress-nginx controller Pods.
ListControllerPods() []*corev1.Pod
// GetRunningControllerPodsCount returns the number of Running ingress-nginx controller Pods.
GetRunningControllerPodsCount() int
// GetLocalSSLCert returns the local copy of a SSLCert
GetLocalSSLCert(name string) (*ingress.SSLCert, error)
@ -288,12 +288,10 @@ func New(checkOCSP bool,
store.informers.Pod = cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (k8sruntime.Object, error) {
options.LabelSelector = labelSelector.String()
return client.CoreV1().Pods(store.pod.Namespace).List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.LabelSelector = labelSelector.String()
return client.CoreV1().Pods(store.pod.Namespace).Watch(options)
},
@ -832,9 +830,9 @@ func (s *k8sStore) Run(stopCh chan struct{}) {
}
}
// ListControllerPods returns a list of ingress-nginx controller Pods
func (s *k8sStore) ListControllerPods() []*corev1.Pod {
var pods []*corev1.Pod
// GetRunningControllerPodsCount returns the number of Running ingress-nginx controller Pods
func (s k8sStore) GetRunningControllerPodsCount() int {
count := 0
for _, i := range s.listers.Pod.List() {
pod := i.(*corev1.Pod)
@ -843,8 +841,8 @@ func (s *k8sStore) ListControllerPods() []*corev1.Pod {
continue
}
pods = append(pods, pod)
count++
}
return pods
return count
}

View file

@ -1062,7 +1062,7 @@ func TestWriteSSLSessionTicketKey(t *testing.T) {
}
}
func TestListControllerPods(t *testing.T) {
func TestGetRunningControllerPodsCount(t *testing.T) {
os.Setenv("POD_NAMESPACE", "testns")
os.Setenv("POD_NAME", "ingress-1")
@ -1117,8 +1117,8 @@ func TestListControllerPods(t *testing.T) {
}
s.listers.Pod.Add(pod)
pods := s.ListControllerPods()
if s := len(pods); s != 2 {
podsCount := s.GetRunningControllerPodsCount()
if podsCount != 2 {
t.Errorf("Expected 1 controller Pods but got %v", s)
}
}

View file

@ -71,6 +71,9 @@ type Configuration struct {
// ConfigurationChecksum contains the particular checksum of a Configuration object
ConfigurationChecksum string `json:"configurationChecksum,omitempty"`
// ControllerPodsCount contains the list of running ingress controller Pod(s)
ControllerPodsCount int `json:"controllerPodsCount,omitempty"`
}
// Backend describes one or more remote server/s (endpoints) associated with a service
@ -338,3 +341,8 @@ type Ingress struct {
extensions.Ingress
ParsedAnnotations *annotations.Ingress
}
// GeneralConfig holds the definition of lua general configuration data
type GeneralConfig struct {
ControllerPodsCount int `json:"controllerPodsCount"`
}

View file

@ -105,6 +105,10 @@ func (c1 *Configuration) Equal(c2 *Configuration) bool {
return false
}
if c1.ControllerPodsCount != c2.ControllerPodsCount {
return false
}
return true
}