Skip to content

Commit

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

* Fix validation in kubectl create ingress

Signed-off-by: Ricardo Pchevuzinske Katz <ricardo.katz@gmail.com>

* Fix validation in kubectl create ingress

Signed-off-by: Ricardo Pchevuzinske Katz <ricardo.katz@gmail.com>
  • Loading branch information
rikatz authored May 6, 2021
1 parent 5bd3ebf commit fe7d806
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,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 @@ -289,8 +295,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 staging/src/k8s.io/kubectl/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 fe7d806

Please sign in to comment.