-
Notifications
You must be signed in to change notification settings - Fork 131
Fix bug causing status to be cleared after adding Finalizer #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2019 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 v1 | ||
|
||
import "k8s.io/apimachinery/pkg/runtime" | ||
|
||
// See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties | ||
|
||
// A Status reflects the observed status of a resource. | ||
type Status map[string]runtime.RawExtension | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think to be able to reach in and get/set conditions directly? |
||
|
||
// A Statused resource is one that has a status. | ||
type Statused struct { | ||
Status Status `json:"status"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1164,6 +1164,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (resu | |
managed.SetConditions(xpv1.ReconcileError(err)) | ||
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus) | ||
} | ||
// Set the status back to its original value before the AddFinalizer call. | ||
// The AddFinalizer call may overwrite the managed object with what is | ||
// currently in the kube API which would make us lose information that may | ||
// have been populated by the Observe call. | ||
managed.SetStatus(managedPreOp.GetStatus()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try to make a new pave function that copies the original pre-op status into managed rather than introduce the .Set/GetStatus? I think it has the same effect and does not spread out the API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I absolutely don't mind, I simply followed initial advice provided on the issue in this comment. |
||
|
||
if !observation.ResourceExists && policy.ShouldCreate() { | ||
// We write this annotation for two reasons. Firstly, it helps | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,6 +653,52 @@ func TestReconciler(t *testing.T) { | |
}, | ||
want: want{result: reconcile.Result{Requeue: true}}, | ||
}, | ||
"AddFinalizerShouldNotClearStatus": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to comment out |
||
reason: "Adding a finalizer should not clear the resource's status object.", | ||
args: args{ | ||
m: &fake.Manager{ | ||
Client: &test.MockClient{ | ||
MockGet: test.NewMockGetFn(nil), | ||
MockUpdate: test.NewMockUpdateFn(nil), | ||
MockStatusUpdate: test.MockSubResourceUpdateFn(func(_ context.Context, obj client.Object, _ ...client.SubResourceUpdateOption) error { | ||
want := &fake.Managed{} | ||
want.SetConditions(xpv1.ReconcileSuccess(), xpv1.Available()) | ||
if diff := cmp.Diff(want, obj, test.EquateConditions()); diff != "" { | ||
reason := "Adding a finalizer should not clear the resource's status object." | ||
t.Errorf("\nReason: %s\n-want, +got:\n%s", reason, diff) | ||
} | ||
return nil | ||
}), | ||
}, | ||
Scheme: fake.SchemeWith(&fake.Managed{}), | ||
}, | ||
mg: resource.ManagedKind(fake.GVK(&fake.Managed{})), | ||
o: []ReconcilerOption{ | ||
WithInitializers(), | ||
WithReferenceResolver(ReferenceResolverFn(func(_ context.Context, _ resource.Managed) error { return nil })), | ||
WithExternalConnecter(ExternalConnectorFn(func(_ context.Context, _ resource.Managed) (ExternalClient, error) { | ||
c := &ExternalClientFns{ | ||
ObserveFn: func(_ context.Context, obj resource.Managed) (ExternalObservation, error) { | ||
obj.SetConditions(xpv1.Available()) | ||
return ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil | ||
}, | ||
DisconnectFn: func(_ context.Context) error { | ||
return nil | ||
}, | ||
} | ||
return c, nil | ||
})), | ||
WithConnectionPublishers(), | ||
WithFinalizer(resource.FinalizerFns{AddFinalizerFn: func(_ context.Context, obj resource.Object) error { | ||
// Imitate the behavior that the kube client would have when | ||
// adding a finalizer to an object that does not have a status yet | ||
obj.(*fake.Managed).Status = nil | ||
return nil | ||
}}), | ||
}, | ||
}, | ||
want: want{result: reconcile.Result{RequeueAfter: defaultPollInterval}}, | ||
}, | ||
"AddFinalizerError": { | ||
reason: "Errors adding a finalizer should trigger a requeue after a short wait.", | ||
args: args{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,20 @@ import ( | |
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/reference" | ||
) | ||
|
||
// A Conditioned may have conditions set or retrieved. Conditions are typically | ||
// A Conditioned may have conditions set or retrieved. Conditions typically | ||
// indicate the status of both a resource and its reconciliation process. | ||
type Conditioned interface { | ||
SetConditions(c ...xpv1.Condition) | ||
GetCondition(ct xpv1.ConditionType) xpv1.Condition | ||
} | ||
|
||
// A Statused may have status set or retrieved. Status is typically | ||
// a struct containing various runtime status properties. | ||
type Statused interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if we need this... perhaps we just need |
||
SetStatus(c xpv1.Status) | ||
GetStatus() xpv1.Status | ||
} | ||
|
||
// A ClaimReferencer may reference a resource claim. | ||
type ClaimReferencer interface { | ||
SetClaimReference(r *reference.Claim) | ||
|
@@ -200,6 +207,7 @@ type Managed interface { //nolint:interfacebloat // This interface has to be big | |
Orphanable | ||
|
||
Conditioned | ||
Statused | ||
} | ||
|
||
// A ManagedList is a list of managed resources. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,20 @@ func (cr *Unstructured) SetConditions(c ...xpv1.Condition) { | |
_ = fieldpath.Pave(cr.Object).SetValue("status.conditions", conditioned.Conditions) | ||
} | ||
|
||
// GetStatus of this Composed resource. | ||
func (cr *Unstructured) GetStatus() xpv1.Status { | ||
status := xpv1.Statused{} | ||
if err := fieldpath.Pave(cr.Object).GetValueInto("status", &status); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am concerned about |
||
return xpv1.Status{} | ||
} | ||
return status.Status | ||
} | ||
|
||
// SetStatus of this Composed resource. | ||
func (cr *Unstructured) SetStatus(s xpv1.Status) { | ||
_ = fieldpath.Pave(cr.Object).SetValue("status", s) | ||
} | ||
|
||
// GetWriteConnectionSecretToReference of this Composed resource. | ||
func (cr *Unstructured) GetWriteConnectionSecretToReference() *xpv1.SecretReference { | ||
out := &xpv1.SecretReference{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: