-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kuma-cp) validate proxy template
- Loading branch information
1 parent
c71ced8
commit 5695181
Showing
4 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
pkg/core/resources/apis/mesh/proxytemplate_validator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}), | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters