diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f2ddd9a2..c014d76f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,14 +14,12 @@ jobs: - uses: actions/checkout@v2 - - name: Install and run mockgen + - name: Run go generate run: | set -x apt-get -y update && apt-get -y install git - cd / go install github.com/golang/mock/mockgen@v1.6.0 - cd - - go env + go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0 go generate ./... git diff | cat git status --porcelain=v1 diff --git a/client.go b/client.go index ac0f9c9f..1aaab81d 100644 --- a/client.go +++ b/client.go @@ -200,29 +200,29 @@ func (c *HelmClient) UpdateChartRepos() error { // InstallOrUpgradeChart installs or upgrades the provided chart and returns the corresponding release. // Namespace and other context is provided via the helmclient.Options struct when instantiating a client. -func (c *HelmClient) InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) { +func (c *HelmClient) InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) { exists, err := c.chartExists(spec) if err != nil { return nil, err } if exists { - return c.upgrade(ctx, spec) + return c.upgrade(ctx, spec, opts) } - return c.install(ctx, spec) + return c.install(ctx, spec, opts) } // InstallChart installs the provided chart and returns the corresponding release. // Namespace and other context is provided via the helmclient.Options struct when instantiating a client. -func (c *HelmClient) InstallChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) { - return c.install(ctx, spec) +func (c *HelmClient) InstallChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) { + return c.install(ctx, spec, opts) } // UpgradeChart upgrades the provided chart and returns the corresponding release. // Namespace and other context is provided via the helmclient.Options struct when instantiating a client. -func (c *HelmClient) UpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) { - return c.upgrade(ctx, spec) +func (c *HelmClient) UpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) { + return c.upgrade(ctx, spec, opts) } // ListDeployedReleases lists all deployed releases. @@ -265,7 +265,7 @@ func (c *HelmClient) UninstallReleaseByName(name string) error { // install installs the provided chart. // Optionally lints the chart if the linting flag is set. -func (c *HelmClient) install(ctx context.Context, spec *ChartSpec) (*release.Release, error) { +func (c *HelmClient) install(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) { client := action.NewInstall(c.ActionConfig) mergeInstallOptions(spec, client) @@ -281,8 +281,10 @@ func (c *HelmClient) install(ctx context.Context, spec *ChartSpec) (*release.Rel client.Version = ">0.0.0-0" } - if spec.PostRenderer != nil { - client.PostRenderer = spec.PostRenderer + if opts != nil { + if opts.PostRenderer != nil { + client.PostRenderer = opts.PostRenderer + } } helmChart, chartPath, err := c.getChart(spec.ChartName, &client.ChartPathOptions) @@ -327,7 +329,7 @@ func (c *HelmClient) install(ctx context.Context, spec *ChartSpec) (*release.Rel // upgrade upgrades a chart and CRDs. // Optionally lints the chart if the linting flag is set. -func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) (*release.Release, error) { +func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) { client := action.NewUpgrade(c.ActionConfig) mergeUpgradeOptions(spec, client) client.Install = true @@ -336,8 +338,10 @@ func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) (*release.Rel client.Version = ">0.0.0-0" } - if spec.PostRenderer != nil { - client.PostRenderer = spec.PostRenderer + if opts != nil { + if opts.PostRenderer != nil { + client.PostRenderer = opts.PostRenderer + } } helmChart, chartPath, err := c.getChart(spec.ChartName, &client.ChartPathOptions) diff --git a/client_test.go b/client_test.go index 3020a315..03043b99 100644 --- a/client_test.go +++ b/client_test.go @@ -115,7 +115,7 @@ func ExampleHelmClient_InstallOrUpgradeChart() { // Install a chart release. // Note that helmclient.Options.Namespace should ideally match the namespace in chartSpec.Namespace. - if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { + if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec, nil); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func ExampleHelmClient_InstallOrUpgradeChart_useChartDirectory() { Wait: true, } - if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { + if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec, nil); err != nil { panic(err) } } @@ -145,7 +145,7 @@ func ExampleHelmClient_InstallOrUpgradeChart_useLocalChartArchive() { Wait: true, } - if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { + if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec, nil); err != nil { panic(err) } } @@ -160,7 +160,7 @@ func ExampleHelmClient_InstallOrUpgradeChart_useURL() { Wait: true, } - if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { + if _, err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec, nil); err != nil { panic(err) } } diff --git a/hack/boilerplate.go.txt b/hack/boilerplate.go.txt new file mode 100644 index 00000000..29c55ecd --- /dev/null +++ b/hack/boilerplate.go.txt @@ -0,0 +1,15 @@ +/* +Copyright 2022. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ \ No newline at end of file diff --git a/interface.go b/interface.go index c23c02bc..c370368c 100644 --- a/interface.go +++ b/interface.go @@ -15,9 +15,9 @@ import ( type Client interface { AddOrUpdateChartRepo(entry repo.Entry) error UpdateChartRepos() error - InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) - InstallChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) - UpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) + InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) + InstallChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) + UpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) ListDeployedReleases() ([]*release.Release, error) ListReleasesByStateMask(action.ListStates) ([]*release.Release, error) GetRelease(name string) (*release.Release, error) diff --git a/mock/interface.go b/mock/interface.go index 4ce7afef..fecf6043 100644 --- a/mock/interface.go +++ b/mock/interface.go @@ -83,33 +83,33 @@ func (mr *MockClientMockRecorder) GetReleaseValues(name, allValues interface{}) } // InstallChart mocks base method. -func (m *MockClient) InstallChart(ctx context.Context, spec *helmclient.ChartSpec) (*release.Release, error) { +func (m *MockClient) InstallChart(ctx context.Context, spec *helmclient.ChartSpec, opts *helmclient.GenericHelmOptions) (*release.Release, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InstallChart", ctx, spec) + ret := m.ctrl.Call(m, "InstallChart", ctx, spec, opts) ret0, _ := ret[0].(*release.Release) ret1, _ := ret[1].(error) return ret0, ret1 } // InstallChart indicates an expected call of InstallChart. -func (mr *MockClientMockRecorder) InstallChart(ctx, spec interface{}) *gomock.Call { +func (mr *MockClientMockRecorder) InstallChart(ctx, spec, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InstallChart", reflect.TypeOf((*MockClient)(nil).InstallChart), ctx, spec) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InstallChart", reflect.TypeOf((*MockClient)(nil).InstallChart), ctx, spec, opts) } // InstallOrUpgradeChart mocks base method. -func (m *MockClient) InstallOrUpgradeChart(ctx context.Context, spec *helmclient.ChartSpec) (*release.Release, error) { +func (m *MockClient) InstallOrUpgradeChart(ctx context.Context, spec *helmclient.ChartSpec, opts *helmclient.GenericHelmOptions) (*release.Release, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InstallOrUpgradeChart", ctx, spec) + ret := m.ctrl.Call(m, "InstallOrUpgradeChart", ctx, spec, opts) ret0, _ := ret[0].(*release.Release) ret1, _ := ret[1].(error) return ret0, ret1 } // InstallOrUpgradeChart indicates an expected call of InstallOrUpgradeChart. -func (mr *MockClientMockRecorder) InstallOrUpgradeChart(ctx, spec interface{}) *gomock.Call { +func (mr *MockClientMockRecorder) InstallOrUpgradeChart(ctx, spec, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InstallOrUpgradeChart", reflect.TypeOf((*MockClient)(nil).InstallOrUpgradeChart), ctx, spec) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InstallOrUpgradeChart", reflect.TypeOf((*MockClient)(nil).InstallOrUpgradeChart), ctx, spec, opts) } // LintChart mocks base method. @@ -255,16 +255,16 @@ func (mr *MockClientMockRecorder) UpdateChartRepos() *gomock.Call { } // UpgradeChart mocks base method. -func (m *MockClient) UpgradeChart(ctx context.Context, spec *helmclient.ChartSpec) (*release.Release, error) { +func (m *MockClient) UpgradeChart(ctx context.Context, spec *helmclient.ChartSpec, opts *helmclient.GenericHelmOptions) (*release.Release, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpgradeChart", ctx, spec) + ret := m.ctrl.Call(m, "UpgradeChart", ctx, spec, opts) ret0, _ := ret[0].(*release.Release) ret1, _ := ret[1].(error) return ret0, ret1 } // UpgradeChart indicates an expected call of UpgradeChart. -func (mr *MockClientMockRecorder) UpgradeChart(ctx, spec interface{}) *gomock.Call { +func (mr *MockClientMockRecorder) UpgradeChart(ctx, spec, opts interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpgradeChart", reflect.TypeOf((*MockClient)(nil).UpgradeChart), ctx, spec) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpgradeChart", reflect.TypeOf((*MockClient)(nil).UpgradeChart), ctx, spec, opts) } diff --git a/types.go b/types.go index 1b1a0e6c..a9f322af 100644 --- a/types.go +++ b/types.go @@ -61,7 +61,14 @@ type HelmClient struct { DebugLog action.DebugLog } +type GenericHelmOptions struct { + PostRenderer postrender.PostRenderer +} + +//go:generate controller-gen object object:headerFile="./hack/boilerplate.go.txt" paths="./..." output:dir=. + // ChartSpec defines the values of a helm chart +// +kubebuilder:object:generate:=true type ChartSpec struct { ReleaseName string `json:"release"` ChartName string `json:"chart"` @@ -139,8 +146,4 @@ type ChartSpec struct { // DryRun indicates whether to perform a dry run. // +optional DryRun bool `json:"dryRun,omitempty"` - // PostRenderer to run on the Helm Chart - // +kubebuilder:validation:Schemaless - // +optional - PostRenderer postrender.PostRenderer `json:"postRenderer,omitempty"` } diff --git a/zz_generated.deepcopy.go b/zz_generated.deepcopy.go new file mode 100644 index 00000000..394f9b28 --- /dev/null +++ b/zz_generated.deepcopy.go @@ -0,0 +1,37 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright 2022. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package helmclient + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ChartSpec) DeepCopyInto(out *ChartSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ChartSpec. +func (in *ChartSpec) DeepCopy() *ChartSpec { + if in == nil { + return nil + } + out := new(ChartSpec) + in.DeepCopyInto(out) + return out +}