-
Notifications
You must be signed in to change notification settings - Fork 872
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
Feat: optimize empty patch request #5600
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"github.com/crossplane/crossplane-runtime/pkg/meta" | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
@@ -121,6 +122,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu | |
} | ||
return r.result(client.IgnoreNotFound(err)).ret() | ||
} | ||
ctx = withOriginalApp(ctx, app) | ||
|
||
if !r.matchControllerRequirement(app) { | ||
logCtx.Info("skip app: not match the controller requirement of app") | ||
|
@@ -410,6 +412,9 @@ func (r *Reconciler) endWithNegativeCondition(ctx context.Context, app *v1beta1. | |
func (r *Reconciler) patchStatus(ctx context.Context, app *v1beta1.Application, phase common.ApplicationPhase) error { | ||
app.Status.Phase = phase | ||
updateObservedGeneration(app) | ||
if oldApp, ok := originalAppFrom(ctx); ok && oldApp != nil && equality.Semantic.DeepEqual(oldApp.Status, app.Status) { | ||
return nil | ||
} | ||
ctx, cancel := ctrlrec.NewReconcileTerminationContext(ctx) | ||
defer cancel() | ||
if err := r.Status().Patch(ctx, app, client.Merge); err != nil { | ||
|
@@ -423,6 +428,9 @@ func (r *Reconciler) patchStatus(ctx context.Context, app *v1beta1.Application, | |
func (r *Reconciler) updateStatus(ctx context.Context, app *v1beta1.Application, phase common.ApplicationPhase) error { | ||
app.Status.Phase = phase | ||
updateObservedGeneration(app) | ||
if oldApp, ok := originalAppFrom(ctx); ok && oldApp != nil && equality.Semantic.DeepEqual(oldApp.Status, app.Status) { | ||
return nil | ||
} | ||
ctx, cancel := ctrlrec.NewReconcileTerminationContext(ctx) | ||
defer cancel() | ||
if !r.disableStatusUpdate { | ||
|
@@ -632,3 +640,23 @@ func (r *Reconciler) matchControllerRequirement(app *v1beta1.Application) bool { | |
} | ||
return true | ||
} | ||
|
||
const ( | ||
// ComponentNamespaceContextKey is the key in context that defines the override namespace of component | ||
ComponentNamespaceContextKey contextKey = iota | ||
// ComponentContextKey is the key in context that records the component | ||
ComponentContextKey | ||
// ReplicaKeyContextKey is the key in context that records the replica key | ||
ReplicaKeyContextKey | ||
// OriginalAppKey is the key in the context that records the in coming original app | ||
OriginalAppKey | ||
) | ||
|
||
func withOriginalApp(ctx context.Context, app *v1beta1.Application) context.Context { | ||
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 make this function to be a method of 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. NewReconcileContext is not only used for application controller. It is used for all KubeVela eco-system controllers. |
||
return context.WithValue(ctx, OriginalAppKey, app.DeepCopy()) | ||
} | ||
|
||
func originalAppFrom(ctx context.Context) (*v1beta1.Application, bool) { | ||
app, ok := ctx.Value(OriginalAppKey).(*v1beta1.Application) | ||
return app, ok | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -169,3 +169,11 @@ func getOriginalConfiguration(obj runtime.Object) ([]byte, error) { | |||
} | ||||
return []byte(original), nil | ||||
} | ||||
|
||||
func isEmptyPatch(patch client.Patch) bool { | ||||
if patch == nil { | ||||
return true | ||||
} | ||||
data, _ := patch.Data(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. why pass a nil into the patch.Data? 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. Because that is the interface parameter. The generated patch already contain data.
|
||||
return data != nil && string(data) == "{}" | ||||
} |
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.
will it be overriden by
context[0] = "other-thing"
?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.
No. There is no int key 0. It will be pkg/controller/core.oam.dev/v1alpha2/application:contextKey(0).