@@ -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
1618func 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+
139134const (
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" }}
144138ipam:
145139 mode: eni
146140{{- else }}
@@ -151,15 +145,9 @@ ipam:
151145{{- if .EnableKubeProxyReplacement }}
152146kubeProxyReplacement: 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" }}
163151enableIPv4Masquerade: false
164152eni:
165153 enabled: true
@@ -188,6 +176,7 @@ endpointRoutes:
188176ipam:
189177 mode: kubernetes
190178kubeProxyReplacement: true
191- k8sServiceHost: auto
179+ k8sServiceHost: "192.168.1.100"
180+ k8sServicePort: "6443"
192181`
193182)
0 commit comments