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

@ -185,9 +185,9 @@ global kube-system true true incubator/raw
check(t, testcase{ check(t, testcase{
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,22 +2269,24 @@ 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
// Let the release name, namespace, and chart be used as a tag r.Labels = maps.Clone(r.Labels)
r.Labels["name"] = r.Name
r.Labels["namespace"] = r.Namespace
// Strip off just the last portion for the name stable/newrelic would give newrelic
chartSplit := strings.Split(r.Chart, "/")
r.Labels["chart"] = chartSplit[len(chartSplit)-1]
// Merge CommonLabels into release labels
for k, v := range commonLabels {
r.Labels[k] = v
}
} }
// Let the release name, namespace, and chart be used as a tag
r.Labels["name"] = r.Name
r.Labels["namespace"] = r.Namespace
// Strip off just the last portion for the name stable/newrelic would give newrelic
chartSplit := strings.Split(r.Chart, "/")
r.Labels["chart"] = chartSplit[len(chartSplit)-1]
// Merge CommonLabels into release labels
for k, v := range commonLabels {
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),