Skip to content

Commit

Permalink
feat: add reset then reuse values for chart upsert action (#116)
Browse files Browse the repository at this point in the history
* feat: add reset then reuse values for chart upsert action

* revert go.mod

* add test

* add log

* add log

* add log
  • Loading branch information
aldor007 authored May 27, 2024
1 parent 9b8e8f8 commit d02cc03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
10 changes: 6 additions & 4 deletions actions/chart_upsert_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ func (c *chartUpsertHandler) Handle(ctx context.Context, action *castai.ClusterA
}
}

c.log.Debugf("upgrading release %q in namespace %q with resetThenReuseValues %t", req.ReleaseName, req.Namespace, req.ResetThenReuseValues)
_, err = c.helm.Upgrade(ctx, helm.UpgradeOptions{
ChartSource: &req.ChartSource,
Release: rel,
ValuesOverrides: req.ValuesOverrides,
MaxHistory: 3, // Keep last 3 releases history.
ChartSource: &req.ChartSource,
Release: rel,
ValuesOverrides: req.ValuesOverrides,
MaxHistory: 3, // Keep last 3 releases history.
ResetThenReuseValues: req.ResetThenReuseValues,
})
return err
}
Expand Down
11 changes: 6 additions & 5 deletions castai/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ type ActionCheckNodeStatus struct {
}

type ActionChartUpsert struct {
Namespace string `json:"namespace"`
ReleaseName string `json:"releaseName"`
ValuesOverrides map[string]string `json:"valuesOverrides,omitempty"`
ChartSource ChartSource `json:"chartSource"`
CreateNamespace bool `json:"createNamespace"`
Namespace string `json:"namespace"`
ReleaseName string `json:"releaseName"`
ValuesOverrides map[string]string `json:"valuesOverrides,omitempty"`
ChartSource ChartSource `json:"chartSource"`
CreateNamespace bool `json:"createNamespace"`
ResetThenReuseValues bool `json:"resetThenReuseValues,omitempty"`
}

type ActionChartUninstall struct {
Expand Down
10 changes: 6 additions & 4 deletions helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ type UninstallOptions struct {
}

type UpgradeOptions struct {
ChartSource *castai.ChartSource
Release *release.Release
ValuesOverrides map[string]string
MaxHistory int
ChartSource *castai.ChartSource
Release *release.Release
ValuesOverrides map[string]string
MaxHistory int
ResetThenReuseValues bool
}

type GetReleaseOptions struct {
Expand Down Expand Up @@ -155,6 +156,7 @@ func (c *client) Upgrade(ctx context.Context, opts UpgradeOptions) (*release.Rel
upgrade.Namespace = namespace
upgrade.MaxHistory = opts.MaxHistory
upgrade.PostRenderer = hook.NewLabelIgnoreHook(cfg.KubeClient, opts.Release)
upgrade.ResetThenReuseValues = opts.ResetThenReuseValues
name := opts.Release.Name

// Prepare user value overrides.
Expand Down
30 changes: 30 additions & 0 deletions helm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ func TestClientUpdate(t *testing.T) {
r.Equal("noop", rel.Config["random"])
}

func TestClientUpdateResetThenReuseValue(t *testing.T) {
r := require.New(t)

currentRelease := buildNginxIngressRelease(release.StatusDeployed)
client := &client{
log: logrus.New(),
chartLoader: &testChartLoader{chart: buildNginxIngressChart()},
configurationGetter: &testConfigurationGetter{
t: t,
currentRelease: currentRelease,
},
}

rel, err := client.Upgrade(context.Background(), UpgradeOptions{
Release: currentRelease,
ValuesOverrides: map[string]string{
"controller.replicaCount": "100",
"controller.service.type": "NodePort",
"random": "noop",
},
ResetThenReuseValues: true,
})

r.NoError(err)
r.NotNil(rel)
r.Equal("nginx-ingress", rel.Name)
r.Equal(int64(100), rel.Config["controller"].(map[string]interface{})["replicaCount"])
r.Equal("noop", rel.Config["random"])
}

func TestClientUninstall(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit d02cc03

Please sign in to comment.