Skip to content

Commit

Permalink
Extend ControlPlane/Deployment/Pool builtin to include metadata
Browse files Browse the repository at this point in the history
This patch extends the ControlPlane, MachineDeployment and MachinePool builtins to
include metadata.

Signed-off-by: Sagar Muchhal <muchhals@vmware.com>
  • Loading branch information
srm09 committed Jul 8, 2024
1 parent 0cdfb88 commit 1fc709f
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,19 @@ referenced in patches:
- `builtin.cluster.topology.{version,class}`
- `builtin.cluster.network.{serviceDomain,services,pods,ipFamily}`
- Note: ipFamily is deprecated and will be removed in a future release. see https://github.com/kubernetes-sigs/cluster-api/issues/7521.
- `builtin.controlPlane.{replicas,version,name}`
- `builtin.controlPlane.{replicas,version,name,metadata.labels,metadata.annotations}`
- Please note, these variables are only available when patching control plane or control plane
machine templates.
- `builtin.controlPlane.machineTemplate.infrastructureRef.name`
- Please note, these variables are only available when using a control plane with machines and
when patching control plane or control plane machine templates.
- `builtin.machineDeployment.{replicas,version,class,name,topologyName}`
- `builtin.machineDeployment.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
- Please note, these variables are only available when patching the templates of a MachineDeployment
and contain the values of the current `MachineDeployment` topology.
- `builtin.machineDeployment.{infrastructureRef.name,bootstrap.configRef.name}`
- Please note, these variables are only available when patching the templates of a MachineDeployment
and contain the values of the current `MachineDeployment` topology.
- `builtin.machinePool.{replicas,version,class,name,topologyName}`
- `builtin.machinePool.{replicas,version,class,name,topologyName,metadata.labels,metadata.annotations}`
- Please note, these variables are only available when patching the templates of a MachinePool
and contain the values of the current `MachinePool` topology.
- `builtin.machinePool.{infrastructureRef.name,bootstrap.configRef.name}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package v1alpha1

import "k8s.io/apimachinery/pkg/types"
import (
"k8s.io/apimachinery/pkg/types"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// BuiltinsName is the name of the builtin variable.
const BuiltinsName = "builtin"
Expand Down Expand Up @@ -82,6 +86,9 @@ type ControlPlaneBuiltins struct {
// being orchestrated.
Version string `json:"version,omitempty"`

// Metadata is the metadata set on the ControlPlane object.
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`

// Name is the name of the ControlPlane,
// to which the current template belongs to.
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -116,6 +123,9 @@ type MachineDeploymentBuiltins struct {
// being orchestrated.
Version string `json:"version,omitempty"`

// Metadata is the metadata set on the MachineDeployment.
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`

// Class is the class name of the MachineDeployment,
// to which the current template belongs to.
Class string `json:"class,omitempty"`
Expand Down Expand Up @@ -149,6 +159,9 @@ type MachinePoolBuiltins struct {
// being orchestrated.
Version string `json:"version,omitempty"`

// Metadata is the metadata set on the MachinePool.
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`

// Class is the class name of the MachinePool,
// to which the current template belongs to.
Class string `json:"class,omitempty"`
Expand Down
15 changes: 15 additions & 0 deletions exp/runtime/hooks/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions exp/runtime/hooks/api/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/controllers/topology/cluster/patches/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
Workers: &clusterv1.WorkersTopology{
MachineDeployments: []clusterv1.MachineDeploymentTopology{
{
Metadata: clusterv1.ObjectMeta{},
Metadata: clusterv1.ObjectMeta{Labels: map[string]string{"foo": "bar"}, Annotations: map[string]string{"fizz": "buzz"}},
Class: "default-worker",
Name: "default-worker-topo1",
Variables: &clusterv1.MachineDeploymentVariables{
Expand All @@ -1161,7 +1161,7 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
},
MachinePools: []clusterv1.MachinePoolTopology{
{
Metadata: clusterv1.ObjectMeta{},
Metadata: clusterv1.ObjectMeta{Labels: map[string]string{"foo": "bar"}, Annotations: map[string]string{"fizz": "buzz"}},
Class: "default-mp-worker",
Name: "default-mp-worker-topo1",
Variables: &clusterv1.MachinePoolVariables{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ func ControlPlane(cpTopology *clusterv1.ControlPlaneTopology, cp, cpInfrastructu
}
builtin.ControlPlane.Replicas = replicas
}
if cp.GetLabels() != nil || cp.GetAnnotations() != nil {
builtin.ControlPlane.Metadata = &clusterv1.ObjectMeta{
Annotations: cp.GetAnnotations(),
Labels: cp.GetLabels(),
}
}

version, err := contract.ControlPlane().Version().Get(cp)
if err != nil {
Expand Down Expand Up @@ -182,6 +188,12 @@ func MachineDeployment(mdTopology *clusterv1.MachineDeploymentTopology, md *clus
if md.Spec.Replicas != nil {
builtin.MachineDeployment.Replicas = ptr.To[int64](int64(*md.Spec.Replicas))
}
if md.Labels != nil || md.Annotations != nil {
builtin.MachineDeployment.Metadata = &clusterv1.ObjectMeta{
Annotations: md.Annotations,
Labels: md.Labels,
}
}

if mdBootstrapTemplate != nil {
builtin.MachineDeployment.Bootstrap = &runtimehooksv1.MachineBootstrapBuiltins{
Expand Down Expand Up @@ -235,6 +247,12 @@ func MachinePool(mpTopology *clusterv1.MachinePoolTopology, mp *expv1.MachinePoo
if mp.Spec.Replicas != nil {
builtin.MachinePool.Replicas = ptr.To[int64](int64(*mp.Spec.Replicas))
}
if mp.Labels != nil || mp.Annotations != nil {
builtin.MachinePool.Metadata = &clusterv1.ObjectMeta{
Annotations: mp.Annotations,
Labels: mp.Labels,
}
}

if mpBootstrapObject != nil {
builtin.MachinePool.Bootstrap = &runtimehooksv1.MachineBootstrapBuiltins{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ func TestControlPlane(t *testing.T) {
controlPlane: builder.ControlPlane(metav1.NamespaceDefault, "controlPlane1").
WithReplicas(3).
WithVersion("v1.21.1").
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"fizz": "buzz"}).
Build(),
want: []runtimehooksv1.Variable{
{
Expand All @@ -460,6 +462,7 @@ func TestControlPlane(t *testing.T) {
Value: toJSONCompact(`{
"controlPlane":{
"version": "v1.21.1",
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
"name":"controlPlane1",
"replicas":3
}}`),
Expand Down Expand Up @@ -503,6 +506,7 @@ func TestControlPlane(t *testing.T) {
controlPlane: builder.ControlPlane(metav1.NamespaceDefault, "controlPlane1").
WithReplicas(3).
WithVersion("v1.21.1").
WithLabels(map[string]string{"foo": "bar"}).
Build(),
want: []runtimehooksv1.Variable{
{
Expand All @@ -518,6 +522,7 @@ func TestControlPlane(t *testing.T) {
Value: toJSONCompact(`{
"controlPlane":{
"version": "v1.21.1",
"metadata": {"labels":{"foo":"bar"}},
"name":"controlPlane1",
"replicas":3
}}`),
Expand Down Expand Up @@ -684,6 +689,8 @@ func TestMachineDeployment(t *testing.T) {
md: builder.MachineDeployment(metav1.NamespaceDefault, "md1").
WithReplicas(3).
WithVersion("v1.21.1").
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"fizz": "buzz"}).
Build(),
want: []runtimehooksv1.Variable{
{
Expand All @@ -699,6 +706,7 @@ func TestMachineDeployment(t *testing.T) {
Value: toJSONCompact(`{
"machineDeployment":{
"version": "v1.21.1",
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
"class": "md-class",
"name": "md1",
"topologyName": "md-topology",
Expand Down Expand Up @@ -1051,6 +1059,8 @@ func TestMachinePool(t *testing.T) {
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"fizz": "buzz"}).
Build(),
want: []runtimehooksv1.Variable{
{
Expand All @@ -1066,6 +1076,7 @@ func TestMachinePool(t *testing.T) {
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
Expand Down
40 changes: 34 additions & 6 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,18 @@ func (c *TestControlPlaneBuilder) WithVersion(version string) *TestControlPlaneB
return c
}

// WithLabels adds the passed labels to the ControlPlaneBuilder.
func (c *ControlPlaneBuilder) WithLabels(labels map[string]string) *ControlPlaneBuilder {
c.obj.SetLabels(labels)
return c
}

// WithAnnotations adds the passed annotations to the ControlPlaneBuilder.
func (c *ControlPlaneBuilder) WithAnnotations(annotations map[string]string) *ControlPlaneBuilder {
c.obj.SetAnnotations(annotations)
return c
}

// WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds
// to the value of the spec field.
//
Expand Down Expand Up @@ -1540,6 +1552,7 @@ type MachinePoolBuilder struct {
clusterName string
replicas *int32
labels map[string]string
annotations map[string]string
status *expv1.MachinePoolStatus
minReadySeconds *int32
}
Expand Down Expand Up @@ -1570,6 +1583,12 @@ func (m *MachinePoolBuilder) WithLabels(labels map[string]string) *MachinePoolBu
return m
}

// WithAnnotations adds the given annotations to the MachinePoolBuilder.
func (m *MachinePoolBuilder) WithAnnotations(annotations map[string]string) *MachinePoolBuilder {
m.annotations = annotations
return m
}

// WithVersion sets the passed version on the MachinePool spec.
func (m *MachinePoolBuilder) WithVersion(version string) *MachinePoolBuilder {
m.version = &version
Expand Down Expand Up @@ -1608,9 +1627,10 @@ func (m *MachinePoolBuilder) Build() *expv1.MachinePool {
APIVersion: expv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
Annotations: m.annotations,
},
Spec: expv1.MachinePoolSpec{
ClusterName: m.clusterName,
Expand Down Expand Up @@ -1648,6 +1668,7 @@ type MachineDeploymentBuilder struct {
replicas *int32
generation *int64
labels map[string]string
annotations map[string]string
status *clusterv1.MachineDeploymentStatus
minReadySeconds *int32
}
Expand Down Expand Up @@ -1690,6 +1711,12 @@ func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *Machine
return m
}

// WithAnnotations adds the given annotations to the MachineDeploymentBuilder.
func (m *MachineDeploymentBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentBuilder {
m.annotations = annotations
return m
}

// WithVersion sets the passed version on the machine deployment spec.
func (m *MachineDeploymentBuilder) WithVersion(version string) *MachineDeploymentBuilder {
m.version = &version
Expand Down Expand Up @@ -1728,9 +1755,10 @@ func (m *MachineDeploymentBuilder) Build() *clusterv1.MachineDeployment {
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
Name: m.name,
Namespace: m.namespace,
Labels: m.labels,
Annotations: m.annotations,
},
}
if m.generation != nil {
Expand Down
Loading

0 comments on commit 1fc709f

Please sign in to comment.