Skip to content

Commit

Permalink
Extend Deployment/Pool builtin to include metadata
Browse files Browse the repository at this point in the history
This patch extends the 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 bc7bbca
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,13 @@ referenced in patches:
- `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 @@ -116,6 +120,9 @@ type MachineDeploymentBuiltins struct {
// being orchestrated.
Version string `json:"version,omitempty"`

// Metadata is the additional 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 +156,9 @@ type MachinePoolBuiltins struct {
// being orchestrated.
Version string `json:"version,omitempty"`

// Metadata is the additional 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
10 changes: 10 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.

16 changes: 14 additions & 2 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 @@ -182,6 +182,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 +241,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 @@ -684,6 +684,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 +701,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 +1054,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 +1071,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
27 changes: 21 additions & 6 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,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 +1571,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 +1615,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 +1656,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 +1699,11 @@ func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *Machine
return m
}

func (m *MachineDeploymentBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentBuilder {

Check failure on line 1702 in internal/test/builder/builders.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method MachineDeploymentBuilder.WithAnnotations should have comment or be unexported (revive)
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 +1742,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
4 changes: 4 additions & 0 deletions internal/webhooks/patch_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ var builtinVariables = sets.Set[string]{}.Insert(
"builtin.machineDeployment.replicas",
"builtin.machineDeployment.topologyName",
"builtin.machineDeployment.version",
"builtin.machineDeployment.metadata.labels",
"builtin.machineDeployment.metadata.annotations",
// MachineDeployment ref builtins.
"builtin.machineDeployment.bootstrap.configRef.name",
"builtin.machineDeployment.infrastructureRef.name",
Expand All @@ -511,6 +513,8 @@ var builtinVariables = sets.Set[string]{}.Insert(
"builtin.machinePool.replicas",
"builtin.machinePool.topologyName",
"builtin.machinePool.version",
"builtin.machinePool.metadata.labels",
"builtin.machinePool.metadata.annotations",
// MachinePool ref builtins.
"builtin.machinePool.bootstrap.configRef.name",
"builtin.machinePool.infrastructureRef.name",
Expand Down

0 comments on commit bc7bbca

Please sign in to comment.