Skip to content

Commit 5b0d62f

Browse files
committed
Extend Cluster builtin to include metadata
This ptach extends the cluster builtins to include the labels and annotations set on the Cluster, if any. This can be used by the runtime extensions or JSON patches to set metadata based info. Signed-off-by: Sagar Muchhal <[email protected]>
1 parent 129b395 commit 5b0d62f

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ spec:
608608

609609
In addition to variables specified in the ClusterClass, the following builtin variables can be
610610
referenced in patches:
611-
- `builtin.cluster.{name,namespace,uid}`
611+
- `builtin.cluster.{name,namespace,uid,metadata.labels,metadata.annotations}`
612612
- `builtin.cluster.topology.{version,class}`
613613
- `builtin.cluster.network.{serviceDomain,services,pods,ipFamily}`
614614
- Note: ipFamily is deprecated and will be removed in a future release. see https://github.com/kubernetes-sigs/cluster-api/issues/7521.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type ClusterBuiltins struct {
5555
// +optional
5656
UID types.UID `json:"uid,omitempty"`
5757

58+
// Metadata is the metadata set on the Cluster object.
59+
// +optional
60+
Metadata *clusterv1.ObjectMeta `json:"metadata,omitempty"`
61+
5862
// topology represents the cluster topology variables.
5963
// +optional
6064
Topology *ClusterTopologyBuiltins `json:"topology,omitempty"`

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,11 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
10781078
Kind: "Cluster",
10791079
},
10801080
ObjectMeta: metav1.ObjectMeta{
1081-
Name: "cluster1",
1082-
Namespace: metav1.NamespaceDefault,
1083-
UID: uuid.NewUUID(),
1081+
Name: "cluster1",
1082+
Namespace: metav1.NamespaceDefault,
1083+
UID: uuid.NewUUID(),
1084+
Labels: map[string]string{"foo": "bar"},
1085+
Annotations: map[string]string{"fizz": "buzz"},
10841086
},
10851087
Spec: clusterv1.ClusterSpec{
10861088
Paused: false,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func Global(clusterTopology *clusterv1.Topology, cluster *clusterv1.Cluster, pat
6060
},
6161
},
6262
}
63+
if cluster.Labels != nil || cluster.Annotations != nil {
64+
builtin.Cluster.Metadata = &clusterv1.ObjectMeta{
65+
Labels: cluster.Labels,
66+
Annotations: cluster.Annotations,
67+
}
68+
}
6369
if cluster.Spec.ClusterNetwork != nil {
6470
clusterNetworkIPFamily, _ := cluster.GetIPFamily()
6571
builtin.Cluster.Network = &runtimehooksv1.ClusterNetworkBuiltins{

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ func TestGlobal(t *testing.T) {
6666
},
6767
cluster: &clusterv1.Cluster{
6868
ObjectMeta: metav1.ObjectMeta{
69-
Name: "cluster1",
70-
Namespace: metav1.NamespaceDefault,
71-
UID: types.UID(clusterUID),
69+
Name: "cluster1",
70+
Namespace: metav1.NamespaceDefault,
71+
UID: types.UID(clusterUID),
72+
Labels: map[string]string{"foo": "bar"},
73+
Annotations: map[string]string{"fizz": "buzz"},
7274
},
7375
Spec: clusterv1.ClusterSpec{
7476
Topology: &clusterv1.Topology{
@@ -102,6 +104,7 @@ func TestGlobal(t *testing.T) {
102104
"name": "cluster1",
103105
"namespace": "default",
104106
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
107+
"metadata": {"labels":{"foo":"bar"}, "annotations":{"fizz":"buzz"}},
105108
"topology":{
106109
"version": "v1.21.1",
107110
"class": "clusterClass1"

internal/webhooks/patch_validation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ var builtinVariables = sets.Set[string]{}.Insert(
472472
"builtin.cluster.name",
473473
"builtin.cluster.namespace",
474474
"builtin.cluster.uid",
475+
"builtin.cluster.metadata.labels",
476+
"builtin.cluster.metadata.annotations",
475477

476478
// ClusterTopology builtins.
477479
"builtin.cluster.topology",

0 commit comments

Comments
 (0)