Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Support different targetNamespace for HelmRelease #2128 #2334

Merged
merged 1 commit into from
Aug 12, 2019
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
3 changes: 3 additions & 0 deletions chart/flux/templates/helm-operator-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ spec:
releaseName:
type: string
pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
targetNamespace:
type: string
pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
timeout:
type: integer
format: int64
Expand Down
3 changes: 3 additions & 0 deletions deploy-helm/flux-helm-release-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ spec:
releaseName:
type: string
pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
targetNamespace:
type: string
pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
timeout:
type: integer
format: int64
Expand Down
42 changes: 34 additions & 8 deletions integrations/apis/flux.weave.works/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/ghodss/yaml"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/helm/pkg/chartutil"

Expand Down Expand Up @@ -33,17 +33,40 @@ func (fhr HelmRelease) ResourceID() resource.ID {

// ReleaseName returns the configured release name, or constructs and
// returns one based on the namespace and name of the HelmRelease.
// When the HelmRelease's metadata.namespace and spec.targetNamespace
// differ, both are used in the generated name.
// This name is used for naming and operating on the release in Helm.
func (fhr HelmRelease) ReleaseName() string {
namespace := fhr.Namespace
if namespace == "" {
namespace = "default"
if fhr.Spec.ReleaseName == "" {
namespace := fhr.GetDefaultedNamespace()
targetNamespace := fhr.GetTargetNamespace()

if namespace != targetNamespace {
// prefix the releaseName with the administering HelmRelease namespace as well
return fmt.Sprintf("%s-%s-%s", namespace, targetNamespace, fhr.Name)
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
}
return fmt.Sprintf("%s-%s", targetNamespace, fhr.Name)
}
releaseName := fhr.Spec.ReleaseName
if releaseName == "" {
releaseName = fmt.Sprintf("%s-%s", namespace, fhr.Name)

return fhr.Spec.ReleaseName
}

// GetDefaultedNamespace returns the HelmRelease's namespace
// defaulting to the "default" if not set.
func (fhr HelmRelease) GetDefaultedNamespace() string {
if fhr.GetNamespace() == "" {
return "default"
}
return fhr.Namespace
}

return releaseName
// GetTargetNamespace returns the configured release targetNamespace
// defaulting to the namespace of the HelmRelease if not set.
func (fhr HelmRelease) GetTargetNamespace() string {
if fhr.Spec.TargetNamespace == "" {
return fhr.GetDefaultedNamespace()
}
return fhr.Spec.TargetNamespace
}

// ValuesFromSource represents a source of values.
Expand Down Expand Up @@ -143,6 +166,9 @@ type HelmReleaseSpec struct {
ValueFileSecrets []v1.LocalObjectReference `json:"valueFileSecrets,omitempty"`
ValuesFrom []ValuesFromSource `json:"valuesFrom,omitempty"`
HelmValues `json:",inline"`
// Override the target namespace, defaults to metadata.namespace
// +optional
TargetNamespace string `json:"targetNamespace,omitempty"`
// Install or upgrade timeout in seconds
// +optional
Timeout *int64 `json:"timeout,omitempty"`
Expand Down
10 changes: 2 additions & 8 deletions integrations/helm/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

fluxk8s "github.com/weaveworks/flux/cluster/kubernetes"
flux_v1beta1 "github.com/weaveworks/flux/integrations/apis/flux.weave.works/v1beta1"
"github.com/weaveworks/flux/resource"
)

type Action string
Expand Down Expand Up @@ -203,7 +202,7 @@ func (r *Release) Install(chartPath, releaseName string, fhr flux_v1beta1.HelmRe
case InstallAction:
res, err := r.HelmClient.InstallRelease(
chartPath,
fhr.GetNamespace(),
fhr.GetTargetNamespace(),
k8shelm.ValueOverrides(rawVals),
k8shelm.ReleaseName(releaseName),
k8shelm.InstallDryRun(opts.DryRun),
Expand Down Expand Up @@ -354,7 +353,7 @@ func (r *Release) annotateResources(release *hapi_release.Release, fhr flux_v1be
args := []string{"annotate", "--overwrite"}
args = append(args, "--namespace", namespace)
args = append(args, res...)
args = append(args, fluxk8s.AntecedentAnnotation+"="+fhrResourceID(fhr).String())
args = append(args, fluxk8s.AntecedentAnnotation+"="+fhr.ResourceID().String())

// The timeout is set to a high value as it may take some time
// to annotate large umbrella charts.
Expand Down Expand Up @@ -483,11 +482,6 @@ func ValuesChecksum(rawValues []byte) string {
return hex.EncodeToString(hasher.Sum(nil))
}

// fhrResourceID constructs a resource.ID for a HelmRelease resource.
func fhrResourceID(fhr flux_v1beta1.HelmRelease) resource.ID {
return resource.MakeID(fhr.Namespace, "HelmRelease", fhr.Name)
}

// Merges source and destination map, preferring values from the source Values
// This is slightly adapted from https://github.com/helm/helm/blob/2332b480c9cb70a0d8a85247992d6155fbe82416/cmd/helm/install.go#L359
func mergeValues(dest, src map[string]interface{}) map[string]interface{} {
Expand Down