|
4 | 4 | package cluster |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bytes" |
7 | 8 | "context" |
8 | 9 | "fmt" |
9 | 10 | "net/http" |
| 11 | + "strings" |
| 12 | + "text/template" |
10 | 13 |
|
11 | 14 | v1 "k8s.io/api/admission/v1" |
12 | 15 | corev1 "k8s.io/api/core/v1" |
@@ -117,6 +120,33 @@ func hasSkipAnnotation(cluster *clusterv1.Cluster) bool { |
117 | 120 | return ok && val == "true" |
118 | 121 | } |
119 | 122 |
|
| 123 | +// templateValues applies Go template expansion to the values string using only ControlPlaneEndpoint. |
| 124 | +func templateValues(cluster *clusterv1.Cluster, text string) (string, error) { |
| 125 | + funcMap := template.FuncMap{ |
| 126 | + "trimPrefix": strings.TrimPrefix, |
| 127 | + } |
| 128 | + valuesTemplate, err := template.New("").Funcs(funcMap).Parse(text) |
| 129 | + if err != nil { |
| 130 | + return "", fmt.Errorf("failed to parse template: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + type input struct { |
| 134 | + ControlPlaneEndpoint clusterv1.APIEndpoint |
| 135 | + } |
| 136 | + |
| 137 | + templateInput := input{ |
| 138 | + ControlPlaneEndpoint: cluster.Spec.ControlPlaneEndpoint, |
| 139 | + } |
| 140 | + |
| 141 | + var b bytes.Buffer |
| 142 | + err = valuesTemplate.Execute(&b, templateInput) |
| 143 | + if err != nil { |
| 144 | + return "", fmt.Errorf("failed templating values: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + return b.String(), nil |
| 148 | +} |
| 149 | + |
120 | 150 | type ciliumValues struct { |
121 | 151 | KubeProxyReplacement bool `json:"kubeProxyReplacement"` |
122 | 152 | } |
@@ -156,9 +186,20 @@ func getCiliumValues( |
156 | 186 | return nil, nil |
157 | 187 | } |
158 | 188 |
|
159 | | - // Unmarshal the YAML |
| 189 | + // Apply templating to the values |
| 190 | + templatedValues, err := templateValues(cluster, valuesYAML) |
| 191 | + if err != nil { |
| 192 | + return nil, fmt.Errorf( |
| 193 | + "failed to template Cilium values from ConfigMap %s/%s: %w", |
| 194 | + configMapNamespace, |
| 195 | + configMapName, |
| 196 | + err, |
| 197 | + ) |
| 198 | + } |
| 199 | + |
| 200 | + // Unmarshal the templated YAML |
160 | 201 | values := &ciliumValues{} |
161 | | - if err := yaml.Unmarshal([]byte(valuesYAML), values); err != nil { |
| 202 | + if err := yaml.Unmarshal([]byte(templatedValues), values); err != nil { |
162 | 203 | return nil, fmt.Errorf( |
163 | 204 | "failed to unmarshal Cilium values from ConfigMap %s/%s: %w", |
164 | 205 | configMapNamespace, |
|
0 commit comments