Skip to content

Commit

Permalink
Use app label as app-change label for new apps
Browse files Browse the repository at this point in the history
Use app label as app-change label while renaming existing apps
Signed-off-by: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com>
  • Loading branch information
praveenrewar committed Feb 21, 2023
1 parent 348bc02 commit dd01037
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
58 changes: 47 additions & 11 deletions pkg/kapp/app/recorded_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")}
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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())
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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())
}
Expand All @@ -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)
}

Expand All @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -486,6 +505,7 @@ func (a *RecordedApp) meta() (Meta, error) {

if foundMigratedApp {
a.isMigrated = true
_, a.isAppLabelAppChangeLabel = app.Annotations[KappIsAppLabelAsAppChangeLabelKey]
return a.setMeta(*app)
}

Expand All @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 6 additions & 14 deletions pkg/kapp/app/recorded_app_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand All @@ -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(),
}

Expand Down Expand Up @@ -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(),
}

Expand Down Expand Up @@ -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(),
Expand All @@ -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
}

0 comments on commit dd01037

Please sign in to comment.