feat: add labels for helm release (#1046)

feat: add labels for k8s resources

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2025-03-30 19:24:41 -04:00
committed by GitHub
parent 7f44408541
commit e4273d050e
18 changed files with 251 additions and 80 deletions
+7 -1
View File
@@ -1106,13 +1106,17 @@ func (a *App) visitStatesWithSelectorsAndRemoteSupport(fileOrDir string, converg
}
}
// pre-overrides HelmState
// pre-handles HelmState
fHelmStatsWithOverrides := func(st *state.HelmState) (bool, []error) {
var err error
// override release settings
st.Releases, err = st.GetReleasesWithOverrides()
if err != nil {
return false, []error{err}
}
// override release labels
st.Releases = st.GetReleasesWithLabels()
return f(st)
}
@@ -1502,6 +1506,7 @@ Do you really want to apply?
SyncArgs: c.SyncArgs(),
HideNotes: c.HideNotes(),
TakeOwnership: c.TakeOwnership(),
SyncReleaseLabels: c.SyncReleaseLabels(),
}
return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), syncOpts)
}))
@@ -1900,6 +1905,7 @@ Do you really want to sync?
HideNotes: c.HideNotes(),
TakeOwnership: c.TakeOwnership(),
SkipSchemaValidation: c.SkipSchemaValidation(),
SyncReleaseLabels: c.SyncReleaseLabels(),
}
return subst.SyncReleases(&affectedReleases, helm, c.Values(), c.Concurrency(), opts)
}))
+28 -28
View File
@@ -159,17 +159,17 @@ releases:
t.Run("default environment includes all releases", func(t *testing.T) {
check(t, testcase{
environment: "default",
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
logging kube-system true true incubator/raw
kubernetes-external-secrets kube-system true true incubator/raw
external-secrets default true true app:test incubator/raw
my-release default true true app:test incubator/raw
disabled kube-system true false incubator/raw
test2 true true incubator/raw
test3 true true incubator/raw
cache my-app true true app:test bitnami/redis 17.0.7
database my-app true true bitnami/postgres 11.6.22
global kube-system true true incubator/raw
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
logging kube-system true true chart:raw,name:logging,namespace:kube-system incubator/raw
kubernetes-external-secrets kube-system true true chart:raw,name:kubernetes-external-secrets,namespace:kube-system incubator/raw
external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw
my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw
disabled kube-system true false chart:raw,name:disabled,namespace:kube-system incubator/raw
test2 true true chart:raw,name:test2,namespace: incubator/raw
test3 true true chart:raw,name:test3,namespace: incubator/raw
cache my-app true true app:test,chart:redis,name:cache,namespace:my-app bitnami/redis 17.0.7
database my-app true true chart:postgres,name:database,namespace:my-app bitnami/postgres 11.6.22
global kube-system true true chart:raw,name:global,namespace:kube-system incubator/raw
`,
}, cfg)
})
@@ -185,9 +185,9 @@ global kube-system true true incubator/raw
check(t, testcase{
environment: "development",
selectors: []string{"app=test"},
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
external-secrets default true true app:test incubator/raw
my-release default true true app:test incubator/raw
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw
my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw
`,
}, cfg)
})
@@ -195,9 +195,9 @@ my-release default true true app:test incubator/raw
t.Run("filters releases for environment used in one file only", func(t *testing.T) {
check(t, testcase{
environment: "test",
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
cache my-app true true app:test bitnami/redis 17.0.7
database my-app true true bitnami/postgres 11.6.22
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
cache my-app true true app:test,chart:redis,name:cache,namespace:my-app bitnami/redis 17.0.7
database my-app true true chart:postgres,name:database,namespace:my-app bitnami/postgres 11.6.22
`,
}, cfg)
})
@@ -206,16 +206,16 @@ database my-app true true bitnami/postgres 11.6.22
check(t, testcase{
environment: "shared",
// 'global' release has no environments, so is still excluded
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
logging kube-system true true incubator/raw
kubernetes-external-secrets kube-system true true incubator/raw
external-secrets default true true app:test incubator/raw
my-release default true true app:test incubator/raw
disabled kube-system true false incubator/raw
test2 true true incubator/raw
test3 true true incubator/raw
cache my-app true true app:test bitnami/redis 17.0.7
database my-app true true bitnami/postgres 11.6.22
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
logging kube-system true true chart:raw,name:logging,namespace:kube-system incubator/raw
kubernetes-external-secrets kube-system true true chart:raw,name:kubernetes-external-secrets,namespace:kube-system incubator/raw
external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw
my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw
disabled kube-system true false chart:raw,name:disabled,namespace:kube-system incubator/raw
test2 true true chart:raw,name:test2,namespace: incubator/raw
test3 true true chart:raw,name:test3,namespace: incubator/raw
cache my-app true true app:test,chart:redis,name:cache,namespace:my-app bitnami/redis 17.0.7
database my-app true true chart:postgres,name:database,namespace:my-app bitnami/postgres 11.6.22
`,
}, cfg)
})
@@ -285,7 +285,7 @@ releases:
})
assert.NoError(t, err)
expected := `[{"name":"myrelease1","namespace":"testNamespace","enabled":true,"installed":false,"labels":"id:myrelease1","chart":"mychart1","version":""},{"name":"myrelease2","namespace":"testNamespace","enabled":false,"installed":true,"labels":"","chart":"mychart1","version":""},{"name":"myrelease3","namespace":"testNamespace","enabled":true,"installed":true,"labels":"","chart":"mychart1","version":""},{"name":"myrelease4","namespace":"testNamespace","enabled":true,"installed":true,"labels":"id:myrelease1","chart":"mychart1","version":""}]
expected := `[{"name":"myrelease1","namespace":"testNamespace","enabled":true,"installed":false,"labels":"chart:mychart1,id:myrelease1,name:myrelease1,namespace:testNamespace","chart":"mychart1","version":""},{"name":"myrelease2","namespace":"testNamespace","enabled":false,"installed":true,"labels":"chart:mychart1,name:myrelease2,namespace:testNamespace","chart":"mychart1","version":""},{"name":"myrelease3","namespace":"testNamespace","enabled":true,"installed":true,"labels":"chart:mychart1,name:myrelease3,namespace:testNamespace","chart":"mychart1","version":""},{"name":"myrelease4","namespace":"testNamespace","enabled":true,"installed":true,"labels":"chart:mychart1,id:myrelease1,name:myrelease4,namespace:testNamespace","chart":"mychart1","version":""}]
`
assert.Equal(t, expected, out)
}
+10 -5
View File
@@ -2259,6 +2259,7 @@ type applyConfig struct {
showOnly []string
hideNotes bool
takeOwnership bool
syncReleaseLabels bool
// template-only options
includeCRDs, skipTests bool
@@ -2453,6 +2454,10 @@ func (a applyConfig) TakeOwnership() bool {
return a.takeOwnership
}
func (a applyConfig) SyncReleaseLabels() bool {
return a.syncReleaseLabels
}
type depsConfig struct {
skipRepos bool
includeTransitiveNeeds bool
@@ -3977,11 +3982,11 @@ releases:
})
assert.NoError(t, err)
expected := `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
myrelease1 testNamespace true false common:label,id:myrelease1 mychart1
myrelease2 testNamespace false true common:label mychart1
myrelease3 testNamespace true true mychart1
myrelease4 testNamespace true true id:myrelease1 mychart1
expected := `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
myrelease1 testNamespace true false chart:mychart1,common:label,id:myrelease1,name:myrelease1,namespace:testNamespace mychart1
myrelease2 testNamespace false true chart:mychart1,common:label,name:myrelease2,namespace:testNamespace mychart1
myrelease3 testNamespace true true chart:mychart1,name:myrelease3,namespace:testNamespace mychart1
myrelease4 testNamespace true true chart:mychart1,id:myrelease1,name:myrelease4,namespace:testNamespace mychart1
`
assert.Equal(t, expected, out)
+5
View File
@@ -80,6 +80,8 @@ type ApplyConfigProvider interface {
DiffArgs() string
SyncArgs() string
SyncReleaseLabels() bool
DAGConfig
concurrencyConfig
@@ -112,6 +114,9 @@ type SyncConfigProvider interface {
SkipNeeds() bool
IncludeNeeds() bool
IncludeTransitiveNeeds() bool
SyncReleaseLabels() bool
DAGConfig
concurrencyConfig