From db216120a9524fd27a3724d3ac292076787c203d Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Mon, 4 May 2020 09:53:07 +0200 Subject: [PATCH 1/4] add basic github workflow, change package name --- .github/workflows/build.yml | 22 ++++++++++++++++++++++ README.md | 20 +++++++++++++------- client.go | 6 +++--- client_getter.go | 9 +++++++-- spec.go | 2 +- types.go | 2 +- 6 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..84b242f1 --- /dev/null +++ b/.github/workflows/build.yml @@ -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 ./... \ No newline at end of file diff --git a/README.md b/README.md index a987a87b..0176d53a 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,27 @@ 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 -import "github.com/mittwald/go-helm-client" +import helmclient "github.com/mittwald/go-helm-client" ``` Construct a new Helm client, then use the various services on the client to manage helm chart repositories and releases: ```go package main import ( - "github.com/mittwald/go-helm-client" + helmclient "github.com/mittwald/go-helm-client" "helm.sh/helm/v3/pkg/repo" ) 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, @@ -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", @@ -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 @@ -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 \ No newline at end of file diff --git a/client.go b/client.go index a26206a9..4d58cc59 100644 --- a/client.go +++ b/client.go @@ -1,4 +1,4 @@ -package go_helm_client +package helmclient import ( "bytes" @@ -45,10 +45,10 @@ const ( defaultRepositoryConfigPath = "/tmp/.helmrepo" ) -// NewClient +// New // // Wrapper function returning a new Helm client -func NewClient(options *ClientOptions) (*Client, error) { +func New(options *ClientOptions) (*Client, error) { settings := cli.New() err := setEnvSettings(options, settings) diff --git a/client_getter.go b/client_getter.go index 0398878b..6080ca8c 100644 --- a/client_getter.go +++ b/client_getter.go @@ -1,4 +1,4 @@ -package go_helm_client +package helmclient import ( "k8s.io/apimachinery/pkg/api/meta" @@ -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, @@ -19,6 +21,9 @@ func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.C } } +// ToRESTConfig +// +// Return a REST config build from a given kubeconfig func (c *RESTClientGetter) ToRESTConfig() (*rest.Config, error) { if c.restConfig != nil { return c.restConfig, nil diff --git a/spec.go b/spec.go index 1b4d43c4..56ac9074 100644 --- a/spec.go +++ b/spec.go @@ -1,4 +1,4 @@ -package go_helm_client +package helmclient import ( "gopkg.in/yaml.v3" diff --git a/types.go b/types.go index 56520c0e..c37eb7bc 100644 --- a/types.go +++ b/types.go @@ -1,4 +1,4 @@ -package go_helm_client +package helmclient import ( "time" From f5911914fe8c915ede0fbe660c2ee994af015c52 Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Mon, 4 May 2020 10:01:39 +0200 Subject: [PATCH 2/4] sort imports --- client.go | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/client.go b/client.go index 4d58cc59..dd12d3ae 100644 --- a/client.go +++ b/client.go @@ -7,35 +7,21 @@ import ( "log" "os" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - - apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" - - "k8s.io/apimachinery/pkg/util/yaml" - - "k8s.io/cli-runtime/pkg/genericclioptions" - "github.com/spf13/pflag" - - "helm.sh/helm/v3/pkg/getter" - - "helm.sh/helm/v3/pkg/cli" - - "helm.sh/helm/v3/pkg/downloader" - + "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" - - "helm.sh/helm/v3/pkg/storage/driver" - "helm.sh/helm/v3/pkg/chart/loader" - - "helm.sh/helm/v3/pkg/action" - + "helm.sh/helm/v3/pkg/cli" + "helm.sh/helm/v3/pkg/downloader" + "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/repo" + "helm.sh/helm/v3/pkg/storage/driver" + apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" + "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/yaml" + "k8s.io/cli-runtime/pkg/genericclioptions" ) var storage = repo.File{} From 99d50064475632ec41318d659aa58749e11cdae7 Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Mon, 4 May 2020 10:06:59 +0200 Subject: [PATCH 3/4] rename ClientOptions --- client.go | 16 ++++++++-------- types.go | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index dd12d3ae..d94b39fc 100644 --- a/client.go +++ b/client.go @@ -34,7 +34,7 @@ const ( // New // // Wrapper function returning a new Helm client -func New(options *ClientOptions) (*Client, error) { +func New(options *Options) (*Client, error) { settings := cli.New() err := setEnvSettings(options, settings) @@ -55,7 +55,7 @@ func NewClientFromKubeConf(options *KubeConfClientOptions) (*Client, error) { } clientGetter := NewRESTClientGetter(options.Namespace, options.KubeConfig, nil) - err := setEnvSettings(options.ClientOptions, settings) + err := setEnvSettings(options.Options, settings) if err != nil { return nil, err } @@ -64,7 +64,7 @@ func NewClientFromKubeConf(options *KubeConfClientOptions) (*Client, error) { settings.KubeContext = options.KubeContext } - return newClient(options.ClientOptions, clientGetter, settings) + return newClient(options.Options, clientGetter, settings) } // NewClientFromRestConf @@ -75,18 +75,18 @@ func NewClientFromRestConf(options *RestConfClientOptions) (*Client, error) { 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) { +func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter, settings *cli.EnvSettings) (*Client, error) { err := setEnvSettings(options, settings) if err != nil { return nil, err @@ -117,9 +117,9 @@ func newClient(options *ClientOptions, clientGetter genericclioptions.RESTClient // setEnvSettings // // Set the client's environment settings based on the provided client configuration -func setEnvSettings(options *ClientOptions, settings *cli.EnvSettings) error { +func setEnvSettings(options *Options, settings *cli.EnvSettings) error { if options == nil { - options = &ClientOptions{ + options = &Options{ RepositoryConfig: defaultRepositoryConfigPath, RepositoryCache: defaultCachePath, Linting: true, diff --git a/types.go b/types.go index c37eb7bc..4cfe4da3 100644 --- a/types.go +++ b/types.go @@ -13,19 +13,19 @@ import ( // KubeConfClientOptions defines the options used for constructing a client via kubeconfig type KubeConfClientOptions struct { - *ClientOptions + *Options KubeContext string KubeConfig []byte } // RestConfClientOptions defines the options used for constructing a client via REST config type RestConfClientOptions struct { - *ClientOptions + *Options RestConfig *rest.Config } -// ClientOptions defines the options of a client -type ClientOptions struct { +// Options defines the options of a client +type Options struct { Namespace string RepositoryConfig string RepositoryCache string From f11bb863a53ec872451c1a5563b81aca6daeb966 Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Mon, 4 May 2020 11:37:02 +0200 Subject: [PATCH 4/4] unify comments for godoc --- README.md | 4 +-- client.go | 85 +++++++++++++----------------------------------- client_getter.go | 4 +-- spec.go | 4 +-- 4 files changed, 26 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 0176d53a..d3388120 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ This library is build upon `helm/v3.1.2` and available under the MIT License: ## Usage ```go -import helmclient "github.com/mittwald/go-helm-client" +import "github.com/mittwald/go-helm-client" ``` Construct a new Helm client, then use the various services on the client to manage helm chart repositories and releases: ```go package main import ( - helmclient "github.com/mittwald/go-helm-client" + "github.com/mittwald/go-helm-client" "helm.sh/helm/v3/pkg/repo" ) diff --git a/client.go b/client.go index d94b39fc..c3207f18 100644 --- a/client.go +++ b/client.go @@ -31,9 +31,7 @@ const ( defaultRepositoryConfigPath = "/tmp/.helmrepo" ) -// New -// -// Wrapper function returning a new Helm client +// New returns a new Helm client with the provided options func New(options *Options) (*Client, error) { settings := cli.New() @@ -45,9 +43,7 @@ func New(options *Options) (*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 { @@ -67,9 +63,7 @@ func NewClientFromKubeConf(options *KubeConfClientOptions) (*Client, error) { 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() @@ -83,9 +77,7 @@ func NewClientFromRestConf(options *RestConfClientOptions) (*Client, error) { return newClient(options.Options, clientGetter, settings) } -// newClient -// -// Return a new Helm client +// 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 { @@ -114,9 +106,7 @@ func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter }, nil } -// setEnvSettings -// -// Set the client's environment settings based on the provided client configuration +// 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 = &Options{ @@ -152,9 +142,7 @@ func setEnvSettings(options *Options, 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 { @@ -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) @@ -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 { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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() @@ -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 { @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/client_getter.go b/client_getter.go index 6080ca8c..56db71b6 100644 --- a/client_getter.go +++ b/client_getter.go @@ -21,9 +21,7 @@ func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.C } } -// ToRESTConfig -// -// Return a REST config build from a given kubeconfig +// 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 diff --git a/spec.go b/spec.go index 56ac9074..1682756d 100644 --- a/spec.go +++ b/spec.go @@ -4,9 +4,7 @@ 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{}