Skip to content

Commit

Permalink
Merge pull request #1380 from aledbf/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
aledbf authored Sep 17, 2017
2 parents 456793f + 7fda959 commit f478084
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 166 deletions.
10 changes: 10 additions & 0 deletions core/pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions core/pkg/ingress/controller/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
42 changes: 0 additions & 42 deletions core/pkg/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/")
Expand Down
118 changes: 0 additions & 118 deletions core/pkg/k8s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f478084

Please sign in to comment.