diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go index 64a0d696ac7..fc4bd83581b 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go @@ -228,6 +228,12 @@ func (o *CreateIngressOptions) Validate() error { } } + for _, annotation := range o.Annotations { + if an := strings.SplitN(annotation, "=", 2); len(an) != 2 { + return fmt.Errorf("annotation %s is invalid and should be in format key=[value]", annotation) + } + } + if len(o.DefaultBackend) > 0 && len(strings.Split(o.DefaultBackend, ":")) != 2 { return fmt.Errorf("default-backend should be in format servicename:serviceport") } @@ -285,8 +291,8 @@ func (o *CreateIngressOptions) createIngress() *networkingv1.Ingress { } func (o *CreateIngressOptions) buildAnnotations() map[string]string { - var annotations map[string]string - annotations = make(map[string]string) + + var annotations = make(map[string]string) for _, annotation := range o.Annotations { an := strings.SplitN(annotation, "=", 2) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go index 5a00bd8eb80..6d27167548a 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go @@ -30,6 +30,7 @@ func TestCreateIngressValidation(t *testing.T) { defaultbackend string ingressclass string rules []string + annotations []string expected string }{ "no default backend and rule": { @@ -49,6 +50,22 @@ func TestCreateIngressValidation(t *testing.T) { defaultbackend: "xpto:4444", expected: "", }, + "invalid annotation": { + defaultbackend: "xpto:4444", + annotations: []string{ + "key1=value1", + "key2", + }, + expected: "annotation key2 is invalid and should be in format key=[value]", + }, + "valid annotations": { + defaultbackend: "xpto:4444", + annotations: []string{ + "key1=value1", + "key2=", + }, + expected: "", + }, "multiple conformant rules": { rules: []string{ "foo.com/path*=svc:8080", @@ -122,6 +139,7 @@ func TestCreateIngressValidation(t *testing.T) { DefaultBackend: tc.defaultbackend, Rules: tc.rules, IngressClass: tc.ingressclass, + Annotations: tc.annotations, } err := o.Validate()