Skip to content

Commit

Permalink
ProxyTemplate validation (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz authored Nov 14, 2019
1 parent ea9afe4 commit b5e5613
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 153 deletions.
100 changes: 27 additions & 73 deletions api/mesh/v1alpha1/proxy_template.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions api/mesh/v1alpha1/proxy_template.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ package kuma.mesh.v1alpha1;

option go_package = "v1alpha1";

import "mesh/v1alpha1/selector.proto";

// ProxyTemplate defines the desired state of ProxyTemplate
message ProxyTemplate {

// Selector defines a tag-based selector of Dataplanes.
message Selector {

// Match Dataplanes with the following key-value pairs.
// +optional
map<string, string> match = 1;
}

// List of Dataplane selectors.
// +optional
repeated Selector selectors = 1;
Expand Down
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
7 changes: 7 additions & 0 deletions pkg/core/resources/apis/mesh/proxytemplate_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mesh

const (
ProfileDefaultProxy = "default-proxy"
)

var AvailableProfiles = []string{ProfileDefaultProxy}
66 changes: 66 additions & 0 deletions pkg/core/resources/apis/mesh/proxytemplate_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package mesh

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

var availableProfiles map[string]bool
var availableProfilesMsg string

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

func (t *ProxyTemplateResource) Validate() error {
var verr validators.ValidationError
verr.Add(validateImports(t.Spec.Imports))
verr.Add(validateResources(t.Spec.Resources))
verr.Add(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")
} else if _, err := envoy.ResourceFromYaml(resource.Resource); err != nil {
verr.AddViolationAt(validators.RootedAt("resources").Index(i).Field("resource"), fmt.Sprintf("native Envoy resource is not valid: %s", err.Error()))
}
}
return verr
}

func validateSelectors(selectors []*v1alpha1.Selector) validators.ValidationError {
return ValidateSelectors(validators.RootedAt("selectors"), selectors, ValidateSelectorsOpts{})
}
Loading

0 comments on commit b5e5613

Please sign in to comment.