From e3215feb80ce1e6e007d5068620c128a29b4b332 Mon Sep 17 00:00:00 2001 From: lsviben Date: Wed, 10 Jan 2024 13:35:27 +0100 Subject: [PATCH] handle non controlling management policy readiness Signed-off-by: lsviben --- apis/object/v1alpha2/types.go | 2 +- internal/controller/object/object.go | 6 +++++ internal/controller/object/object_test.go | 23 +++++++++++++++++++ .../kubernetes.crossplane.io_objects.yaml | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/apis/object/v1alpha2/types.go b/apis/object/v1alpha2/types.go index 5685fa14..75210ed8 100644 --- a/apis/object/v1alpha2/types.go +++ b/apis/object/v1alpha2/types.go @@ -120,7 +120,7 @@ const ( type Readiness struct { // Policy defines how the Object's readiness condition should be computed. // +optional - // +kubebuilder:validation:Enum=SuccessfulCreate;DeriveFromObject + // +kubebuilder:validation:Enum=SuccessfulCreate;DeriveFromObject;AllTrue // +kubebuilder:default=SuccessfulCreate Policy ReadinessPolicy `json:"policy,omitempty"` } diff --git a/internal/controller/object/object.go b/internal/controller/object/object.go index 750a42b3..82f25a13 100644 --- a/internal/controller/object/object.go +++ b/internal/controller/object/object.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -497,6 +498,11 @@ func (c *external) resolveReferencies(ctx context.Context, obj *v1alpha2.Object) func (c *external) handleLastApplied(ctx context.Context, obj *v1alpha2.Object, last, desired *unstructured.Unstructured) (managed.ExternalObservation, error) { isUpToDate := false + if !sets.New[xpv1.ManagementAction](obj.GetManagementPolicies()...). + HasAny(xpv1.ManagementActionUpdate, xpv1.ManagementActionCreate, xpv1.ManagementActionAll) { + // Treated as up-to-date as we don't update or create the resource + isUpToDate = true + } if last != nil && equality.Semantic.DeepEqual(last, desired) { // Mark as up-to-date since last is equal to desired isUpToDate = true diff --git a/internal/controller/object/object_test.go b/internal/controller/object/object_test.go index 6010b49e..2373366a 100644 --- a/internal/controller/object/object_test.go +++ b/internal/controller/object/object_test.go @@ -98,6 +98,7 @@ func kubernetesObject(om ...kubernetesObjectModifier) *v1alpha2.Object { ProviderConfigReference: &xpv1.Reference{ Name: providerName, }, + ManagementPolicies: xpv1.ManagementPolicies{xpv1.ManagementActionAll}, }, ForProvider: v1alpha2.ObjectParameters{ Manifest: runtime.RawExtension{Raw: externalResourceRaw}, @@ -1005,6 +1006,28 @@ func Test_helmExternal_Observe(t *testing.T) { err: errors.Wrap(errors.Wrap(errBoom, errGetObject), errGetConnectionDetails), }, }, + "Observe Only - up to date by default": { + args: args{ + mg: kubernetesObject(func(obj *v1alpha2.Object) { + obj.Spec.ManagementPolicies = xpv1.ManagementPolicies{xpv1.ManagementActionObserve} + }), + client: resource.ClientApplicator{ + Client: &test.MockClient{ + MockGet: test.NewMockGetFn(nil, func(obj client.Object) error { + *obj.(*unstructured.Unstructured) = + *externalResourceWithLastAppliedConfigAnnotation( + `{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"crossplane-system", "labels": {"old-label":"gone"}}}`, + ) + return nil + }), + }, + }, + }, + want: want{ + out: managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true, ConnectionDetails: managed.ConnectionDetails{}}, + err: nil, + }, + }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { diff --git a/package/crds/kubernetes.crossplane.io_objects.yaml b/package/crds/kubernetes.crossplane.io_objects.yaml index f097a70d..9e8f2a5f 100644 --- a/package/crds/kubernetes.crossplane.io_objects.yaml +++ b/package/crds/kubernetes.crossplane.io_objects.yaml @@ -721,6 +721,7 @@ spec: enum: - SuccessfulCreate - DeriveFromObject + - AllTrue type: string type: object references: