Skip to content

Commit

Permalink
pkg/client: add Config method to ActionClient (#424)
Browse files Browse the repository at this point in the history
This exposes action.Configuration of a specific
actionClient instance.

General motivation behind it is for the users to have
an 'escape hatch' access to the underlying dependency
objects that action.Configuration holds, which include
for example:
- release records store
- registry client
- Kubernetes API client

More specific motivation comes from a scenario where
an access to the underlying release store instance
is needed in order to gracefully handle pending helm
releases which were previously interrupted.
For additional context on this, see:
operator-framework/operator-controller#1776

Signed-off-by: Artur Zych <[email protected]>
Co-authored-by: Artur Zych <[email protected]>
  • Loading branch information
azych and azych authored Feb 22, 2025
1 parent 0e346e6 commit 6261f25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ActionInterface interface {
Upgrade(name, namespace string, chrt *chart.Chart, vals map[string]interface{}, opts ...UpgradeOption) (*release.Release, error)
Uninstall(name string, opts ...UninstallOption) (*release.UninstallReleaseResponse, error)
Reconcile(rel *release.Release) error
Config() *action.Configuration
}

type GetOption func(*action.Get) error
Expand Down Expand Up @@ -212,6 +213,11 @@ type actionClient struct {

var _ ActionInterface = &actionClient{}

// Config returns action.Configuration that this actionClient uses.
func (c *actionClient) Config() *action.Configuration {
return c.conf
}

func (c *actionClient) Get(name string, opts ...GetOption) (*release.Release, error) {
get := action.NewGet(c.conf)
for _, o := range concat(c.defaultGetOpts, opts...) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/client/actionclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ var _ = Describe("ActionClient", func() {
Expect(resp).To(BeNil())
})
})
var _ = Describe("Config", func() {
It("should succeed", func() {
config := ac.Config()
Expect(config).NotTo(BeNil())
})
})
})

When("release is installed", func() {
Expand Down Expand Up @@ -674,6 +680,12 @@ var _ = Describe("ActionClient", func() {
verifyRelease(cl, obj, installedRelease)
})
})
var _ = Describe("Config", func() {
It("should succeed", func() {
config := ac.Config()
Expect(config).NotTo(BeNil())
})
})
})
})

Expand Down
15 changes: 15 additions & 0 deletions pkg/reconciler/internal/fake/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/release"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -55,13 +56,15 @@ type ActionClient struct {
Upgrades []UpgradeCall
Uninstalls []UninstallCall
Reconciles []ReconcileCall
Configs []ConfigCall

HandleGet func() (*release.Release, error)
HandleHistory func() ([]*release.Release, error)
HandleInstall func() (*release.Release, error)
HandleUpgrade func() (*release.Release, error)
HandleUninstall func() (*release.UninstallReleaseResponse, error)
HandleReconcile func() error
HandleConfig func() *action.Configuration
}

func NewActionClient() ActionClient {
Expand All @@ -77,20 +80,25 @@ func NewActionClient() ActionClient {
recFunc := func(err error) func() error {
return func() error { return err }
}
conFunc := func(conf *action.Configuration) func() *action.Configuration {
return func() *action.Configuration { return conf }
}
return ActionClient{
Gets: make([]GetCall, 0),
Histories: make([]HistoryCall, 0),
Installs: make([]InstallCall, 0),
Upgrades: make([]UpgradeCall, 0),
Uninstalls: make([]UninstallCall, 0),
Reconciles: make([]ReconcileCall, 0),
Configs: make([]ConfigCall, 0),

HandleGet: relFunc(errors.New("get not implemented")),
HandleHistory: historyFunc(errors.New("history not implemented")),
HandleInstall: relFunc(errors.New("install not implemented")),
HandleUpgrade: relFunc(errors.New("upgrade not implemented")),
HandleUninstall: uninstFunc(errors.New("uninstall not implemented")),
HandleReconcile: recFunc(errors.New("reconcile not implemented")),
HandleConfig: conFunc(nil),
}
}

Expand Down Expand Up @@ -131,6 +139,8 @@ type ReconcileCall struct {
Release *release.Release
}

type ConfigCall struct{}

func (c *ActionClient) Get(name string, opts ...client.GetOption) (*release.Release, error) {
c.Gets = append(c.Gets, GetCall{name, opts})
return c.HandleGet()
Expand Down Expand Up @@ -160,3 +170,8 @@ func (c *ActionClient) Reconcile(rel *release.Release) error {
c.Reconciles = append(c.Reconciles, ReconcileCall{rel})
return c.HandleReconcile()
}

func (c *ActionClient) Config() *action.Configuration {
c.Configs = append(c.Configs, ConfigCall{})
return c.HandleConfig()
}

0 comments on commit 6261f25

Please sign in to comment.