Skip to content

Commit 76feb78

Browse files
authored
Merge pull request #1262 from tasdikrahman/feat-add-ability-to-specify-mtu
feat: Add ability to specify MTU when creating Network
2 parents 3c2e7de + e3a61fc commit 76feb78

7 files changed

+179
-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+
}

api/v1beta1/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ type NetworkSpec struct {
136136
// HostProject is the name of the project hosting the shared VPC network resources.
137137
// +optional
138138
HostProject *string `json:"hostProject,omitempty"`
139+
140+
// Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
141+
// 1300 and the maximum value is 8896. The suggested value is 1500, which is
142+
// the default MTU used on the Internet, or 8896 if you want to use Jumbo
143+
// frames. If unspecified, the value defaults to 1460.
144+
// More info: https://pkg.go.dev/google.golang.org/api/compute/v1#Network
145+
// +kubebuilder:validation:Minimum:=1300
146+
// +kubebuilder:validation:Maximum:=8896
147+
// +kubebuilder:default:=1460
148+
// +optional
149+
Mtu int64 `json:"mtu,omitempty"`
139150
}
140151

141152
// LoadBalancerType defines the Load Balancer that should be created.

cloud/scope/cluster.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ func (s *ClusterScope) NetworkName() string {
131131
return ptr.Deref(s.GCPCluster.Spec.Network.Name, "default")
132132
}
133133

134+
// NetworkMtu returns the Network MTU of 1440 which is the default, otherwise returns back what is being set.
135+
// Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
136+
// 1300 and the maximum value is 8896. The suggested value is 1500, which is
137+
// the default MTU used on the Internet, or 8896 if you want to use Jumbo
138+
// frames. If unspecified, the value defaults to 1460.
139+
// More info
140+
// - https://pkg.go.dev/google.golang.org/api/compute/v1#Network
141+
// - https://cloud.google.com/vpc/docs/mtu
142+
func (s *ClusterScope) NetworkMtu() int64 {
143+
if s.GCPCluster.Spec.Network.Mtu == 0 {
144+
return int64(1460)
145+
}
146+
return s.GCPCluster.Spec.Network.Mtu
147+
}
148+
134149
// NetworkLink returns the partial URL for the network.
135150
func (s *ClusterScope) NetworkLink() string {
136151
return fmt.Sprintf("projects/%s/global/networks/%s", s.NetworkProject(), s.NetworkName())
@@ -206,6 +221,7 @@ func (s *ClusterScope) NetworkSpec() *compute.Network {
206221
Description: infrav1.ClusterTagKey(s.Name()),
207222
AutoCreateSubnetworks: createSubnet,
208223
ForceSendFields: []string{"AutoCreateSubnetworks"},
224+
Mtu: s.NetworkMtu(),
209225
}
210226

211227
return network

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclusters.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ spec:
168168
(useful for changing apiserver port)
169169
format: int32
170170
type: integer
171+
mtu:
172+
default: 1460
173+
description: |-
174+
Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
175+
1300 and the maximum value is 8896. The suggested value is 1500, which is
176+
the default MTU used on the Internet, or 8896 if you want to use Jumbo
177+
frames. If unspecified, the value defaults to 1460.
178+
More info: https://pkg.go.dev/google.golang.org/api/compute/v1#Network
179+
format: int64
180+
maximum: 8896
181+
minimum: 1300
182+
type: integer
171183
name:
172184
description: Name is the name of the network to be used.
173185
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ spec:
185185
backend (useful for changing apiserver port)
186186
format: int32
187187
type: integer
188+
mtu:
189+
default: 1460
190+
description: |-
191+
Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
192+
1300 and the maximum value is 8896. The suggested value is 1500, which is
193+
the default MTU used on the Internet, or 8896 if you want to use Jumbo
194+
frames. If unspecified, the value defaults to 1460.
195+
More info: https://pkg.go.dev/google.golang.org/api/compute/v1#Network
196+
format: int64
197+
maximum: 8896
198+
minimum: 1300
199+
type: integer
188200
name:
189201
description: Name is the name of the network to be used.
190202
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmanagedclusters.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ spec:
164164
(useful for changing apiserver port)
165165
format: int32
166166
type: integer
167+
mtu:
168+
default: 1460
169+
description: |-
170+
Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
171+
1300 and the maximum value is 8896. The suggested value is 1500, which is
172+
the default MTU used on the Internet, or 8896 if you want to use Jumbo
173+
frames. If unspecified, the value defaults to 1460.
174+
More info: https://pkg.go.dev/google.golang.org/api/compute/v1#Network
175+
format: int64
176+
maximum: 8896
177+
minimum: 1300
178+
type: integer
167179
name:
168180
description: Name is the name of the network to be used.
169181
type: string

0 commit comments

Comments
 (0)