Skip to content

Commit a389cf8

Browse files
committed
fixup! refactor: Address review feedback
1 parent 2634787 commit a389cf8

File tree

7 files changed

+85
-31
lines changed

7 files changed

+85
-31
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ const (
345345

346346
type KubeProxy struct {
347347
// Mode specifies the mode for kube-proxy:
348-
//
349348
// - iptables means that kube-proxy is installed in iptables mode.
350349
// - nftables means that kube-proxy is installed in nftables mode.
351350
// +kubebuilder:validation:Optional

api/v1alpha1/crds/caren.nutanix.com_awsclusterconfigs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ spec:
566566
mode:
567567
description: |-
568568
Mode specifies the mode for kube-proxy:
569-
570569
- iptables means that kube-proxy is installed in iptables mode.
571570
- nftables means that kube-proxy is installed in nftables mode.
572571
enum:

api/v1alpha1/crds/caren.nutanix.com_dockerclusterconfigs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ spec:
503503
mode:
504504
description: |-
505505
Mode specifies the mode for kube-proxy:
506-
507506
- iptables means that kube-proxy is installed in iptables mode.
508507
- nftables means that kube-proxy is installed in nftables mode.
509508
enum:

api/v1alpha1/crds/caren.nutanix.com_genericclusterconfigs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ spec:
181181
mode:
182182
description: |-
183183
Mode specifies the mode for kube-proxy:
184-
185184
- iptables means that kube-proxy is installed in iptables mode.
186185
- nftables means that kube-proxy is installed in nftables mode.
187186
enum:

api/v1alpha1/crds/caren.nutanix.com_nutanixclusterconfigs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ spec:
682682
mode:
683683
description: |-
684684
Mode specifies the mode for kube-proxy:
685-
686685
- iptables means that kube-proxy is installed in iptables mode.
687686
- nftables means that kube-proxy is installed in nftables mode.
688687
enum:

pkg/handlers/generic/mutation/kubeproxymode/inject.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1212
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1413
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
1514
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
1615
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -22,7 +21,6 @@ import (
2221
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
2322
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2423
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
25-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
2624
)
2725

2826
const (
@@ -36,11 +34,13 @@ kind: KubeProxyConfiguration
3634
mode: %s
3735
`
3836

39-
// kubeProxyConfigYAMLTemplateForDockerProvider is the kube-proxy configuration template for Docker provider.
40-
// CAPD already configures some stuff in KubeProxyConfiguration, so we only need to set the mode.
41-
kubeProxyConfigYAMLTemplateForDockerProvider = `
42-
mode: %s
43-
`
37+
// addKubeProxyModeToExistingKubeProxyConfiguration is a sed command to add the kube-proxy mode to
38+
// an existing KubeProxyConfiguration present in the kubeadm config file. If there is no existing
39+
// KubeProxyConfiguration, it will exit with a non-zero status code which allows to run the fallback
40+
// command to append the KubeProxyConfiguration specified in the template above to the kubeadm config file.
41+
addKubeProxyModeToExistingKubeProxyConfiguration = `grep -q "^kind: KubeProxyConfiguration$" %[1]s && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: %[2]s/" %[1]s` //nolint:lll // Just a long command.
42+
43+
kubeadmConfigFilePath = "/run/kubeadm/kubeadm.yaml"
4444
)
4545

4646
type kubeProxyMode struct {
@@ -145,19 +145,32 @@ func (h *kubeProxyMode) Mutate(
145145

146146
switch kubeProxyMode {
147147
case v1alpha1.KubeProxyModeIPTables, v1alpha1.KubeProxyModeNFTables:
148-
kubeProxyConfigProviderTemplate := templateForClusterProvider(cluster)
149-
150148
kubeProxyConfig := bootstrapv1.File{
151149
Path: "/etc/kubernetes/kubeproxy-config.yaml",
152150
Owner: "root:root",
153151
Permissions: "0644",
154-
Content: fmt.Sprintf(kubeProxyConfigProviderTemplate, kubeProxyMode),
152+
Content: fmt.Sprintf(kubeProxyConfigYAMLTemplate, kubeProxyMode),
155153
}
156154
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
157155
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
158156
kubeProxyConfig,
159157
)
160-
mergeKubeProxyConfigCmd := "/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'"
158+
159+
sedCommand := fmt.Sprintf(
160+
addKubeProxyModeToExistingKubeProxyConfiguration,
161+
kubeadmConfigFilePath,
162+
kubeProxyMode,
163+
)
164+
catCommand := fmt.Sprintf(
165+
"cat /etc/kubernetes/kubeproxy-config.yaml >>%s",
166+
kubeadmConfigFilePath,
167+
)
168+
mergeKubeProxyConfigCmd := fmt.Sprintf(
169+
"/bin/sh -ec '(%s) || (%s)'",
170+
sedCommand,
171+
catCommand,
172+
)
173+
161174
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(
162175
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands,
163176
mergeKubeProxyConfigCmd,
@@ -170,13 +183,3 @@ func (h *kubeProxyMode) Mutate(
170183
},
171184
)
172185
}
173-
174-
// templateForClusterProvider returns the kube-proxy config template based on the cluster provider.
175-
func templateForClusterProvider(cluster *clusterv1.Cluster) string {
176-
switch utils.GetProvider(cluster) {
177-
case "docker":
178-
return kubeProxyConfigYAMLTemplateForDockerProvider
179-
default:
180-
return kubeProxyConfigYAMLTemplate
181-
}
182-
}

pkg/handlers/generic/mutation/kubeproxymode/inject_test.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,56 @@ var _ = Describe("Generate kube proxy mode patches", func() {
158158
},
159159
},
160160
},
161+
}, {
162+
patchTest: capitest.PatchTestDef{
163+
Name: "kube proxy iptables mode with Nutanix",
164+
Vars: []runtimehooksv1.Variable{
165+
capitest.VariableWithValue(
166+
v1alpha1.ClusterConfigVariableName,
167+
v1alpha1.AWSClusterConfigSpec{
168+
GenericClusterConfigSpec: v1alpha1.GenericClusterConfigSpec{
169+
KubeProxy: &v1alpha1.KubeProxy{
170+
Mode: v1alpha1.KubeProxyModeIPTables,
171+
},
172+
},
173+
},
174+
),
175+
},
176+
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""),
177+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
178+
Operation: "add",
179+
Path: "/spec/template/spec/kubeadmConfigSpec/files",
180+
ValueMatcher: gomega.ConsistOf(
181+
gomega.SatisfyAll(
182+
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
183+
gomega.HaveKeyWithValue("owner", "root:root"),
184+
gomega.HaveKeyWithValue("permissions", "0644"),
185+
gomega.HaveKeyWithValue("content", `
186+
---
187+
apiVersion: kubeproxy.config.k8s.io/v1alpha1
188+
kind: KubeProxyConfiguration
189+
mode: iptables
190+
`,
191+
),
192+
),
193+
),
194+
}, {
195+
Operation: "add",
196+
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
197+
ValueMatcher: gomega.ConsistOf(
198+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: iptables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
199+
),
200+
}},
201+
},
202+
cluster: &clusterv1.Cluster{
203+
ObjectMeta: metav1.ObjectMeta{
204+
Name: "test-cluster",
205+
Namespace: request.Namespace,
206+
Labels: map[string]string{
207+
clusterv1.ProviderNameLabel: "nutanix",
208+
},
209+
},
210+
},
161211
}, {
162212
patchTest: capitest.PatchTestDef{
163213
Name: "kube proxy iptables mode with AWS",
@@ -195,7 +245,7 @@ mode: iptables
195245
Operation: "add",
196246
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
197247
ValueMatcher: gomega.ConsistOf(
198-
"/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'",
248+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: iptables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
199249
),
200250
}},
201251
},
@@ -233,6 +283,9 @@ mode: iptables
233283
gomega.HaveKeyWithValue("owner", "root:root"),
234284
gomega.HaveKeyWithValue("permissions", "0644"),
235285
gomega.HaveKeyWithValue("content", `
286+
---
287+
apiVersion: kubeproxy.config.k8s.io/v1alpha1
288+
kind: KubeProxyConfiguration
236289
mode: iptables
237290
`,
238291
),
@@ -242,7 +295,7 @@ mode: iptables
242295
Operation: "add",
243296
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
244297
ValueMatcher: gomega.ConsistOf(
245-
"/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'",
298+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: iptables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
246299
),
247300
}},
248301
},
@@ -292,7 +345,7 @@ mode: nftables
292345
Operation: "add",
293346
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
294347
ValueMatcher: gomega.ConsistOf(
295-
"/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'",
348+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: nftables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
296349
),
297350
}},
298351
},
@@ -342,7 +395,7 @@ mode: nftables
342395
Operation: "add",
343396
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
344397
ValueMatcher: gomega.ConsistOf(
345-
"/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'",
398+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: nftables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
346399
),
347400
}},
348401
},
@@ -380,6 +433,9 @@ mode: nftables
380433
gomega.HaveKeyWithValue("owner", "root:root"),
381434
gomega.HaveKeyWithValue("permissions", "0644"),
382435
gomega.HaveKeyWithValue("content", `
436+
---
437+
apiVersion: kubeproxy.config.k8s.io/v1alpha1
438+
kind: KubeProxyConfiguration
383439
mode: nftables
384440
`,
385441
),
@@ -389,7 +445,7 @@ mode: nftables
389445
Operation: "add",
390446
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
391447
ValueMatcher: gomega.ConsistOf(
392-
"/bin/sh -ec 'cat /etc/kubernetes/kubeproxy-config.yaml >> /run/kubeadm/kubeadm.yaml'",
448+
`/bin/sh -ec '(grep -q "^kind: KubeProxyConfiguration$" /run/kubeadm/kubeadm.yaml && sed -i -e "s/^\(kind: KubeProxyConfiguration\)$/\1\nmode: nftables/" /run/kubeadm/kubeadm.yaml) || (cat /etc/kubernetes/kubeproxy-config.yaml >>/run/kubeadm/kubeadm.yaml)'`, //nolint:lll // Just a long command.
393449
),
394450
}},
395451
},

0 commit comments

Comments
 (0)