Skip to content
Closed
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
2 changes: 1 addition & 1 deletion cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ func groupObjsForDiff(resources *application.ManagedResourcesResponse, objs map[
}
if local, ok := objs[key]; ok || live != nil {
if local != nil && !kube.IsCRD(local) {
err = argokube.SetAppInstanceLabel(local, argoSettings.AppLabelKey, appName)
err = argokube.SetAppInstanceIdentifier(local, argoSettings.AppLabelKey, appName)
errors.CheckError(err)
}

Expand Down
3 changes: 2 additions & 1 deletion controller/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/util/argo"
"github.com/argoproj/argo-cd/util/db"
argokube "github.com/argoproj/argo-cd/util/kube"
logutils "github.com/argoproj/argo-cd/util/log"
"github.com/argoproj/argo-cd/util/lua"
"github.com/argoproj/argo-cd/util/settings"
Expand Down Expand Up @@ -256,7 +257,7 @@ func (c *liveStateCache) getCluster(server string) (clustercache.ClusterCache, e
res := &ResourceInfo{}
populateNodeInfo(un, res)
res.Health, _ = health.GetResourceHealth(un, cacheSettings.clusterSettings.ResourceHealthOverride)
appName := kube.GetAppInstanceLabel(un, cacheSettings.appInstanceLabelKey)
appName := argokube.GetAppInstanceIdentifier(un, cacheSettings.appInstanceLabelKey)
if isRoot && appName != "" {
res.AppName = appName
}
Expand Down
3 changes: 2 additions & 1 deletion controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/argoproj/argo-cd/util/gpg"
argohealth "github.com/argoproj/argo-cd/util/health"
"github.com/argoproj/argo-cd/util/io"
argokube "github.com/argoproj/argo-cd/util/kube"
"github.com/argoproj/argo-cd/util/settings"
"github.com/argoproj/argo-cd/util/stats"
)
Expand Down Expand Up @@ -409,7 +410,7 @@ func (m *appStateManager) CompareAppState(app *v1alpha1.Application, project *ap

for _, liveObj := range liveObjByKey {
if liveObj != nil {
appInstanceName := kubeutil.GetAppInstanceLabel(liveObj, appLabelKey)
appInstanceName := argokube.GetAppInstanceIdentifier(liveObj, appLabelKey)
if appInstanceName != "" && appInstanceName != app.Name {
conditions = append(conditions, v1alpha1.ApplicationCondition{
Type: v1alpha1.ApplicationConditionSharedResourceWarning,
Expand Down
3 changes: 2 additions & 1 deletion controller/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
listersv1alpha1 "github.com/argoproj/argo-cd/pkg/client/listers/application/v1alpha1"
"github.com/argoproj/argo-cd/util/argo"
argokube "github.com/argoproj/argo-cd/util/kube"
logutils "github.com/argoproj/argo-cd/util/log"
"github.com/argoproj/argo-cd/util/lua"
"github.com/argoproj/argo-cd/util/rand"
Expand Down Expand Up @@ -160,7 +161,7 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, state *v1alpha
}),
sync.WithManifestValidation(!syncOp.SyncOptions.HasOption("Validate=false")),
sync.WithNamespaceCreation(syncOp.SyncOptions.HasOption("CreateNamespace=true"), func(un *unstructured.Unstructured) bool {
if un != nil && kube.GetAppInstanceLabel(un, cdcommon.LabelKeyAppInstance) != "" {
if un != nil && argokube.GetAppInstanceIdentifier(un, cdcommon.LabelKeyAppInstance) != "" {
kube.UnsetLabel(un, cdcommon.LabelKeyAppInstance)
return true
}
Expand Down
2 changes: 1 addition & 1 deletion reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func GenerateManifests(appPath, repoRoot, revision string, q *apiclient.Manifest

for _, target := range targets {
if q.AppLabelKey != "" && q.AppLabelValue != "" && !kube.IsCRD(target) {
err = argokube.SetAppInstanceLabel(target, q.AppLabelKey, q.AppLabelValue)
err = argokube.SetAppInstanceIdentifier(target, q.AppLabelKey, q.AppLabelValue)
if err != nil {
return nil, err
}
Expand Down
66 changes: 66 additions & 0 deletions util/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import (
"github.com/argoproj/argo-cd/common"
)

// GetAppInstanceLabel returns the application instance name from labels
func GetAppInstanceIdentifier(un *unstructured.Unstructured, key string) string {
if labels := un.GetLabels(); labels != nil {
return labels[key]
}
return ""
}

func SetAppInstanceIdentifier(target *unstructured.Unstructured, key, val string) error {
return SetAppInstanceIdentifier(target, key, val)
}

// SetAppInstanceLabel the recommended app.kubernetes.io/instance label against an unstructured object
// Uses the legacy labeling if environment variable is set
func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) error {
Expand Down Expand Up @@ -83,3 +95,57 @@ func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) err
}
return nil
}

// SetAppInstanceAnnotation the recommended app.kubernetes.io/instance annotation against an unstructured object
// Uses the legacy annotation if environment variable is set
func SetAppInstanceAnnotation(target *unstructured.Unstructured, key, val string) error {
annotations := target.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[key] = val
target.SetAnnotations(annotations)
if key != common.LabelKeyLegacyApplicationName {
// we no longer annotate the pod template sub resources in v0.11
return nil
}

gvk := schema.FromAPIVersionAndKind(target.GetAPIVersion(), target.GetKind())
// special case for deployment and job types: make sure that derived replicaset, and pod has
// the application annotation
switch gvk.Group {
case "apps", "extensions":
switch gvk.Kind {
case kube.DeploymentKind, kube.ReplicaSetKind, kube.StatefulSetKind, kube.DaemonSetKind:
templateAnnotations, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "annotations")
if err != nil {
return err
}
if !ok || templateAnnotations == nil {
templateAnnotations = make(map[string]interface{})
}
templateAnnotations[key] = val
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateAnnotations, "spec", "template", "metadata", "annotations")
if err != nil {
return err
}
}
case "batch":
switch gvk.Kind {
case kube.JobKind:
templateAnnotations, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "annotations")
if err != nil {
return err
}
if !ok || templateAnnotations == nil {
templateAnnotations = make(map[string]interface{})
}
templateAnnotations[key] = val
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateAnnotations, "spec", "template", "metadata", "annotations")
if err != nil {
return err
}
}
}
return nil
}
8 changes: 4 additions & 4 deletions util/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSetLabels(t *testing.T) {
err := yaml.Unmarshal([]byte(yamlStr), &obj)
assert.Nil(t, err)

err = SetAppInstanceLabel(&obj, common.LabelKeyAppInstance, "my-app")
err = SetAppInstanceIdentifier(&obj, common.LabelKeyAppInstance, "my-app")
assert.Nil(t, err)

manifestBytes, err := json.MarshalIndent(obj.Object, "", " ")
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSetLegacyLabels(t *testing.T) {
err := yaml.Unmarshal([]byte(yamlStr), &obj)
assert.Nil(t, err)

err = SetAppInstanceLabel(&obj, common.LabelKeyLegacyApplicationName, "my-app")
err = SetAppInstanceIdentifier(&obj, common.LabelKeyLegacyApplicationName, "my-app")
assert.Nil(t, err)

manifestBytes, err := json.MarshalIndent(obj.Object, "", " ")
Expand All @@ -114,7 +114,7 @@ func TestSetLegacyJobLabel(t *testing.T) {
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
assert.Nil(t, err)
err = SetAppInstanceLabel(&obj, common.LabelKeyLegacyApplicationName, "my-app")
err = SetAppInstanceIdentifier(&obj, common.LabelKeyLegacyApplicationName, "my-app")
assert.Nil(t, err)

manifestBytes, err := json.MarshalIndent(obj.Object, "", " ")
Expand All @@ -140,7 +140,7 @@ func TestSetSvcLabel(t *testing.T) {
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
assert.Nil(t, err)
err = SetAppInstanceLabel(&obj, common.LabelKeyAppInstance, "my-app")
err = SetAppInstanceIdentifier(&obj, common.LabelKeyAppInstance, "my-app")
assert.Nil(t, err)

manifestBytes, err := json.MarshalIndent(obj.Object, "", " ")
Expand Down