From 168f47ed30518bcbff5dfeccb06f75561f15fb67 Mon Sep 17 00:00:00 2001 From: Robert Aistleitner Date: Tue, 27 May 2025 10:48:16 +0000 Subject: [PATCH] Fix common prefix calculation ...by making commonPrefix variable nullable to detect the case that no commonPrefix is set (and not start finding a common prefix once no common prefix is found, which means the commonPrefix is an empty string) --- pkg/metadata/metadata.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go index 6039d951..038736e4 100644 --- a/pkg/metadata/metadata.go +++ b/pkg/metadata/metadata.go @@ -30,7 +30,7 @@ func New(conf config.Config) *Service { } type Service struct { - commonPrefix string + commonPrefix *string namespace string names map[string]struct{} conf config.Config @@ -44,7 +44,7 @@ func (a *Service) Config() config.Config { // If no common prefix - returns name as it is. // It is better to trim common prefix because Helm also adds release name as common prefix. func (a *Service) TrimName(objName string) string { - trimmed := strings.TrimPrefix(objName, a.commonPrefix) + trimmed := strings.TrimPrefix(objName, *a.commonPrefix) trimmed = strings.TrimLeft(trimmed, "-./_ ") if trimmed == "" { return objName @@ -58,7 +58,8 @@ var _ helmify.AppMetadata = &Service{} // other app meta information. func (a *Service) Load(obj *unstructured.Unstructured) { a.names[obj.GetName()] = struct{}{} - a.commonPrefix = detectCommonPrefix(obj, a.commonPrefix) + newCommonPrefix := detectCommonPrefix(obj, a.commonPrefix) + a.commonPrefix = &newCommonPrefix objNs := extractAppNamespace(obj) if objNs == "" { return @@ -106,14 +107,14 @@ func extractAppNamespace(obj *unstructured.Unstructured) string { return obj.GetNamespace() } -func detectCommonPrefix(obj *unstructured.Unstructured, prevName string) string { +func detectCommonPrefix(obj *unstructured.Unstructured, prevName *string) string { if obj.GroupVersionKind() == crdGVK || obj.GroupVersionKind() == nsGVK { - return prevName + return *prevName } - if prevName == "" { + if prevName == nil { return obj.GetName() } - return commonPrefix(obj.GetName(), prevName) + return commonPrefix(obj.GetName(), *prevName) } func commonPrefix(one, two string) string {