Skip to content

Commit

Permalink
basic metadata validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Oct 11, 2023
1 parent 0714bdb commit 6390b6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 7 additions & 9 deletions manifest/v1alpha/slo/test_data/expected_error.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
Validation for Service 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE' in project 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT' has failed for the following fields:
- 'metadata.name' with value 'MY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICEMY SERVICE...':
Validation for SLO 'MY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLO' in project 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT' has failed for the following fields:
- 'spec.description' with value 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...':
- length must be between 0 and 1050
- '.metadata.name' with value 'MY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY SLOMY S...':
- length must be between 1 and 63
- a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$')
- 'metadata.displayName' with value 'my-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-servicemy-service':
- length must be between 0 and 63
- 'metadata.project' with value 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT...':
- '.metadata.project' with value 'MY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECTMY PROJECT...':
- length must be between 1 and 63
- a DNS-1123 compliant name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$')
- 'metadata.labels' with value '{"L O L":["dip","dip"]}':
- '.metadata.labels' with value '{"L O L":["dip","dip"]}':
- label key 'L O L' does not match the regex: ^\p{L}([_\-0-9\p{L}]*[0-9\p{L}])?$
- 'spec.description' with value 'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...':
- length must be between 0 and 1050
Manifest source: /home/me/service.yaml
Manifest source: /home/me/slo.yaml
11 changes: 8 additions & 3 deletions manifest/v1alpha/slo/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
)

var sloValidation = validation.New[SLO](
v1alpha.FieldRuleMetadataName(func(s SLO) string { return s.Metadata.Name }),
v1alpha.FieldRuleMetadataDisplayName(func(s SLO) string { return s.Metadata.DisplayName }),
v1alpha.FieldRuleMetadataLabels(func(s SLO) v1alpha.Labels { return s.Metadata.Labels }),
v1alpha.FieldRuleSpecDescription(func(s SLO) string { return s.Spec.Description }),
validation.RulesFor(func(s SLO) Metadata { return s.Metadata }).Include(sloMetadataValidation),
)

var sloMetadataValidation = validation.New[Metadata](
v1alpha.FieldRuleMetadataName(func(m Metadata) string { return m.Name }),
v1alpha.FieldRuleMetadataDisplayName(func(m Metadata) string { return m.DisplayName }),
v1alpha.FieldRuleMetadataProject(func(m Metadata) string { return m.Project }),
v1alpha.FieldRuleMetadataLabels(func(m Metadata) v1alpha.Labels { return m.Labels }),
)

func validate(s SLO) error {
Expand Down
9 changes: 5 additions & 4 deletions manifest/v1alpha/slo/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ var expectedError string

func TestValidate_AllErrors(t *testing.T) {
err := validate(SLO{
Kind: manifest.KindProject,
Kind: manifest.KindSLO,
Metadata: Metadata{
Name: strings.Repeat("MY PROJECT", 20),
DisplayName: strings.Repeat("my-project", 10),
Name: strings.Repeat("MY SLO", 20),
DisplayName: strings.Repeat("my-slo", 10),
Project: strings.Repeat("MY PROJECT", 20),
Labels: v1alpha.Labels{
"L O L": []string{"dip", "dip"},
},
},
Spec: Spec{
Description: strings.Repeat("l", 2000),
},
ManifestSource: "/home/me/project.yaml",
ManifestSource: "/home/me/slo.yaml",
})
assert.Equal(t, expectedError, err.Error())
}

0 comments on commit 6390b6c

Please sign in to comment.