Skip to content

Commit 9f96f25

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

File tree

3 files changed

+78
-51
lines changed

3 files changed

+78
-51
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
apiVersion: kubeproxy.config.k8s.io/v1alpha1
3+
kind: KubeProxyConfiguration
4+
mode: {{ .Mode }}

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

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package kubeproxymode
55

66
import (
7+
"bytes"
78
"context"
9+
_ "embed"
810
"fmt"
911
"slices"
12+
"text/template"
1013

1114
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1215
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -27,13 +30,6 @@ const (
2730
// VariableName is the external patch variable name.
2831
VariableName = "kubeProxy"
2932

30-
kubeProxyConfigYAMLTemplate = `
31-
---
32-
apiVersion: kubeproxy.config.k8s.io/v1alpha1
33-
kind: KubeProxyConfiguration
34-
mode: %s
35-
`
36-
3733
// addKubeProxyModeToExistingKubeProxyConfiguration is a sed command to add the kube-proxy mode to
3834
// an existing KubeProxyConfiguration present in the kubeadm config file. If there is no existing
3935
// KubeProxyConfiguration, it will exit with a non-zero status code which allows to run the fallback
@@ -43,6 +39,13 @@ mode: %s
4339
kubeadmConfigFilePath = "/run/kubeadm/kubeadm.yaml"
4440
)
4541

42+
var (
43+
//go:embed embedded/kubeproxyconfig.yaml
44+
kubeProxyConfigYAML []byte
45+
46+
kubeProxyConfigTemplate = template.Must(template.New("kubeProxyConfig").Parse(string(kubeProxyConfigYAML)))
47+
)
48+
4649
type kubeProxyMode struct {
4750
variableName string
4851
variableFieldPath []string
@@ -145,41 +148,67 @@ func (h *kubeProxyMode) Mutate(
145148

146149
switch kubeProxyMode {
147150
case v1alpha1.KubeProxyModeIPTables, v1alpha1.KubeProxyModeNFTables:
148-
kubeProxyConfig := bootstrapv1.File{
149-
Path: "/etc/kubernetes/kubeproxy-config.yaml",
150-
Owner: "root:root",
151-
Permissions: "0644",
152-
Content: fmt.Sprintf(kubeProxyConfigYAMLTemplate, kubeProxyMode),
153-
}
154-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
155-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
156-
kubeProxyConfig,
157-
)
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-
174-
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(
175-
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands,
176-
mergeKubeProxyConfigCmd,
177-
)
151+
return addKubeProxyConfigFileAndCommand(obj, kubeProxyMode)
178152
default:
179153
return fmt.Errorf("unknown kube proxy mode %q", kubeProxyMode)
180154
}
181-
182-
return nil
183155
},
184156
)
185157
}
158+
159+
// addKubeProxyConfigFileAndCommand adds the kube-proxy configuration file and command to the KCPTemplate.
160+
// It creates a KubeProxyConfiguration file with the specified mode and appends it to the kubeadm config file.
161+
// It also adds a command to the PreKubeadmCommands to merge the kube-proxy configuration into the kubeadm config file.
162+
// If the kubeadm config file already contains a KubeProxyConfiguration, it uses a sed command to add the mode to
163+
// the existing configuration.
164+
// If the kubeadm config file does not contain a KubeProxyConfiguration, it appends the new configuration
165+
// to the kubeadm config file using a cat command.
166+
//
167+
// TODO: KubeProxyConfiguration should be exposed upstream in CAPI to be able to configure kube-proxy mode directly
168+
// without the need for the messy commands in this implementation.
169+
func addKubeProxyConfigFileAndCommand(
170+
obj *controlplanev1.KubeadmControlPlaneTemplate, kubeProxyMode v1alpha1.KubeProxyMode,
171+
) error {
172+
templateInput := struct {
173+
Mode string
174+
}{
175+
Mode: string(kubeProxyMode),
176+
}
177+
var b bytes.Buffer
178+
err := kubeProxyConfigTemplate.Execute(&b, templateInput)
179+
if err != nil {
180+
return fmt.Errorf("failed executing kube-proxy config template: %w", err)
181+
}
182+
183+
kubeProxyConfig := bootstrapv1.File{
184+
Path: "/etc/kubernetes/kubeproxy-config.yaml",
185+
Owner: "root:root",
186+
Permissions: "0644",
187+
Content: b.String(),
188+
}
189+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
190+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
191+
kubeProxyConfig,
192+
)
193+
194+
sedCommand := fmt.Sprintf(
195+
addKubeProxyModeToExistingKubeProxyConfiguration,
196+
kubeadmConfigFilePath,
197+
kubeProxyMode,
198+
)
199+
catCommand := fmt.Sprintf(
200+
"cat /etc/kubernetes/kubeproxy-config.yaml >>%s",
201+
kubeadmConfigFilePath,
202+
)
203+
mergeKubeProxyConfigCmd := fmt.Sprintf(
204+
"/bin/sh -ec '(%s) || (%s)'",
205+
sedCommand,
206+
catCommand,
207+
)
208+
209+
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands = append(
210+
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands,
211+
mergeKubeProxyConfigCmd,
212+
)
213+
return nil
214+
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ var _ = Describe("Generate kube proxy mode patches", func() {
182182
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
183183
gomega.HaveKeyWithValue("owner", "root:root"),
184184
gomega.HaveKeyWithValue("permissions", "0644"),
185-
gomega.HaveKeyWithValue("content", `
186-
---
185+
gomega.HaveKeyWithValue("content", `---
187186
apiVersion: kubeproxy.config.k8s.io/v1alpha1
188187
kind: KubeProxyConfiguration
189188
mode: iptables
@@ -232,8 +231,7 @@ mode: iptables
232231
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
233232
gomega.HaveKeyWithValue("owner", "root:root"),
234233
gomega.HaveKeyWithValue("permissions", "0644"),
235-
gomega.HaveKeyWithValue("content", `
236-
---
234+
gomega.HaveKeyWithValue("content", `---
237235
apiVersion: kubeproxy.config.k8s.io/v1alpha1
238236
kind: KubeProxyConfiguration
239237
mode: iptables
@@ -282,8 +280,7 @@ mode: iptables
282280
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
283281
gomega.HaveKeyWithValue("owner", "root:root"),
284282
gomega.HaveKeyWithValue("permissions", "0644"),
285-
gomega.HaveKeyWithValue("content", `
286-
---
283+
gomega.HaveKeyWithValue("content", `---
287284
apiVersion: kubeproxy.config.k8s.io/v1alpha1
288285
kind: KubeProxyConfiguration
289286
mode: iptables
@@ -332,8 +329,7 @@ mode: iptables
332329
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
333330
gomega.HaveKeyWithValue("owner", "root:root"),
334331
gomega.HaveKeyWithValue("permissions", "0644"),
335-
gomega.HaveKeyWithValue("content", `
336-
---
332+
gomega.HaveKeyWithValue("content", `---
337333
apiVersion: kubeproxy.config.k8s.io/v1alpha1
338334
kind: KubeProxyConfiguration
339335
mode: nftables
@@ -382,8 +378,7 @@ mode: nftables
382378
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
383379
gomega.HaveKeyWithValue("owner", "root:root"),
384380
gomega.HaveKeyWithValue("permissions", "0644"),
385-
gomega.HaveKeyWithValue("content", `
386-
---
381+
gomega.HaveKeyWithValue("content", `---
387382
apiVersion: kubeproxy.config.k8s.io/v1alpha1
388383
kind: KubeProxyConfiguration
389384
mode: nftables
@@ -432,8 +427,7 @@ mode: nftables
432427
gomega.HaveKeyWithValue("path", "/etc/kubernetes/kubeproxy-config.yaml"),
433428
gomega.HaveKeyWithValue("owner", "root:root"),
434429
gomega.HaveKeyWithValue("permissions", "0644"),
435-
gomega.HaveKeyWithValue("content", `
436-
---
430+
gomega.HaveKeyWithValue("content", `---
437431
apiVersion: kubeproxy.config.k8s.io/v1alpha1
438432
kind: KubeProxyConfiguration
439433
mode: nftables

0 commit comments

Comments
 (0)