Skip to content

Commit

Permalink
Automated cherry pick of #101377: Fix validation in kubectl create in…
Browse files Browse the repository at this point in the history
…gress (#101426)

* Fix validation in kubectl create ingress

Signed-off-by: Ricardo Pchevuzinske Katz <[email protected]>

* Fix validation in kubectl create ingress

Signed-off-by: Ricardo Pchevuzinske Katz <[email protected]>

Kubernetes-commit: cd0d8fb960ab519df97c0f2991cb277e18af350a
  • Loading branch information
rikatz authored and k8s-publishing-bot committed May 7, 2021
1 parent 8848261 commit 8e4adf3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/cmd/create/create_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/cmd/create/create_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestCreateIngressValidation(t *testing.T) {
defaultbackend string
ingressclass string
rules []string
annotations []string
expected string
}{
"no default backend and rule": {
Expand All @@ -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",
Expand Down Expand Up @@ -122,6 +139,7 @@ func TestCreateIngressValidation(t *testing.T) {
DefaultBackend: tc.defaultbackend,
Rules: tc.rules,
IngressClass: tc.ingressclass,
Annotations: tc.annotations,
}

err := o.Validate()
Expand Down

0 comments on commit 8e4adf3

Please sign in to comment.