Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Copy link
Collaborator

@wonderflow wonderflow Mar 6, 2023

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"

Copy link
Collaborator Author

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).

// 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this function to be a method of ctrlrec.NewReconcileContext

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -66,7 +66,7 @@ import (
pkgutils "github.com/oam-dev/kubevela/pkg/utils"
)

type contextKey string
type contextKey int

const (
// ConfigMapKeyComponents is the key in ConfigMap Data field for containing data of components
Expand All @@ -79,12 +79,6 @@ const (
ManifestKeyTraits = "Traits"
// ManifestKeyScopes is the key in Component Manifest for containing scope cr reference.
ManifestKeyScopes = "Scopes"
// ComponentNamespaceContextKey is the key in context that defines the override namespace of component
ComponentNamespaceContextKey = contextKey("component-namespace")
// ComponentContextKey is the key in context that records the component
ComponentContextKey = contextKey("component")
// ReplicaKeyContextKey is the key in context that records the replica key
ReplicaKeyContextKey = contextKey("replica-key")
)

const rolloutTraitName = "rollout"
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ func (a *APIApplicator) Apply(ctx context.Context, desired client.Object, ao ...
if err != nil {
return errors.Wrap(err, "cannot calculate patch by computing a three way diff")
}
if isEmptyPatch(patch) {
return nil
}
if applyAct.dryRun {
return errors.Wrapf(a.c.Patch(ctx, desired, patch, client.DryRunAll), "cannot patch object")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestAPIApplicator(t *testing.T) {
return tc.args.existing, tc.args.creatorErr
}),
patcher: patcherFn(func(c, m client.Object, a *applyAction) (client.Patch, error) {
return nil, tc.args.patcherErr
return client.RawPatch(types.MergePatchType, []byte(`err`)), tc.args.patcherErr
}),
c: tc.c,
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/apply/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why pass a nil into the patch.Data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that is the interface parameter. The generated patch already contain data.
Reference

  1. https://github.com/kubernetes-sigs/controller-runtime/blob/cd0058ad295c268da1e7233e609a9a18dd60b5f6/pkg/client/patch.go#L48
  2. return client.RawPatch(patchType, patchData), nil

return data != nil && string(data) == "{}"
}
2 changes: 1 addition & 1 deletion test/e2e-multicluster-test/multicluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ var _ = Describe("Test multicluster scenario", func() {
}
}
g.Expect(cnt).Should(Equal(2))
}).WithTimeout(10 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
}).WithTimeout(30 * time.Second).WithPolling(2 * time.Second).Should(Succeed())

By("try update application again")
Expect(k8sClient.Get(hubCtx, key, app)).Should(Succeed())
Expand Down