Skip to content

Commit c388ee7

Browse files
committed
feat(charts): allow overriding of the cluster's name
Signed-off-by: Colden Cullen <[email protected]>
1 parent 4b6d47f commit c388ee7

6 files changed

Lines changed: 110 additions & 9 deletions

File tree

‎charts/cozystack/templates/_helpers.tpl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ cluster:
103103
{{- toYaml .Values.podSubnets | nindent 6 }}
104104
serviceSubnets:
105105
{{- toYaml .Values.serviceSubnets | nindent 6 }}
106-
clusterName: "{{ .Chart.Name }}"
106+
clusterName: {{ .Values.clusterName | default .Chart.Name | regexFind "^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" | required "clusterName must be a valid DNS-1123 label" | quote }}
107107
controlPlane:
108108
endpoint: {{ required "values.yaml: `endpoint` must be set to the cluster control-plane URL (e.g. https://<vip>:6443). This field is cluster-wide: every node's kubelet and kube-proxy dials it, so it cannot be auto-derived from the current node's IP -- `talm template` runs once per node and has no way to reconcile per-node IPs into a single shared endpoint. For multi-node setups use a VIP (cozystack floatingIP) or an external load balancer; for single-node clusters the node's routable IP works." .Values.endpoint | quote }}
109109
{{- if eq .MachineType "controlplane" }}

‎charts/cozystack/values.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
# Example: endpoint: "https://192.168.0.1:6443"
2020
endpoint: ""
2121

22+
# Optional override for the cluster's name (defaults to Chart.Name).
23+
# Note that changing this value on a live cluster is considered
24+
# dangerous as it is baked into PKI (cert SANs) and ETCD identity at
25+
# bootstrap.
26+
clusterName: ""
27+
2228
clusterDomain: cozy.local
2329
# Layer-2 VIP for cozystack multi-node setups. When set, the chart
2430
# emits a Layer2VIPConfig document pinning this IP as a floating

‎charts/generic/templates/_helpers.tpl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ machine:
4545

4646
{{- /* Shared cluster section */ -}}
4747
{{- define "talos.config.cluster" }}
48+
4849
cluster:
4950
network:
5051
podSubnets:
5152
{{- toYaml .Values.podSubnets | nindent 6 }}
5253
serviceSubnets:
5354
{{- toYaml .Values.serviceSubnets | nindent 6 }}
54-
clusterName: "{{ .Chart.Name }}"
55+
clusterName: {{ .Values.clusterName | default .Chart.Name | regexFind "^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" | required "clusterName must be a valid DNS-1123 label" | quote }}
5556
controlPlane:
5657
endpoint: {{ required "values.yaml: `endpoint` must be set to the cluster control-plane URL (e.g. https://<vip>:6443). This field is cluster-wide: every node's kubelet and kube-proxy dials it, so it cannot be auto-derived from the current node's IP -- `talm template` runs once per node and has no way to reconcile per-node IPs into a single shared endpoint. For multi-node setups use a VIP or an external load balancer; for single-node clusters the node's routable IP works." .Values.endpoint | quote }}
5758
{{- if eq .MachineType "controlplane" }}

‎charts/generic/values.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ floatingIP: ""
3838
# Example: vipLink: eth0.4000
3939
vipLink: ""
4040

41+
# Optional override for the cluster's name (defaults to Chart.Name).
42+
# Note that changing this value on a live cluster is considered
43+
# dangerous as it is baked into PKI (cert SANs) and ETCD identity at
44+
# bootstrap.
45+
clusterName: ""
46+
4147
podSubnets:
4248
- 10.244.0.0/16
4349
serviceSubnets:

‎pkg/commands/talosconfig.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This command:
4343
4. Re-encrypts if encryption is used
4444
4545
Use this command when your client certificate has expired.`,
46-
Args: cobra.NoArgs,
46+
Args: cobra.NoArgs,
4747
PreRunE: func(cmd *cobra.Command, args []string) error {
4848
// Ensure project root is detected
4949
if !Config.RootDirExplicit {
@@ -190,8 +190,19 @@ func regenerateTalosconfig() error {
190190
return nil
191191
}
192192

193-
// getClusterNameFromChart reads the cluster name from Chart.yaml
193+
// getClusterNameFromChart reads the cluster name from values.yaml or Chart.yaml
194194
func getClusterNameFromChart() string {
195+
valuesYamlPath := filepath.Join(Config.RootDir, "values.yaml")
196+
if data, err := os.ReadFile(valuesYamlPath); err == nil {
197+
var valuesData struct {
198+
ClusterName string `yaml:"clusterName"`
199+
}
200+
201+
if err = yaml.Unmarshal(data, &valuesData); err == nil && valuesData.ClusterName != "" {
202+
return valuesData.ClusterName
203+
}
204+
}
205+
195206
chartYamlPath := filepath.Join(Config.RootDir, "Chart.yaml")
196207
data, err := os.ReadFile(chartYamlPath)
197208
if err != nil {
@@ -208,4 +219,3 @@ func getClusterNameFromChart() string {
208219

209220
return chartData.Name
210221
}
211-

‎pkg/engine/render_test.go‎

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestLegacyCozystack_ControlPlane(t *testing.T) {
228228

229229
// Legacy format: cluster section present
230230
assertContains(t, output, "cluster:")
231-
assertContains(t, output, "clusterName:")
231+
assertContains(t, output, "clusterName: \"cozystack\"")
232232
assertContains(t, output, "controlPlane:")
233233
assertContains(t, output, "endpoint:")
234234

@@ -289,7 +289,7 @@ func TestLegacyGeneric_ControlPlane(t *testing.T) {
289289

290290
// Legacy format: cluster section present
291291
assertContains(t, output, "cluster:")
292-
assertContains(t, output, "clusterName:")
292+
assertContains(t, output, "clusterName: \"generic\"")
293293
assertContains(t, output, "controlPlane:")
294294
assertContains(t, output, "endpoint:")
295295

@@ -342,7 +342,7 @@ func TestMultiDocCozystack_ControlPlane(t *testing.T) {
342342

343343
// Multi-doc: cluster section unchanged
344344
assertContains(t, output, "cluster:")
345-
assertContains(t, output, "clusterName:")
345+
assertContains(t, output, "clusterName: \"cozystack\"")
346346
assertContains(t, output, "controlPlane:")
347347
assertContains(t, output, "allowSchedulingOnControlPlanes:")
348348
assertContains(t, output, "etcd:")
@@ -504,7 +504,7 @@ func TestMultiDocGeneric_ControlPlane(t *testing.T) {
504504

505505
// Multi-doc: cluster section still present
506506
assertContains(t, output, "cluster:")
507-
assertContains(t, output, "clusterName:")
507+
assertContains(t, output, "clusterName: \"generic\"")
508508
assertContains(t, output, "controlPlane:")
509509
assertContains(t, output, "endpoint:")
510510

@@ -4107,6 +4107,84 @@ func TestMultiDocCozystack_EndpointRequired(t *testing.T) {
41074107
}
41084108
}
41094109

4110+
// TestMultiDocCozystack_InvalidClusterNameOverride ensures invalid
4111+
// clusterName overrides are rejected
4112+
func TestMultiDocCozystack_InvalidClusterNameOverride(t *testing.T) {
4113+
origLookup := helmEngine.LookupFunc
4114+
t.Cleanup(func() { helmEngine.LookupFunc = origLookup })
4115+
helmEngine.LookupFunc = simpleNicLookup()
4116+
4117+
chrt, err := loader.LoadDir("../../charts/cozystack")
4118+
if err != nil {
4119+
t.Fatalf("load chart: %v", err)
4120+
}
4121+
values := make(map[string]any)
4122+
maps.Copy(values, chrt.Values)
4123+
values["clusterName"] = "InvalidClusterName"
4124+
4125+
eng := helmEngine.Engine{}
4126+
_, err = eng.Render(chrt, chartutil.Values{
4127+
"Values": values,
4128+
"TalosVersion": "v1.12",
4129+
})
4130+
if err == nil {
4131+
t.Fatal("expected render to fail with required() error when clusterName is invalid")
4132+
}
4133+
if !strings.Contains(err.Error(), "clusterName") {
4134+
t.Errorf("error should mention 'clusterName'; got: %v", err)
4135+
}
4136+
}
4137+
4138+
// TestMultiDocCozystack_ValidClusterNameOverride ensures clusterName
4139+
// overrides make it through to the result.
4140+
func TestMultiDocCozystack_ValidClusterNameOverride(t *testing.T) {
4141+
result := renderCozystackWith(t, simpleNicLookup(), map[string]any{
4142+
"clusterName": "differentclustername",
4143+
})
4144+
4145+
assertContains(t, result, "clusterName: \"differentclustername\"")
4146+
}
4147+
4148+
// TestMultiDocGeneric_InvalidSubnetsFallsBackToDiscovery mirrors the
4149+
// cozystack-side smoke test for ensuring invalid clusterName
4150+
// overrides are rejected.
4151+
func TestMultiDocGeneric_InvalidClusterNameOverride(t *testing.T) {
4152+
origLookup := helmEngine.LookupFunc
4153+
t.Cleanup(func() { helmEngine.LookupFunc = origLookup })
4154+
helmEngine.LookupFunc = simpleNicLookup()
4155+
4156+
chrt, err := loader.LoadDir("../../charts/generic")
4157+
if err != nil {
4158+
t.Fatalf("load chart: %v", err)
4159+
}
4160+
values := make(map[string]any)
4161+
maps.Copy(values, chrt.Values)
4162+
values["clusterName"] = "InvalidClusterName"
4163+
4164+
eng := helmEngine.Engine{}
4165+
_, err = eng.Render(chrt, chartutil.Values{
4166+
"Values": values,
4167+
"TalosVersion": "v1.12",
4168+
})
4169+
if err == nil {
4170+
t.Fatal("expected render to fail with required() error when clusterName is invalid")
4171+
}
4172+
if !strings.Contains(err.Error(), "clusterName") {
4173+
t.Errorf("error should mention 'clusterName'; got: %v", err)
4174+
}
4175+
}
4176+
4177+
// TestMultiDocGeneric_ValidSubnetsFallsBackToDiscovery mirrors the
4178+
// cozystack-side smoke test for ensuring clusterName overrides make
4179+
// it through to the result.
4180+
func TestMultiDocGeneric_ValidClusterNameOverride(t *testing.T) {
4181+
result := renderGenericWith(t, simpleNicLookup(), map[string]any{
4182+
"clusterName": "differentclustername",
4183+
})
4184+
4185+
assertContains(t, result, "clusterName: \"differentclustername\"")
4186+
}
4187+
41104188
// TestMultiDocGeneric_ValidSubnetsFallsBackToDiscovery mirrors the
41114189
// cozystack-side smoke test for the generic preset. A single
41124190
// representative assertion proves the edits apply symmetrically to

0 commit comments

Comments
 (0)