From 7fda959a45f5cb16056f100af8619738393f7a5d Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Sun, 17 Sep 2017 13:34:29 -0300 Subject: [PATCH] Cleanup --- core/pkg/ingress/controller/controller.go | 10 ++ core/pkg/ingress/controller/launch.go | 21 ++-- core/pkg/k8s/main.go | 42 -------- core/pkg/k8s/main_test.go | 118 ---------------------- 4 files changed, 25 insertions(+), 166 deletions(-) diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index b08469a171..694f469a70 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -381,6 +381,16 @@ func (ic GenericController) GetDefaultBackend() defaults.Backend { return ic.cfg.Backend.BackendDefaults() } +// GetPublishService returns the configured service used to set ingress status +func (ic GenericController) GetPublishService() *api.Service { + s, err := ic.GetService(ic.cfg.PublishService) + if err != nil { + return nil + } + + return s +} + // GetRecorder returns the event recorder func (ic GenericController) GetRecorder() record.EventRecorder { return ic.recorder diff --git a/core/pkg/ingress/controller/launch.go b/core/pkg/ingress/controller/launch.go index fd2403f3a0..153036435c 100644 --- a/core/pkg/ingress/controller/launch.go +++ b/core/pkg/ingress/controller/launch.go @@ -16,6 +16,7 @@ import ( "github.com/spf13/pflag" api "k8s.io/api/core/v1" + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apiserver/pkg/server/healthz" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -124,7 +125,12 @@ func NewIngressController(backend ingress.Controller) *GenericController { handleFatalInitError(err) } - _, err = k8s.IsValidService(kubeClient, *defaultSvc) + ns, name, err := k8s.ParseNameNS(*defaultSvc) + if err != nil { + glog.Fatalf("invalid format for service %v: %v", *defaultSvc, err) + } + + _, err = kubeClient.Core().Services(ns).Get(name, meta_v1.GetOptions{}) if err != nil { if strings.Contains(err.Error(), "cannot get services in the namespace") { glog.Fatalf("✖ It seems the cluster it is running with Authorization enabled (like RBAC) and there is no permissions for the ingress controller. Please check the configuration") @@ -134,9 +140,14 @@ func NewIngressController(backend ingress.Controller) *GenericController { glog.Infof("validated %v as the default backend", *defaultSvc) if *publishSvc != "" { - svc, err := k8s.IsValidService(kubeClient, *publishSvc) + ns, name, err := k8s.ParseNameNS(*publishSvc) + if err != nil { + glog.Fatalf("invalid service format: %v", err) + } + + svc, err := kubeClient.CoreV1().Services(ns).Get(name, meta_v1.GetOptions{}) if err != nil { - glog.Fatalf("no service with name %v found: %v", *publishSvc, err) + glog.Fatalf("unexpected error getting information about service %v: %v", *publishSvc, err) } if len(svc.Status.LoadBalancer.Ingress) == 0 { @@ -152,9 +163,7 @@ func NewIngressController(backend ingress.Controller) *GenericController { } if *watchNamespace != "" { - - _, err = k8s.IsValidNamespace(kubeClient, *watchNamespace) - + _, err = kubeClient.CoreV1().Namespaces().Get(*watchNamespace, meta_v1.GetOptions{}) if err != nil { glog.Fatalf("no watchNamespace with name %v found: %v", *watchNamespace, err) } diff --git a/core/pkg/k8s/main.go b/core/pkg/k8s/main.go index 82a1ecdcc2..42f90a6b4c 100644 --- a/core/pkg/k8s/main.go +++ b/core/pkg/k8s/main.go @@ -26,48 +26,6 @@ import ( clientset "k8s.io/client-go/kubernetes" ) -// IsValidService checks if exists a service with the specified name -func IsValidService(kubeClient clientset.Interface, name string) (*api.Service, error) { - ns, name, err := ParseNameNS(name) - if err != nil { - return nil, err - } - return kubeClient.Core().Services(ns).Get(name, meta_v1.GetOptions{}) -} - -// IsValidConfigMap check if exists a configmap with the specified name -func IsValidConfigMap(kubeClient clientset.Interface, fullName string) (*api.ConfigMap, error) { - - ns, name, err := ParseNameNS(fullName) - - if err != nil { - return nil, err - } - - configMap, err := kubeClient.Core().ConfigMaps(ns).Get(name, meta_v1.GetOptions{}) - - if err != nil { - return nil, fmt.Errorf("configmap not found: %v", err) - } - - return configMap, nil - -} - -// IsValidNamespace chck if exists a namespace with the specified name -func IsValidNamespace(kubeClient clientset.Interface, name string) (*api.Namespace, error) { - return kubeClient.Core().Namespaces().Get(name, meta_v1.GetOptions{}) -} - -// IsValidSecret checks if exists a secret with the specified name -func IsValidSecret(kubeClient clientset.Interface, name string) (*api.Secret, error) { - ns, name, err := ParseNameNS(name) - if err != nil { - return nil, err - } - return kubeClient.Core().Secrets(ns).Get(name, meta_v1.GetOptions{}) -} - // ParseNameNS parses a string searching a namespace and name func ParseNameNS(input string) (string, string, error) { nsName := strings.Split(input, "/") diff --git a/core/pkg/k8s/main_test.go b/core/pkg/k8s/main_test.go index 820b1216ee..7f0b1e1f42 100644 --- a/core/pkg/k8s/main_test.go +++ b/core/pkg/k8s/main_test.go @@ -56,124 +56,6 @@ func TestParseNameNS(t *testing.T) { } } -func TestIsValidService(t *testing.T) { - fk := testclient.NewSimpleClientset(&api.Service{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: api.NamespaceDefault, - Name: "demo", - }, - }) - - _, err := IsValidService(fk, "") - if err == nil { - t.Errorf("expected error but returned nil") - } - s, err := IsValidService(fk, "default/demo") - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if s == nil { - t.Errorf("expected a Service but returned nil") - } - - fk = testclient.NewSimpleClientset() - s, err = IsValidService(fk, "default/demo") - if err == nil { - t.Errorf("expected an error but returned nil") - } - if s != nil { - t.Errorf("unexpected Service returned: %v", s) - } -} - -func TestIsValidNamespace(t *testing.T) { - - fk := testclient.NewSimpleClientset(&api.Namespace{ - ObjectMeta: meta_v1.ObjectMeta{ - Name: "default", - }, - }) - - _, err := IsValidNamespace(fk, "empty") - if err == nil { - t.Errorf("expected error but return nill") - } - - ns, err := IsValidNamespace(fk, "default") - if err != nil { - t.Errorf("unexpected error: %v", err) - } - - if ns == nil { - t.Errorf("expected a configmap but returned nil") - } - -} - -func TestIsValidConfigMap(t *testing.T) { - - fk := testclient.NewSimpleClientset(&api.ConfigMap{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: api.NamespaceDefault, - Name: "demo", - }, - }) - - _, err := IsValidConfigMap(fk, "") - if err == nil { - t.Errorf("expected error but return nill") - } - - s, err := IsValidConfigMap(fk, "default/demo") - if err != nil { - t.Errorf("unexpected error: %v", err) - } - - if s == nil { - t.Errorf("expected a configmap but returned nil") - } - - fk = testclient.NewSimpleClientset() - s, err = IsValidConfigMap(fk, "default/demo") - if err == nil { - t.Errorf("expected an error but returned nil") - } - if s != nil { - t.Errorf("unexpected Configmap returned: %v", s) - } - -} - -func TestIsValidSecret(t *testing.T) { - fk := testclient.NewSimpleClientset(&api.Secret{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: api.NamespaceDefault, - Name: "demo", - }, - }) - - _, err := IsValidSecret(fk, "") - if err == nil { - t.Errorf("expected error but returned nil") - } - s, err := IsValidSecret(fk, "default/demo") - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if s == nil { - t.Errorf("expected a Secret but returned nil") - } - - fk = testclient.NewSimpleClientset() - s, err = IsValidSecret(fk, "default/demo") - if err == nil { - t.Errorf("expected an error but returned nil") - } - if s != nil { - t.Errorf("unexpected Secret returned: %v", s) - } -} - func TestGetNodeIP(t *testing.T) { fKNodes := []struct { cs *testclient.Clientset