|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cilium |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 13 | + controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_templateValues(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + cluster *clusterv1.Cluster |
| 20 | + expectedRenderedValuesTemplate string |
| 21 | + }{ |
| 22 | + { |
| 23 | + 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 | + }, |
| 47 | + }, |
| 48 | + expectedRenderedValuesTemplate: expectedCiliumTemplateForEKS, |
| 49 | + }, |
| 50 | + { |
| 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 | + }, |
| 75 | + }, |
| 76 | + expectedRenderedValuesTemplate: expectedCiliumTemplateForNutanix, |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + got, err := templateValues(tt.cluster, ciliumTemplate) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Equal(t, tt.expectedRenderedValuesTemplate, got) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func Test_templateValues_TrimPrefixFunction(t *testing.T) { |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + inputHost string |
| 92 | + expectedOutput string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "trim https prefix", |
| 96 | + inputHost: "https://api.example.com", |
| 97 | + expectedOutput: "api.example.com", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "no prefix to trim", |
| 101 | + inputHost: "api.example.com", |
| 102 | + expectedOutput: "api.example.com", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "trim https prefix with port", |
| 106 | + inputHost: "https://api.example.com:8443", |
| 107 | + expectedOutput: "api.example.com:8443", |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + 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://" }}"` |
| 130 | + expected := `k8sServiceHost: "` + tt.expectedOutput + `"` |
| 131 | + |
| 132 | + got, err := templateValues(cluster, template) |
| 133 | + require.NoError(t, err) |
| 134 | + assert.Equal(t, expected, got) |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +const ( |
| 140 | + // the template value is sourced from the Cilium values template in the project's helm chart |
| 141 | + ciliumTemplate = ` |
| 142 | +{{- $capiProvider := index .Cluster.Labels "cluster.x-k8s.io/provider" }} |
| 143 | +{{- if eq $capiProvider "eks" }} |
| 144 | +ipam: |
| 145 | + mode: eni |
| 146 | +{{- else }} |
| 147 | +ipam: |
| 148 | + mode: kubernetes |
| 149 | +{{- end }} |
| 150 | +
|
| 151 | +{{- if .EnableKubeProxyReplacement }} |
| 152 | +kubeProxyReplacement: true |
| 153 | +{{- 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" }} |
| 163 | +enableIPv4Masquerade: false |
| 164 | +eni: |
| 165 | + enabled: true |
| 166 | + awsReleaseExcessIPs: true |
| 167 | +routingMode: native |
| 168 | +endpointRoutes: |
| 169 | + enabled: true |
| 170 | +{{- end }} |
| 171 | +` |
| 172 | + expectedCiliumTemplateForEKS = ` |
| 173 | +ipam: |
| 174 | + mode: eni |
| 175 | +kubeProxyReplacement: true |
| 176 | +k8sServiceHost: "test.eks.amazonaws.com" |
| 177 | +k8sServicePort: "443" |
| 178 | +enableIPv4Masquerade: false |
| 179 | +eni: |
| 180 | + enabled: true |
| 181 | + awsReleaseExcessIPs: true |
| 182 | +routingMode: native |
| 183 | +endpointRoutes: |
| 184 | + enabled: true |
| 185 | +` |
| 186 | + |
| 187 | + expectedCiliumTemplateForNutanix = ` |
| 188 | +ipam: |
| 189 | + mode: kubernetes |
| 190 | +kubeProxyReplacement: true |
| 191 | +k8sServiceHost: auto |
| 192 | +` |
| 193 | +) |
0 commit comments