Skip to content

Commit

Permalink
Merge pull request #2 from mittwald/task/repository_cleanup
Browse files Browse the repository at this point in the history
add basic github workflow, change package name
  • Loading branch information
martin-helmich authored May 4, 2020
2 parents a02c365 + f11bb86 commit fa1b7d8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 88 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Compile & Test

on: [push]

jobs:
build:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.14', '1.13', '1.12' ]

steps:
- uses: actions/checkout@v1

- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}

- name: Run unit tests
run: go test ./...
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

Go client library for accessing [Helm](https://github.com/helm/helm), enabling the user to programmatically change helm charts and releases.

This library is build upon `helm/v3.1.2`
This library is build upon `helm/v3.1.2` and available under the MIT License:

[![GitHub license](https://img.shields.io/github/license/mittwald/go-helm-client.svg)](https://github.com/mittwald/go-helm-client/blob/master/LICENSE)

## Usage

```go
Expand All @@ -19,7 +22,7 @@ import (

func main() {
// Create a client
helmClient, err := helm.NewClient(&helm.ClientOptions{
helmClient, err := helmclient.New(&helmclient.Options{
RepositoryCache: "/tmp/.helmcache",
RepositoryConfig: "/tmp/.helmrepo",
Debug: true,
Expand All @@ -39,7 +42,7 @@ func main() {
}

// Define the chart you want to install
chartSpec := helm.ChartSpec{
chartSpec := helmclient.ChartSpec{
ReleaseName: "etcd-operator",
ChartName: "stable/etcd-operator",
Namespace: "default",
Expand All @@ -56,12 +59,12 @@ func main() {

Alternatively, you can create a client via REST config:
```go
helmClient, err := helm.NewClientFromRestConf(&restClientOpts)
helmClient, err := helmclient.NewClientFromRestConf(&restClientOpts)
```
or via Kubeconfig:

```go
helmClient, err := helm.NewClientFromKubeConf()
helmClient, err := helmclient.NewClientFromKubeConf()
```

#### Private chart repository
Expand All @@ -75,3 +78,6 @@ err := helmClient.AddOrUpdateChartRepo(repo.Entry{
Password: "bar",
})
```

## Documentation
For more specific documentation, please refer to the [godoc](https://pkg.go.dev/github.com/mittwald/go-helm-client/) of this library
103 changes: 31 additions & 72 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_helm_client
package helmclient

import (
"bytes"
Expand Down Expand Up @@ -31,10 +31,8 @@ const (
defaultRepositoryConfigPath = "/tmp/.helmrepo"
)

// NewClient
//
// Wrapper function returning a new Helm client
func NewClient(options *ClientOptions) (*Client, error) {
// New returns a new Helm client with the provided options
func New(options *Options) (*Client, error) {
settings := cli.New()

err := setEnvSettings(options, settings)
Expand All @@ -45,17 +43,15 @@ func NewClient(options *ClientOptions) (*Client, error) {
return newClient(options, settings.RESTClientGetter(), settings)
}

// NewClientFromKubeConf
//
// Wrapper function returning a new Helm client constructed with the provided kubeconfig options
// NewClientFromKubeConf returns a new Helm client constructed with the provided kubeconfig options
func NewClientFromKubeConf(options *KubeConfClientOptions) (*Client, error) {
settings := cli.New()
if options.KubeConfig == nil {
return nil, fmt.Errorf("kubeconfig missing")
}

clientGetter := NewRESTClientGetter(options.Namespace, options.KubeConfig, nil)
err := setEnvSettings(options.ClientOptions, settings)
err := setEnvSettings(options.Options, settings)
if err != nil {
return nil, err
}
Expand All @@ -64,29 +60,25 @@ func NewClientFromKubeConf(options *KubeConfClientOptions) (*Client, error) {
settings.KubeContext = options.KubeContext
}

return newClient(options.ClientOptions, clientGetter, settings)
return newClient(options.Options, clientGetter, settings)
}

// NewClientFromRestConf
//
// Wrapper function returning a new Helm client constructed with the provided REST config options
// NewClientFromRestConf returns a new Helm client constructed with the provided REST config options
func NewClientFromRestConf(options *RestConfClientOptions) (*Client, error) {
settings := cli.New()

clientGetter := NewRESTClientGetter(options.Namespace, nil, options.RestConfig)

err := setEnvSettings(options.ClientOptions, settings)
err := setEnvSettings(options.Options, settings)
if err != nil {
return nil, err
}

return newClient(options.ClientOptions, clientGetter, settings)
return newClient(options.Options, clientGetter, settings)
}

// newClient
//
// Return a new Helm client
func newClient(options *ClientOptions, clientGetter genericclioptions.RESTClientGetter, settings *cli.EnvSettings) (*Client, error) {
// newClient returns a new Helm client via the provided options and REST config
func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter, settings *cli.EnvSettings) (*Client, error) {
err := setEnvSettings(options, settings)
if err != nil {
return nil, err
Expand Down Expand Up @@ -114,12 +106,10 @@ func newClient(options *ClientOptions, clientGetter genericclioptions.RESTClient
}, nil
}

// setEnvSettings
//
// Set the client's environment settings based on the provided client configuration
func setEnvSettings(options *ClientOptions, settings *cli.EnvSettings) error {
// setEnvSettings sets the client's environment settings based on the provided client configuration
func setEnvSettings(options *Options, settings *cli.EnvSettings) error {
if options == nil {
options = &ClientOptions{
options = &Options{
RepositoryConfig: defaultRepositoryConfigPath,
RepositoryCache: defaultCachePath,
Linting: true,
Expand Down Expand Up @@ -152,9 +142,7 @@ func setEnvSettings(options *ClientOptions, settings *cli.EnvSettings) error {
return nil
}

// AddOrUpdateChartRepo
//
// Add or update the provided helm chart repository
// AddOrUpdateChartRepo adds or updates the provided helm chart repository
func (c *Client) AddOrUpdateChartRepo(entry repo.Entry) error {
chartRepo, err := repo.NewChartRepository(&entry, c.Providers)
if err != nil {
Expand Down Expand Up @@ -182,9 +170,7 @@ func (c *Client) AddOrUpdateChartRepo(entry repo.Entry) error {
return nil
}

// UpdateChartRepos
//
// Update the list of chart repositories stored in the client's cache
// UpdateChartRepos updates the list of chart repositories stored in the client's cache
func (c *Client) UpdateChartRepos() error {
for _, entry := range c.storage.Repositories {
chartRepo, err := repo.NewChartRepository(entry, c.Providers)
Expand All @@ -204,9 +190,8 @@ func (c *Client) UpdateChartRepos() error {
return c.storage.WriteFile(c.Settings.RepositoryConfig, 0644)
}

// InstallOrUpgradeChart
//
// Triggers the installation of the provided chart. If the chart is already installed, trigger an upgrade instead
// InstallOrUpgradeChart triggers the installation of the provided chart.
// If the chart is already installed, trigger an upgrade instead
func (c *Client) InstallOrUpgradeChart(spec *ChartSpec) error {
installed, err := c.chartIsInstalled(spec.ReleaseName)
if err != nil {
Expand All @@ -219,23 +204,17 @@ func (c *Client) InstallOrUpgradeChart(spec *ChartSpec) error {
return c.install(spec)
}

// DeleteChartFromCache
//
// Wrapper function for deleting the provided chart from the client's cache
// DeleteChartFromCache deletes the provided chart from the client's cache
func (c *Client) DeleteChartFromCache(spec *ChartSpec) error {
return c.deleteChartFromCache(spec)
}

// UninstallRelease
//
// Wrapper function for uninstalling the provided release
// UninstallRelease uninstalls the provided release
func (c *Client) UninstallRelease(spec *ChartSpec) error {
return c.uninstallRelease(spec)
}

// install
//
// Lint and install the provided chart
// install lints and installs the provided chart
func (c *Client) install(spec *ChartSpec) error {
client := action.NewInstall(c.ActionConfig)
c.mergeInstallOptions(spec, client)
Expand Down Expand Up @@ -299,9 +278,7 @@ func (c *Client) install(spec *ChartSpec) error {
return nil
}

// upgrade
//
// Upgrade a chart and CRDs
// upgrade upgrades a chart and CRDs
func (c *Client) upgrade(spec *ChartSpec) error {
client := action.NewUpgrade(c.ActionConfig)
c.mergeUpgradeOptions(spec, client)
Expand Down Expand Up @@ -351,9 +328,7 @@ func (c *Client) upgrade(spec *ChartSpec) error {
return nil
}

// deleteChartFromCache
//
// Delete the provided chart from the client's cache
// deleteChartFromCache deletes the provided chart from the client's cache
func (c *Client) deleteChartFromCache(spec *ChartSpec) error {
client := action.NewChartRemove(c.ActionConfig)

Expand All @@ -373,9 +348,7 @@ func (c *Client) deleteChartFromCache(spec *ChartSpec) error {
return nil
}

// uninstallRelease
//
// Uninstall the provided release
// uninstallRelease uninstalls the provided release
func (c *Client) uninstallRelease(spec *ChartSpec) error {
client := action.NewUninstall(c.ActionConfig)

Expand All @@ -391,9 +364,7 @@ func (c *Client) uninstallRelease(spec *ChartSpec) error {
return nil
}

// lint
//
// Lint a chart's values
// lint lints a chart's values
func (c *Client) lint(chartPath string, values map[string]interface{}) error {
client := action.NewLint()

Expand All @@ -410,9 +381,7 @@ func (c *Client) lint(chartPath string, values map[string]interface{}) error {
return nil
}

// upgradeCRDs
//
// Upgrade the CRDs of the provided chart
// upgradeCRDs upgrades the CRDs of the provided chart
func (c *Client) upgradeCRDs(chartInstance *chart.Chart) error {
cfg, err := c.Settings.RESTClientGetter().ToRESTConfig()
if err != nil {
Expand Down Expand Up @@ -480,9 +449,7 @@ func (c *Client) upgradeCRDs(chartInstance *chart.Chart) error {
return nil
}

// getChart
//
// Return a chart matching the provided chart name and options
// getChart returns a chart matching the provided chart name and options
func (c *Client) getChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) {
chartPath, err := chartPathOptions.LocateChart(chartName, c.Settings)
if err != nil {
Expand All @@ -501,9 +468,7 @@ func (c *Client) getChart(chartName string, chartPathOptions *action.ChartPathOp
return helmChart, chartPath, err
}

// chartIsInstalled
//
// Check whether a chart is already installed or not by the provided release name
// chartIsInstalled checks whether a chart is already installed or not by the provided release name
func (c *Client) chartIsInstalled(release string) (bool, error) {
histClient := action.NewHistory(c.ActionConfig)
histClient.Max = 1
Expand All @@ -516,9 +481,7 @@ func (c *Client) chartIsInstalled(release string) (bool, error) {
return true, nil
}

// mergeInstallOptions
//
// Merge values of the provided chart to helm install options used by the client
// mergeInstallOptions merges values of the provided chart to helm install options used by the client
func (c *Client) mergeInstallOptions(chartSpec *ChartSpec, installOptions *action.Install) {
installOptions.DisableHooks = chartSpec.DisableHooks
installOptions.Replace = chartSpec.Replace
Expand All @@ -535,9 +498,7 @@ func (c *Client) mergeInstallOptions(chartSpec *ChartSpec, installOptions *actio
installOptions.SubNotes = chartSpec.SubNotes
}

// mergeUpgradeOptions
//
// Merge values of the provided chart to helm upgrade options used by the client
// mergeUpgradeOptions merges values of the provided chart to helm upgrade options used by the client
func (c *Client) mergeUpgradeOptions(chartSpec *ChartSpec, upgradeOptions *action.Upgrade) {
upgradeOptions.Version = chartSpec.Version
upgradeOptions.Namespace = chartSpec.Namespace
Expand All @@ -554,9 +515,7 @@ func (c *Client) mergeUpgradeOptions(chartSpec *ChartSpec, upgradeOptions *actio
upgradeOptions.SubNotes = chartSpec.SubNotes
}

// mergeUninstallReleaseOptions
//
// Merge values of the provided chart to helm uninstall options used by the client
// mergeUninstallReleaseOptions merges values of the provided chart to helm uninstall options used by the client
func (c *Client) mergeUninstallReleaseOptions(chartSpec *ChartSpec, uninstallReleaseOptions *action.Uninstall) {
uninstallReleaseOptions.DisableHooks = chartSpec.DisableHooks
uninstallReleaseOptions.Timeout = chartSpec.Timeout
Expand Down
7 changes: 5 additions & 2 deletions client_getter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package go_helm_client
package helmclient

import (
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -9,8 +9,10 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// source: https://github.com/helm/helm/issues/6910#issuecomment-601277026

// NewRESTClientGetter
//
// source: https://github.com/helm/helm/issues/6910#issuecomment-601277026
func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.Config) *RESTClientGetter {
return &RESTClientGetter{
namespace: namespace,
Expand All @@ -19,6 +21,7 @@ func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.C
}
}

// ToRESTConfig returns a REST config build from a given kubeconfig
func (c *RESTClientGetter) ToRESTConfig() (*rest.Config, error) {
if c.restConfig != nil {
return c.restConfig, nil
Expand Down
6 changes: 2 additions & 4 deletions spec.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package go_helm_client
package helmclient

import (
"gopkg.in/yaml.v3"
)

// GetValuesMap
//
// Return the mapped out values of a chart
// GetValuesMap returns the mapped out values of a chart
func (spec *ChartSpec) GetValuesMap() (map[string]interface{}, error) {
var values map[string]interface{}

Expand Down
Loading

0 comments on commit fa1b7d8

Please sign in to comment.