From d6bbef87df148add727661d918cabaeb6c7fd657 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Mon, 4 Nov 2024 13:01:09 +0300 Subject: [PATCH 1/2] Add reference type for composite Signed-off-by: Hasan Turken (cherry picked from commit 99dd2c0b51ce197a43ffa06a7d38225ba29be029) --- pkg/resource/interfaces.go | 6 ++-- pkg/resource/unstructured/claim/claim.go | 26 ++++------------- .../unstructured/composite/composite.go | 17 ++++++++--- .../unstructured/reference/reference.go | 28 +++++++++++++++++++ 4 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 pkg/resource/unstructured/reference/reference.go diff --git a/pkg/resource/interfaces.go b/pkg/resource/interfaces.go index 702187e1b..003148845 100644 --- a/pkg/resource/interfaces.go +++ b/pkg/resource/interfaces.go @@ -18,6 +18,7 @@ package resource import ( "context" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -25,7 +26,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/claim" ) // A Conditioned may have conditions set or retrieved. Conditions are typically @@ -37,8 +37,8 @@ type Conditioned interface { // A ClaimReferencer may reference a resource claim. type ClaimReferencer interface { - SetClaimReference(r *claim.Reference) - GetClaimReference() *claim.Reference + SetClaimReference(r *reference.Claim) + GetClaimReference() *reference.Claim } // A ManagedResourceReferencer may reference a concrete managed resource. diff --git a/pkg/resource/unstructured/claim/claim.go b/pkg/resource/unstructured/claim/claim.go index 7a071fa61..bd7672daa 100644 --- a/pkg/resource/unstructured/claim/claim.go +++ b/pkg/resource/unstructured/claim/claim.go @@ -18,6 +18,7 @@ limitations under the License. package claim import ( + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -63,21 +64,6 @@ type Unstructured struct { unstructured.Unstructured } -// Reference to a claim. -type Reference struct { - // APIVersion of the referenced claim. - APIVersion string `json:"apiVersion"` - - // Kind of the referenced claim. - Kind string `json:"kind"` - - // Name of the referenced claim. - Name string `json:"name"` - - // Namespace of the referenced claim. - Namespace string `json:"namespace"` -} - // GetUnstructured returns the underlying *unstructured.Unstructured. func (c *Unstructured) GetUnstructured() *unstructured.Unstructured { return &c.Unstructured @@ -170,8 +156,8 @@ func (c *Unstructured) GetCompositeDeletePolicy() *xpv1.CompositeDeletePolicy { } // GetResourceReference of this composite resource claim. -func (c *Unstructured) GetResourceReference() *corev1.ObjectReference { - out := &corev1.ObjectReference{} +func (c *Unstructured) GetResourceReference() *reference.Composite { + out := &reference.Composite{} if err := fieldpath.Pave(c.Object).GetValueInto("spec.resourceRef", out); err != nil { return nil } @@ -179,13 +165,13 @@ func (c *Unstructured) GetResourceReference() *corev1.ObjectReference { } // SetResourceReference of this composite resource claim. -func (c *Unstructured) SetResourceReference(ref *corev1.ObjectReference) { +func (c *Unstructured) SetResourceReference(ref *reference.Composite) { _ = fieldpath.Pave(c.Object).SetValue("spec.resourceRef", ref) } // GetReference returns reference to this claim. -func (c *Unstructured) GetReference() *Reference { - return &Reference{ +func (c *Unstructured) GetReference() *reference.Claim { + return &reference.Claim{ APIVersion: c.GetAPIVersion(), Kind: c.GetKind(), Name: c.GetName(), diff --git a/pkg/resource/unstructured/composite/composite.go b/pkg/resource/unstructured/composite/composite.go index 8f9f12683..917280aab 100644 --- a/pkg/resource/unstructured/composite/composite.go +++ b/pkg/resource/unstructured/composite/composite.go @@ -18,6 +18,7 @@ limitations under the License. package composite import ( + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -26,7 +27,6 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/claim" ) // An Option modifies an unstructured composite resource. @@ -142,8 +142,8 @@ func (c *Unstructured) GetCompositionUpdatePolicy() *xpv1.UpdatePolicy { } // GetClaimReference of this Composite resource. -func (c *Unstructured) GetClaimReference() *claim.Reference { - out := &claim.Reference{} +func (c *Unstructured) GetClaimReference() *reference.Claim { + out := &reference.Claim{} if err := fieldpath.Pave(c.Object).GetValueInto("spec.claimRef", out); err != nil { return nil } @@ -151,7 +151,7 @@ func (c *Unstructured) GetClaimReference() *claim.Reference { } // SetClaimReference of this Composite resource. -func (c *Unstructured) SetClaimReference(ref *claim.Reference) { +func (c *Unstructured) SetClaimReference(ref *reference.Claim) { _ = fieldpath.Pave(c.Object).SetValue("spec.claimRef", ref) } @@ -177,6 +177,15 @@ func (c *Unstructured) SetResourceReferences(refs []corev1.ObjectReference) { _ = fieldpath.Pave(c.Object).SetValue("spec.resourceRefs", filtered) } +// GetReference returns reference to this composite. +func (c *Unstructured) GetReference() *reference.Composite { + return &reference.Composite{ + APIVersion: c.GetAPIVersion(), + Kind: c.GetKind(), + Name: c.GetName(), + } +} + // GetWriteConnectionSecretToReference of this Composite resource. func (c *Unstructured) GetWriteConnectionSecretToReference() *xpv1.SecretReference { out := &xpv1.SecretReference{} diff --git a/pkg/resource/unstructured/reference/reference.go b/pkg/resource/unstructured/reference/reference.go new file mode 100644 index 000000000..c517e4779 --- /dev/null +++ b/pkg/resource/unstructured/reference/reference.go @@ -0,0 +1,28 @@ +package reference + +// A Claim is a reference to a claim. +type Claim struct { + // APIVersion of the referenced claim. + APIVersion string `json:"apiVersion"` + + // Kind of the referenced claim. + Kind string `json:"kind"` + + // Name of the referenced claim. + Name string `json:"name"` + + // Namespace of the referenced claim. + Namespace string `json:"namespace"` +} + +// A Composite is a reference to a composite. +type Composite struct { + // APIVersion of the referenced composite. + APIVersion string `json:"apiVersion"` + + // Kind of the referenced composite. + Kind string `json:"kind"` + + // Name of the referenced composite. + Name string `json:"name"` +} From 8453f6dfd6837a7185e116a9e436480fccd9e0d4 Mon Sep 17 00:00:00 2001 From: Hasan Turken Date: Mon, 4 Nov 2024 13:08:17 +0300 Subject: [PATCH 2/2] CompositionRevisionRefence only has Name, so LocalObjectReference Signed-off-by: Hasan Turken (cherry picked from commit 70499a4eddccf016f2e50727baaf9155593430d5) --- pkg/reconciler/managed/reconciler_typed.go | 1 + pkg/resource/fake/mocks.go | 20 ++++++------ pkg/resource/interfaces.go | 10 +++--- pkg/resource/unstructured/claim/claim.go | 8 ++--- pkg/resource/unstructured/claim/claim_test.go | 15 ++++----- .../unstructured/composite/composite.go | 8 ++--- .../unstructured/composite/composite_test.go | 14 ++++----- .../unstructured/reference/reference.go | 31 +++++++++++++++++++ 8 files changed, 70 insertions(+), 37 deletions(-) diff --git a/pkg/reconciler/managed/reconciler_typed.go b/pkg/reconciler/managed/reconciler_typed.go index 1ca27721d..3db15a9cf 100644 --- a/pkg/reconciler/managed/reconciler_typed.go +++ b/pkg/reconciler/managed/reconciler_typed.go @@ -52,6 +52,7 @@ func (c *typedExternalClientWrapper[managed]) Create(ctx context.Context, mg res } return c.c.Create(ctx, cr) } + func (c *typedExternalClientWrapper[managed]) Update(ctx context.Context, mg resource.Managed) (ExternalUpdate, error) { cr, ok := mg.(managed) if !ok { diff --git a/pkg/resource/fake/mocks.go b/pkg/resource/fake/mocks.go index 79db4ccdd..ae8f80e0b 100644 --- a/pkg/resource/fake/mocks.go +++ b/pkg/resource/fake/mocks.go @@ -35,7 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/claim" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" ) // Conditioned is a mock that implements Conditioned interface. @@ -50,13 +50,13 @@ func (m *Conditioned) GetCondition(ct xpv1.ConditionType) xpv1.Condition { } // ClaimReferencer is a mock that implements ClaimReferencer interface. -type ClaimReferencer struct{ Ref *claim.Reference } +type ClaimReferencer struct{ Ref *reference.Claim } // SetClaimReference sets the ClaimReference. -func (m *ClaimReferencer) SetClaimReference(r *claim.Reference) { m.Ref = r } +func (m *ClaimReferencer) SetClaimReference(r *reference.Claim) { m.Ref = r } // GetClaimReference gets the ClaimReference. -func (m *ClaimReferencer) GetClaimReference() *claim.Reference { return m.Ref } +func (m *ClaimReferencer) GetClaimReference() *reference.Claim { return m.Ref } // ManagedResourceReferencer is a mock that implements ManagedResourceReferencer interface. type ManagedResourceReferencer struct{ Ref *corev1.ObjectReference } @@ -184,15 +184,15 @@ func (m *CompositionSelector) SetCompositionSelector(s *metav1.LabelSelector) { func (m *CompositionSelector) GetCompositionSelector() *metav1.LabelSelector { return m.Sel } // CompositionRevisionReferencer is a mock that implements CompositionRevisionReferencer interface. -type CompositionRevisionReferencer struct{ Ref *corev1.ObjectReference } +type CompositionRevisionReferencer struct{ Ref *corev1.LocalObjectReference } // SetCompositionRevisionReference sets the CompositionRevisionReference. -func (m *CompositionRevisionReferencer) SetCompositionRevisionReference(r *corev1.ObjectReference) { +func (m *CompositionRevisionReferencer) SetCompositionRevisionReference(r *corev1.LocalObjectReference) { m.Ref = r } // GetCompositionRevisionReference gets the CompositionRevisionReference. -func (m *CompositionRevisionReferencer) GetCompositionRevisionReference() *corev1.ObjectReference { +func (m *CompositionRevisionReferencer) GetCompositionRevisionReference() *corev1.LocalObjectReference { return m.Ref } @@ -236,13 +236,13 @@ func (m *CompositeResourceDeleter) GetCompositeDeletePolicy() *xpv1.CompositeDel } // CompositeResourceReferencer is a mock that implements CompositeResourceReferencer interface. -type CompositeResourceReferencer struct{ Ref *corev1.ObjectReference } +type CompositeResourceReferencer struct{ Ref *reference.Composite } // SetResourceReference sets the composite resource reference. -func (m *CompositeResourceReferencer) SetResourceReference(p *corev1.ObjectReference) { m.Ref = p } +func (m *CompositeResourceReferencer) SetResourceReference(p *reference.Composite) { m.Ref = p } // GetResourceReference gets the composite resource reference. -func (m *CompositeResourceReferencer) GetResourceReference() *corev1.ObjectReference { return m.Ref } +func (m *CompositeResourceReferencer) GetResourceReference() *reference.Composite { return m.Ref } // ComposedResourcesReferencer is a mock that implements ComposedResourcesReferencer interface. type ComposedResourcesReferencer struct{ Refs []corev1.ObjectReference } diff --git a/pkg/resource/interfaces.go b/pkg/resource/interfaces.go index 003148845..e58778f7e 100644 --- a/pkg/resource/interfaces.go +++ b/pkg/resource/interfaces.go @@ -18,7 +18,6 @@ package resource import ( "context" - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -26,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" ) // A Conditioned may have conditions set or retrieved. Conditions are typically @@ -120,8 +120,8 @@ type CompositionReferencer interface { // A CompositionRevisionReferencer may reference a specific revision of a // composition of resources. type CompositionRevisionReferencer interface { - SetCompositionRevisionReference(ref *corev1.ObjectReference) - GetCompositionRevisionReference() *corev1.ObjectReference + SetCompositionRevisionReference(ref *corev1.LocalObjectReference) + GetCompositionRevisionReference() *corev1.LocalObjectReference } // A CompositionRevisionSelector may reference a set of @@ -153,8 +153,8 @@ type ComposedResourcesReferencer interface { // A CompositeResourceReferencer can reference a composite resource. type CompositeResourceReferencer interface { - SetResourceReference(r *corev1.ObjectReference) - GetResourceReference() *corev1.ObjectReference + SetResourceReference(r *reference.Composite) + GetResourceReference() *reference.Composite } // An EnvironmentConfigReferencer references a list of EnvironmentConfigs. diff --git a/pkg/resource/unstructured/claim/claim.go b/pkg/resource/unstructured/claim/claim.go index bd7672daa..4c7cf4c9a 100644 --- a/pkg/resource/unstructured/claim/claim.go +++ b/pkg/resource/unstructured/claim/claim.go @@ -18,7 +18,6 @@ limitations under the License. package claim import ( - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -26,6 +25,7 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" ) // An Option modifies an unstructured composite resource claim. @@ -98,8 +98,8 @@ func (c *Unstructured) SetCompositionReference(ref *corev1.ObjectReference) { } // GetCompositionRevisionReference of this resource claim. -func (c *Unstructured) GetCompositionRevisionReference() *corev1.ObjectReference { - out := &corev1.ObjectReference{} +func (c *Unstructured) GetCompositionRevisionReference() *corev1.LocalObjectReference { + out := &corev1.LocalObjectReference{} if err := fieldpath.Pave(c.Object).GetValueInto("spec.compositionRevisionRef", out); err != nil { return nil } @@ -107,7 +107,7 @@ func (c *Unstructured) GetCompositionRevisionReference() *corev1.ObjectReference } // SetCompositionRevisionReference of this resource claim. -func (c *Unstructured) SetCompositionRevisionReference(ref *corev1.ObjectReference) { +func (c *Unstructured) SetCompositionRevisionReference(ref *corev1.LocalObjectReference) { _ = fieldpath.Pave(c.Object).SetValue("spec.compositionRevisionRef", ref) } diff --git a/pkg/resource/unstructured/claim/claim_test.go b/pkg/resource/unstructured/claim/claim_test.go index 3d218a311..b267cac23 100644 --- a/pkg/resource/unstructured/claim/claim_test.go +++ b/pkg/resource/unstructured/claim/claim_test.go @@ -29,6 +29,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" ) var _ client.Object = &Unstructured{} @@ -172,11 +173,11 @@ func TestCompositionReference(t *testing.T) { } func TestCompositionRevisionReference(t *testing.T) { - ref := &corev1.ObjectReference{Namespace: "ns", Name: "cool"} + ref := &corev1.LocalObjectReference{Name: "cool"} cases := map[string]struct { u *Unstructured - set *corev1.ObjectReference - want *corev1.ObjectReference + set *corev1.LocalObjectReference + want *corev1.LocalObjectReference }{ "NewRef": { u: New(), @@ -272,11 +273,11 @@ func TestCompositeDeletePolicy(t *testing.T) { } func TestResourceReference(t *testing.T) { - ref := &corev1.ObjectReference{Namespace: "ns", Name: "cool"} + ref := &reference.Composite{Name: "cool"} cases := map[string]struct { u *Unstructured - set *corev1.ObjectReference - want *corev1.ObjectReference + set *reference.Composite + want *reference.Composite }{ "NewRef": { u: New(), @@ -297,7 +298,7 @@ func TestResourceReference(t *testing.T) { } func TestClaimReference(t *testing.T) { - ref := &Reference{Namespace: "ns", Name: "cool", APIVersion: "foo.com/v1", Kind: "Foo"} + ref := &reference.Claim{Namespace: "ns", Name: "cool", APIVersion: "foo.com/v1", Kind: "Foo"} u := &Unstructured{} u.SetName(ref.Name) u.SetNamespace(ref.Namespace) diff --git a/pkg/resource/unstructured/composite/composite.go b/pkg/resource/unstructured/composite/composite.go index 917280aab..17c0cfb43 100644 --- a/pkg/resource/unstructured/composite/composite.go +++ b/pkg/resource/unstructured/composite/composite.go @@ -18,7 +18,6 @@ limitations under the License. package composite import ( - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -27,6 +26,7 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" ) // An Option modifies an unstructured composite resource. @@ -99,8 +99,8 @@ func (c *Unstructured) SetCompositionReference(ref *corev1.ObjectReference) { } // GetCompositionRevisionReference of this Composite resource. -func (c *Unstructured) GetCompositionRevisionReference() *corev1.ObjectReference { - out := &corev1.ObjectReference{} +func (c *Unstructured) GetCompositionRevisionReference() *corev1.LocalObjectReference { + out := &corev1.LocalObjectReference{} if err := fieldpath.Pave(c.Object).GetValueInto("spec.compositionRevisionRef", out); err != nil { return nil } @@ -108,7 +108,7 @@ func (c *Unstructured) GetCompositionRevisionReference() *corev1.ObjectReference } // SetCompositionRevisionReference of this Composite resource. -func (c *Unstructured) SetCompositionRevisionReference(ref *corev1.ObjectReference) { +func (c *Unstructured) SetCompositionRevisionReference(ref *corev1.LocalObjectReference) { _ = fieldpath.Pave(c.Object).SetValue("spec.compositionRevisionRef", ref) } diff --git a/pkg/resource/unstructured/composite/composite_test.go b/pkg/resource/unstructured/composite/composite_test.go index 40eaf7141..6d3432173 100644 --- a/pkg/resource/unstructured/composite/composite_test.go +++ b/pkg/resource/unstructured/composite/composite_test.go @@ -30,7 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" - "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/claim" + "github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" "github.com/crossplane/crossplane-runtime/pkg/test" ) @@ -259,11 +259,11 @@ func TestCompositionReference(t *testing.T) { } func TestCompositionRevisionReference(t *testing.T) { - ref := &corev1.ObjectReference{Namespace: "ns", Name: "cool"} + ref := &corev1.LocalObjectReference{Name: "cool"} cases := map[string]struct { u *Unstructured - set *corev1.ObjectReference - want *corev1.ObjectReference + set *corev1.LocalObjectReference + want *corev1.LocalObjectReference }{ "NewRef": { u: New(), @@ -334,11 +334,11 @@ func TestCompositionUpdatePolicy(t *testing.T) { } func TestClaimReference(t *testing.T) { - ref := &claim.Reference{Namespace: "ns", Name: "cool", APIVersion: "acme.com/v1", Kind: "Foo"} + ref := &reference.Claim{Namespace: "ns", Name: "cool", APIVersion: "acme.com/v1", Kind: "Foo"} cases := map[string]struct { u *Unstructured - set *claim.Reference - want *claim.Reference + set *reference.Claim + want *reference.Claim }{ "NewRef": { u: New(), diff --git a/pkg/resource/unstructured/reference/reference.go b/pkg/resource/unstructured/reference/reference.go index c517e4779..afe1fa29a 100644 --- a/pkg/resource/unstructured/reference/reference.go +++ b/pkg/resource/unstructured/reference/reference.go @@ -1,5 +1,26 @@ +/* +Copyright 2024 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package reference contains references to resources. package reference +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + // A Claim is a reference to a claim. type Claim struct { // APIVersion of the referenced claim. @@ -26,3 +47,13 @@ type Composite struct { // Name of the referenced composite. Name string `json:"name"` } + +// GroupVersionKind returns the GroupVersionKind of the claim reference. +func (c *Claim) GroupVersionKind() schema.GroupVersionKind { + return schema.FromAPIVersionAndKind(c.APIVersion, c.Kind) +} + +// GroupVersionKind returns the GroupVersionKind of the composite reference. +func (c *Composite) GroupVersionKind() schema.GroupVersionKind { + return schema.FromAPIVersionAndKind(c.APIVersion, c.Kind) +}