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 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 5430234..660c7ca 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 +%[7]s labels: %[5]s {{- include "%[4]s.labels" . | nindent 4 }} @@ -56,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,6 +82,13 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, } } + if (obj.GetNamespace() != "") && (appMeta.Config().PreserveNs){ + namespace, err = yamlformat.Marshal(map[string]interface{}{"namespace": obj.GetNamespace()}, 2) + if err != nil { + return "", err + } + } + templatedName := appMeta.TemplatedName(obj.GetName()) apiVersion, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind() @@ -100,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) + 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