Skip to content

Commit

Permalink
handle non controlling management policy readiness
Browse files Browse the repository at this point in the history
Signed-off-by: lsviben <[email protected]>
  • Loading branch information
lsviben committed Jan 10, 2024
1 parent 1e57b70 commit e3215fe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apis/object/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions internal/controller/object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions package/crds/kubernetes.crossplane.io_objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ spec:
enum:
- SuccessfulCreate
- DeriveFromObject
- AllTrue
type: string
type: object
references:
Expand Down

0 comments on commit e3215fe

Please sign in to comment.