From 754cc6a66510ea32f57bf0b4892173a3932a14b5 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Sun, 5 Nov 2017 22:22:49 -0300 Subject: [PATCH] Fix diff execution --- cmd/nginx/flags.go | 5 ++--- pkg/ingress/controller/nginx.go | 13 +++++-------- pkg/ingress/status/status.go | 17 ++++++++--------- pkg/k8s/main.go | 21 +++++++++++---------- pkg/k8s/main_test.go | 2 +- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/cmd/nginx/flags.go b/cmd/nginx/flags.go index d65e3bc82..0bc386f4b 100644 --- a/cmd/nginx/flags.go +++ b/cmd/nginx/flags.go @@ -58,9 +58,8 @@ func parseFlags() (bool, *controller.Configuration, error) { `Name of the ConfigMap that contains the custom configuration to use`) publishSvc = flags.String("publish-service", "", - `Service fronting the ingress controllers. Takes the form - namespace/name. The controller will set the endpoint records on the - ingress objects to reflect those on the service.`) + `Service fronting the ingress controllers. Takes the form namespace/name. + The controller will set the endpoint records on the ingress objects to reflect those on the service.`) tcpConfigMapName = flags.String("tcp-services-configmap", "", `Name of the ConfigMap that contains the definition of the TCP services to expose. diff --git a/pkg/ingress/controller/nginx.go b/pkg/ingress/controller/nginx.go index 68f9b6636..0dba82b1e 100644 --- a/pkg/ingress/controller/nginx.go +++ b/pkg/ingress/controller/nginx.go @@ -121,13 +121,13 @@ func NewNGINXController(config *Configuration) *NGINXController { if config.UpdateStatus { n.syncStatus = status.NewStatusSyncer(status.Config{ Client: config.Client, - PublishService: n.cfg.PublishService, + PublishService: config.PublishService, IngressLister: n.listers.Ingress, ElectionID: config.ElectionID, IngressClass: config.IngressClass, DefaultIngressClass: config.DefaultIngressClass, UpdateStatusOnShutdown: config.UpdateStatusOnShutdown, - UseNodeInternalIP: n.cfg.UseNodeInternalIP, + UseNodeInternalIP: config.UseNodeInternalIP, }) } else { glog.Warning("Update of ingress status is disabled (flag --update-status=false was specified)") @@ -324,8 +324,7 @@ func (n *NGINXController) Stop() error { // Wait for the Nginx process disappear timer := time.NewTicker(time.Second * 1) - for t := range timer.C { - glog.V(3).Infof("tick at", t) + for _ = range timer.C { if !process.IsNginxRunning() { glog.Info("NGINX process has stopped") timer.Stop() @@ -632,10 +631,8 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error { return err } - diffOutput, err := exec.Command("diff", "-u", cfgPath, tmpfile.Name()).CombinedOutput() - if err != nil { - return err - } + // executing diff can return exit code != 0 + diffOutput, _ := exec.Command("diff", "-u", cfgPath, tmpfile.Name()).CombinedOutput() glog.Infof("NGINX configuration diff\n") glog.Infof("%v\n", string(diffOutput)) diff --git a/pkg/ingress/status/status.go b/pkg/ingress/status/status.go index 9be568b09..3c3bb7d51 100644 --- a/pkg/ingress/status/status.go +++ b/pkg/ingress/status/status.go @@ -234,6 +234,8 @@ func NewStatusSyncer(config Config) Sync { // runningAddresses returns a list of IP addresses and/or FQDN where the // ingress controller is currently running func (s *statusSync) runningAddresses() ([]string, error) { + addrs := []string{} + if s.PublishService != "" { ns, name, _ := k8s.ParseNameNS(s.PublishService) svc, err := s.Client.CoreV1().Services(ns).Get(name, metav1.GetOptions{}) @@ -241,7 +243,6 @@ func (s *statusSync) runningAddresses() ([]string, error) { return nil, err } - addrs := []string{} for _, ip := range svc.Status.LoadBalancer.Ingress { if ip.IP == "" { addrs = append(addrs, ip.Hostname) @@ -251,7 +252,6 @@ func (s *statusSync) runningAddresses() ([]string, error) { } addrs = append(addrs, svc.Spec.ExternalIPs...) - return addrs, nil } @@ -263,13 +263,13 @@ func (s *statusSync) runningAddresses() ([]string, error) { return nil, err } - addrs := []string{} for _, pod := range pods.Items { - name := k8s.GetNodeIP(s.Client, pod.Spec.NodeName, s.UseNodeInternalIP) + name := k8s.GetNodeIPOrName(s.Client, pod.Spec.NodeName, s.UseNodeInternalIP) if !sliceutils.StringInSlice(name, addrs) { addrs = append(addrs, name) } } + return addrs, nil } @@ -332,13 +332,12 @@ func runUpdate(ing *extensions.Ingress, status []apiv1.LoadBalancerIngress, return nil, nil } - addrs := status - sort.SliceStable(addrs, lessLoadBalancerIngress(addrs)) + sort.SliceStable(status, lessLoadBalancerIngress(status)) curIPs := ing.Status.LoadBalancer.Ingress sort.SliceStable(curIPs, lessLoadBalancerIngress(curIPs)) - if ingressSliceEqual(addrs, curIPs) { + if ingressSliceEqual(status, curIPs) { glog.V(3).Infof("skipping update of Ingress %v/%v (no change)", ing.Namespace, ing.Name) return true, nil } @@ -350,8 +349,8 @@ func runUpdate(ing *extensions.Ingress, status []apiv1.LoadBalancerIngress, return nil, errors.Wrap(err, fmt.Sprintf("unexpected error searching Ingress %v/%v", ing.Namespace, ing.Name)) } - glog.Infof("updating Ingress %v/%v status to %v", currIng.Namespace, currIng.Name, addrs) - currIng.Status.LoadBalancer.Ingress = addrs + glog.Infof("updating Ingress %v/%v status to %v", currIng.Namespace, currIng.Name, status) + currIng.Status.LoadBalancer.Ingress = status _, err = ingClient.UpdateStatus(currIng) if err != nil { glog.Warningf("error updating ingress rule: %v", err) diff --git a/pkg/k8s/main.go b/pkg/k8s/main.go index 3a74e0fc9..af3da11bc 100644 --- a/pkg/k8s/main.go +++ b/pkg/k8s/main.go @@ -36,26 +36,27 @@ func ParseNameNS(input string) (string, string, error) { return nsName[0], nsName[1], nil } -// GetNodeIP returns the IP address of a node in the cluster -func GetNodeIP(kubeClient clientset.Interface, name string, useInternalIP bool) string { +// GetNodeIPOrName returns the IP address or the name of a node in the cluster +func GetNodeIPOrName(kubeClient clientset.Interface, name string, useInternalIP bool) string { node, err := kubeClient.Core().Nodes().Get(name, metav1.GetOptions{}) if err != nil { return "" } - for _, address := range node.Status.Addresses { - if useInternalIP { + if useInternalIP { + for _, address := range node.Status.Addresses { if address.Type == apiv1.NodeInternalIP { if address.Address != "" { return address.Address } } - continue } - - if address.Type == apiv1.NodeExternalIP { - if address.Address != "" { - return address.Address + } else { + for _, address := range node.Status.Addresses { + if address.Type == apiv1.NodeExternalIP { + if address.Address != "" { + return address.Address + } } } } @@ -91,7 +92,7 @@ func GetPodDetails(kubeClient clientset.Interface) (*PodInfo, error) { return &PodInfo{ Name: podName, Namespace: podNs, - NodeIP: GetNodeIP(kubeClient, pod.Spec.NodeName, true), + NodeIP: GetNodeIPOrName(kubeClient, pod.Spec.NodeName, true), Labels: pod.GetLabels(), }, nil } diff --git a/pkg/k8s/main_test.go b/pkg/k8s/main_test.go index 2b664f3a4..f724f5cee 100644 --- a/pkg/k8s/main_test.go +++ b/pkg/k8s/main_test.go @@ -164,7 +164,7 @@ func TestGetNodeIP(t *testing.T) { } for _, fk := range fKNodes { - address := GetNodeIP(fk.cs, fk.n, fk.i) + address := GetNodeIPOrName(fk.cs, fk.n, fk.i) if address != fk.ea { t.Errorf("expected %s, but returned %s", fk.ea, address) }