Skip to content

Commit 036cb25

Browse files
committed
fixup! fix: Fix nftables template for Docker provider
1 parent 952007e commit 036cb25

File tree

3 files changed

+112
-13
lines changed

3 files changed

+112
-13
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,10 @@ var _ = Describe("Generate Extra API server certificate patches", func() {
175175
utilruntime.Must(clientgoscheme.AddToScheme(clientScheme))
176176
utilruntime.Must(clusterv1.AddToScheme(clientScheme))
177177
cl, err := helpers.TestEnv.GetK8sClientWithScheme(clientScheme)
178-
gomega.Expect(err).To(gomega.BeNil())
179-
err = cl.Create(context.Background(), &tt.cluster)
180-
gomega.Expect(err).To(gomega.BeNil())
178+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
179+
gomega.Expect(cl.Create(context.Background(), &tt.cluster)).To(gomega.Succeed())
180+
DeferCleanup(cl.Delete, context.Background(), &tt.cluster)
181181
capitest.AssertGeneratePatches(GinkgoT(), patchGenerator, &tt.patchTest)
182-
err = cl.Delete(context.Background(), &tt.cluster)
183-
gomega.Expect(err).To(gomega.BeNil())
184182
})
185183
}
186184
})

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
2121
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2222
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
23+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
2324
)
2425

2526
const (
@@ -31,6 +32,12 @@ const (
3132
apiVersion: kubeproxy.config.k8s.io/v1alpha1
3233
kind: KubeProxyConfiguration
3334
mode: %s
35+
`
36+
37+
// kubeProxyConfigYAMLTemplateForDockerProvider is the kube-proxy configuration template for Docker provider.
38+
// CAPD already configures some stuff in KubeProxyConfiguration, so we only need to set the mode.
39+
kubeProxyConfigYAMLTemplateForDockerProvider = `
40+
mode: %s
3441
`
3542
)
3643

@@ -121,11 +128,20 @@ func (h *kubeProxyMode) Mutate(
121128
"addon/kube-proxy",
122129
)
123130
case v1alpha1.KubeProxyModeIPTables, v1alpha1.KubeProxyModeNFTables:
131+
kubeProxyConfigProviderTemplate, err := templateForClusterProvider(ctx, clusterGetter)
132+
if err != nil {
133+
log.Error(
134+
err,
135+
"failed to get kube proxy config template for cluster provider",
136+
)
137+
return fmt.Errorf("failed to get cluster for kube proxy mode mutation: %w", err)
138+
}
139+
124140
kubeProxyConfig := bootstrapv1.File{
125141
Path: "/etc/kubernetes/kubeproxy-config.yaml",
126142
Owner: "root:root",
127143
Permissions: "0644",
128-
Content: fmt.Sprintf(kubeProxyConfigYAMLTemplate, kubeProxyMode),
144+
Content: fmt.Sprintf(kubeProxyConfigProviderTemplate, kubeProxyMode),
129145
}
130146
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
131147
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
@@ -144,3 +160,18 @@ func (h *kubeProxyMode) Mutate(
144160
},
145161
)
146162
}
163+
164+
// templateForClusterProvider returns the kube-proxy config template based on the cluster provider.
165+
func templateForClusterProvider(ctx context.Context, clusterGetter mutation.ClusterGetter) (string, error) {
166+
cluster, err := clusterGetter(ctx)
167+
if err != nil {
168+
return "", err
169+
}
170+
171+
switch utils.GetProvider(cluster) {
172+
case "docker":
173+
return kubeProxyConfigYAMLTemplateForDockerProvider, nil
174+
default:
175+
return kubeProxyConfigYAMLTemplate, nil
176+
}
177+
}

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

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44
package kubeproxymode
55

66
import (
7+
"context"
78
"testing"
89

910
. "github.com/onsi/ginkgo/v2"
1011
"github.com/onsi/gomega"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
15+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
16+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1117
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1218

1319
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1420
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1521
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1622
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
23+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
1724
)
1825

1926
func TestKubeProxyModePatch(t *testing.T) {
@@ -23,11 +30,17 @@ func TestKubeProxyModePatch(t *testing.T) {
2330

2431
type testObj struct {
2532
patchTest capitest.PatchTestDef
33+
cluster *clusterv1.Cluster
2634
}
2735

2836
var _ = Describe("Generate kube proxy mode patches", func() {
2937
patchGenerator := func() mutation.GeneratePatches {
30-
return mutation.NewMetaGeneratePatchesHandler("", nil, NewPatch()).(mutation.GeneratePatches)
38+
clientScheme := runtime.NewScheme()
39+
utilruntime.Must(clientgoscheme.AddToScheme(clientScheme))
40+
utilruntime.Must(clusterv1.AddToScheme(clientScheme))
41+
cl, err := helpers.TestEnv.GetK8sClientWithScheme(clientScheme)
42+
gomega.Expect(err).To(gomega.BeNil())
43+
return mutation.NewMetaGeneratePatchesHandler("", cl, NewPatch()).(mutation.GeneratePatches)
3144
}
3245

3346
testDefs := []testObj{{
@@ -96,6 +109,15 @@ var _ = Describe("Generate kube proxy mode patches", func() {
96109
ValueMatcher: gomega.ConsistOf("addon/kube-proxy"),
97110
}},
98111
},
112+
cluster: &clusterv1.Cluster{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "test-cluster",
115+
Namespace: request.Namespace,
116+
Labels: map[string]string{
117+
clusterv1.ProviderNameLabel: "nutanix",
118+
},
119+
},
120+
},
99121
}, {
100122
patchTest: capitest.PatchTestDef{
101123
Name: "kube proxy iptables mode with AWS",
@@ -137,6 +159,15 @@ mode: iptables
137159
),
138160
}},
139161
},
162+
cluster: &clusterv1.Cluster{
163+
ObjectMeta: metav1.ObjectMeta{
164+
Name: "test-cluster",
165+
Namespace: request.Namespace,
166+
Labels: map[string]string{
167+
clusterv1.ProviderNameLabel: "aws",
168+
},
169+
},
170+
},
140171
}, {
141172
patchTest: capitest.PatchTestDef{
142173
Name: "kube proxy iptables mode with Docker",
@@ -162,9 +193,6 @@ mode: iptables
162193
gomega.HaveKeyWithValue("owner", "root:root"),
163194
gomega.HaveKeyWithValue("permissions", "0644"),
164195
gomega.HaveKeyWithValue("content", `
165-
---
166-
apiVersion: kubeproxy.config.k8s.io/v1alpha1
167-
kind: KubeProxyConfiguration
168196
mode: iptables
169197
`,
170198
),
@@ -178,6 +206,15 @@ mode: iptables
178206
),
179207
}},
180208
},
209+
cluster: &clusterv1.Cluster{
210+
ObjectMeta: metav1.ObjectMeta{
211+
Name: "test-cluster",
212+
Namespace: request.Namespace,
213+
Labels: map[string]string{
214+
clusterv1.ProviderNameLabel: "docker",
215+
},
216+
},
217+
},
181218
}, {
182219
patchTest: capitest.PatchTestDef{
183220
Name: "kube proxy nftables mode with Nutanix",
@@ -219,6 +256,15 @@ mode: nftables
219256
),
220257
}},
221258
},
259+
cluster: &clusterv1.Cluster{
260+
ObjectMeta: metav1.ObjectMeta{
261+
Name: "test-cluster",
262+
Namespace: request.Namespace,
263+
Labels: map[string]string{
264+
clusterv1.ProviderNameLabel: "nutanix",
265+
},
266+
},
267+
},
222268
}, {
223269
patchTest: capitest.PatchTestDef{
224270
Name: "kube proxy nftables mode with AWS",
@@ -260,6 +306,15 @@ mode: nftables
260306
),
261307
}},
262308
},
309+
cluster: &clusterv1.Cluster{
310+
ObjectMeta: metav1.ObjectMeta{
311+
Name: "test-cluster",
312+
Namespace: request.Namespace,
313+
Labels: map[string]string{
314+
clusterv1.ProviderNameLabel: "aws",
315+
},
316+
},
317+
},
263318
}, {
264319
patchTest: capitest.PatchTestDef{
265320
Name: "kube proxy nftables mode with Docker",
@@ -285,9 +340,6 @@ mode: nftables
285340
gomega.HaveKeyWithValue("owner", "root:root"),
286341
gomega.HaveKeyWithValue("permissions", "0644"),
287342
gomega.HaveKeyWithValue("content", `
288-
---
289-
apiVersion: kubeproxy.config.k8s.io/v1alpha1
290-
kind: KubeProxyConfiguration
291343
mode: nftables
292344
`,
293345
),
@@ -301,11 +353,29 @@ mode: nftables
301353
),
302354
}},
303355
},
356+
cluster: &clusterv1.Cluster{
357+
ObjectMeta: metav1.ObjectMeta{
358+
Name: "test-cluster",
359+
Namespace: request.Namespace,
360+
Labels: map[string]string{
361+
clusterv1.ProviderNameLabel: "docker",
362+
},
363+
},
364+
},
304365
}}
305366

306367
// create test node for each case
307368
for _, tt := range testDefs {
308369
It(tt.patchTest.Name, func() {
370+
if tt.cluster != nil {
371+
clientScheme := runtime.NewScheme()
372+
utilruntime.Must(clientgoscheme.AddToScheme(clientScheme))
373+
utilruntime.Must(clusterv1.AddToScheme(clientScheme))
374+
cl, err := helpers.TestEnv.GetK8sClientWithScheme(clientScheme)
375+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
376+
gomega.Expect(cl.Create(context.Background(), tt.cluster)).To(gomega.Succeed())
377+
DeferCleanup(cl.Delete, context.Background(), tt.cluster)
378+
}
309379
capitest.AssertGeneratePatches(GinkgoT(), patchGenerator, &tt.patchTest)
310380
})
311381
}

0 commit comments

Comments
 (0)