-
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.
- Loading branch information
1 parent
ea9afe4
commit b5e5613
Showing
15 changed files
with
384 additions
and
153 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,7 @@ | ||
package mesh | ||
|
||
const ( | ||
ProfileDefaultProxy = "default-proxy" | ||
) | ||
|
||
var AvailableProfiles = []string{ProfileDefaultProxy} |
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,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{}) | ||
} |
Oops, something went wrong.