From dd01037c11082c662ba8c45d8bb5ad21bc2e05f4 Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Thu, 16 Feb 2023 10:19:32 +0530 Subject: [PATCH] Use app label as app-change label for new apps Use app label as app-change label while renaming existing apps Signed-off-by: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> --- pkg/kapp/app/recorded_app.go | 58 ++++++++++++++++++++++------ pkg/kapp/app/recorded_app_changes.go | 20 +++------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/pkg/kapp/app/recorded_app.go b/pkg/kapp/app/recorded_app.go index 79b37a29c..1e4167885 100644 --- a/pkg/kapp/app/recorded_app.go +++ b/pkg/kapp/app/recorded_app.go @@ -26,13 +26,16 @@ const ( KappIsConfigmapMigratedAnnotationKey = "kapp.k14s.io/is-configmap-migrated" KappIsConfigmapMigratedAnnotationValue = "" AppSuffix = ".apps.k14s.io" + KappIsAppLabelAsAppChangeLabelKey = "kapp.k14s.io/is-app-label-app-change-label" + KappIsAppLabelAsAppChangeLabelValue = "" ) type RecordedApp struct { - name string - nsName string - isMigrated bool - creationTimestamp time.Time + name string + nsName string + isMigrated bool + creationTimestamp time.Time + isAppLabelAppChangeLabel bool coreClient kubernetes.Interface identifiedResources ctlres.IdentifiedResources @@ -46,7 +49,7 @@ func NewRecordedApp(name, nsName string, creationTimestamp time.Time, coreClient identifiedResources ctlres.IdentifiedResources, appInDiffNsHintMsgFunc func(string) string, logger logger.Logger) *RecordedApp { // Always trim suffix, even if user added it manually (to avoid double migration) - return &RecordedApp{strings.TrimSuffix(name, AppSuffix), nsName, false, creationTimestamp, coreClient, identifiedResources, appInDiffNsHintMsgFunc, + return &RecordedApp{strings.TrimSuffix(name, AppSuffix), nsName, false, creationTimestamp, false, coreClient, identifiedResources, appInDiffNsHintMsgFunc, nil, logger.NewPrefixed("RecordedApp")} } @@ -137,6 +140,7 @@ func (a *RecordedApp) CreateOrUpdate(labels map[string]string, isDiffRun bool) ( if foundMigratedApp { a.isMigrated = true + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] return false, a.updateApp(app, labels) } @@ -146,6 +150,7 @@ func (a *RecordedApp) CreateOrUpdate(labels map[string]string, isDiffRun bool) ( } if foundNonMigratedApp { + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] if a.isMigrationEnabled() { return false, a.migrate(app, labels, a.fqName()) } @@ -174,6 +179,10 @@ func (a *RecordedApp) create(labels map[string]string, isDiffRun bool) error { Labels: map[string]string{ KappIsAppLabelKey: kappIsAppLabelValue, }, + // Use app label as app change label for all new apps + Annotations: map[string]string{ + KappIsAppLabelAsAppChangeLabelKey: "", + }, }, Data: Meta{ LabelKey: kappAppLabelKey, @@ -203,6 +212,7 @@ func (a *RecordedApp) create(labels map[string]string, isDiffRun bool) error { } app, err := a.coreClient.CoreV1().ConfigMaps(a.nsName).Create(context.TODO(), configMap, createOpts) + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] a.setMeta(*app) return err @@ -232,6 +242,7 @@ func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string if foundMigratedApp { a.isMigrated = true + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] return a.updateApp(app, labels) } @@ -241,6 +252,7 @@ func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string } if foundNonMigratedApp { + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] if a.isMigrationEnabled() { return a.migrate(app, labels, a.fqName()) } @@ -254,6 +266,8 @@ func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string if foundMigratedPrevApp { a.isMigrated = true + // Use app label as app change label while renaming apps + a.mergeAppAnnotationUpdates(app, map[string]string{KappIsAppLabelAsAppChangeLabelKey: KappIsAppLabelAsAppChangeLabelValue}) return a.renameConfigMap(app, a.fqName(), a.nsName) } @@ -263,6 +277,8 @@ func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string } if foundNonMigratedPrevApp { + // Use app label as app change label while renaming apps + a.mergeAppAnnotationUpdates(app, map[string]string{KappIsAppLabelAsAppChangeLabelKey: KappIsAppLabelAsAppChangeLabelValue}) if a.isMigrationEnabled() { return a.migrate(app, labels, a.fqName()) } @@ -359,12 +375,12 @@ func (a *RecordedApp) Delete() error { return err } - meta, err := a.meta() + changeLabelValue, err := a.appChangeLabelValue() if err != nil { return err } - err = NewRecordedAppChanges(a.nsName, a.name, meta.LabelValue, a.coreClient).DeleteAll() + err = NewRecordedAppChanges(a.nsName, a.name, changeLabelValue, a.coreClient).DeleteAll() if err != nil { return fmt.Errorf("Deleting app changes: %w", err) } @@ -403,6 +419,9 @@ func (a *RecordedApp) Rename(newName string, newNamespace string) error { a.name, a.nsName, a.appInDiffNsHintMsgFunc(name)) } + // Use app label as app change label while renaming apps + a.mergeAppAnnotationUpdates(app, map[string]string{KappIsAppLabelAsAppChangeLabelKey: KappIsAppLabelAsAppChangeLabelValue}) + // use fully qualified name if app had been previously migrated if a.isMigrated || a.isMigrationEnabled() { a.mergeAppAnnotationUpdates(app, map[string]string{KappIsConfigmapMigratedAnnotationKey: KappIsConfigmapMigratedAnnotationValue}) @@ -486,6 +505,7 @@ func (a *RecordedApp) meta() (Meta, error) { if foundMigratedApp { a.isMigrated = true + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] return a.setMeta(*app) } @@ -499,15 +519,17 @@ func (a *RecordedApp) meta() (Meta, error) { a.name, a.nsName, a.appInDiffNsHintMsgFunc(a.name)) } + _, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey] return a.setMeta(*app) } func (a *RecordedApp) Changes() ([]Change, error) { - meta, err := a.meta() + changeLabelValue, err := a.appChangeLabelValue() if err != nil { return nil, err } - return NewRecordedAppChanges(a.nsName, a.name, meta.LabelValue, a.coreClient).List() + + return NewRecordedAppChanges(a.nsName, a.name, changeLabelValue, a.coreClient).List() } func (a *RecordedApp) LastChange() (Change, error) { @@ -531,12 +553,12 @@ func (a *RecordedApp) LastChange() (Change, error) { } func (a *RecordedApp) BeginChange(meta ChangeMeta) (Change, error) { - appMeta, err := a.meta() + changeLabelValue, err := a.appChangeLabelValue() if err != nil { return nil, err } - change, err := NewRecordedAppChanges(a.nsName, a.name, appMeta.LabelValue, a.coreClient).Begin(meta) + change, err := NewRecordedAppChanges(a.nsName, a.name, changeLabelValue, a.coreClient).Begin(meta) if err != nil { return nil, err } @@ -572,6 +594,7 @@ func (a *RecordedApp) update(doFunc func(*Meta)) error { change.Data = meta.AsData() + _, a.isAppLabelAppChangeLabel = change.Annotations[KappIsAppLabelAsAppChangeLabelKey] _, err = a.setMeta(*change) if err != nil { return err @@ -585,6 +608,19 @@ func (a *RecordedApp) update(doFunc func(*Meta)) error { return nil } +func (a *RecordedApp) appChangeLabelValue() (string, error) { + meta, err := a.meta() + if err != nil { + return "", err + } + + if a.isAppLabelAppChangeLabel { + return meta.LabelValue, nil + } + + return a.name, nil +} + type appTrackingChange struct { change *ChangeImpl app *RecordedApp diff --git a/pkg/kapp/app/recorded_app_changes.go b/pkg/kapp/app/recorded_app_changes.go index 3d273cd19..29955e9c5 100644 --- a/pkg/kapp/app/recorded_app_changes.go +++ b/pkg/kapp/app/recorded_app_changes.go @@ -12,7 +12,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/kubernetes" ) @@ -26,13 +25,13 @@ type RecordedAppChanges struct { nsName string appName string - appLabelValue string + changeLabelValue string coreClient kubernetes.Interface } -func NewRecordedAppChanges(nsName, appName, appLabelValue string, coreClient kubernetes.Interface) RecordedAppChanges { - return RecordedAppChanges{nsName, appName, appLabelValue, coreClient} +func NewRecordedAppChanges(nsName, appName, changeLabelValue string, coreClient kubernetes.Interface) RecordedAppChanges { + return RecordedAppChanges{nsName, appName, changeLabelValue, coreClient} } func (a RecordedAppChanges) List() ([]Change, error) { @@ -41,7 +40,7 @@ func (a RecordedAppChanges) List() ([]Change, error) { listOpts := metav1.ListOptions{ LabelSelector: labels.Set(map[string]string{ isChangeLabelKey: isChangeLabelValue, - changeLabelKey: a.changeLabelValue(), + changeLabelKey: a.changeLabelValue, }).String(), } @@ -73,7 +72,7 @@ func (a RecordedAppChanges) DeleteAll() error { listOpts := metav1.ListOptions{ LabelSelector: labels.Set(map[string]string{ isChangeLabelKey: isChangeLabelValue, - changeLabelKey: a.changeLabelValue(), + changeLabelKey: a.changeLabelValue, }).String(), } @@ -105,7 +104,7 @@ func (a RecordedAppChanges) Begin(meta ChangeMeta) (*ChangeImpl, error) { Namespace: a.nsName, Labels: map[string]string{ isChangeLabelKey: isChangeLabelValue, - changeLabelKey: a.changeLabelValue(), + changeLabelKey: a.changeLabelValue, }, }, Data: newMeta.AsData(), @@ -125,10 +124,3 @@ func (a RecordedAppChanges) Begin(meta ChangeMeta) (*ChangeImpl, error) { return change, nil } - -func (a RecordedAppChanges) changeLabelValue() string { - if len(a.appName) > validation.LabelValueMaxLength { - return a.appLabelValue - } - return a.appName -}