Skip to content

Commit 6d93056

Browse files
author
Cyril TOVENA
committed
add hostPort and container validation to webhook
1 parent 4e12060 commit 6d93056

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

pkg/apis/stable/v1alpha1/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ func (gs *GameServer) ApplyDefaults() {
190190
func (gs *GameServer) Validate() (bool, []metav1.StatusCause) {
191191
var causes []metav1.StatusCause
192192

193+
// make sure a name is specified when there is multiple containers in the pod.
194+
if len(gs.Spec.Container) == 0 && len(gs.Spec.Template.Spec.Containers) > 1 {
195+
causes = append(causes, metav1.StatusCause{
196+
Type: metav1.CauseTypeFieldValueInvalid,
197+
Field: "container",
198+
Message: "Container is required when using multiple containers in the pod templace",
199+
})
200+
}
201+
202+
// no host port when using dynamic PortPolicy
203+
if gs.Spec.HostPort > 0 && gs.Spec.PortPolicy == Dynamic {
204+
causes = append(causes, metav1.StatusCause{
205+
Type: metav1.CauseTypeFieldValueInvalid,
206+
Field: "hostPort",
207+
Message: "HostPort cannot be specified with a Dynamic PortPolicy",
208+
})
209+
}
210+
193211
// make sure the container value points to a valid container
194212
_, _, err := gs.FindGameServerContainer()
195213
if err != nil {

pkg/apis/stable/v1alpha1/types_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,23 @@ func TestGameServerValidate(t *testing.T) {
180180

181181
gs = GameServer{
182182
Spec: GameServerSpec{
183-
Container: "nope",
183+
Container: "",
184+
HostPort: 5001,
185+
PortPolicy: Dynamic,
184186
Template: corev1.PodTemplateSpec{
185-
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}},
187+
Spec: corev1.PodSpec{Containers: []corev1.Container{
188+
{Name: "testing", Image: "testing/image"},
189+
{Name: "anothertest", Image: "testing/image"},
190+
}}}},
186191
}
187192
ok, causes = gs.Validate()
193+
fields := []string{}
194+
for _, f := range causes {
195+
fields = append(fields, f.Field)
196+
}
188197
assert.False(t, ok)
189-
assert.Len(t, causes, 1)
190-
assert.Equal(t, causes[0].Field, "container")
198+
assert.Len(t, causes, 3)
199+
assert.Contains(t, fields, "container", "hostPort")
191200
assert.Equal(t, causes[0].Type, metav1.CauseTypeFieldValueInvalid)
192201
}
193202

0 commit comments

Comments
 (0)