From 6c76c4ec0338fc4ed5bcd508df8f42ceb39206cd Mon Sep 17 00:00:00 2001 From: Suvaansh <34331549+suvaanshkumar@users.noreply.github.com> Date: Thu, 25 Apr 2024 22:51:34 -0400 Subject: [PATCH 1/4] Add namespace to metadata --- pkg/processor/meta.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/processor/meta.go b/pkg/processor/meta.go index 5430234..e7b984a 100644 --- a/pkg/processor/meta.go +++ b/pkg/processor/meta.go @@ -16,6 +16,7 @@ const metaTemplate = `apiVersion: %[1]s kind: %[2]s metadata: name: %[3]s + namespace: %[7]s # Include the namespace here labels: %[5]s {{- include "%[4]s.labels" . | nindent 4 }} @@ -80,6 +81,7 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, return "", err } } + ns := obj.GetNamespace() templatedName := appMeta.TemplatedName(obj.GetName()) apiVersion, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind() @@ -100,7 +102,7 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, annotations = fmt.Sprintf(annotationsTemplate, name, kind) } - metaStr = fmt.Sprintf(metaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations) + metaStr = fmt.Sprintf(metaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations, ns) metaStr = strings.Trim(metaStr, " \n") metaStr = strings.ReplaceAll(metaStr, "\n\n", "\n") return metaStr, nil From 0349b79058dcdd4522146de8a5e466b23f0ebb27 Mon Sep 17 00:00:00 2001 From: Suvaansh <34331549+suvaanshkumar@users.noreply.github.com> Date: Thu, 25 Apr 2024 23:28:13 -0400 Subject: [PATCH 2/4] Adjusting namespace creation to ignore if there is no namespace --- pkg/processor/meta.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/processor/meta.go b/pkg/processor/meta.go index e7b984a..39c023d 100644 --- a/pkg/processor/meta.go +++ b/pkg/processor/meta.go @@ -16,7 +16,7 @@ const metaTemplate = `apiVersion: %[1]s kind: %[2]s metadata: name: %[3]s - namespace: %[7]s # Include the namespace here + %[7]s labels: %[5]s {{- include "%[4]s.labels" . | nindent 4 }} @@ -57,7 +57,7 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, } var err error - var labels, annotations string + var labels, annotations, namespace string if len(obj.GetLabels()) != 0 { l := obj.GetLabels() // provided by Helm @@ -81,7 +81,13 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, return "", err } } - ns := obj.GetNamespace() + + if obj.GetNamespace() != "" { + namespace, err = yamlformat.Marshal(map[string]interface{}{"namespace": obj.GetNamespace()}, 0) + if err != nil { + return "", err + } + } templatedName := appMeta.TemplatedName(obj.GetName()) apiVersion, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind() @@ -102,7 +108,7 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, annotations = fmt.Sprintf(annotationsTemplate, name, kind) } - metaStr = fmt.Sprintf(metaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations, ns) + metaStr = fmt.Sprintf(metaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations, namespace) metaStr = strings.Trim(metaStr, " \n") metaStr = strings.ReplaceAll(metaStr, "\n\n", "\n") return metaStr, nil From db5b0eecf2a840e87ed332f354d6fb22fd0fff9c Mon Sep 17 00:00:00 2001 From: Suvaansh <34331549+suvaanshkumar@users.noreply.github.com> Date: Fri, 10 May 2024 04:01:57 +0000 Subject: [PATCH 3/4] fix blank lines for namespace, add new flag --preserve-ns --- cmd/helmify/flags.go | 7 +++++-- pkg/config/config.go | 2 ++ pkg/processor/meta.go | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/helmify/flags.go b/cmd/helmify/flags.go index 79b4c68..7fca65d 100644 --- a/cmd/helmify/flags.go +++ b/cmd/helmify/flags.go @@ -54,7 +54,7 @@ func (i *arrayFlags) Set(value string) error { func ReadFlags() config.Config { files := arrayFlags{} result := config.Config{} - var h, help, version, crd bool + var h, help, version, crd , preservens bool flag.BoolVar(&h, "h", false, "Print help. Example: helmify -h") flag.BoolVar(&help, "help", false, "Print help. Example: helmify -help") flag.BoolVar(&version, "version", false, "Print helmify version. Example: helmify -version") @@ -68,7 +68,7 @@ func ReadFlags() config.Config { flag.BoolVar(&result.FilesRecursively, "r", false, "Scan dirs from -f option recursively") flag.BoolVar(&result.OriginalName, "original-name", false, "Use the object's original name instead of adding the chart's release name as the common prefix.") flag.Var(&files, "f", "File or directory containing k8s manifests") - + flag.BoolVar(&preservens, "preserve-ns", false, "Use the object's original namespace instead of adding all the resources to a common namespace") flag.Parse() if h || help { fmt.Print(helpText) @@ -87,6 +87,9 @@ func ReadFlags() config.Config { if crd { result.Crd = crd } + if preservens { + result.PreserveNs = true + } result.Files = files return result } diff --git a/pkg/config/config.go b/pkg/config/config.go index cd6c3fc..506ff9d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -36,6 +36,8 @@ type Config struct { FilesRecursively bool // OriginalName retains Kubernetes resource's original name OriginalName bool + // PreserveNs retains the namespaces on the Kubernetes manifests + PreserveNs bool } func (c *Config) Validate() error { diff --git a/pkg/processor/meta.go b/pkg/processor/meta.go index 39c023d..660c7ca 100644 --- a/pkg/processor/meta.go +++ b/pkg/processor/meta.go @@ -16,7 +16,7 @@ const metaTemplate = `apiVersion: %[1]s kind: %[2]s metadata: name: %[3]s - %[7]s +%[7]s labels: %[5]s {{- include "%[4]s.labels" . | nindent 4 }} @@ -82,8 +82,8 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, } } - if obj.GetNamespace() != "" { - namespace, err = yamlformat.Marshal(map[string]interface{}{"namespace": obj.GetNamespace()}, 0) + if (obj.GetNamespace() != "") && (appMeta.Config().PreserveNs){ + namespace, err = yamlformat.Marshal(map[string]interface{}{"namespace": obj.GetNamespace()}, 2) if err != nil { return "", err } From d574327eeca3f267ed013c574d43ec09854099cb Mon Sep 17 00:00:00 2001 From: Suvaansh <34331549+suvaanshkumar@users.noreply.github.com> Date: Tue, 21 May 2024 20:31:48 -0400 Subject: [PATCH 4/4] Add preserve-ns in README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7a46bc6..4cd9fc7 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ Usage: | -original-name | Use the object's original name instead of adding the chart's release name as the common prefix. | `helmify -original-name` | | -cert-manager-as-subchart | Allows the user to install cert-manager as a subchart | `helmify -cert-manager-as-subchart` | | -cert-manager-version | Allows the user to specify cert-manager subchart version. Only useful with cert-manager-as-subchart. (default "v1.12.2") | `helmify -cert-manager-as-subchart` | +| -preserve-ns | Allows users to use the object's original namespace instead of adding all the resources to a common namespace. (default "false") | `helmify -preserve-ns` | ## Status Supported k8s resources: - Deployment, DaemonSet, StatefulSet