Skip to content

Commit

Permalink
Validate if configmap exist and is in the namespace/name format
Browse files Browse the repository at this point in the history
Verifiy if watch-namespace option exist
  • Loading branch information
gianrubio committed Mar 14, 2017
1 parent 0cb8f59 commit c6195c4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/pkg/ingress/controller/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,20 @@ func NewIngressController(backend ingress.Controller) *GenericController {
glog.Infof("service %v validated as source of Ingress status", *publishSvc)
}

if *configMap != "" {
_, _, err = k8s.ParseNameNS(*configMap)
for _, configMap := range []string{*configMap, *tcpConfigMapName, *udpConfigMapName} {
_, err = k8s.IsValidConfigMap(kubeClient, configMap)

if err != nil {
glog.Fatalf("%v", err)
}
}

if *watchNamespace != "" {

_, err = k8s.IsValidNamespace(kubeClient, *watchNamespace)

if err != nil {
glog.Fatalf("configmap error: %v", err)
glog.Fatalf("no watchNamespace with name %v found: %v", *watchNamespace, err)
}
}

Expand Down
24 changes: 24 additions & 0 deletions core/pkg/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ func IsValidService(kubeClient clientset.Interface, name string) (*api.Service,
return kubeClient.Core().Services(ns).Get(name)
}

// 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)

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)
}

// 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)
Expand Down
58 changes: 58 additions & 0 deletions core/pkg/k8s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,64 @@ func TestIsValidService(t *testing.T) {
}
}

func TestIsValidNamespace(t *testing.T) {

fk := testclient.NewSimpleClientset(&api.Namespace{
ObjectMeta: api.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: api.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: api.ObjectMeta{
Expand Down

0 comments on commit c6195c4

Please sign in to comment.