Skip to content

Commit 2c2b8e7

Browse files
srm09Sagar Muchhal
authored and
Sagar Muchhal
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.
1 parent 129b395 commit 2c2b8e7

File tree

8 files changed

+36
-8
lines changed

8 files changed

+36
-8
lines changed

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

+1-1
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

+4
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"`

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

+5
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

+7-1
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

+5-3
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

+6
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

+6-3
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

+2
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)