Files
kubevela/pkg/resourcekeeper/statekeep.go
github-actions[bot] 28488a4e9b [Backport release-1.6] Feat: enhance the apply-once capability (#4919)
* Feat: enhance the apply-once capability

Signed-off-by: 朱晓兵 <596908030@qq.com>
(cherry picked from commit 43eef883d0)

* Fix: add unit-test

Signed-off-by: 朱晓兵 <596908030@qq.com>
(cherry picked from commit 514c2bc8bd)

* Fix: adjustment variable name

Signed-off-by: 朱晓兵 <596908030@qq.com>
(cherry picked from commit 70b3621ac6)

* Fix: add doc

Signed-off-by: 朱晓兵 <596908030@qq.com>
(cherry picked from commit 5506fe9cda)

* Fix: adjustment variable name

Signed-off-by: 朱晓兵 <596908030@qq.com>
(cherry picked from commit 4e5f8d9443)

Co-authored-by: 朱晓兵 <596908030@qq.com>
2022-10-26 16:36:08 +08:00

131 lines
4.5 KiB
Go

/*
Copyright 2021 The KubeVela 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 resourcekeeper
import (
"context"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/auth"
"github.com/oam-dev/kubevela/pkg/multicluster"
"github.com/oam-dev/kubevela/pkg/utils/apply"
)
// StateKeep run this function to keep resources up-to-date
func (h *resourceKeeper) StateKeep(ctx context.Context) error {
if h.applyOncePolicy != nil && h.applyOncePolicy.Enable && h.applyOncePolicy.Rules == nil {
return nil
}
ctx = auth.ContextWithUserInfo(ctx, h.app)
for _, rt := range []*v1beta1.ResourceTracker{h._currentRT, h._rootRT} {
if rt != nil && rt.GetDeletionTimestamp() == nil {
for _, mr := range rt.Spec.ManagedResources {
entry := h.cache.get(ctx, mr)
if entry.err != nil {
return entry.err
}
if mr.Deleted {
if entry.exists && entry.obj != nil && entry.obj.GetDeletionTimestamp() == nil {
deleteCtx := multicluster.ContextWithClusterName(ctx, mr.Cluster)
if err := h.Client.Delete(deleteCtx, entry.obj); err != nil {
return errors.Wrapf(err, "failed to delete outdated resource %s in resourcetracker %s", mr.ResourceKey(), rt.Name)
}
}
} else {
if mr.Data == nil || mr.Data.Raw == nil {
// no state-keep
continue
}
manifest, err := mr.ToUnstructuredWithData()
if err != nil {
return errors.Wrapf(err, "failed to decode resource %s from resourcetracker", mr.ResourceKey())
}
applyCtx := multicluster.ContextWithClusterName(ctx, mr.Cluster)
manifest, err = ApplyStrategies(applyCtx, h, manifest, v1alpha1.ApplyOnceStrategyOnAppStateKeep)
if err != nil {
return errors.Wrapf(err, "failed to apply once resource %s from resourcetracker %s", mr.ResourceKey(), rt.Name)
}
ao := []apply.ApplyOption{apply.MustBeControlledByApp(h.app)}
if h.isShared(manifest) {
ao = append([]apply.ApplyOption{apply.SharedByApp(h.app)}, ao...)
}
if err = h.applicator.Apply(applyCtx, manifest, ao...); err != nil {
return errors.Wrapf(err, "failed to re-apply resource %s from resourcetracker %s", mr.ResourceKey(), rt.Name)
}
}
}
}
}
return nil
}
// ApplyStrategies will generate manifest with applyOnceStrategy
func ApplyStrategies(ctx context.Context, h *resourceKeeper, manifest *unstructured.Unstructured, matchedAffectStage v1alpha1.ApplyOnceAffectStrategy) (*unstructured.Unstructured, error) {
if h.applyOncePolicy == nil {
return manifest, nil
}
strategy := h.applyOncePolicy.FindStrategy(manifest)
if strategy != nil {
affectStage := strategy.ApplyOnceAffectStrategy
if shouldMerge(affectStage, matchedAffectStage) {
un := new(unstructured.Unstructured)
un.SetAPIVersion(manifest.GetAPIVersion())
un.SetKind(manifest.GetKind())
err := h.Get(ctx, types.NamespacedName{Name: manifest.GetName(), Namespace: manifest.GetNamespace()}, un)
if err != nil {
if kerrors.IsNotFound(err) {
return manifest, nil
}
return nil, err
}
return mergeValue(strategy.Path, manifest, un)
}
}
return manifest, nil
}
func shouldMerge(affectStage v1alpha1.ApplyOnceAffectStrategy, matchedAffectType v1alpha1.ApplyOnceAffectStrategy) bool {
return affectStage == "" || affectStage == v1alpha1.ApplyOnceStrategyAlways || affectStage == matchedAffectType
}
func mergeValue(paths []string, manifest *unstructured.Unstructured, un *unstructured.Unstructured) (*unstructured.Unstructured, error) {
for _, path := range paths {
if path == "*" {
manifest = un.DeepCopy()
break
}
value, err := fieldpath.Pave(un.UnstructuredContent()).GetValue(path)
if err != nil {
return nil, err
}
err = fieldpath.Pave(manifest.UnstructuredContent()).SetValue(path, value)
if err != nil {
return nil, err
}
}
return manifest, nil
}