Skip to content

Commit 233db3f

Browse files
authored
Merge pull request kubernetes-sigs#10837 from srm09/add-metata-to-md-builtins
✨ Extend ControlPlane/ MD / MP builtin to include metadata
2 parents 2d45e62 + dc97411 commit 233db3f

File tree

10 files changed

+138
-15
lines changed

10 files changed

+138
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,19 +536,19 @@ referenced in patches:
536536
- `builtin.cluster.topology.{version,class}`
537537
- `builtin.cluster.network.{serviceDomain,services,pods,ipFamily}`
538538
- 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}`
539+
- `builtin.controlPlane.{replicas,version,name,metadata.labels,metadata.annotations}`
540540
- Please note, these variables are only available when patching control plane or control plane
541541
machine templates.
542542
- `builtin.controlPlane.machineTemplate.infrastructureRef.name`
543543
- Please note, these variables are only available when using a control plane with machines and
544544
when patching control plane or control plane machine templates.
545-
- `builtin.machineDeployment.{replicas,version,class,name,topologyName}`
545+
- `builtin.machineDeployment.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
546546
- Please note, these variables are only available when patching the templates of a MachineDeployment
547547
and contain the values of the current `MachineDeployment` topology.
548548
- `builtin.machineDeployment.{infrastructureRef.name,bootstrap.configRef.name}`
549549
- Please note, these variables are only available when patching the templates of a MachineDeployment
550550
and contain the values of the current `MachineDeployment` topology.
551-
- `builtin.machinePool.{replicas,version,class,name,topologyName}`
551+
- `builtin.machinePool.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
552552
- Please note, these variables are only available when patching the templates of a MachinePool
553553
and contain the values of the current `MachinePool` topology.
554554
- `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"
@@ -82,6 +86,9 @@ type ControlPlaneBuiltins struct {
8286
// being orchestrated.
8387
Version string `json:"version,omitempty"`
8488

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

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

162+
// Metadata is the metadata set on the MachinePool.
163+
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`
164+
152165
// Class is the class name of the MachinePool,
153166
// to which the current template belongs to.
154167
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
@@ -1469,6 +1469,18 @@ func (c *TestControlPlaneBuilder) WithVersion(version string) *TestControlPlaneB
14691469
return c
14701470
}
14711471

1472+
// WithLabels adds the passed labels to the ControlPlaneBuilder.
1473+
func (c *ControlPlaneBuilder) WithLabels(labels map[string]string) *ControlPlaneBuilder {
1474+
c.obj.SetLabels(labels)
1475+
return c
1476+
}
1477+
1478+
// WithAnnotations adds the passed annotations to the ControlPlaneBuilder.
1479+
func (c *ControlPlaneBuilder) WithAnnotations(annotations map[string]string) *ControlPlaneBuilder {
1480+
c.obj.SetAnnotations(annotations)
1481+
return c
1482+
}
1483+
14721484
// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
14731485
// to the value of the spec field.
14741486
//
@@ -1540,6 +1552,7 @@ type MachinePoolBuilder struct {
15401552
clusterName string
15411553
replicas *int32
15421554
labels map[string]string
1555+
annotations map[string]string
15431556
status *expv1.MachinePoolStatus
15441557
minReadySeconds *int32
15451558
}
@@ -1570,6 +1583,12 @@ func (m *MachinePoolBuilder) WithLabels(labels map[string]string) *MachinePoolBu
15701583
return m
15711584
}
15721585

1586+
// WithAnnotations adds the given annotations to the MachinePoolBuilder.
1587+
func (m *MachinePoolBuilder) WithAnnotations(annotations map[string]string) *MachinePoolBuilder {
1588+
m.annotations = annotations
1589+
return m
1590+
}
1591+
15731592
// WithVersion sets the passed version on the MachinePool spec.
15741593
func (m *MachinePoolBuilder) WithVersion(version string) *MachinePoolBuilder {
15751594
m.version = &version
@@ -1608,9 +1627,10 @@ func (m *MachinePoolBuilder) Build() *expv1.MachinePool {
16081627
APIVersion: expv1.GroupVersion.String(),
16091628
},
16101629
ObjectMeta: metav1.ObjectMeta{
1611-
Name: m.name,
1612-
Namespace: m.namespace,
1613-
Labels: m.labels,
1630+
Name: m.name,
1631+
Namespace: m.namespace,
1632+
Labels: m.labels,
1633+
Annotations: m.annotations,
16141634
},
16151635
Spec: expv1.MachinePoolSpec{
16161636
ClusterName: m.clusterName,
@@ -1648,6 +1668,7 @@ type MachineDeploymentBuilder struct {
16481668
replicas *int32
16491669
generation *int64
16501670
labels map[string]string
1671+
annotations map[string]string
16511672
status *clusterv1.MachineDeploymentStatus
16521673
minReadySeconds *int32
16531674
}
@@ -1690,6 +1711,12 @@ func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *Machine
16901711
return m
16911712
}
16921713

1714+
// WithAnnotations adds the given annotations to the MachineDeploymentBuilder.
1715+
func (m *MachineDeploymentBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentBuilder {
1716+
m.annotations = annotations
1717+
return m
1718+
}
1719+
16931720
// WithVersion sets the passed version on the machine deployment spec.
16941721
func (m *MachineDeploymentBuilder) WithVersion(version string) *MachineDeploymentBuilder {
16951722
m.version = &version
@@ -1728,9 +1755,10 @@ func (m *MachineDeploymentBuilder) Build() *clusterv1.MachineDeployment {
17281755
APIVersion: clusterv1.GroupVersion.String(),
17291756
},
17301757
ObjectMeta: metav1.ObjectMeta{
1731-
Name: m.name,
1732-
Namespace: m.namespace,
1733-
Labels: m.labels,
1758+
Name: m.name,
1759+
Namespace: m.namespace,
1760+
Labels: m.labels,
1761+
Annotations: m.annotations,
17341762
},
17351763
}
17361764
if m.generation != nil {

0 commit comments

Comments
 (0)