@@ -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
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 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+
139165const (
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" }}
144169ipam:
145170 mode: eni
146171{{- else }}
@@ -151,15 +176,9 @@ ipam:
151176{{- if .EnableKubeProxyReplacement }}
152177kubeProxyReplacement: 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" }}
163182enableIPv4Masquerade: false
164183eni:
165184 enabled: true
@@ -188,6 +207,7 @@ endpointRoutes:
188207ipam:
189208 mode: kubernetes
190209kubeProxyReplacement: true
191- k8sServiceHost: auto
210+ k8sServiceHost: "192.168.1.100"
211+ k8sServicePort: "6443"
192212`
193213)
0 commit comments