Skip to content

Commit

Permalink
feat(kuma-cp) validate proxy template
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz committed Nov 13, 2019
1 parent c71ced8 commit 5695181
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 3 deletions.
3 changes: 0 additions & 3 deletions pkg/core/resources/apis/mesh/proxytemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ func (t *ProxyTemplateResource) SetSpec(spec model.ResourceSpec) error {
return nil
}
}
func (t *ProxyTemplateResource) Validate() error {
return nil
}

var _ model.ResourceList = &ProxyTemplateResourceList{}

Expand Down
78 changes: 78 additions & 0 deletions pkg/core/resources/apis/mesh/proxytemplate_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package mesh

import (
"fmt"
"github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core/validators"
"github.com/Kong/kuma/pkg/xds/template"
"strings"
)

var availableProfiles map[string]bool
var availableProfilesMsg string

func init() {
profiles := []string{}
availableProfiles = map[string]bool{}
for _, profile := range template.AvailableProfiles {
availableProfiles[profile] = true
profiles = append(profiles, profile)
}
availableProfilesMsg = strings.Join(profiles, ",")
}

func (t *ProxyTemplateResource) Validate() error {
var verr validators.ValidationError
verr.AddError("", validateImports(t.Spec.Imports))
verr.AddError("", validateResources(t.Spec.Resources))
verr.AddError("", validateSelectors(t.Spec.Selectors))
return verr.OrNil()
}

func validateImports(imports []string) validators.ValidationError {
var verr validators.ValidationError
for i, imp := range imports {
if imp == "" {
verr.AddViolationAt(validators.RootedAt("imports").Index(i), "cannot be empty")
continue
}
if !availableProfiles[imp] {
verr.AddViolationAt(validators.RootedAt("imports").Index(i), fmt.Sprintf("profile not found. Available profiles: %s", availableProfilesMsg))
}
}
return verr
}

func validateResources(resources []*v1alpha1.ProxyTemplateRawResource) validators.ValidationError {
var verr validators.ValidationError
for i, resource := range resources {
if resource.Name == "" {
verr.AddViolationAt(validators.RootedAt("resources").Index(i).Field("name"), "cannot be empty")
}
if resource.Version == "" {
verr.AddViolationAt(validators.RootedAt("resources").Index(i).Field("version"), "cannot be empty")
}
if resource.Resource == "" {
verr.AddViolationAt(validators.RootedAt("resources").Index(i).Field("resource"), "cannot be empty")
}
}
return verr
}

func validateSelectors(selectors []*v1alpha1.ProxyTemplate_Selector) validators.ValidationError {
var verr validators.ValidationError
for i, selector := range selectors {
if len(selector.Match) == 0 {
verr.AddViolationAt(validators.RootedAt("selectors").Index(i), "has to contain at least one tag")
}
for key, value := range selector.Match {
if key == "" {
verr.AddViolationAt(validators.RootedAt("selectors").Index(i).Key(key), "tag cannot be empty")
}
if value == "" {
verr.AddViolationAt(validators.RootedAt("selectors").Index(i).Key(key), "value of tag cannot be empty")
}
}
}
return verr
}
131 changes: 131 additions & 0 deletions pkg/core/resources/apis/mesh/proxytemplate_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package mesh_test

import (
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

util_proto "github.com/Kong/kuma/pkg/util/proto"
"github.com/ghodss/yaml"
)

var _ = Describe("ProxyTemplate", func() {
Describe("Validate()", func() {
It("should pass validation", func() {
// given
spec := `
selectors:
- match:
service: backend
imports:
- default-proxy
resources:
- name: additional
version: v1
resource: test`

proxyTemplate := mesh.ProxyTemplateResource{}

// when
err := util_proto.FromYAML([]byte(spec), &proxyTemplate.Spec)
// then
Expect(err).ToNot(HaveOccurred())

// when
err = proxyTemplate.Validate()
// then
Expect(err).ToNot(HaveOccurred())
})

type testCase struct {
proxyTemplate string
expected string
}
DescribeTable("should validate fields",
func(given testCase) {
// given
proxyTemplate := mesh.ProxyTemplateResource{}

// when
err := util_proto.FromYAML([]byte(given.proxyTemplate), &proxyTemplate.Spec)
// then
Expect(err).ToNot(HaveOccurred())

// when
verr := proxyTemplate.Validate()
// and
actual, err := yaml.Marshal(verr)

// then
Expect(err).ToNot(HaveOccurred())
// and
Expect(actual).To(MatchYAML(given.expected))
},
Entry("empty import", testCase{
proxyTemplate: `
imports:
- ""
selectors:
- match:
service: backend`,
expected: `
violations:
- field: imports[0]
message: cannot be empty`,
}),
Entry("unknown profile", testCase{
proxyTemplate: `
imports:
- unknown-porfile
selectors:
- match:
service: backend`,
expected: `
violations:
- field: imports[0]
message: 'profile not found. Available profiles: default-proxy'`,
}),
Entry("resources empty fields", testCase{
proxyTemplate: `
selectors:
- match:
service: backend
resources:
- name:
version:
resource:`,
expected: `
violations:
- field: resources[0].name
message: cannot be empty
- field: resources[0].version
message: cannot be empty
- field: resources[0].resource
message: cannot be empty`,
}),
Entry("selector without tags", testCase{
proxyTemplate: `
selectors:
- match:`,
expected: `
violations:
- field: selectors[0]
message: has to contain at least one tag`,
}),
Entry("invalid tags", testCase{
proxyTemplate: `
selectors:
- match:
"": asdf
service:`,
expected: `
violations:
- field: selectors[0][""]
message: tag cannot be empty
- field: 'selectors[0]["service"]'
message: value of tag cannot be empty`,
}),
)
})
})
2 changes: 2 additions & 0 deletions pkg/xds/template/proxy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const (
ProfileDefaultProxy = "default-proxy"
)

var AvailableProfiles = []string{ProfileDefaultProxy}

var (
DefaultProxyTemplate = &kuma_mesh.ProxyTemplate{
Imports: []string{
Expand Down

0 comments on commit 5695181

Please sign in to comment.