Skip to content

Commit

Permalink
update based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnotashwin committed Sep 14, 2020
1 parent 656417c commit d0a466c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
8 changes: 4 additions & 4 deletions api/v1alpha1/servicedefaults_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ func (e ExposeConfig) toConsul() capi.ExposeConfig {

func (e ExposeConfig) validate() []*field.Error {
var errs field.ErrorList
protocols := []string{"http", "http2"}
for i, path := range e.Paths {
if path.Path != "" && !strings.HasPrefix(path.Path, "/") {
errs = append(errs, field.Invalid(field.NewPath("spec").Child("expose").Child(fmt.Sprintf("paths[%d]", i)).Child("path"), path.Path, `must begin with a '/'`))
}
switch path.Protocol {
case "http", "http2":
if sliceContains(protocols, path.Protocol) {
continue
default:
errs = append(errs, field.Invalid(field.NewPath("spec").Child("expose").Child(fmt.Sprintf("paths[%d]", i)).Child("protocol"), path.Protocol, `must be one of "http" or "http2"`))
} else {
errs = append(errs, field.Invalid(field.NewPath("spec").Child("expose").Child(fmt.Sprintf("paths[%d]", i)).Child("protocol"), path.Protocol, notInSliceMessage(protocols)))
}
}
return errs
Expand Down
6 changes: 3 additions & 3 deletions api/v1alpha1/servicedefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func TestValidate(t *testing.T) {
},
},
},
`servicedefaults.consul.hashicorp.com "my-service" is invalid: spec.meshGateway.mode: Invalid value: "foobar": must be one of "remote", "local", "none" or ""`,
`servicedefaults.consul.hashicorp.com "my-service" is invalid: spec.meshGateway.mode: Invalid value: "foobar": must be one of "remote", "local", "none", ""`,
},
"expose.paths[].protocol": {
&ServiceDefaults{
Expand All @@ -689,7 +689,7 @@ func TestValidate(t *testing.T) {
},
},
},
`servicedefaults.consul.hashicorp.com "my-service" is invalid: spec.expose.paths[0].protocol: Invalid value: "invalid-protocol": must be one of "http" or "http2"`,
`servicedefaults.consul.hashicorp.com "my-service" is invalid: spec.expose.paths[0].protocol: Invalid value: "invalid-protocol": must be one of "http", "http2"`,
},
"expose.paths[].path": {
&ServiceDefaults{
Expand Down Expand Up @@ -728,7 +728,7 @@ func TestValidate(t *testing.T) {
},
},
},
`servicedefaults.consul.hashicorp.com "my-service" is invalid: [spec.meshGateway.mode: Invalid value: "invalid-mode": must be one of "remote", "local", "none" or "", spec.expose.paths[0].path: Invalid value: "invalid-path": must begin with a '/', spec.expose.paths[0].protocol: Invalid value: "invalid-protocol": must be one of "http" or "http2"]`,
`servicedefaults.consul.hashicorp.com "my-service" is invalid: [spec.meshGateway.mode: Invalid value: "invalid-mode": must be one of "remote", "local", "none", "", spec.expose.paths[0].path: Invalid value: "invalid-path": must begin with a '/', spec.expose.paths[0].protocol: Invalid value: "invalid-protocol": must be one of "http", "http2"]`,
},
}

Expand Down
28 changes: 24 additions & 4 deletions api/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1alpha1

import (
"fmt"

capi "github.com/hashicorp/consul/api"
"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand Down Expand Up @@ -50,10 +52,28 @@ func (m MeshGatewayConfig) toConsul() capi.MeshGatewayConfig {
}

func (m MeshGatewayConfig) validate() *field.Error {
switch m.Mode {
case "", "local", "remote", "none":
modes := []string{"remote", "local", "none", ""}
if sliceContains(modes, m.Mode) {
return nil
default:
return field.Invalid(field.NewPath("spec").Child("meshGateway").Child("mode"), m.Mode, `must be one of "remote", "local", "none" or ""`)
} else {
return field.Invalid(field.NewPath("spec").Child("meshGateway").Child("mode"), m.Mode, notInSliceMessage(modes))
}
}

func notInSliceMessage(slice []string) string {
message := ""
for _, s := range slice {
message = fmt.Sprintf(`%s, "%s"`, message, s)
}
runes := []rune(message)
return fmt.Sprintf(`must be one of %s`, string(runes[2:]))
}

func sliceContains(slice []string, entry string) bool {
for _, s := range slice {
if entry == s {
return true
}
}
return false
}

0 comments on commit d0a466c

Please sign in to comment.