feat: refactor label creation in state.go (#1758)

* feat: refactor label creation in state.go

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix more

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix test

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix more issue

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2024-11-11 07:28:01 +08:00 committed by GitHub
parent eb6425c20d
commit 1464bd2bfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 18 deletions

View File

@ -186,8 +186,8 @@ global kube-system true true incubator/raw
environment: "development", environment: "development",
selectors: []string{"app=test"}, selectors: []string{"app=test"},
expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION expected: `NAME NAMESPACE ENABLED INSTALLED LABELS CHART VERSION
external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw external-secrets default true true app:test incubator/raw
my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw my-release default true true app:test incubator/raw
`, `,
}, cfg) }, cfg)
}) })

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -2268,10 +2269,12 @@ func markExcludedReleases(releases []ReleaseSpec, selectors []string, commonLabe
filters = append(filters, f) filters = append(filters, f)
} }
for _, r := range releases { for _, r := range releases {
// Do not add any label without any filter, see #276 orginReleaseLabel := maps.Clone(r.Labels)
if len(filters) > 0 {
if r.Labels == nil { if r.Labels == nil {
r.Labels = map[string]string{} r.Labels = map[string]string{}
} else {
// Make a copy of the labels to avoid mutating the original
r.Labels = maps.Clone(r.Labels)
} }
// Let the release name, namespace, and chart be used as a tag // Let the release name, namespace, and chart be used as a tag
r.Labels["name"] = r.Name r.Labels["name"] = r.Name
@ -2283,7 +2286,7 @@ func markExcludedReleases(releases []ReleaseSpec, selectors []string, commonLabe
for k, v := range commonLabels { for k, v := range commonLabels {
r.Labels[k] = v r.Labels[k] = v
} }
}
var filterMatch bool var filterMatch bool
for _, f := range filters { for _, f := range filters {
if r.Labels == nil { if r.Labels == nil {
@ -2299,6 +2302,8 @@ func markExcludedReleases(releases []ReleaseSpec, selectors []string, commonLabe
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse condition in release %s: %w", r.Name, err) return nil, fmt.Errorf("failed to parse condition in release %s: %w", r.Name, err)
} }
// reset the labels to the original
r.Labels = orginReleaseLabel
res := Release{ res := Release{
ReleaseSpec: r, ReleaseSpec: r,
Filtered: (len(filters) > 0 && !filterMatch) || (!conditionMatch), Filtered: (len(filters) > 0 && !filterMatch) || (!conditionMatch),