44package kubeproxymode
55
66import (
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+
4649type 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+ }
0 commit comments