Skip to content

Commit 3a966a2

Browse files
srm09sbueringer
authored andcommitted
Extend ControlPlane/Deployment/Pool builtin to include metadata
This patch extends the ControlPlane, MachineDeployment and MachinePool builtins to include metadata. Signed-off-by: Sagar Muchhal <[email protected]>
1 parent 8dfdece commit 3a966a2

File tree

10 files changed

+139
-15
lines changed

10 files changed

+139
-15
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,20 @@ referenced in patches:
535535
- `builtin.cluster.{name,namespace,uid}`
536536
- `builtin.cluster.topology.{version,class}`
537537
- `builtin.cluster.network.{serviceDomain,services,pods,ipFamily}`
538-
- `builtin.controlPlane.{replicas,version,name}`
538+
- Note: ipFamily is deprecated and will be removed in a future release. see https://github.com/kubernetes-sigs/cluster-api/issues/7521.
539+
- `builtin.controlPlane.{replicas,version,name,metadata.labels,metadata.annotations}`
539540
- Please note, these variables are only available when patching control plane or control plane
540541
machine templates.
541542
- `builtin.controlPlane.machineTemplate.infrastructureRef.name`
542543
- Please note, these variables are only available when using a control plane with machines and
543544
when patching control plane or control plane machine templates.
544-
- `builtin.machineDeployment.{replicas,version,class,name,topologyName}`
545+
- `builtin.machineDeployment.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
545546
- Please note, these variables are only available when patching the templates of a MachineDeployment
546547
and contain the values of the current `MachineDeployment` topology.
547548
- `builtin.machineDeployment.{infrastructureRef.name,bootstrap.configRef.name}`
548549
- Please note, these variables are only available when patching the templates of a MachineDeployment
549550
and contain the values of the current `MachineDeployment` topology.
550-
- `builtin.machinePool.{replicas,version,class,name,topologyName}`
551+
- `builtin.machinePool.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
551552
- Please note, these variables are only available when patching the templates of a MachinePool
552553
and contain the values of the current `MachinePool` topology.
553554
- `builtin.machinePool.{infrastructureRef.name,bootstrap.configRef.name}`

exp/runtime/hooks/api/v1alpha1/topologymutation_variable_types.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
import "k8s.io/apimachinery/pkg/types"
19+
import (
20+
"k8s.io/apimachinery/pkg/types"
21+
22+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
23+
)
2024

2125
// BuiltinsName is the name of the builtin variable.
2226
const BuiltinsName = "builtin"
@@ -81,6 +85,9 @@ type ControlPlaneBuiltins struct {
8185
// being orchestrated.
8286
Version string `json:"version,omitempty"`
8387

88+
// Metadata is the metadata set on the ControlPlane object.
89+
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`
90+
8491
// Name is the name of the ControlPlane,
8592
// to which the current template belongs to.
8693
Name string `json:"name,omitempty"`
@@ -115,6 +122,9 @@ type MachineDeploymentBuiltins struct {
115122
// being orchestrated.
116123
Version string `json:"version,omitempty"`
117124

125+
// Metadata is the metadata set on the MachineDeployment.
126+
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`
127+
118128
// Class is the class name of the MachineDeployment,
119129
// to which the current template belongs to.
120130
Class string `json:"class,omitempty"`
@@ -148,6 +158,9 @@ type MachinePoolBuiltins struct {
148158
// being orchestrated.
149159
Version string `json:"version,omitempty"`
150160

161+
// Metadata is the metadata set on the MachinePool.
162+
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`
163+
151164
// Class is the class name of the MachinePool,
152165
// to which the current template belongs to.
153166
Class string `json:"class,omitempty"`

exp/runtime/hooks/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/runtime/hooks/api/v1alpha1/zz_generated.openapi.go

Lines changed: 21 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
11391139
Workers: &clusterv1.WorkersTopology{
11401140
MachineDeployments: []clusterv1.MachineDeploymentTopology{
11411141
{
1142-
Metadata: clusterv1.ObjectMeta{},
1142+
Metadata: clusterv1.ObjectMeta{Labels: map[string]string{"foo": "bar"}, Annotations: map[string]string{"fizz": "buzz"}},
11431143
Class: "default-worker",
11441144
Name: "default-worker-topo1",
11451145
Variables: &clusterv1.MachineDeploymentVariables{
@@ -1161,7 +1161,7 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
11611161
},
11621162
MachinePools: []clusterv1.MachinePoolTopology{
11631163
{
1164-
Metadata: clusterv1.ObjectMeta{},
1164+
Metadata: clusterv1.ObjectMeta{Labels: map[string]string{"foo": "bar"}, Annotations: map[string]string{"fizz": "buzz"}},
11651165
Class: "default-mp-worker",
11661166
Name: "default-mp-worker-topo1",
11671167
Variables: &clusterv1.MachinePoolVariables{

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ func ControlPlane(cpTopology *clusterv1.ControlPlaneTopology, cp, cpInfrastructu
129129
}
130130
builtin.ControlPlane.Replicas = replicas
131131
}
132+
if cp.GetLabels() != nil || cp.GetAnnotations() != nil {
133+
builtin.ControlPlane.Metadata = &clusterv1.ObjectMeta{
134+
Annotations: cp.GetAnnotations(),
135+
Labels: cp.GetLabels(),
136+
}
137+
}
132138

133139
version, err := contract.ControlPlane().Version().Get(cp)
134140
if err != nil {
@@ -182,6 +188,12 @@ func MachineDeployment(mdTopology *clusterv1.MachineDeploymentTopology, md *clus
182188
if md.Spec.Replicas != nil {
183189
builtin.MachineDeployment.Replicas = ptr.To[int64](int64(*md.Spec.Replicas))
184190
}
191+
if md.Labels != nil || md.Annotations != nil {
192+
builtin.MachineDeployment.Metadata = &clusterv1.ObjectMeta{
193+
Annotations: md.Annotations,
194+
Labels: md.Labels,
195+
}
196+
}
185197

186198
if mdBootstrapTemplate != nil {
187199
builtin.MachineDeployment.Bootstrap = &runtimehooksv1.MachineBootstrapBuiltins{
@@ -235,6 +247,12 @@ func MachinePool(mpTopology *clusterv1.MachinePoolTopology, mp *expv1.MachinePoo
235247
if mp.Spec.Replicas != nil {
236248
builtin.MachinePool.Replicas = ptr.To[int64](int64(*mp.Spec.Replicas))
237249
}
250+
if mp.Labels != nil || mp.Annotations != nil {
251+
builtin.MachinePool.Metadata = &clusterv1.ObjectMeta{
252+
Annotations: mp.Annotations,
253+
Labels: mp.Labels,
254+
}
255+
}
238256

239257
if mpBootstrapObject != nil {
240258
builtin.MachinePool.Bootstrap = &runtimehooksv1.MachineBootstrapBuiltins{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ func TestControlPlane(t *testing.T) {
445445
controlPlane: builder.ControlPlane(metav1.NamespaceDefault, "controlPlane1").
446446
WithReplicas(3).
447447
WithVersion("v1.21.1").
448+
WithLabels(map[string]string{"foo": "bar"}).
449+
WithAnnotations(map[string]string{"fizz": "buzz"}).
448450
Build(),
449451
want: []runtimehooksv1.Variable{
450452
{
@@ -460,6 +462,7 @@ func TestControlPlane(t *testing.T) {
460462
Value: toJSONCompact(`{
461463
"controlPlane":{
462464
"version": "v1.21.1",
465+
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
463466
"name":"controlPlane1",
464467
"replicas":3
465468
}}`),
@@ -503,6 +506,7 @@ func TestControlPlane(t *testing.T) {
503506
controlPlane: builder.ControlPlane(metav1.NamespaceDefault, "controlPlane1").
504507
WithReplicas(3).
505508
WithVersion("v1.21.1").
509+
WithLabels(map[string]string{"foo": "bar"}).
506510
Build(),
507511
want: []runtimehooksv1.Variable{
508512
{
@@ -518,6 +522,7 @@ func TestControlPlane(t *testing.T) {
518522
Value: toJSONCompact(`{
519523
"controlPlane":{
520524
"version": "v1.21.1",
525+
"metadata": {"labels":{"foo":"bar"}},
521526
"name":"controlPlane1",
522527
"replicas":3
523528
}}`),
@@ -684,6 +689,8 @@ func TestMachineDeployment(t *testing.T) {
684689
md: builder.MachineDeployment(metav1.NamespaceDefault, "md1").
685690
WithReplicas(3).
686691
WithVersion("v1.21.1").
692+
WithLabels(map[string]string{"foo": "bar"}).
693+
WithAnnotations(map[string]string{"fizz": "buzz"}).
687694
Build(),
688695
want: []runtimehooksv1.Variable{
689696
{
@@ -699,6 +706,7 @@ func TestMachineDeployment(t *testing.T) {
699706
Value: toJSONCompact(`{
700707
"machineDeployment":{
701708
"version": "v1.21.1",
709+
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
702710
"class": "md-class",
703711
"name": "md1",
704712
"topologyName": "md-topology",
@@ -1051,6 +1059,8 @@ func TestMachinePool(t *testing.T) {
10511059
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
10521060
WithReplicas(3).
10531061
WithVersion("v1.21.1").
1062+
WithLabels(map[string]string{"foo": "bar"}).
1063+
WithAnnotations(map[string]string{"fizz": "buzz"}).
10541064
Build(),
10551065
want: []runtimehooksv1.Variable{
10561066
{
@@ -1066,6 +1076,7 @@ func TestMachinePool(t *testing.T) {
10661076
Value: toJSONCompact(`{
10671077
"machinePool":{
10681078
"version": "v1.21.1",
1079+
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
10691080
"class": "mp-class",
10701081
"name": "mp1",
10711082
"topologyName": "mp-topology",

internal/test/builder/builders.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,18 @@ func (c *TestControlPlaneBuilder) WithVersion(version string) *TestControlPlaneB
14511451
return c
14521452
}
14531453

1454+
// WithLabels adds the passed labels to the ControlPlaneBuilder.
1455+
func (c *ControlPlaneBuilder) WithLabels(labels map[string]string) *ControlPlaneBuilder {
1456+
c.obj.SetLabels(labels)
1457+
return c
1458+
}
1459+
1460+
// WithAnnotations adds the passed annotations to the ControlPlaneBuilder.
1461+
func (c *ControlPlaneBuilder) WithAnnotations(annotations map[string]string) *ControlPlaneBuilder {
1462+
c.obj.SetAnnotations(annotations)
1463+
return c
1464+
}
1465+
14541466
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
14551467
// to the value of the spec field.
14561468
//
@@ -1522,6 +1534,7 @@ type MachinePoolBuilder struct {
15221534
clusterName string
15231535
replicas *int32
15241536
labels map[string]string
1537+
annotations map[string]string
15251538
status *expv1.MachinePoolStatus
15261539
minReadySeconds *int32
15271540
}
@@ -1552,6 +1565,12 @@ func (m *MachinePoolBuilder) WithLabels(labels map[string]string) *MachinePoolBu
15521565
return m
15531566
}
15541567

1568+
// WithAnnotations adds the given annotations to the MachinePoolBuilder.
1569+
func (m *MachinePoolBuilder) WithAnnotations(annotations map[string]string) *MachinePoolBuilder {
1570+
m.annotations = annotations
1571+
return m
1572+
}
1573+
15551574
// WithVersion sets the passed version on the MachinePool spec.
15561575
func (m *MachinePoolBuilder) WithVersion(version string) *MachinePoolBuilder {
15571576
m.version = &version
@@ -1590,9 +1609,10 @@ func (m *MachinePoolBuilder) Build() *expv1.MachinePool {
15901609
APIVersion: expv1.GroupVersion.String(),
15911610
},
15921611
ObjectMeta: metav1.ObjectMeta{
1593-
Name: m.name,
1594-
Namespace: m.namespace,
1595-
Labels: m.labels,
1612+
Name: m.name,
1613+
Namespace: m.namespace,
1614+
Labels: m.labels,
1615+
Annotations: m.annotations,
15961616
},
15971617
Spec: expv1.MachinePoolSpec{
15981618
ClusterName: m.clusterName,
@@ -1630,6 +1650,7 @@ type MachineDeploymentBuilder struct {
16301650
replicas *int32
16311651
generation *int64
16321652
labels map[string]string
1653+
annotations map[string]string
16331654
status *clusterv1.MachineDeploymentStatus
16341655
minReadySeconds *int32
16351656
}
@@ -1672,6 +1693,12 @@ func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *Machine
16721693
return m
16731694
}
16741695

1696+
// WithAnnotations adds the given annotations to the MachineDeploymentBuilder.
1697+
func (m *MachineDeploymentBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentBuilder {
1698+
m.annotations = annotations
1699+
return m
1700+
}
1701+
16751702
// WithVersion sets the passed version on the machine deployment spec.
16761703
func (m *MachineDeploymentBuilder) WithVersion(version string) *MachineDeploymentBuilder {
16771704
m.version = &version
@@ -1710,9 +1737,10 @@ func (m *MachineDeploymentBuilder) Build() *clusterv1.MachineDeployment {
17101737
APIVersion: clusterv1.GroupVersion.String(),
17111738
},
17121739
ObjectMeta: metav1.ObjectMeta{
1713-
Name: m.name,
1714-
Namespace: m.namespace,
1715-
Labels: m.labels,
1740+
Name: m.name,
1741+
Namespace: m.namespace,
1742+
Labels: m.labels,
1743+
Annotations: m.annotations,
17161744
},
17171745
}
17181746
if m.generation != nil {

0 commit comments

Comments
 (0)