From c4a0c1480d206507581c8d7fb75fd9ab1397943a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=99=93=E5=85=B5?= <596908030@qq.com> Date: Tue, 25 Oct 2022 21:15:21 +0800 Subject: [PATCH] Feat: enhance the apply-once capability (#4874) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Feat: enhance the apply-once capability Signed-off-by: 朱晓兵 <596908030@qq.com> * Fix: add unit-test Signed-off-by: 朱晓兵 <596908030@qq.com> * Fix: adjustment variable name Signed-off-by: 朱晓兵 <596908030@qq.com> * Fix: add doc Signed-off-by: 朱晓兵 <596908030@qq.com> * Fix: adjustment variable name Signed-off-by: 朱晓兵 <596908030@qq.com> Signed-off-by: 朱晓兵 <596908030@qq.com> --- apis/core.oam.dev/v1alpha1/applyonce_types.go | 12 + .../templates/defwithtemplate/apply-once.yaml | 2 + .../templates/defwithtemplate/apply-once.yaml | 2 + .../apply-once-policy/apply-once.md | 59 +++- .../application/apply_once_policy_test.go | 281 ++++++++++++++++++ pkg/resourcekeeper/dispatch.go | 5 + pkg/resourcekeeper/statekeep.go | 62 ++-- .../internal/policy/apply-once.cue | 2 + 8 files changed, 402 insertions(+), 23 deletions(-) create mode 100644 pkg/controller/core.oam.dev/v1alpha2/application/apply_once_policy_test.go diff --git a/apis/core.oam.dev/v1alpha1/applyonce_types.go b/apis/core.oam.dev/v1alpha1/applyonce_types.go index d713c18dc..9493f1b96 100644 --- a/apis/core.oam.dev/v1alpha1/applyonce_types.go +++ b/apis/core.oam.dev/v1alpha1/applyonce_types.go @@ -23,8 +23,17 @@ import ( const ( // ApplyOncePolicyType refers to the type of configuration drift policy ApplyOncePolicyType = "apply-once" + // ApplyOnceStrategyOnAppUpdate policy takes effect on application updating + ApplyOnceStrategyOnAppUpdate ApplyOnceAffectStrategy = "onUpdate" + // ApplyOnceStrategyOnAppStateKeep policy takes effect on application state keep + ApplyOnceStrategyOnAppStateKeep ApplyOnceAffectStrategy = "onStateKeep" + // ApplyOnceStrategyAlways policy takes effect always + ApplyOnceStrategyAlways ApplyOnceAffectStrategy = "always" ) +// ApplyOnceAffectStrategy is a string that mark the policy effective stage +type ApplyOnceAffectStrategy string + // ApplyOncePolicySpec defines the spec of preventing configuration drift type ApplyOncePolicySpec struct { Enable bool `json:"enable"` @@ -45,6 +54,9 @@ type ApplyOnceStrategy struct { // Path the specified path that allow configuration drift // like 'spec.template.spec.containers[0].resources' and '*' means the whole target allow configuration drift Path []string `json:"path"` + // ApplyOnceAffectStrategy Decide when the strategy will take effect + // like affect:onUpdate/onStateKeep/always + ApplyOnceAffectStrategy ApplyOnceAffectStrategy `json:"affect"` } // FindStrategy find apply-once strategy for target resource diff --git a/charts/vela-core/templates/defwithtemplate/apply-once.yaml b/charts/vela-core/templates/defwithtemplate/apply-once.yaml index 6a07df1b0..68563ff44 100644 --- a/charts/vela-core/templates/defwithtemplate/apply-once.yaml +++ b/charts/vela-core/templates/defwithtemplate/apply-once.yaml @@ -12,6 +12,8 @@ spec: cue: template: | #ApplyOnceStrategy: { + // +usage=When the strategy takes effect,e.g. onUpdate、onStateKeep + affect?: string // +usage=Specify the path of the resource that allow configuration drift path: [...string] } diff --git a/charts/vela-minimal/templates/defwithtemplate/apply-once.yaml b/charts/vela-minimal/templates/defwithtemplate/apply-once.yaml index 6a07df1b0..68563ff44 100644 --- a/charts/vela-minimal/templates/defwithtemplate/apply-once.yaml +++ b/charts/vela-minimal/templates/defwithtemplate/apply-once.yaml @@ -12,6 +12,8 @@ spec: cue: template: | #ApplyOnceStrategy: { + // +usage=When the strategy takes effect,e.g. onUpdate、onStateKeep + affect?: string // +usage=Specify the path of the resource that allow configuration drift path: [...string] } diff --git a/docs/examples/app-with-policy/apply-once-policy/apply-once.md b/docs/examples/app-with-policy/apply-once-policy/apply-once.md index 94755b5ff..f5f2ecb5d 100644 --- a/docs/examples/app-with-policy/apply-once-policy/apply-once.md +++ b/docs/examples/app-with-policy/apply-once-policy/apply-once.md @@ -120,4 +120,61 @@ EOF In the `apply-once-app-3` case, any changes of `hello-cosmos` deployment will not be brought back and any changes of `hello-cosmos` service will be brought back in the next reconcile loop. In the same time, any changes -of `hello-world` component will be brought back in the next reconcile loop. \ No newline at end of file +of `hello-world` component will be brought back in the next reconcile loop. + +```shell +$ cat <