Skip to content

Commit f4ededd

Browse files
authored
Merge pull request #6208 from sbueringer/pr-add-ref-builtilns-vars
🌱 ClusterClass: add ref and controlPlane name builtin variables
2 parents f42bf51 + 4b91d22 commit f4ededd

File tree

7 files changed

+347
-39
lines changed

7 files changed

+347
-39
lines changed

docs/book/src/tasks/experimental-features/cluster-class/write-clusterclass.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,18 @@ In addition to variables specified in the ClusterClass, the following builtin va
382382
referenced in patches:
383383
- `builtin.cluster.{name,namespace}`
384384
- `builtin.cluster.topology.{version,class}`
385-
- `builtin.controlPlane.{replicas,version}`
385+
- `builtin.controlPlane.{replicas,version,name}`
386386
- Please note, these variables are only available when patching control plane or control plane
387387
machine templates.
388+
- `builtin.controlPlane.machineTemplate.infrastructureRef.name`
389+
- Please note, these variables are only available when using a control plane with machines and
390+
when patching control plane or control plane machine templates.
388391
- `builtin.machineDeployment.{replicas,version,class,name,topologyName}`
389392
- Please note, these variables are only available when patching the templates of a MachineDeployment
390393
and contain the values of the current `MachineDeployment` topology.
394+
- `builtin.machineDeployment.{infrastructureRef.name,bootstrap.configRef.name}`
395+
- Please note, these variables are only available when patching the templates of a MachineDeployment
396+
and contain the values of the current `MachineDeployment` topology.
391397

392398
Builtin variables can be referenced just like regular variables, e.g.:
393399
```yaml

docs/proposals/202105256-cluster-class-and-managed-topologies.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,17 @@ Note: Builtin variables are defined in [Builtin variables](#builtin-variables) b
649649
It’s also possible to use so-called builtin variables in addition to user-defined variables. The following builtin variables are available:
650650
- `builtin.cluster.{name,namespace}`
651651
- `builtin.cluster.topology.{version,class}`
652-
- `builtin.controlPlane.{replicas,version}`
652+
- `builtin.controlPlane.{replicas,version,name}`
653653
- **Note**: these variables are only available when patching control plane or control plane machine templates.
654+
- `builtin.controlPlane.machineTemplate.infrastructureRef.name`
655+
- **Note**: these variables are only available when using a control plane with machines and
656+
when patching control plane or control plane machine templates.
654657
- `builtin.machineDeployment.{replicas,version,class,name,topologyName}`
655658
- **Note**: these variables are only available when patching MachineDeployment templates and contain the values
656659
of the current `MachineDeploymentTopology`.
660+
- `builtin.machineDeployment.{infrastructureRef.name,bootstrap.configRef.name}`
661+
- **Note**: these variables are only available when patching the templates of a MachineDeployment
662+
and contain the values of the current `MachineDeployment` topology.
657663

658664
Builtin variables are available under the `builtin.` prefix. Some examples:
659665
- Usage of `cluster.name` via variable:

internal/controllers/topology/cluster/patches/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func createRequest(blueprint *scope.ClusterBlueprint, desired *scope.ClusterStat
138138
req.Items = append(req.Items, t)
139139

140140
// Calculate controlPlane variables.
141-
controlPlaneVariables, err := variables.ControlPlane(&blueprint.Topology.ControlPlane, desired.ControlPlane.Object)
141+
controlPlaneVariables, err := variables.ControlPlane(&blueprint.Topology.ControlPlane, desired.ControlPlane.Object, desired.ControlPlane.InfrastructureMachineTemplate)
142142
if err != nil {
143143
return nil, errors.Wrapf(err, "failed to calculate ControlPlane variables")
144144
}
@@ -188,7 +188,7 @@ func createRequest(blueprint *scope.ClusterBlueprint, desired *scope.ClusterStat
188188
}
189189

190190
// Calculate MachineDeployment variables.
191-
mdVariables, err := variables.MachineDeployment(mdTopology, md.Object)
191+
mdVariables, err := variables.MachineDeployment(mdTopology, md.Object, md.BootstrapTemplate, md.InfrastructureMachineTemplate)
192192
if err != nil {
193193
return nil, errors.Wrapf(err, "failed to calculate variables for %s", tlog.KObj{Obj: md.Object})
194194
}

internal/controllers/topology/cluster/patches/inline/json_patch_generator_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ func TestGenerate(t *testing.T) {
102102
Variable: pointer.String("builtin.controlPlane.replicas"),
103103
},
104104
},
105+
// test .builtin.controlPlane.machineTemplate.InfrastructureRef.name var.
106+
{
107+
Op: "replace",
108+
Path: "/spec/template/spec/files",
109+
ValueFrom: &clusterv1.JSONPatchValue{
110+
Template: pointer.String(`[{"contentFrom":{"secret":{"key":"control-plane-azure.json","name":"{{ .builtin.controlPlane.machineTemplate.infrastructureRef.name }}-azure-json"}}}]`),
111+
},
112+
},
105113
},
106114
},
107115
},
@@ -121,7 +129,7 @@ func TestGenerate(t *testing.T) {
121129
TemplateType: api.ControlPlaneTemplateType,
122130
},
123131
Variables: map[string]apiextensionsv1.JSON{
124-
"builtin": {Raw: []byte(`{"controlPlane":{"replicas":3}}`)},
132+
"builtin": {Raw: []byte(`{"controlPlane":{"replicas":3,"machineTemplate":{"infrastructureRef":{"name":"controlPlaneInfrastructureMachineTemplate1"}}}}`)},
125133
"variableC": {Raw: []byte(`"C-template"`)},
126134
},
127135
},
@@ -141,8 +149,15 @@ func TestGenerate(t *testing.T) {
141149
{"op":"replace","path":"/spec/valueFrom/template","value":"template bbbbb"},
142150
{"op":"replace","path":"/spec/templatePrecedent","value":"C-template"},
143151
{"op":"replace","path":"/spec/builtinClusterName","value":"cluster-name"},
144-
{"op":"replace","path":"/spec/builtinControlPlaneReplicas","value":3}
145-
]`),
152+
{"op":"replace","path":"/spec/builtinControlPlaneReplicas","value":3},
153+
{"op":"replace","path":"/spec/template/spec/files","value":[{
154+
"contentFrom":{
155+
"secret":{
156+
"key":"control-plane-azure.json",
157+
"name":"controlPlaneInfrastructureMachineTemplate1-azure-json"
158+
}
159+
}
160+
}]}]`),
146161
PatchType: api.JSONPatchType,
147162
},
148163
},

internal/controllers/topology/cluster/patches/variables/variables.go

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,28 @@ type ControlPlaneBuiltins struct {
7373
// being orchestrated.
7474
Version string `json:"version,omitempty"`
7575

76+
// Name is the name of the ControlPlane,
77+
// to which the current template belongs to.
78+
Name string `json:"name,omitempty"`
79+
7680
// Replicas is the value of the replicas field of the ControlPlane object.
7781
Replicas *int64 `json:"replicas,omitempty"`
82+
83+
// MachineTemplate is the value of the .spec.machineTemplate field of the ControlPlane object.
84+
MachineTemplate *ControlPlaneMachineTemplateBuiltins `json:"machineTemplate,omitempty"`
85+
}
86+
87+
// ControlPlaneMachineTemplateBuiltins is the value of the .spec.machineTemplate field of the ControlPlane object.
88+
type ControlPlaneMachineTemplateBuiltins struct {
89+
// InfrastructureRef is the value of the infrastructureRef field of ControlPlane.spec.machineTemplate.
90+
InfrastructureRef ControlPlaneMachineTemplateInfrastructureRefBuiltins `json:"infrastructureRef,omitempty"`
91+
}
92+
93+
// ControlPlaneMachineTemplateInfrastructureRefBuiltins is the value of the infrastructureRef field of
94+
// ControlPlane.spec.machineTemplate.
95+
type ControlPlaneMachineTemplateInfrastructureRefBuiltins struct {
96+
// Name of the infrastructureRef.
97+
Name string `json:"name,omitempty"`
7898
}
7999

80100
// MachineDeploymentBuiltins represents builtin MachineDeployment variables.
@@ -102,6 +122,33 @@ type MachineDeploymentBuiltins struct {
102122
// Replicas is the value of the replicas field of the MachineDeployment,
103123
// to which the current template belongs to.
104124
Replicas *int64 `json:"replicas,omitempty"`
125+
126+
// Bootstrap is the value of the .spec.template.spec.bootstrap field of the MachineDeployment.
127+
Bootstrap *MachineDeploymentBootstrapBuiltins `json:"bootstrap,omitempty"`
128+
129+
// InfrastructureRef is the value of the .spec.template.spec.bootstrap field of the MachineDeployment.
130+
InfrastructureRef *MachineDeploymentInfrastructureRefBuiltins `json:"infrastructureRef,omitempty"`
131+
}
132+
133+
// MachineDeploymentBootstrapBuiltins is the value of the .spec.template.spec.bootstrap field
134+
// of the MachineDeployment.
135+
type MachineDeploymentBootstrapBuiltins struct {
136+
// ConfigRef is the value of the .spec.template.spec.bootstrap.configRef field of the MachineDeployment.
137+
ConfigRef *MachineDeploymentBootstrapConfigRefBuiltins `json:"configRef,omitempty"`
138+
}
139+
140+
// MachineDeploymentBootstrapConfigRefBuiltins is the value of the .spec.template.spec.bootstrap.configRef
141+
// field of the MachineDeployment.
142+
type MachineDeploymentBootstrapConfigRefBuiltins struct {
143+
// Name of the bootstrap.configRef.
144+
Name string `json:"name,omitempty"`
145+
}
146+
147+
// MachineDeploymentInfrastructureRefBuiltins is the value of the .spec.template.spec.infrastructureRef field
148+
// of the MachineDeployment.
149+
type MachineDeploymentInfrastructureRefBuiltins struct {
150+
// Name of the infrastructureRef.
151+
Name string `json:"name,omitempty"`
105152
}
106153

107154
// VariableMap is a name/value map of variables.
@@ -139,32 +186,42 @@ func Global(clusterTopology *clusterv1.Topology, cluster *clusterv1.Cluster) (Va
139186
}
140187

141188
// ControlPlane returns variables that apply to templates belonging to the ControlPlane.
142-
func ControlPlane(controlPlaneTopology *clusterv1.ControlPlaneTopology, controlPlane *unstructured.Unstructured) (VariableMap, error) {
189+
func ControlPlane(cpTopology *clusterv1.ControlPlaneTopology, cp, cpInfrastructureMachineTemplate *unstructured.Unstructured) (VariableMap, error) {
143190
variables := VariableMap{}
144191

145192
// Construct builtin variable.
146193
builtin := Builtins{
147-
ControlPlane: &ControlPlaneBuiltins{},
194+
ControlPlane: &ControlPlaneBuiltins{
195+
Name: cp.GetName(),
196+
},
148197
}
149198

150199
// If it is required to manage the number of replicas for the ControlPlane, set the corresponding variable.
151200
// NOTE: If the Cluster.spec.topology.controlPlane.replicas field is nil, the topology reconciler won't set
152201
// the replicas field on the ControlPlane. This happens either when the ControlPlane provider does
153202
// not implement support for this field or the default value of the ControlPlane is used.
154-
if controlPlaneTopology.Replicas != nil {
155-
replicas, err := contract.ControlPlane().Replicas().Get(controlPlane)
203+
if cpTopology.Replicas != nil {
204+
replicas, err := contract.ControlPlane().Replicas().Get(cp)
156205
if err != nil {
157206
return nil, errors.Wrap(err, "failed to get spec.replicas from the ControlPlane")
158207
}
159208
builtin.ControlPlane.Replicas = replicas
160209
}
161210

162-
version, err := contract.ControlPlane().Version().Get(controlPlane)
211+
version, err := contract.ControlPlane().Version().Get(cp)
163212
if err != nil {
164213
return nil, errors.Wrap(err, "failed to get spec.version from the ControlPlane")
165214
}
166215
builtin.ControlPlane.Version = *version
167216

217+
if cpInfrastructureMachineTemplate != nil {
218+
builtin.ControlPlane.MachineTemplate = &ControlPlaneMachineTemplateBuiltins{
219+
InfrastructureRef: ControlPlaneMachineTemplateInfrastructureRefBuiltins{
220+
Name: cpInfrastructureMachineTemplate.GetName(),
221+
},
222+
}
223+
}
224+
168225
if err := setVariable(variables, BuiltinsName, builtin); err != nil {
169226
return nil, err
170227
}
@@ -173,7 +230,7 @@ func ControlPlane(controlPlaneTopology *clusterv1.ControlPlaneTopology, controlP
173230
}
174231

175232
// MachineDeployment returns variables that apply to templates belonging to a MachineDeployment.
176-
func MachineDeployment(mdTopology *clusterv1.MachineDeploymentTopology, md *clusterv1.MachineDeployment) (VariableMap, error) {
233+
func MachineDeployment(mdTopology *clusterv1.MachineDeploymentTopology, md *clusterv1.MachineDeployment, mdBootstrapTemplate, mdInfrastructureMachineTemplate *unstructured.Unstructured) (VariableMap, error) {
177234
variables := VariableMap{}
178235

179236
// Add variables overrides for the MachineDeployment.
@@ -196,6 +253,20 @@ func MachineDeployment(mdTopology *clusterv1.MachineDeploymentTopology, md *clus
196253
builtin.MachineDeployment.Replicas = pointer.Int64(int64(*md.Spec.Replicas))
197254
}
198255

256+
if mdBootstrapTemplate != nil {
257+
builtin.MachineDeployment.Bootstrap = &MachineDeploymentBootstrapBuiltins{
258+
ConfigRef: &MachineDeploymentBootstrapConfigRefBuiltins{
259+
Name: mdBootstrapTemplate.GetName(),
260+
},
261+
}
262+
}
263+
264+
if mdInfrastructureMachineTemplate != nil {
265+
builtin.MachineDeployment.InfrastructureRef = &MachineDeploymentInfrastructureRefBuiltins{
266+
Name: mdInfrastructureMachineTemplate.GetName(),
267+
}
268+
}
269+
199270
if err := setVariable(variables, BuiltinsName, builtin); err != nil {
200271
return nil, err
201272
}

0 commit comments

Comments
 (0)