Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hostPort and container validations to webhook #106

Merged
merged 1 commit into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/apis/stable/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ func (gs *GameServer) ApplyDefaults() {
func (gs *GameServer) Validate() (bool, []metav1.StatusCause) {
var causes []metav1.StatusCause

// make sure a name is specified when there is multiple containers in the pod.
if len(gs.Spec.Container) == 0 && len(gs.Spec.Template.Spec.Containers) > 1 {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Field: "container",
Message: "Container is required when using multiple containers in the pod templace",
})
}

// no host port when using dynamic PortPolicy
if gs.Spec.HostPort > 0 && gs.Spec.PortPolicy == Dynamic {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Field: "hostPort",
Message: "HostPort cannot be specified with a Dynamic PortPolicy",
})
}

// make sure the container value points to a valid container
_, _, err := gs.FindGameServerContainer()
if err != nil {
Expand Down
17 changes: 13 additions & 4 deletions pkg/apis/stable/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,23 @@ func TestGameServerValidate(t *testing.T) {

gs = GameServer{
Spec: GameServerSpec{
Container: "nope",
Container: "",
HostPort: 5001,
PortPolicy: Dynamic,
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}},
Spec: corev1.PodSpec{Containers: []corev1.Container{
{Name: "testing", Image: "testing/image"},
{Name: "anothertest", Image: "testing/image"},
}}}},
}
ok, causes = gs.Validate()
fields := []string{}
for _, f := range causes {
fields = append(fields, f.Field)
}
assert.False(t, ok)
assert.Len(t, causes, 1)
assert.Equal(t, causes[0].Field, "container")
assert.Len(t, causes, 3)
assert.Contains(t, fields, "container", "hostPort")
assert.Equal(t, causes[0].Type, metav1.CauseTypeFieldValueInvalid)
}

Expand Down