Fix validation in kubectl create ingress (#101377)

* 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>
This commit is contained in:
Ricardo Katz 2021-04-22 21:44:23 -03:00 committed by GitHub
parent 0e05558717
commit 3dfbcbc878
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -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 { if len(o.DefaultBackend) > 0 && len(strings.Split(o.DefaultBackend, ":")) != 2 {
return fmt.Errorf("default-backend should be in format servicename:serviceport") 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 { 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 { for _, annotation := range o.Annotations {
an := strings.SplitN(annotation, "=", 2) an := strings.SplitN(annotation, "=", 2)

View File

@ -30,6 +30,7 @@ func TestCreateIngressValidation(t *testing.T) {
defaultbackend string defaultbackend string
ingressclass string ingressclass string
rules []string rules []string
annotations []string
expected string expected string
}{ }{
"no default backend and rule": { "no default backend and rule": {
@ -49,6 +50,22 @@ func TestCreateIngressValidation(t *testing.T) {
defaultbackend: "xpto:4444", defaultbackend: "xpto:4444",
expected: "", 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": { "multiple conformant rules": {
rules: []string{ rules: []string{
"foo.com/path*=svc:8080", "foo.com/path*=svc:8080",
@ -122,6 +139,7 @@ func TestCreateIngressValidation(t *testing.T) {
DefaultBackend: tc.defaultbackend, DefaultBackend: tc.defaultbackend,
Rules: tc.rules, Rules: tc.rules,
IngressClass: tc.ingressclass, IngressClass: tc.ingressclass,
Annotations: tc.annotations,
} }
err := o.Validate() err := o.Validate()