From 6d9305637314228c4e3d2d68afe74568c7e874fc Mon Sep 17 00:00:00 2001 From: Cyril TOVENA Date: Sat, 24 Feb 2018 19:24:08 -0500 Subject: [PATCH] add hostPort and container validation to webhook --- pkg/apis/stable/v1alpha1/types.go | 18 ++++++++++++++++++ pkg/apis/stable/v1alpha1/types_test.go | 17 +++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pkg/apis/stable/v1alpha1/types.go b/pkg/apis/stable/v1alpha1/types.go index 719d1e70a3..69bd92d05a 100644 --- a/pkg/apis/stable/v1alpha1/types.go +++ b/pkg/apis/stable/v1alpha1/types.go @@ -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 { diff --git a/pkg/apis/stable/v1alpha1/types_test.go b/pkg/apis/stable/v1alpha1/types_test.go index 97e52c4dea..7205d7d79a 100644 --- a/pkg/apis/stable/v1alpha1/types_test.go +++ b/pkg/apis/stable/v1alpha1/types_test.go @@ -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) }