Skip to content
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
4 changes: 4 additions & 0 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ func (m *appStateManager) CompareAppState(app *v1alpha1.Application, project *ap
_, refreshRequested := app.IsRefreshRequested()
noCache = noCache || refreshRequested || app.Status.Expired(m.statusRefreshTimeout)

for i := range reconciliation.Target {
_ = m.resourceTracking.Normalize(reconciliation.Target[i], reconciliation.Live[i], appLabelKey, string(trackingMethod))
}

if noCache || specChanged || revisionChanged || m.cache.GetAppManagedResources(app.Name, &cachedDiff) != nil {
// (rare) cache miss
diffResults, err = diff.DiffArray(reconciliation.Target, reconciliation.Live, diffOpts...)
Expand Down
35 changes: 35 additions & 0 deletions util/argo/resource_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"github.com/argoproj/gitops-engine/pkg/utils/kube"

"github.com/argoproj/argo-cd/v2/common"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -30,6 +32,7 @@ type ResourceTracking interface {
SetAppInstance(un *unstructured.Unstructured, key, val, namespace string, trackingMethod v1alpha1.TrackingMethod) error
BuildAppInstanceValue(value AppInstanceValue) string
ParseAppInstanceValue(value string) (*AppInstanceValue, error)
Normalize(config, live *unstructured.Unstructured, labelKey, trackingMethod string) error
}

//AppInstanceValue store information about resource tracking info
Expand Down Expand Up @@ -57,6 +60,10 @@ func GetTrackingMethod(settingsMgr *settings.SettingsManager) v1alpha1.TrackingM
return v1alpha1.TrackingMethod(tm)
}

func IsOldTrackingMethod(trackingMethod string) bool {
return trackingMethod == "" || trackingMethod == string(TrackingMethodLabel)
}

// GetAppName retrieve application name base on tracking method
func (rt *resourceTracking) GetAppName(un *unstructured.Unstructured, key string, trackingMethod v1alpha1.TrackingMethod) string {
retrieveAppInstanceValue := func() string {
Expand Down Expand Up @@ -142,3 +149,31 @@ func (rt *resourceTracking) ParseAppInstanceValue(value string) (*AppInstanceVal
appInstanceValue.Name = nsParts[1]
return &appInstanceValue, nil
}

func (rt *resourceTracking) Normalize(config, live *unstructured.Unstructured, labelKey, trackingMethod string) error {
if IsOldTrackingMethod(trackingMethod) {
return nil
}

if live == nil || config == nil {
return nil
}

label := kube.GetAppInstanceLabel(live, labelKey)
if label == "" {
return nil
}

annotation := argokube.GetAppInstanceAnnotation(config, common.AnnotationKeyAppInstance)
err := argokube.SetAppInstanceAnnotation(live, common.AnnotationKeyAppInstance, annotation)
if err != nil {
return err
}

err = argokube.SetAppInstanceLabel(config, labelKey, label)
if err != nil {
return err
}

return nil
}
32 changes: 32 additions & 0 deletions util/argo/resource_tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io/ioutil"
"testing"

"github.com/argoproj/argo-cd/v2/util/kube"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -104,3 +106,33 @@ func TestParseAppInstanceValueCorrectFormat(t *testing.T) {
_, err := resourceTracking.ParseAppInstanceValue("app:group/kind:test/ns")
assert.NoError(t, err)
}

func TestResourceIdNormalizer_Normalize(t *testing.T) {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
var obj *unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
assert.Nil(t, err)

rt := NewResourceTracking()

err = rt.SetAppInstance(obj, common.LabelKeyAppInstance, "my-app", "", TrackingMethodLabel)
assert.Nil(t, err)

yamlBytes, err = ioutil.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
var obj2 *unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj2)
assert.Nil(t, err)

err = rt.SetAppInstance(obj2, common.AnnotationKeyAppInstance, "my-app2", "", TrackingMethodAnnotation)
assert.Nil(t, err)

_ = rt.Normalize(obj2, obj, common.LabelKeyAppInstance, string(TrackingMethodAnnotation))
annotation := kube.GetAppInstanceAnnotation(obj2, common.AnnotationKeyAppInstance)
assert.Equal(t, obj.GetAnnotations()[common.AnnotationKeyAppInstance], annotation)
}

func TestIsOldTrackingMethod(t *testing.T) {
assert.Equal(t, true, IsOldTrackingMethod(string(TrackingMethodLabel)))
}