Skip to content

Commit

Permalink
Merge pull request #40 from mittwald/add/list-release-hist
Browse files Browse the repository at this point in the history
Add ListReleaseHistory func
  • Loading branch information
jkmw authored Oct 7, 2021
2 parents 8823249 + 585a25b commit fb6d506
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
15 changes: 12 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,21 @@ func (c *HelmClient) LintChart(spec *ChartSpec) error {
return c.lint(chartPath, values)
}

// SetDebugLog set's a Helm client's DebugLog to the desired 'debugLog'.
func (c *HelmClient) SetDebugLog(debugLog action.DebugLog) {
c.DebugLog = debugLog
}

// ListReleaseHistory lists the last 'max' number of entries
// in the history of the release identified by 'name'.
func (c *HelmClient) ListReleaseHistory(name string, max int) ([]*release.Release, error) {
client := action.NewHistory(c.ActionConfig)

client.Max = max

return client.Run(name)
}

// upgradeCRDs upgrades the CRDs of the provided chart
func (c *HelmClient) upgradeCRDs(ctx context.Context, chartInstance *chart.Chart) error {
cfg, err := c.ActionConfig.RESTClientGetter.ToRESTConfig()
Expand Down Expand Up @@ -682,9 +693,7 @@ func (c *HelmClient) getChart(chartName string, chartPathOptions *action.ChartPa

// chartIsInstalled checks whether a chart is already installed or not by the provided release name
func (c *HelmClient) chartIsInstalled(release string) (bool, error) {
histClient := action.NewHistory(c.ActionConfig)
histClient.Max = 1
if _, err := histClient.Run(release); err == driver.ErrReleaseNotFound {
if _, err := c.ListReleaseHistory(release, 1); err == driver.ErrReleaseNotFound {
return false, nil
} else if err != nil {
return false, err
Expand Down
9 changes: 5 additions & 4 deletions client_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// NewRESTClientGetter
// NewRESTClientGetter returns a RESTClientGetter using the provided 'namespace', 'kubeConfig' and 'restConfig'.
//
// source: https://github.com/helm/helm/issues/6910#issuecomment-601277026
func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.Config) *RESTClientGetter {
Expand All @@ -29,15 +29,16 @@ func (c *RESTClientGetter) ToRESTConfig() (*rest.Config, error) {
return clientcmd.RESTConfigFromKubeConfig(c.kubeConfig)
}

// ToDiscoveryClient returns a CachedDiscoveryInterface that can be used as a discovery client.
func (c *RESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
config, err := c.ToRESTConfig()
if err != nil {
return nil, err
}

// The more groups you have, the more discovery requests you need to make.
// given 25 groups (our groups + a few custom conf) with one-ish version each, discovery needs to make 50 requests
// double it just so we don't end up here again for a while. This config is only used for discovery.
// The more API groups exist, the more discovery requests need to be made.
// Given 25 API groups with about one version each, discovery needs to make 50 requests.
// This setting is only used for discovery.
config.Burst = 100

discoveryClient, _ := discovery.NewDiscoveryClientForConfig(config)
Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

//go:generate mockgen -source=interface.go -package mockhelmclient -destination=./mock/interface.go -self_package=. Client

// Client holds the method signatures for a Helm client.
type Client interface {
AddOrUpdateChartRepo(entry repo.Entry) error
UpdateChartRepos() error
Expand All @@ -23,4 +24,5 @@ type Client interface {
TemplateChart(spec *ChartSpec) ([]byte, error)
LintChart(spec *ChartSpec) error
SetDebugLog(debugLog action.DebugLog)
ListReleaseHistory(name string, max int) ([]*release.Release, error)
}
15 changes: 15 additions & 0 deletions mock/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestHelmClientInterfaces(t *testing.T) {
defer ctrl.Finish()

mockClient := NewMockClient(ctrl)
if mockClient == nil {
t.Fail()
}

t.Run("UpdateChartRepos", func(t *testing.T) {
mockClient.EXPECT().UpdateChartRepos().Return(nil)
Expand Down

0 comments on commit fb6d506

Please sign in to comment.