Skip to content

Commit b0aaa8f

Browse files
chore: GCPCluster webhook: Throw error if MTU in the new incoming spec is more than 8896 bytes or lesser than 1300 bytes
1 parent d265963 commit b0aaa8f

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

api/v1beta1/gcpcluster_webhook.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ func (c *GCPCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings,
9292
)
9393
}
9494

95+
if c.Spec.Network.Mtu < int64(1300) {
96+
allErrs = append(allErrs,
97+
field.Invalid(field.NewPath("spec", "Network", "Mtu"),
98+
c.Spec.Network.Mtu, "field cannot be lesser than 1300"),
99+
)
100+
}
101+
102+
if c.Spec.Network.Mtu > int64(8896) {
103+
allErrs = append(allErrs,
104+
field.Invalid(field.NewPath("spec", "Network", "Mtu"),
105+
c.Spec.Network.Mtu, "field cannot be greater than 8896"),
106+
)
107+
}
108+
95109
if len(allErrs) == 0 {
96110
return nil, nil
97111
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestGCPCluster_ValidateUpdate(t *testing.T) {
26+
g := NewWithT(t)
27+
28+
tests := []struct {
29+
name string
30+
newCluster *GCPCluster
31+
oldCluster *GCPCluster
32+
wantErr bool
33+
}{
34+
{
35+
name: "GCPCluster with MTU field is within the limits of more than 1300 and less than 8896",
36+
newCluster: &GCPCluster{
37+
Spec: GCPClusterSpec{
38+
Network: NetworkSpec{
39+
Mtu: int64(1500),
40+
},
41+
},
42+
},
43+
oldCluster: &GCPCluster{
44+
Spec: GCPClusterSpec{
45+
Network: NetworkSpec{
46+
Mtu: int64(1400),
47+
},
48+
},
49+
},
50+
wantErr: false,
51+
},
52+
{
53+
name: "GCPCluster with MTU field more than 8896",
54+
newCluster: &GCPCluster{
55+
Spec: GCPClusterSpec{
56+
Network: NetworkSpec{
57+
Mtu: int64(10000),
58+
},
59+
},
60+
},
61+
oldCluster: &GCPCluster{
62+
Spec: GCPClusterSpec{
63+
Network: NetworkSpec{
64+
Mtu: int64(1500),
65+
},
66+
},
67+
},
68+
wantErr: true,
69+
},
70+
{
71+
name: "GCPCluster with MTU field less than 8896",
72+
newCluster: &GCPCluster{
73+
Spec: GCPClusterSpec{
74+
Network: NetworkSpec{
75+
Mtu: int64(1250),
76+
},
77+
},
78+
},
79+
oldCluster: &GCPCluster{
80+
Spec: GCPClusterSpec{
81+
Network: NetworkSpec{
82+
Mtu: int64(1500),
83+
},
84+
},
85+
},
86+
wantErr: true,
87+
},
88+
}
89+
for _, test := range tests {
90+
test := test
91+
t.Run(test.name, func(t *testing.T) {
92+
t.Parallel()
93+
warn, err := test.newCluster.ValidateUpdate(test.oldCluster)
94+
if test.wantErr {
95+
g.Expect(err).To(HaveOccurred())
96+
} else {
97+
g.Expect(err).NotTo(HaveOccurred())
98+
}
99+
g.Expect(warn).To(BeNil())
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)