Skip to content

Commit 66c5e46

Browse files
committed
fix: cilium webhook with templated values
1 parent c6a5709 commit 66c5e46

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

pkg/webhook/cluster/cilium_configuration_validator.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package cluster
55

66
import (
7+
"bytes"
78
"context"
89
"fmt"
910
"net/http"
11+
"strings"
12+
"text/template"
1013

1114
v1 "k8s.io/api/admission/v1"
1215
corev1 "k8s.io/api/core/v1"
@@ -117,6 +120,33 @@ func hasSkipAnnotation(cluster *clusterv1.Cluster) bool {
117120
return ok && val == "true"
118121
}
119122

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+
120150
type ciliumValues struct {
121151
KubeProxyReplacement bool `json:"kubeProxyReplacement"`
122152
}
@@ -156,9 +186,20 @@ func getCiliumValues(
156186
return nil, nil
157187
}
158188

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
160201
values := &ciliumValues{}
161-
if err := yaml.Unmarshal([]byte(valuesYAML), values); err != nil {
202+
if err := yaml.Unmarshal([]byte(templatedValues), values); err != nil {
162203
return nil, fmt.Errorf(
163204
"failed to unmarshal Cilium values from ConfigMap %s/%s: %w",
164205
configMapNamespace,

0 commit comments

Comments
 (0)