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

Commit

Permalink
Adding chartFileRef option to valuesFrom, relative to chartPath pulle…
Browse files Browse the repository at this point in the history
…d from git.
  • Loading branch information
Arturo Contreras committed Apr 9, 2019
1 parent c32e28b commit 3f9a134
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
9 changes: 9 additions & 0 deletions chart/flux/templates/helm-operator-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ spec:
type: string
optional:
type: boolean
chartFileRef:
type: object
required: ['path']
properties:
path:
type: string
optional:
type: boolean
oneOf:
- required: ['configMapKeyRef']
- required: ['secretKeyRef']
- required: ['externalSourceRef']
- required: ['chartFileRef']
values:
type: object
chart:
Expand Down
9 changes: 9 additions & 0 deletions deploy-helm/flux-helm-release-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,19 @@ spec:
type: string
optional:
type: boolean
chartFileRef:
type: object
required: ['path']
properties:
path:
type: string
optional:
type: boolean
oneOf:
- required: ['configMapKeyRef']
- required: ['secretKeyRef']
- required: ['externalSourceRef']
- required: ['chartFileRef']
values:
type: object
chart:
Expand Down
10 changes: 10 additions & 0 deletions integrations/apis/flux.weave.works/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ type ValuesFromSource struct {
// Selects an URL.
// +optional
ExternalSourceRef *ExternalSourceSelector `json:"externalSourceRef,omitempty"`
// Selects a file from git source helm chart.
// +optional
ChartFileRef *ChartFileSelector `json:"chartFileRef,omitempty"`
}

type ChartFileSelector struct {
Path string `json:"path"`
// Do not fail if chart file could not be retrieved
// +optional
Optional *bool `json:"optional,omitempty"`
}

type ExternalSourceSelector struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ import (
runtime "k8s.io/apimachinery/pkg/runtime"
)

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ChartFileSelector) DeepCopyInto(out *ChartFileSelector) {
*out = *in
if in.Optional != nil {
in, out := &in.Optional, &out.Optional
if *in == nil {
*out = nil
} else {
*out = new(bool)
**out = **in
}
}
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ChartFileSelector.
func (in *ChartFileSelector) DeepCopy() *ChartFileSelector {
if in == nil {
return nil
}
out := new(ChartFileSelector)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ChartSource) DeepCopyInto(out *ChartSource) {
*out = *in
Expand Down Expand Up @@ -297,6 +322,15 @@ func (in *ValuesFromSource) DeepCopyInto(out *ValuesFromSource) {
(*in).DeepCopyInto(*out)
}
}
if in.ChartFileRef != nil {
in, out := &in.ChartFileRef, &out.ChartFileRef
if *in == nil {
*out = nil
} else {
*out = new(ChartFileSelector)
(*in).DeepCopyInto(*out)
}
}
return
}

Expand Down
33 changes: 31 additions & 2 deletions integrations/helm/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package release
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -159,7 +161,7 @@ func (r *Release) Install(chartPath, releaseName string, fhr flux_v1beta1.HelmRe
}
valuesFrom = append(secretKeyRefs, valuesFrom...)
}
vals, err := values(kubeClient.CoreV1(), fhr.Namespace, valuesFrom, fhr.Spec.Values)
vals, err := values(kubeClient.CoreV1(), fhr.Namespace, chartPath, valuesFrom, fhr.Spec.Values)
if err != nil {
r.logger.Log("error", fmt.Sprintf("Failed to compose values for Chart release [%s]: %v", fhr.Spec.ReleaseName, err))
return nil, err
Expand Down Expand Up @@ -275,7 +277,7 @@ func fhrResourceID(fhr flux_v1beta1.HelmRelease) flux.ResourceID {

// values tries to resolve all given value file sources and merges
// them into one Values struct. It returns the merged Values.
func values(corev1 k8sclientv1.CoreV1Interface, ns string, valuesFromSource []flux_v1beta1.ValuesFromSource, values chartutil.Values) (chartutil.Values, error) {
func values(corev1 k8sclientv1.CoreV1Interface, ns string, chartPath string, valuesFromSource []flux_v1beta1.ValuesFromSource, values chartutil.Values) (chartutil.Values, error) {
result := chartutil.Values{}

for _, v := range valuesFromSource {
Expand Down Expand Up @@ -352,6 +354,23 @@ func values(corev1 k8sclientv1.CoreV1Interface, ns string, valuesFromSource []fl
}
return result, fmt.Errorf("unable to yaml.Unmarshal %v from URL %s", b, url)
}
case v.ChartFileRef != nil:
cf := v.ChartFileRef
filePath := cf.Path
optional := cf.Optional != nil && *cf.Optional
f, err := readLocalChartFile(filepath.Join("%s/%s", chartPath, filePath))
if err != nil {
if optional {
continue
}
return result, fmt.Errorf("unable to read value file from path %s", filePath)
}
if err := yaml.Unmarshal(f, &valueFile); err != nil {
if optional {
continue
}
return result, fmt.Errorf("unable to yaml.Unmarshal %v from URL %s", f, filePath)
}
}

result = mergeValues(result, valueFile)
Expand Down Expand Up @@ -415,6 +434,16 @@ func readURL(URL string) ([]byte, error) {
return data.Bytes(), err
}

// readLocalChartFile attempts to read a file from the chart path.
func readLocalChartFile(filePath string) ([]byte, error) {
f, err := ioutil.ReadFile(filePath)
if err != nil {
return []byte{}, err
}

return f, nil
}

// releaseManifestToUnstructured turns a string containing YAML
// manifests into an array of Unstructured objects.
func releaseManifestToUnstructured(manifest string, logger log.Logger) []unstructured.Unstructured {
Expand Down
15 changes: 15 additions & 0 deletions site/helm-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ menu_order: 90
* [Config maps](#config-maps)
* [Secrets](#secrets)
* [External sources](#external-sources)
* [Chart files](#chart-files)
* [Upgrading images in a `HelmRelease` using Flux](#upgrading-images-in-a-helmrelease-using-flux)
+ [Using annotations to control updates to HelmRelease resources](#using-annotations-to-control-updates-to-helmrelease-resources)
* [Authentication](#authentication)
Expand Down Expand Up @@ -214,6 +215,20 @@ spec:
optional: true # optional; defaults to false
```

#### Chart files

```yaml
spec:
# chart: ...
valuesFrom:
- chartFileRef:
# path within the helm chart (from git repo) where environment-prod.yaml is located
path: overrides/environment-prod.yaml # mandatory
# If set to true successful retrieval of the values file is no
# longer mandatory
optional: true # optional; defaults to false
```

## Upgrading images in a `HelmRelease` using Flux

If the chart you're using in a `HelmRelease` lets you specify the
Expand Down

0 comments on commit 3f9a134

Please sign in to comment.