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

fix: optionally allow non-standard namespace #984

Merged
merged 2 commits into from
Nov 7, 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ lint: ## Lint the code
all: fmt build test lint generate-refdocs

install-refdocs:
$(GO) get github.com/jenkins-x/gen-crd-api-reference-docs
$(GO) install github.com/jenkins-x/gen-crd-api-reference-docs

generate-refdocs: install-refdocs
Expand Down
4 changes: 2 additions & 2 deletions docs/cmd/jx-gitops_helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Commands for working with helm charts
* [jx-gitops](jx-gitops.md) - commands for working with GitOps based git repositories
* [jx-gitops helm build](jx-gitops_helm_build.md) - Builds and lints any helm charts
* [jx-gitops helm escape](jx-gitops_helm_escape.md) - Escapes any {{ or }} characters in the YAML files so they can be included in a helm chart
* [jx-gitops helm mirror](jx-gitops_helm_mirror.md) - Creates a helm mirror
* [jx-gitops helm mirror](jx-gitops_helm_mirror.md) - Mirror a helm repository
* [jx-gitops helm release](jx-gitops_helm_release.md) - Performs a release of all the charts in the charts folder
* [jx-gitops helm template](jx-gitops_helm_template.md) - Generate the kubernetes resources from a helm chart

###### Auto generated by spf13/cobra on 5-Oct-2022
###### Auto generated by spf13/cobra on 6-Nov-2024
14 changes: 9 additions & 5 deletions docs/cmd/jx-gitops_helm_mirror.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## jx-gitops helm mirror

Creates a helm mirror
Mirror a helm repository

### Usage

Expand All @@ -10,12 +10,15 @@ jx-gitops helm mirror

### Synopsis

Escapes any {{ or }} characters in the YAML files so they can be included in a helm chart
Mirrors a set of remote Helm repositories specified locally in charts/repositories.yml to a remote git repository

### Examples

# escapes any yaml files so they can be included in a helm chart
jx-gitops helm escape --dir myyaml
# Mirror all Helm repositories defined in charts/repositories.yml to a default github pages branch
jx-gitops mirror --url=https://github.com/example/charts.git --no-push=false

# Run the mirror command, ignoring unused repositories
%!s(MISSING) mirror --url=https://github.com/example/charts.git --no-push=false --exclude=bitnami

### Options

Expand All @@ -29,11 +32,12 @@ Escapes any {{ or }} characters in the YAML files so they can be included in a h
--git-username string the git username used to operate on the git repository. If not specified it's loaded from the git credentials file
-h, --help help for mirror
-m, --message string the commit message (default "chore: upgrade mirrored charts")
--no-push disables pushing changes back to the git repository (default true)
-u, --url string the git URL of the repository to mirror the charts into
```

### SEE ALSO

* [jx-gitops helm](jx-gitops_helm.md) - Commands for working with helm charts

###### Auto generated by spf13/cobra on 5-Oct-2022
###### Auto generated by spf13/cobra on 6-Nov-2024
36 changes: 21 additions & 15 deletions docs/cmd/jx-gitops_helmfile_move.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ jx-gitops helmfile move

### Synopsis

Moves the generated template files from 'helmfile template' into the right gitops directory
Moves the generated template files from 'helmfile template' into the right gitops directory.

The output of 'helmfile template' ignores the namespace specified in the 'helmfile.yaml' and there is a dummy top level directory.

So this command applies the namespace to all the generated resources and then moves the namespaced resources into the config-root/namespaces/$ns/$releaseName directory and then moves any CRDs or cluster level resources into 'config-root/cluster/$releaseName'
So by default this command applies the namespace to all the generated resources

Then it moves the namespaced resources into the config-root/namespaces/$ns/$releaseName directory and any CRDs or cluster level resources into 'config-root/cluster/$releaseName'.

If supplied with --dir-includes-release-name then by default we will annotate the resources with the annotations "app.kubernetes.io/instance" to preserve the helm release name.

If supplied with --dir-includes-release-name then by default we will annotate the resources with the annotation 'meta.helm.sh/release-name' to preserve the helm release name
The annotation "meta.helm.sh/release-namespace" will be added by default and contain the namespace specified in the release.

### Examples

Expand All @@ -28,20 +32,22 @@ If supplied with --dir-includes-release-name then by default we will annotate th
### Options

```
--annotate-release-name if using --dir-includes-release-name layout then lets add the 'meta.helm.sh/release-name' annotation to record the helm release name (default true)
--dir string the directory containing the generated resources
--dir-includes-release-name the directory containing the generated resources has a path segment that is the release name
-h, --help help for move
--invert-selector inverts the effect of selector to exclude resources matched by selector
-k, --kind stringArray adds Kubernetes resource kinds to filter on. For kind expressions see: https://github.com/jenkins-x/jx-helpers/tree/master/docs/kind_filters.md
--kind-ignore stringArray adds Kubernetes resource kinds to exclude. For kind expressions see: https://github.com/jenkins-x/jx-helpers/tree/master/docs/kind_filters.md
-o, --output-dir string the output directory (default "config-root")
--selector stringToString adds Kubernetes label selector to filter on, e.g. --selector app=pusher-wave,heritage=Helm (default [])
--selector-target string sets which path in the Kubernetes resources to select on instead of metadata.labels.
--annotate-release-name if using --dir-includes-release-name layout then lets add the 'meta.helm.sh/release-name' annotation to record the helm release name (default true)
--annotate-release-namespace add the 'meta.helm.sh/release-namespace' annotation to record the helm release namespace (default true)
--dir string the directory containing the generated resources
--dir-includes-release-name the directory containing the generated resources has a path segment that is the release name
-h, --help help for move
--invert-selector inverts the effect of selector to exclude resources matched by selector
-k, --kind stringArray adds Kubernetes resource kinds to filter on. For kind expressions see: https://github.com/jenkins-x/jx-helpers/tree/master/docs/kind_filters.md
--kind-ignore stringArray adds Kubernetes resource kinds to exclude. For kind expressions see: https://github.com/jenkins-x/jx-helpers/tree/master/docs/kind_filters.md
-o, --output-dir string the output directory (default "config-root")
--override-namespace applies the namespace specified in helmfile to all the generated resources (default true)
--selector stringToString adds Kubernetes label selector to filter on, e.g. --selector app=pusher-wave,heritage=Helm (default [])
--selector-target string sets which path in the Kubernetes resources to select on instead of metadata.labels.
```

### SEE ALSO

* [jx-gitops helmfile](jx-gitops_helmfile.md) - Commands for working with helmfile

###### Auto generated by spf13/cobra on 7-Oct-2024
###### Auto generated by spf13/cobra on 6-Nov-2024
18 changes: 14 additions & 4 deletions docs/man/man1/jx-gitops-helm-mirror.1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.SH NAME
.PP
jx\-gitops\-helm\-mirror \- Creates a helm mirror
jx\-gitops\-helm\-mirror \- Mirror a helm repository


.SH SYNOPSIS
Expand All @@ -15,7 +15,7 @@ jx\-gitops\-helm\-mirror \- Creates a helm mirror

.SH DESCRIPTION
.PP
Escapes any {{ or }} characters in the YAML files so they can be included in a helm chart
Mirrors a set of remote Helm repositories specified locally in charts/repositories.yml to a remote git repository


.SH OPTIONS
Expand Down Expand Up @@ -55,15 +55,25 @@ Escapes any {{ or }} characters in the YAML files so they can be included in a h
\fB\-m\fP, \fB\-\-message\fP="chore: upgrade mirrored charts"
the commit message

.PP
\fB\-\-no\-push\fP[=true]
disables pushing changes back to the git repository

.PP
\fB\-u\fP, \fB\-\-url\fP=""
the git URL of the repository to mirror the charts into


.SH EXAMPLE
.PP
# escapes any yaml files so they can be included in a helm chart
jx\-gitops helm escape \-\-dir myyaml
# Mirror all Helm repositories defined in charts/repositories.yml to a default github pages branch
jx\-gitops mirror \-\-url=
\[la]https://github.com/example/charts.git\[ra] \-\-no\-push=false

.PP
# Run the mirror command, ignoring unused repositories
%!s(MISSING) mirror \-\-url=
\[la]https://github.com/example/charts.git\[ra] \-\-no\-push=false \-\-exclude=bitnami


.SH SEE ALSO
Expand Down
20 changes: 17 additions & 3 deletions docs/man/man1/jx-gitops-helmfile-move.1
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,33 @@ jx\-gitops\-helmfile\-move \- Moves the generated template files from 'helmfile

.SH DESCRIPTION
.PP
Moves the generated template files from 'helmfile template' into the right gitops directory
Moves the generated template files from 'helmfile template' into the right gitops directory.

.PP
The output of 'helmfile template' ignores the namespace specified in the 'helmfile.yaml' and there is a dummy top level directory.

.PP
So this command applies the namespace to all the generated resources and then moves the namespaced resources into the config\-root/namespaces/$ns/$releaseName directory and then moves any CRDs or cluster level resources into 'config\-root/cluster/$releaseName'
So by default this command applies the namespace to all the generated resources

.PP
If supplied with \-\-dir\-includes\-release\-name then by default we will annotate the resources with the annotation 'meta.helm.sh/release\-name' to preserve the helm release name
Then it moves the namespaced resources into the config\-root/namespaces/$ns/$releaseName directory and any CRDs or cluster level resources into 'config\-root/cluster/$releaseName'.

.PP
If supplied with \-\-dir\-includes\-release\-name then by default we will annotate the resources with the annotations "app.kubernetes.io/instance" to preserve the helm release name.

.PP
The annotation "meta.helm.sh/release\-namespace" will be added by default and contain the namespace specified in the release.


.SH OPTIONS
.PP
\fB\-\-annotate\-release\-name\fP[=true]
if using \-\-dir\-includes\-release\-name layout then lets add the 'meta.helm.sh/release\-name' annotation to record the helm release name

.PP
\fB\-\-annotate\-release\-namespace\fP[=true]
add the 'meta.helm.sh/release\-namespace' annotation to record the helm release namespace

.PP
\fB\-\-dir\fP=""
the directory containing the generated resources
Expand Down Expand Up @@ -62,6 +72,10 @@ If supplied with \-\-dir\-includes\-release\-name then by default we will annota
\fB\-o\fP, \fB\-\-output\-dir\fP="config\-root"
the output directory

.PP
\fB\-\-override\-namespace\fP[=true]
applies the namespace specified in helmfile to all the generated resources

.PP
\fB\-\-selector\fP=[]
adds Kubernetes label selector to filter on, e.g. \-\-selector app=pusher\-wave,heritage=Helm
Expand Down
51 changes: 38 additions & 13 deletions pkg/cmd/helmfile/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ import (

const (
// HelmReleaseNameAnnotation the annotation added by helm to denote a release name
HelmReleaseNameAnnotation = "meta.helm.sh/release-name"
HelmReleaseNameAnnotation = "meta.helm.sh/release-name"
HelmReleaseNameSpaceAnnotation = "meta.helm.sh/release-namespace"

pathSeparator = string(os.PathSeparator)
)

var (
namespaceLong = templates.LongDesc(`
Moves the generated template files from 'helmfile template' into the right gitops directory
Moves the generated template files from 'helmfile template' into the right gitops directory.

The output of 'helmfile template' ignores the namespace specified in the 'helmfile.yaml' and there is a dummy top level directory.
The output of 'helmfile template' ignores the namespace specified in the 'helmfile.yaml' and there is a dummy top level directory.

So this command applies the namespace to all the generated resources and then moves the namespaced resources into the config-root/namespaces/$ns/$releaseName directory
and then moves any CRDs or cluster level resources into 'config-root/cluster/$releaseName'
So by default this command applies the namespace to all the generated resources

If supplied with --dir-includes-release-name then by default we will annotate the resources with the annotation 'meta.helm.sh/release-name' to preserve the helm release name
Then it moves the namespaced resources into the config-root/namespaces/$ns/$releaseName directory
and any CRDs or cluster level resources into 'config-root/cluster/$releaseName'.

If supplied with --dir-includes-release-name then by default we will annotate the resources with the annotations "app.kubernetes.io/instance" to preserve the helm release name.

The annotation "meta.helm.sh/release-namespace" will be added by default and contain the namespace specified in the release.
`)

namespaceExample = templates.Examples(`
Expand All @@ -59,9 +64,10 @@ type Options struct {
ClusterResourcesDir string
CustomResourceDefinitionsDir string
NamespacesDir string
SingleNamespace string
DirIncludesReleaseName bool
OverrideNamespace bool
AnnotateReleaseNames bool
AnnotateReleaseNameSpace bool
HelmState *state.HelmState
NamespacedKind map[string]bool
ResourcesToMove []ResourceToMove
Expand All @@ -86,6 +92,8 @@ func NewCmdHelmfileMove() (*cobra.Command, *Options) {
cmd.Flags().StringVarP(&o.OutputDir, "output-dir", "o", "config-root", "the output directory")
cmd.Flags().BoolVarP(&o.DirIncludesReleaseName, "dir-includes-release-name", "", false, "the directory containing the generated resources has a path segment that is the release name")
cmd.Flags().BoolVarP(&o.AnnotateReleaseNames, "annotate-release-name", "", true, "if using --dir-includes-release-name layout then lets add the 'meta.helm.sh/release-name' annotation to record the helm release name")
cmd.Flags().BoolVarP(&o.AnnotateReleaseNameSpace, "annotate-release-namespace", "", true, "add the 'meta.helm.sh/release-namespace' annotation to record the helm release namespace")
cmd.Flags().BoolVarP(&o.OverrideNamespace, "override-namespace", "", true, "applies the namespace specified in helmfile to all the generated resources")

o.Filter.AddFlags(cmd)
return cmd, o
Expand Down Expand Up @@ -195,10 +203,21 @@ noKube:
outDir := filepath.Join(o.ClusterResourcesDir, res.namespace, res.pathname)

if isNamespaced {
err := res.node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "metadata", "namespace"), yaml.FieldSetter{StringValue: res.namespace})
if err != nil {
return errors.Wrapf(err, "failed to set metadata.namespace to %s for path %s", res.namespace, res.path)
setNS := true
namespaceLookup := yaml.LookupCreate(yaml.ScalarNode, "metadata", "namespace")
if !o.OverrideNamespace {
nsNode, _ := res.node.Pipe(namespaceLookup)
nsNodeText, _ := nsNode.String()
setNS = nsNodeText == ""
}
if setNS {
err = res.node.PipeE(namespaceLookup, yaml.FieldSetter{StringValue: res.namespace})

if err != nil {
return errors.Wrapf(err, "failed to set metadata.namespace to %s for path %s", res.namespace, res.path)
}
}

outDir = filepath.Join(o.NamespacesDir, res.namespace, res.pathname)
} else {
err := res.node.PipeE(yaml.Lookup("metadata"), yaml.FieldClearer{Name: "namespace"})
Expand Down Expand Up @@ -320,16 +339,22 @@ func (o *Options) moveFilesToClusterOrNamespacesFolder(dir, ns, releaseName, cha
pathName = fmt.Sprintf("%s-%s", chartName, releaseName)
}

setAnnotations := make(map[string]string)
if o.AnnotateReleaseNames {
k := HelmReleaseNameAnnotation
setAnnotations[HelmReleaseNameAnnotation] = releaseName
}
if o.AnnotateReleaseNameSpace {
setAnnotations[HelmReleaseNameSpaceAnnotation] = ns
}
for k, newValue := range setAnnotations {
v, err := node.Pipe(yaml.GetAnnotation(k))
if err != nil {
return errors.Wrapf(err, "failed to get annotation %s for path %s", k, path)
}
if v == nil {
err = node.PipeE(yaml.SetAnnotation(k, releaseName))
err = node.PipeE(yaml.SetAnnotation(k, newValue))
if err != nil {
return errors.Wrapf(err, "failed to set annotation %s to %s for path %s", k, releaseName, path)
return errors.Wrapf(err, "failed to set annotation %s to %s for path %s", k, newValue, path)
}
}
}
Expand Down
Loading
Loading