Fix: remove last-applied-config annotation for configmap and secret (#3939)

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
This commit is contained in:
Jianbo Sun
2022-05-20 17:08:36 +08:00
committed by GitHub
parent 8ab311f2ff
commit 6bef810dc3
3 changed files with 55 additions and 8 deletions
+1 -1
View File
@@ -117,7 +117,7 @@ const (
AnnotationAppGeneration = "app.oam.dev/generation"
// AnnotationLastAppliedConfig records the previous configuration of a
// resource for use in a three way diff during a patching apply
// resource for use in a three-way diff during a patching apply
AnnotationLastAppliedConfig = "app.oam.dev/last-applied-configuration"
// AnnotationLastAppliedTime indicates the last applied time
+36 -7
View File
@@ -20,19 +20,19 @@ import (
"context"
"fmt"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/oam/util"
)
const (
@@ -107,13 +107,42 @@ func loggingApply(msg string, desired client.Object) {
klog.InfoS(msg, "name", d.GetName(), "resource", desired.GetObjectKind().GroupVersionKind().String())
}
// filterRecordForSpecial will filter special object that can reduce the record for "app.oam.dev/last-applied-configuration" annotation.
func filterRecordForSpecial(desired client.Object) bool {
if desired == nil {
return false
}
gvk := desired.GetObjectKind().GroupVersionKind()
gp, kd := gvk.Group, gvk.Kind
if gp == "" {
// group is empty means it's Kubernetes core API, we won't record annotation for Secret and Configmap
if kd == "Secret" || kd == "ConfigMap" {
return false
}
if _, ok := desired.(*corev1.ConfigMap); ok {
return false
}
if _, ok := desired.(*corev1.Secret); ok {
return false
}
}
ann := desired.GetAnnotations()
if ann != nil {
lac := ann[oam.AnnotationLastAppliedConfig]
if lac == "-" || lac == "skip" {
return false
}
}
return true
}
// Apply applies new state to an object or create it if not exist
func (a *APIApplicator) Apply(ctx context.Context, desired client.Object, ao ...ApplyOption) error {
_, err := generateRenderHash(desired)
if err != nil {
return err
}
applyAct := &applyAction{updateAnnotation: true}
applyAct := &applyAction{updateAnnotation: filterRecordForSpecial(desired)}
existing, err := a.createOrGetExisting(ctx, applyAct, a.c, desired, ao...)
if err != nil {
return err
+18
View File
@@ -23,8 +23,10 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -423,3 +425,19 @@ func TestMustBeControlledByApp(t *testing.T) {
})
}
}
func TestFilterSpecialAnn(t *testing.T) {
var cm = &corev1.ConfigMap{}
var sc = &corev1.Secret{}
var dp = &appsv1.Deployment{}
assert.Equal(t, false, filterRecordForSpecial(cm))
assert.Equal(t, false, filterRecordForSpecial(sc))
assert.Equal(t, true, filterRecordForSpecial(dp))
dp.Annotations = map[string]string{oam.AnnotationLastAppliedConfig: "-"}
assert.Equal(t, false, filterRecordForSpecial(dp))
dp.Annotations = map[string]string{oam.AnnotationLastAppliedConfig: "skip"}
assert.Equal(t, false, filterRecordForSpecial(dp))
dp.Annotations = map[string]string{oam.AnnotationLastAppliedConfig: "xxx"}
assert.Equal(t, true, filterRecordForSpecial(dp))
}