mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-05 17:07:20 +00:00
Feat: compatible with k8s last-applied-configuration while perfer app.oam.dev/last-applied-configuration in annotations (#5804)
* Fix: when taking over an existing resource for the first time, use kubectl last apply anno Signed-off-by: zxbyoyoyo <596908030@qq.com> * Fix: '-' || 'skip' return nil Signed-off-by: zxbyoyoyo <596908030@qq.com> * Fix: check-diff Signed-off-by: zxbyoyoyo <596908030@qq.com> --------- Signed-off-by: zxbyoyoyo <596908030@qq.com>
This commit is contained in:
@@ -20,6 +20,8 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -163,11 +165,20 @@ func getOriginalConfiguration(obj runtime.Object) ([]byte, error) {
|
||||
if annots == nil {
|
||||
return nil, nil
|
||||
}
|
||||
original, ok := annots[oam.AnnotationLastAppliedConfig]
|
||||
if !ok || original == "-" || original == "skip" {
|
||||
return nil, nil
|
||||
|
||||
oamOriginal, oamOk := annots[oam.AnnotationLastAppliedConfig]
|
||||
if oamOk {
|
||||
if oamOriginal == "-" || oamOriginal == "skip" {
|
||||
return nil, nil
|
||||
}
|
||||
return []byte(oamOriginal), nil
|
||||
}
|
||||
return []byte(original), nil
|
||||
|
||||
kubectlOriginal, kubectlOK := annots[corev1.LastAppliedConfigAnnotation]
|
||||
if kubectlOK {
|
||||
return []byte(kubectlOriginal), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func isEmptyPatch(patch client.Patch) bool {
|
||||
|
||||
@@ -20,6 +20,10 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/test"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/pkg/errors"
|
||||
@@ -59,6 +63,17 @@ func TestGetOriginalConfig(t *testing.T) {
|
||||
objNoAnno := &unstructured.Unstructured{}
|
||||
objNoAnno.SetAnnotations(make(map[string]string))
|
||||
|
||||
objHasAnno := &unstructured.Unstructured{}
|
||||
annoMap := make(map[string]string)
|
||||
annoMap[oam.AnnotationLastAppliedConfig] = "oam obj record"
|
||||
annoMap[corev1.LastAppliedConfigAnnotation] = "kubectl obj record"
|
||||
objHasAnno.SetAnnotations(annoMap)
|
||||
|
||||
objOnlyHasKubectlAnno := &unstructured.Unstructured{}
|
||||
annoOnlyKubectlMap := make(map[string]string)
|
||||
annoOnlyKubectlMap[corev1.LastAppliedConfigAnnotation] = "kubectl obj record"
|
||||
objOnlyHasKubectlAnno.SetAnnotations(annoOnlyKubectlMap)
|
||||
|
||||
cases := map[string]struct {
|
||||
reason string
|
||||
obj runtime.Object
|
||||
@@ -78,6 +93,16 @@ func TestGetOriginalConfig(t *testing.T) {
|
||||
reason: "No error should be returned if cannot find last-applied-config annotaion",
|
||||
obj: objNoAnno,
|
||||
},
|
||||
"OAMLastAppliedConfigAnnotationFound": {
|
||||
reason: "No error should be returned if find oam last-applied-config annotaion ",
|
||||
obj: objHasAnno,
|
||||
wantConfig: "oam obj record",
|
||||
},
|
||||
"KubectlLastAppliedConfigAnnotationFound": {
|
||||
reason: "No error should be returned if find last-applied-config annotaion, prefer oam annotations, followed by kubectl ",
|
||||
obj: objOnlyHasKubectlAnno,
|
||||
wantConfig: "kubectl obj record",
|
||||
},
|
||||
}
|
||||
|
||||
for caseName, tc := range cases {
|
||||
|
||||
Reference in New Issue
Block a user