Skip to content

Commit fb91dbb

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

File tree

4 files changed

+87
-90
lines changed

4 files changed

+87
-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: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,49 @@ 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 createTestCluster(
28+
t,
29+
"test-eks-cluster",
30+
"test-namespace",
31+
"eks",
32+
"https://test.eks.amazonaws.com",
33+
443,
34+
)
4735
},
4836
expectedRenderedValuesTemplate: expectedCiliumTemplateForEKS,
4937
},
5038
{
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-
},
39+
name: "Non-EKS (Nutanix) cluster (should set ipam mode to kubernetes)",
40+
cluster: func(t *testing.T) *clusterv1.Cluster {
41+
return createTestCluster(t,
42+
"test-cluster",
43+
"test-namespace",
44+
"nutanix",
45+
"192.168.1.100",
46+
6443)
7547
},
7648
expectedRenderedValuesTemplate: expectedCiliumTemplateForNutanix,
7749
},
7850
}
7951
for _, tt := range tests {
8052
t.Run(tt.name, func(t *testing.T) {
81-
got, err := templateValues(tt.cluster, ciliumTemplate)
53+
got, err := templateValues(tt.cluster(t), ciliumTemplate)
8254
require.NoError(t, err)
8355
assert.Equal(t, tt.expectedRenderedValuesTemplate, got)
8456
})
@@ -110,23 +82,9 @@ func Test_templateValues_TrimPrefixFunction(t *testing.T) {
11082

11183
for _, tt := range tests {
11284
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://" }}"`
85+
cluster := createTestCluster(t, "test-cluster", "test-namespace", "eks", tt.inputHost, 443)
86+
87+
template := `k8sServiceHost: "{{ trimPrefix .ControlPlaneEndpoint.Host "https://" }}"`
13088
expected := `k8sServiceHost: "` + tt.expectedOutput + `"`
13189

13290
got, err := templateValues(cluster, template)
@@ -136,11 +94,47 @@ func Test_templateValues_TrimPrefixFunction(t *testing.T) {
13694
}
13795
}
13896

97+
// createTestCluster creates a test EKS cluster using ClusterBuilder
98+
func createTestCluster(t *testing.T, name, namespace, provider, host string, port int32) *clusterv1.Cluster {
99+
// Create cluster config with kube-proxy disabled
100+
clusterConfigSpec := &apivariables.ClusterConfigSpec{
101+
KubeProxy: &carenv1.KubeProxy{
102+
Mode: carenv1.KubeProxyModeDisabled,
103+
},
104+
}
105+
106+
// Marshal cluster config to cluster variable
107+
variable, err := apivariables.MarshalToClusterVariable(carenv1.ClusterConfigVariableName, clusterConfigSpec)
108+
if err != nil {
109+
t.Fatalf("failed to marshal cluster config to cluster variable: %v", err)
110+
}
111+
112+
topology := &clusterv1.Topology{
113+
Class: "test-cluster-class",
114+
Version: "v1.29.0",
115+
Variables: []clusterv1.ClusterVariable{*variable},
116+
}
117+
118+
cluster := builder.Cluster(namespace, name).
119+
WithLabels(map[string]string{
120+
"cluster.x-k8s.io/provider": provider,
121+
}).
122+
WithTopology(topology).
123+
Build()
124+
125+
// Set ControlPlaneEndpoint after building since ClusterBuilder doesn't support it
126+
cluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
127+
Host: host,
128+
Port: port,
129+
}
130+
131+
return cluster
132+
}
133+
139134
const (
140135
// the template value is sourced from the Cilium values template in the project's helm chart
141136
ciliumTemplate = `
142-
{{- $capiProvider := index .Cluster.Labels "cluster.x-k8s.io/provider" }}
143-
{{- if eq $capiProvider "eks" }}
137+
{{- if eq .Provider "eks" }}
144138
ipam:
145139
mode: eni
146140
{{- else }}
@@ -151,15 +145,9 @@ ipam:
151145
{{- if .EnableKubeProxyReplacement }}
152146
kubeProxyReplacement: true
153147
{{- 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" }}
148+
k8sServiceHost: "{{ trimPrefix .ControlPlaneEndpoint.Host "https://" }}"
149+
k8sServicePort: "{{ .ControlPlaneEndpoint.Port }}"
150+
{{- if eq .Provider "eks" }}
163151
enableIPv4Masquerade: false
164152
eni:
165153
enabled: true
@@ -188,6 +176,7 @@ endpointRoutes:
188176
ipam:
189177
mode: kubernetes
190178
kubeProxyReplacement: true
191-
k8sServiceHost: auto
179+
k8sServiceHost: "192.168.1.100"
180+
k8sServicePort: "6443"
192181
`
193182
)

0 commit comments

Comments
 (0)