Skip to content

Commit

Permalink
Add the ability to reference local and URL based charts as the functi…
Browse files Browse the repository at this point in the history
…onality is provided by helm.
  • Loading branch information
Anthony Spring committed Feb 4, 2020
1 parent 0909c10 commit d9af8cb
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 45 deletions.
27 changes: 25 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- None at this time.

## [0.2.1] - 2020-01-13
## [0.3.0] - 2020-02-04

Breaking Changes:

- Prior to this release if the `repo` for a chart was not specified it defaulted to `local`. This default has been changed to an empty string.

Changes:

- Added the ability to reference a chart on the local file system or URL. To utilize this functionality leave the repo empty for a chart and pass the necessary path/URL as the `name` of the chart.

```yaml
charts:
- name: https://github.com/pantsel/konga/blob/master/charts/konga/konga-1.0.0.tgz?raw=true
namespace: kube-system
release: konga
state: present
```
This example shows the `repo` has been omitted and the name pointing to a URL used to access the desired version of the chart.

## [0.2.1] - 2020-01-129

- Remove the explicit '--force' from the command passed to helm3 upgrade during a `binnacle sync`.

Expand Down Expand Up @@ -54,7 +74,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Initial release

[Unreleased]: https://github.com/traackr/binnacle/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/traackr/binnacle/compare/v0.3.0...HEAD
[0.3.0]: https://github.com/traackr/binnacle/tree/0.3.0
[0.2.1]: https://github.com/traackr/binnacle/tree/0.2.1
[0.2.0]: https://github.com/traackr/binnacle/tree/0.2.0
[0.1.0]: https://github.com/traackr/binnacle/tree/0.1.0
[0.0.5]: https://github.com/traackr/binnacle/tree/0.0.5
[0.0.4]: https://github.com/traackr/binnacle/tree/0.0.4
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.3.0
4 changes: 2 additions & 2 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func diffCmdRun(args ...string) {
var cmdArgs []string
var res Result

log.Debugf("Processing chart: %s", chart.ChartLongName())
log.Debugf("Processing chart: %s", chart.ChartURL())

// Create a temp working directory
dir, err := ioutil.TempDir("", "binnacle-exec")
Expand All @@ -104,7 +104,7 @@ func diffCmdRun(args ...string) {
cmdArgs = append(cmdArgs, "diff")
cmdArgs = append(cmdArgs, "upgrade")
cmdArgs = append(cmdArgs, chart.Release)
cmdArgs = append(cmdArgs, chart.ChartShortName())
cmdArgs = append(cmdArgs, chart.ChartURL())
cmdArgs = append(cmdArgs, "--values")
cmdArgs = append(cmdArgs, valuesFile)

Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func statusCmdRun(args ...string) {
var cmdArgs []string
var res Result

log.Debugf("Processing chart: %s", chart.ChartLongName())
log.Debugf("Processing chart: %s", chart.ChartURL())

cmdArgs = append(cmdArgs, "status")
cmdArgs = append(cmdArgs, chart.Release)
Expand Down
4 changes: 2 additions & 2 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func syncCharts(charts []config.ChartConfig, args ...string) error {
var cmdArgs []string
var res Result

log.Debugf("Processing chart: %s", chart.ChartLongName())
log.Debugf("Processing chart: %s", chart.ChartURL())

if chart.State == config.StatePresent {
// Create a temp working directory
Expand All @@ -101,7 +101,7 @@ func syncCharts(charts []config.ChartConfig, args ...string) error {

cmdArgs = append(cmdArgs, "upgrade")
cmdArgs = append(cmdArgs, chart.Release)
cmdArgs = append(cmdArgs, chart.ChartShortName())
cmdArgs = append(cmdArgs, chart.ChartURL())
cmdArgs = append(cmdArgs, "-i")

if IsHelm2() {
Expand Down
42 changes: 18 additions & 24 deletions cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func templateCmdRun(args ...string) {
var cmdArgs []string
var res Result

log.Debugf("Processing chart: %s", chart.ChartLongName())
log.Debugf("Processing chart: %s", chart.ChartURL())

//
// ORANGE: This loop should evaluate the state of the chart
Expand All @@ -89,32 +89,26 @@ func templateCmdRun(args ...string) {
var valuesFile = dir + "/values.yml"

//
// In order to template against a chart, we need the
// chart to exist locally first. This will fetch the chart,
// untar it and make it availble for the template command
// Fetch the chart for helm2
//
if IsHelm2() {
cmdArgs = append(cmdArgs, "fetch")
cmdArgs = append(cmdArgs, chart.ChartURL())
cmdArgs = append(cmdArgs, "--destination")
cmdArgs = append(cmdArgs, dir)

//
// Fetch the chart
//

cmdArgs = append(cmdArgs, "fetch")
cmdArgs = append(cmdArgs, chart.ChartShortName())

cmdArgs = append(cmdArgs, "--destination")
cmdArgs = append(cmdArgs, dir)

cmdArgs = append(cmdArgs, "--untar")
cmdArgs = append(cmdArgs, "--untar")

if len(chart.Version) > 0 {
cmdArgs = append(cmdArgs, "--version")
cmdArgs = append(cmdArgs, chart.Version)
}
if len(chart.Version) > 0 {
cmdArgs = append(cmdArgs, "--version")
cmdArgs = append(cmdArgs, chart.Version)
}

res, err = RunHelmCommand(cmdArgs...)
if err != nil {
log.Errorf("helm fetch failed with the following:")
log.Fatal(res.Stderr)
res, err = RunHelmCommand(cmdArgs...)
if err != nil {
log.Errorf("helm fetch failed with the following:")
log.Fatal(res.Stderr)
}
}

//
Expand Down Expand Up @@ -150,7 +144,7 @@ func templateCmdRun(args ...string) {
cmdArgs = append(cmdArgs, chart.Release)

// CHART
cmdArgs = append(cmdArgs, dir+"/"+chart.Name)
cmdArgs = append(cmdArgs, chart.ChartURL())

// Add the namespace if given
if len(chart.Namespace) > 0 {
Expand Down
25 changes: 13 additions & 12 deletions config/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ type ChartConfig struct {
Version string `mapstructure:"version"`
}

// ChartLongName returns the name for the chart with version
func (c ChartConfig) ChartLongName() string {
extra := ""
if len(c.Version) > 0 {
extra = "-" + c.Version
// ChartURL returns a URL related to the given repo and name of the chart based off of
// criteria 1 through 4 of the following documentation on how to specify local and remote charts
//
// 1. By chart reference: helm install mymaria example/mariadb
// 2. By path to a packaged chart: helm install mynginx ./nginx-1.2.3.tgz
// 3. By path to an unpacked chart directory: helm install mynginx ./nginx
// 4. By absolute URL: helm install mynginx https://example.com/charts/nginx-1.2.3.tgz
//
func (c ChartConfig) ChartURL() string {
// If a repository is given return the c
if len(c.Repo) > 0 {
return c.Repo + "/" + c.Name
}
return c.Repo + "/" + c.Name + extra
}

// ChartShortName returns the name for the chart without version
func (c ChartConfig) ChartShortName() string {
return c.Repo + "/" + c.Name
return c.Name
}

// WriteValueFile writes the given file containing the Chart's Values
Expand All @@ -59,6 +61,5 @@ func (c ChartConfig) WriteValueFile(file string) error {
if err != nil {
return err
}

return ioutil.WriteFile(file, y, 0644)
}
44 changes: 44 additions & 0 deletions config/chart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2020 Anthony Spring <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config

import (
"github.com/spf13/viper"
"testing"

"github.com/stretchr/testify/assert"
)

func TestChartURL_WithRepo(t *testing.T) {
viper.SetConfigFile("../test-data/demo.yml")
viper.ReadInConfig()
c, _ := LoadAndValidateFromViper()

assert.Equal(t, c.Charts[0].ChartURL(), "stable/concourse")
}

func TestChartURL_WithoutRepo(t *testing.T) {
viper.SetConfigFile("../test-data/without-repo.yml")
viper.ReadInConfig()
c, _ := LoadAndValidateFromViper()

assert.Equal(t, c.Charts[0].ChartURL(), "https://github.com/pantsel/konga/blob/master/charts/konga/konga-1.0.0.tgz?raw=true")
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func LoadAndValidateFromViper() (*BinnacleConfig, error) {
chart := &config.Charts[idx]

if len(chart.Repo) == 0 {
chart.Repo = "local"
chart.Repo = ""
}

if len(chart.State) == 0 {
Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ func TestBooleanIsNotCoerced(t *testing.T) {
ingressConfig := c.Charts[0].Values["ingress"].(map[string]interface{})
assert.Equal(t, ingressConfig["enabled"], true)
}

func TestLoadAndValidateFromViper_Unmarshallable(t *testing.T) {
viper.SetConfigFile("../test-data/unmarshallable.yml")
viper.ReadInConfig()

_, err := LoadAndValidateFromViper()
assert.NotNil(t, err)
}

func TestLoadAndValidateFromViper_DefaultChartState(t *testing.T) {
viper.SetConfigFile("../test-data/default-state.yml")
viper.ReadInConfig()

c, _ := LoadAndValidateFromViper()
assert.Equal(t, c.Charts[0].State, "present")
}

func TestLoadAndValidateFromViper_DefaultRepoState(t *testing.T) {
viper.SetConfigFile("../test-data/default-state.yml")
viper.ReadInConfig()

c, _ := LoadAndValidateFromViper()
assert.Equal(t, c.Repositories[0].State, "present")
}
27 changes: 27 additions & 0 deletions test-data/default-state.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# charts takes a list of chart configurations
charts:
# This is the name of the chart
- name: concourse
# This is the namespace into which the chart is launched
namespace: apps
# This is the name for the release of this chart
release: apps-concourse
# This is the name of the repository within which the helm chart is located
repo: stable
# Any data under values are passed to Helm to configure the given chart
values:
image: concourse/concourse
imageTag: "3.10.0"
ingress:
enabled: true
# This is the version of the Helm chart. If this is omitted, the latest is used.
version: 1.3.1


# repositories takes a list of repository configurations
repositories:
# This is the name of the repository
- name: stable
# This is the URL of the repository
url: https://kubernetes-charts.storage.googleapis.com
4 changes: 4 additions & 0 deletions test-data/unmarshallable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
charts:
foo: bar
biz: baz
6 changes: 6 additions & 0 deletions test-data/without-repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
charts:
- name: https://github.com/pantsel/konga/blob/master/charts/konga/konga-1.0.0.tgz?raw=true
namespace: kube-system
release: konga
state: present

0 comments on commit d9af8cb

Please sign in to comment.