Skip to content

Commit ed38302

Browse files
committed
Extend cluster builtin to include UID
This patch extends the cluster builtins to include the UID of the Cluster object. This can be used by the runtime extensions or JSON patches to set owner references on objects owned by the Cluster object. Signed-off-by: Sagar Muchhal <[email protected]>
1 parent fd94039 commit ed38302

File tree

7 files changed

+29
-1
lines changed

7 files changed

+29
-1
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
@@ -532,7 +532,7 @@ spec:
532532

533533
In addition to variables specified in the ClusterClass, the following builtin variables can be
534534
referenced in patches:
535-
- `builtin.cluster.{name,namespace}`
535+
- `builtin.cluster.{name,namespace,uid}`
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.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19+
import "k8s.io/apimachinery/pkg/types"
20+
1921
// BuiltinsName is the name of the builtin variable.
2022
const BuiltinsName = "builtin"
2123

@@ -35,6 +37,9 @@ type ClusterBuiltins struct {
3537
// Namespace is the namespace of the cluster.
3638
Namespace string `json:"namespace,omitempty"`
3739

40+
// UID is the unqiue identifier of the cluster.
41+
UID types.UID `json:"uid,omitempty"`
42+
3843
// Topology represents the cluster topology variables.
3944
Topology *ClusterTopologyBuiltins `json:"topology,omitempty"`
4045

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

Lines changed: 7 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3030
"k8s.io/apimachinery/pkg/types"
31+
"k8s.io/apimachinery/pkg/util/uuid"
3132
utilfeature "k8s.io/component-base/featuregate/testing"
3233
"k8s.io/utils/ptr"
3334
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
@@ -1073,6 +1074,7 @@ func setupTestObjects() (*scope.ClusterBlueprint, *scope.ClusterState) {
10731074
ObjectMeta: metav1.ObjectMeta{
10741075
Name: "cluster1",
10751076
Namespace: metav1.NamespaceDefault,
1077+
UID: uuid.NewUUID(),
10761078
},
10771079
Spec: clusterv1.ClusterSpec{
10781080
Paused: false,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func Global(clusterTopology *clusterv1.Topology, cluster *clusterv1.Cluster, def
6161
Cluster: &runtimehooksv1.ClusterBuiltins{
6262
Name: cluster.Name,
6363
Namespace: cluster.Namespace,
64+
UID: cluster.UID,
6465
Topology: &runtimehooksv1.ClusterTopologyBuiltins{
6566
Version: cluster.Spec.Topology.Version,
6667
Class: cluster.GetClassKey().Name,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/types"
2829
"k8s.io/utils/ptr"
2930

3031
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -34,6 +35,7 @@ import (
3435
)
3536

3637
func TestGlobal(t *testing.T) {
38+
clusterUID := "8a35f406-6b9b-4b78-8c93-a7f878d90623"
3739
tests := []struct {
3840
name string
3941
clusterTopology *clusterv1.Topology
@@ -68,6 +70,7 @@ func TestGlobal(t *testing.T) {
6870
ObjectMeta: metav1.ObjectMeta{
6971
Name: "cluster1",
7072
Namespace: metav1.NamespaceDefault,
73+
UID: types.UID(clusterUID),
7174
},
7275
Spec: clusterv1.ClusterSpec{
7376
Topology: &clusterv1.Topology{
@@ -100,6 +103,7 @@ func TestGlobal(t *testing.T) {
100103
"cluster":{
101104
"name": "cluster1",
102105
"namespace": "default",
106+
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
103107
"topology":{
104108
"version": "v1.21.1",
105109
"class": "clusterClass1"
@@ -149,6 +153,7 @@ func TestGlobal(t *testing.T) {
149153
ObjectMeta: metav1.ObjectMeta{
150154
Name: "cluster1",
151155
Namespace: metav1.NamespaceDefault,
156+
UID: types.UID(clusterUID),
152157
},
153158
Spec: clusterv1.ClusterSpec{
154159
Topology: &clusterv1.Topology{
@@ -181,6 +186,7 @@ func TestGlobal(t *testing.T) {
181186
"cluster":{
182187
"name": "cluster1",
183188
"namespace": "default",
189+
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
184190
"topology":{
185191
"version": "v1.21.1",
186192
"class": "clusterClass1"
@@ -221,6 +227,7 @@ func TestGlobal(t *testing.T) {
221227
ObjectMeta: metav1.ObjectMeta{
222228
Name: "cluster1",
223229
Namespace: metav1.NamespaceDefault,
230+
UID: types.UID(clusterUID),
224231
},
225232
Spec: clusterv1.ClusterSpec{
226233
Topology: &clusterv1.Topology{
@@ -252,6 +259,7 @@ func TestGlobal(t *testing.T) {
252259
"cluster":{
253260
"name": "cluster1",
254261
"namespace": "default",
262+
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
255263
"topology":{
256264
"version": "v1.21.1",
257265
"class": "clusterClass1"
@@ -291,6 +299,7 @@ func TestGlobal(t *testing.T) {
291299
ObjectMeta: metav1.ObjectMeta{
292300
Name: "cluster1",
293301
Namespace: metav1.NamespaceDefault,
302+
UID: types.UID(clusterUID),
294303
},
295304
Spec: clusterv1.ClusterSpec{
296305
Topology: &clusterv1.Topology{
@@ -319,6 +328,7 @@ func TestGlobal(t *testing.T) {
319328
"cluster":{
320329
"name": "cluster1",
321330
"namespace": "default",
331+
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
322332
"topology":{
323333
"version": "v1.21.1",
324334
"class": "clusterClass1"
@@ -357,6 +367,7 @@ func TestGlobal(t *testing.T) {
357367
ObjectMeta: metav1.ObjectMeta{
358368
Name: "cluster1",
359369
Namespace: metav1.NamespaceDefault,
370+
UID: types.UID(clusterUID),
360371
},
361372
Spec: clusterv1.ClusterSpec{
362373
Topology: &clusterv1.Topology{
@@ -381,6 +392,7 @@ func TestGlobal(t *testing.T) {
381392
"cluster":{
382393
"name": "cluster1",
383394
"namespace": "default",
395+
"uid": "8a35f406-6b9b-4b78-8c93-a7f878d90623",
384396
"topology":{
385397
"version": "v1.21.1",
386398
"class": "clusterClass1"

internal/webhooks/patch_validation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ var builtinVariables = sets.Set[string]{}.Insert(
471471
"builtin.cluster",
472472
"builtin.cluster.name",
473473
"builtin.cluster.namespace",
474+
"builtin.cluster.uid",
474475

475476
// ClusterTopology builtins.
476477
"builtin.cluster.topology",

0 commit comments

Comments
 (0)