Skip to content

Commit eed6ec3

Browse files
committed
fix: use ControlPlaneEndpont object directly in template
1 parent d3fdc29 commit eed6ec3

File tree

4 files changed

+118
-90
lines changed

4 files changed

+118
-90
lines changed

charts/cluster-api-runtime-extensions-nutanix/addons/cni/cilium/values-template.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ hubble:
1818
image:
1919
useDigest: false
2020
priorityClassName: system-cluster-critical
21-
{{- $capiProvider := index .Cluster.Labels "cluster.x-k8s.io/provider" }}
22-
{{- if eq $capiProvider "eks" }}
21+
{{- if eq .Provider "eks" }}
2322
ipam:
2423
mode: eni
2524
{{- else }}
@@ -42,9 +41,9 @@ envoy:
4241
{{- if .EnableKubeProxyReplacement }}
4342
kubeProxyReplacement: true
4443
{{- end }}
45-
k8sServiceHost: "{{ trimPrefix .Cluster.Spec.ControlPlaneEndpoint.Host "https://" }}"
46-
k8sServicePort: "{{ .Cluster.Spec.ControlPlaneEndpoint.Port }}"
47-
{{- if eq $capiProvider "eks" }}
44+
k8sServiceHost: "{{ trimPrefix .ControlPlaneEndpoint.Host "https://" }}"
45+
k8sServicePort: "{{ .ControlPlaneEndpoint.Port }}"
46+
{{- if eq .Provider "eks" }}
4847
enableIPv4Masquerade: false
4948
eni:
5049
enabled: true

hack/addons/update-cilium-manifests.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ envsubst -no-unset <"${KUSTOMIZE_BASE_DIR}/kustomization.yaml.tmpl" >"${ASSETS_D
2525

2626
cat <<EOF >"${ASSETS_DIR}/gomplate-context.yaml"
2727
EnableKubeProxyReplacement: false
28-
Cluster:
29-
Labels:
30-
cluster.x-k8s.io/provider: tmpl-capiprovider-tmpl
28+
Provider: tmpl-capiprovider-tmpl
29+
ControlPlaneEndpoint:
30+
Host: tmpl-controlplaneendpointhost-tmpl
31+
Port: 6443
3132
EOF
3233
# Replace trimPrefix with strings.TrimPrefix to use the in built go function in gomplate.
3334
sed -e 's/trimPrefix/strings.TrimPrefix/g' \

pkg/handlers/generic/lifecycle/cni/cilium/template.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package cilium
66
import (
77
"bytes"
88
"fmt"
9+
"strings"
910
"text/template"
1011

1112
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1213

1314
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
15+
capiutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
1416
)
1517

1618
// templateValues enables kube-proxy replacement when kube-proxy is disabled.
@@ -20,19 +22,25 @@ func templateValues(cluster *clusterv1.Cluster, text string) (string, error) {
2022
return "", fmt.Errorf("failed to check if kube-proxy is disabled: %w", err)
2123
}
2224

23-
ciliumTemplate, err := template.New("").Parse(text)
25+
funcMap := template.FuncMap{
26+
"trimPrefix": strings.TrimPrefix,
27+
}
28+
ciliumTemplate, err := template.New("").Funcs(funcMap).Parse(text)
2429
if err != nil {
2530
return "", fmt.Errorf("failed to parse template: %w", err)
2631
}
2732

2833
type input struct {
29-
Cluster *clusterv1.Cluster
34+
Provider string
35+
ControlPlaneEndpoint clusterv1.APIEndpoint
3036
EnableKubeProxyReplacement bool
3137
}
3238

3339
// Assume when kube-proxy is disabled, we should enable Cilium's kube-proxy replacement feature.
3440
templateInput := input{
3541
EnableKubeProxyReplacement: kubeProxyIsDisabled,
42+
Provider: capiutils.GetProvider(cluster),
43+
ControlPlaneEndpoint: cluster.Spec.ControlPlaneEndpoint,
3644
}
3745

3846
var b bytes.Buffer

pkg/handlers/generic/lifecycle/cni/cilium/template_test.go

Lines changed: 100 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,43 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
13-
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
12+
13+
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
14+
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/internal/test/builder"
1416
)
1517

1618
func Test_templateValues(t *testing.T) {
1719
tests := []struct {
1820
name string
19-
cluster *clusterv1.Cluster
21+
cluster func(t *testing.T) *clusterv1.Cluster
2022
expectedRenderedValuesTemplate string
2123
}{
2224
{
2325
name: "EKS cluster with https prefix in controlPlaneEndpoint.Host",
24-
cluster: &clusterv1.Cluster{
25-
ObjectMeta: metav1.ObjectMeta{
26-
Name: "test-eks-cluster",
27-
Namespace: "test-namespace",
28-
Labels: map[string]string{
29-
"cluster.x-k8s.io/provider": "eks",
30-
},
31-
},
32-
Spec: clusterv1.ClusterSpec{
33-
ControlPlaneEndpoint: clusterv1.APIEndpoint{
34-
Host: "https://test.eks.amazonaws.com",
35-
Port: 443,
36-
},
37-
Topology: &clusterv1.Topology{
38-
ControlPlane: clusterv1.ControlPlaneTopology{
39-
Metadata: clusterv1.ObjectMeta{
40-
Annotations: map[string]string{
41-
controlplanev1.SkipKubeProxyAnnotation: "",
42-
},
43-
},
44-
},
45-
},
46-
},
26+
cluster: func(t *testing.T) *clusterv1.Cluster {
27+
return createTestEKSCluster(
28+
t,
29+
"test-eks-cluster",
30+
"test-namespace",
31+
"https://test.eks.amazonaws.com",
32+
443,
33+
)
4734
},
4835
expectedRenderedValuesTemplate: expectedCiliumTemplateForEKS,
4936
},
5037
{
51-
name: "Non-EKS (Nutanix) cluster (should use auto for controlPlaneEndpointHost)",
52-
cluster: &clusterv1.Cluster{
53-
ObjectMeta: metav1.ObjectMeta{
54-
Name: "test-cluster",
55-
Namespace: "test-namespace",
56-
Labels: map[string]string{
57-
"cluster.x-k8s.io/provider": "nutanix",
58-
},
59-
},
60-
Spec: clusterv1.ClusterSpec{
61-
ControlPlaneEndpoint: clusterv1.APIEndpoint{
62-
Host: "192.168.1.100",
63-
Port: 6443,
64-
},
65-
Topology: &clusterv1.Topology{
66-
ControlPlane: clusterv1.ControlPlaneTopology{
67-
Metadata: clusterv1.ObjectMeta{
68-
Annotations: map[string]string{
69-
controlplanev1.SkipKubeProxyAnnotation: "",
70-
},
71-
},
72-
},
73-
},
74-
},
38+
name: "Non-EKS (Nutanix) cluster (should set ipam mode to kubernetes)",
39+
cluster: func(t *testing.T) *clusterv1.Cluster {
40+
return createTestNutanixCluster(t, "test-cluster", "test-namespace", "192.168.1.100", 6443)
7541
},
7642
expectedRenderedValuesTemplate: expectedCiliumTemplateForNutanix,
7743
},
7844
}
7945
for _, tt := range tests {
8046
t.Run(tt.name, func(t *testing.T) {
81-
got, err := templateValues(tt.cluster, ciliumTemplate)
47+
got, err := templateValues(tt.cluster(t), ciliumTemplate)
8248
require.NoError(t, err)
8349
assert.Equal(t, tt.expectedRenderedValuesTemplate, got)
8450
})
@@ -110,23 +76,9 @@ func Test_templateValues_TrimPrefixFunction(t *testing.T) {
11076

11177
for _, tt := range tests {
11278
t.Run(tt.name, func(t *testing.T) {
113-
cluster := &clusterv1.Cluster{
114-
ObjectMeta: metav1.ObjectMeta{
115-
Name: "test-cluster",
116-
Namespace: "test-namespace",
117-
Labels: map[string]string{
118-
"cluster.x-k8s.io/provider": "eks",
119-
},
120-
},
121-
Spec: clusterv1.ClusterSpec{
122-
ControlPlaneEndpoint: clusterv1.APIEndpoint{
123-
Host: tt.inputHost,
124-
Port: 443,
125-
},
126-
},
127-
}
128-
129-
template := `k8sServiceHost: "{{ trimPrefix .Cluster.Spec.ControlPlaneEndpoint.Host "https://" }}"`
79+
cluster := createTestEKSCluster(t, "test-cluster", "test-namespace", tt.inputHost, 443)
80+
81+
template := `k8sServiceHost: "{{ trimPrefix .ControlPlaneEndpoint.Host "https://" }}"`
13082
expected := `k8sServiceHost: "` + tt.expectedOutput + `"`
13183

13284
got, err := templateValues(cluster, template)
@@ -136,11 +88,84 @@ func Test_templateValues_TrimPrefixFunction(t *testing.T) {
13688
}
13789
}
13890

91+
// createTestEKSCluster creates a test EKS cluster using ClusterBuilder
92+
func createTestEKSCluster(t *testing.T, name, namespace, host string, port int32) *clusterv1.Cluster {
93+
// Create cluster config with kube-proxy disabled
94+
clusterConfigSpec := &apivariables.ClusterConfigSpec{
95+
KubeProxy: &carenv1.KubeProxy{
96+
Mode: carenv1.KubeProxyModeDisabled,
97+
},
98+
}
99+
100+
// Marshal cluster config to cluster variable
101+
variable, err := apivariables.MarshalToClusterVariable(carenv1.ClusterConfigVariableName, clusterConfigSpec)
102+
if err != nil {
103+
t.Fatalf("failed to marshal cluster config to cluster variable: %v", err)
104+
}
105+
106+
topology := &clusterv1.Topology{
107+
Class: "eks-cluster-class",
108+
Version: "v1.29.0",
109+
Variables: []clusterv1.ClusterVariable{*variable},
110+
}
111+
112+
cluster := builder.Cluster(namespace, name).
113+
WithLabels(map[string]string{
114+
"cluster.x-k8s.io/provider": "eks",
115+
}).
116+
WithTopology(topology).
117+
Build()
118+
119+
// Set ControlPlaneEndpoint after building since ClusterBuilder doesn't support it
120+
cluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
121+
Host: host,
122+
Port: port,
123+
}
124+
125+
return cluster
126+
}
127+
128+
// createTestNutanixCluster creates a test Nutanix cluster using ClusterBuilder
129+
func createTestNutanixCluster(t *testing.T, name, namespace, host string, port int32) *clusterv1.Cluster {
130+
// Create cluster config with kube-proxy disabled
131+
clusterConfigSpec := &apivariables.ClusterConfigSpec{
132+
KubeProxy: &carenv1.KubeProxy{
133+
Mode: carenv1.KubeProxyModeDisabled,
134+
},
135+
}
136+
137+
// Marshal cluster config to cluster variable
138+
variable, err := apivariables.MarshalToClusterVariable(carenv1.ClusterConfigVariableName, clusterConfigSpec)
139+
if err != nil {
140+
t.Fatalf("failed to marshal cluster config to cluster variable: %v", err)
141+
}
142+
143+
topology := &clusterv1.Topology{
144+
Class: "nutanix-cluster-class",
145+
Version: "v1.29.0",
146+
Variables: []clusterv1.ClusterVariable{*variable},
147+
}
148+
149+
cluster := builder.Cluster(namespace, name).
150+
WithLabels(map[string]string{
151+
"cluster.x-k8s.io/provider": "nutanix",
152+
}).
153+
WithTopology(topology).
154+
Build()
155+
156+
// Set ControlPlaneEndpoint after building since ClusterBuilder doesn't support it
157+
cluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
158+
Host: host,
159+
Port: port,
160+
}
161+
162+
return cluster
163+
}
164+
139165
const (
140166
// the template value is sourced from the Cilium values template in the project's helm chart
141167
ciliumTemplate = `
142-
{{- $capiProvider := index .Cluster.Labels "cluster.x-k8s.io/provider" }}
143-
{{- if eq $capiProvider "eks" }}
168+
{{- if eq .Provider "eks" }}
144169
ipam:
145170
mode: eni
146171
{{- else }}
@@ -151,15 +176,9 @@ ipam:
151176
{{- if .EnableKubeProxyReplacement }}
152177
kubeProxyReplacement: true
153178
{{- end }}
154-
155-
{{- if eq $capiProvider "eks" }}
156-
k8sServiceHost: "{{ trimPrefix .Cluster.Spec.ControlPlaneEndpoint.Host "https://" }}"
157-
k8sServicePort: "{{ .Cluster.Spec.ControlPlaneEndpoint.Port }}"
158-
{{- else }}
159-
k8sServiceHost: auto
160-
{{- end }}
161-
162-
{{- if eq $capiProvider "eks" }}
179+
k8sServiceHost: "{{ trimPrefix .ControlPlaneEndpoint.Host "https://" }}"
180+
k8sServicePort: "{{ .ControlPlaneEndpoint.Port }}"
181+
{{- if eq .Provider "eks" }}
163182
enableIPv4Masquerade: false
164183
eni:
165184
enabled: true
@@ -188,6 +207,7 @@ endpointRoutes:
188207
ipam:
189208
mode: kubernetes
190209
kubeProxyReplacement: true
191-
k8sServiceHost: auto
210+
k8sServiceHost: "192.168.1.100"
211+
k8sServicePort: "6443"
192212
`
193213
)

0 commit comments

Comments
 (0)