Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Namespace to the metadata of generated resources in the Helm Chart. #144

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions cmd/helmify/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
suvaanshkumar marked this conversation as resolved.
Show resolved Hide resolved
flag.Parse()
if h || help {
fmt.Print(helpText)
Expand All @@ -87,6 +87,9 @@ func ReadFlags() config.Config {
if crd {
result.Crd = crd
}
if preservens {
result.PreserveNs = true
}
result.Files = files
return result
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions pkg/processor/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const metaTemplate = `apiVersion: %[1]s
kind: %[2]s
metadata:
name: %[3]s
%[7]s
arttor marked this conversation as resolved.
Show resolved Hide resolved
labels:
%[5]s
{{- include "%[4]s.labels" . | nindent 4 }}
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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
Expand Down
Loading