Skip to content

Commit

Permalink
Add Annotations validation for Template ObjectMeta
Browse files Browse the repository at this point in the history
Check value and key length for Annotations which belongs to Fleet and
GameServerSpec.
  • Loading branch information
aLekSer committed Jan 9, 2020
1 parent c439d4b commit 6626daf
Show file tree
Hide file tree
Showing 8 changed files with 1,186 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/agones/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1
import (
"fmt"

apivalidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -86,5 +87,16 @@ func validateObjectMeta(objMeta metav1.ObjectMeta) []metav1.StatusCause {
})
}
}
errs = apivalidation.ValidateAnnotations(objMeta.Annotations,
field.NewPath("annotations"))
if len(errs) != 0 {
for _, v := range errs {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Field: "annotations",
Message: v.Error(),
})
}
}
return causes
}
27 changes: 27 additions & 0 deletions pkg/apis/agones/v1/gameserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,33 @@ func TestGameServerValidate(t *testing.T) {
assert.Len(t, causes, 1)
assert.Equal(t, "labels", causes[0].Field)

gs.Spec.Template.ObjectMeta.Labels = make(map[string]string)
gs.Spec.Template.ObjectMeta.Labels["agones.dev/longValueKey"] = longName
causes, ok = gs.Validate()
assert.False(t, ok)
assert.Len(t, causes, 1)
assert.Equal(t, "labels", causes[0].Field)

// Validate Labels and Annotations
gs.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
gs.Spec.Template.ObjectMeta.Annotations[longName] = longName
causes, ok = gs.Validate()
assert.False(t, ok)
assert.Len(t, causes, 2)

// No errors if valid Annotation was used
gs.Spec.Template.ObjectMeta.Labels = make(map[string]string)
gs.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
gs.Spec.Template.ObjectMeta.Annotations["agones.dev/shortName"] = "shortValue"
causes, ok = gs.Validate()
assert.True(t, ok)
assert.Len(t, causes, 0)

gs.Spec.Template.ObjectMeta.Annotations["agones.dev/shortName"] = longName
causes, ok = gs.Validate()
assert.True(t, ok)
assert.Len(t, causes, 0)

gs = GameServer{
Spec: GameServerSpec{
Ports: []GameServerPort{{Name: "one", PortPolicy: Passthrough, ContainerPort: 1294}, {PortPolicy: Passthrough, Name: "two", HostPort: 7890}},
Expand Down
18 changes: 18 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/api/validation/doc.go

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

85 changes: 85 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/api/validation/generic.go

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

Loading

0 comments on commit 6626daf

Please sign in to comment.