Skip to content

Commit 23c2513

Browse files
committed
Added support for upgrade
1 parent c0f848a commit 23c2513

8 files changed

Lines changed: 155 additions & 91 deletions

File tree

cmd/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newHistoryCmd(commandLineOptions *options.CommandLineOption) *cobra.Command
2727

2828
action := action.NewHistoryAction(commandLineOptions)
2929

30-
// listCmd represents the list command
30+
// historyCmd represents the list command
3131
historyCmd := &cobra.Command{
3232
Use: "history RELEASE_NAME",
3333
Short: "Fetch release history",

cmd/index.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func newIndexCmd(commandLineOptions *options.CommandLineOption) *cobra.Command {
4040

4141
return action.Run()
4242

43-
return nil
4443
},
4544
}
4645

cmd/upgrade.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newUpgradeCmd(commandLineOptions *options.CommandLineOption) *cobra.Command
6060
}
6161

6262
upgradeCmd.PersistentFlags().StringVar(&commandLineOptions.Version, "version", "", "specify the exact chart version to use. If this is not specified, the latest version is used")
63+
upgradeCmd.PersistentFlags().BoolVarP(&commandLineOptions.Install, "install", "i", false, "if a release by this name doesn't already exist, run an install")
6364
setValuesOptions(upgradeCmd, commandLineOptions)
6465

6566
return upgradeCmd

pkg/action/common.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package action
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"strings"
7+
"time"
8+
9+
"github.com/redhat-cop/oc-helm/pkg/client"
10+
"github.com/redhat-cop/oc-helm/pkg/options"
11+
"github.com/redhat-cop/oc-helm/pkg/utils"
12+
"helm.sh/helm/v3/pkg/release"
13+
"helm.sh/helm/v3/pkg/repo"
14+
)
15+
16+
type HelmChartInstall struct {
17+
releaseName string
18+
chartReference string
19+
commandLineOptions *options.CommandLineOption
20+
helmChartClient *client.HelmChartClient
21+
upgrade bool
22+
}
23+
24+
func getChartVersion(commandLineOptions *options.CommandLineOption, charts repo.ChartVersions) *repo.ChartVersion {
25+
26+
if len(charts) == 0 {
27+
return nil
28+
}
29+
30+
if commandLineOptions.Version == "" {
31+
return charts[0]
32+
}
33+
34+
for _, chart := range charts {
35+
if chart.Version == commandLineOptions.Version {
36+
return chart
37+
}
38+
}
39+
40+
return nil
41+
42+
}
43+
44+
func installChart(helmChartInstall *HelmChartInstall) error {
45+
index, err := helmChartInstall.helmChartClient.GetIndex()
46+
47+
if err != nil {
48+
return err
49+
}
50+
51+
index.SortEntries()
52+
53+
chartReferenceParts := strings.Split(helmChartInstall.chartReference, "/")
54+
55+
repository := chartReferenceParts[0]
56+
chartName := chartReferenceParts[1]
57+
58+
consoleChartName := utils.CreateRepositoryIndexKey(repository, chartName)
59+
60+
charts := index.Entries[consoleChartName]
61+
62+
if charts == nil {
63+
return fmt.Errorf("Error: Chart '%s' does not exist", helmChartInstall.chartReference)
64+
}
65+
66+
chartVersion := getChartVersion(helmChartInstall.commandLineOptions, charts)
67+
68+
if chartVersion == nil {
69+
70+
if helmChartInstall.commandLineOptions.Version != "" {
71+
return fmt.Errorf("Chart '%s' with version '%s' not found", helmChartInstall.chartReference, helmChartInstall.commandLineOptions.Version)
72+
} else {
73+
return fmt.Errorf("Chart '%s' not found", helmChartInstall.chartReference)
74+
}
75+
76+
}
77+
78+
if len(chartVersion.URLs) == 0 {
79+
return fmt.Errorf("Unable to locate Chart URL")
80+
}
81+
82+
chartURL := chartVersion.URLs[0]
83+
84+
values, err := utils.MergeValues(helmChartInstall.commandLineOptions)
85+
86+
if err != nil {
87+
return err
88+
}
89+
90+
release, err := helmChartInstall.helmChartClient.CreateRelease(helmChartInstall.releaseName, chartURL, values, helmChartInstall.upgrade)
91+
92+
if err != nil {
93+
return err
94+
}
95+
96+
printReleaseDeploymentStatus(helmChartInstall.commandLineOptions.Streams.Out, release)
97+
98+
return nil
99+
}
100+
101+
func printReleaseDeploymentStatus(w io.Writer, release *release.Release) {
102+
fmt.Fprintf(w, "NAME: %s\n", release.Name)
103+
fmt.Fprintf(w, "NAMESPACE: %s\n", release.Namespace)
104+
if !release.Info.LastDeployed.IsZero() {
105+
fmt.Fprintf(w, "LAST DEPLOYED: %s\n", release.Info.LastDeployed.Format(time.ANSIC))
106+
}
107+
fmt.Fprintf(w, "STATUS: %s\n", release.Info.Status.String())
108+
fmt.Fprintf(w, "REVISION: %d\n", release.Version)
109+
110+
}

pkg/action/install.go

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package action
22

33
import (
4-
"fmt"
5-
"strings"
6-
"time"
7-
84
"github.com/redhat-cop/oc-helm/pkg/client"
9-
"github.com/redhat-cop/oc-helm/pkg/utils"
10-
"helm.sh/helm/v3/pkg/repo"
115

126
"github.com/redhat-cop/oc-helm/pkg/options"
137
)
@@ -43,85 +37,13 @@ func (i *InstallAction) BuildHelmChartClient() error {
4337

4438
func (i *InstallAction) Run(releaseName string, chartReference string) error {
4539

46-
index, err := i.helmChartClient.GetIndex()
47-
48-
if err != nil {
49-
return err
50-
}
51-
52-
index.SortEntries()
53-
54-
chartReferenceParts := strings.Split(chartReference, "/")
55-
56-
repository := chartReferenceParts[0]
57-
chartName := chartReferenceParts[1]
58-
59-
consoleChartName := utils.CreateRepositoryIndexKey(repository, chartName)
60-
61-
charts := index.Entries[consoleChartName]
62-
63-
if charts == nil {
64-
return fmt.Errorf("Error: Chart '%s' does not exist", chartReference)
40+
helmChartInstall := &HelmChartInstall{
41+
releaseName: releaseName,
42+
chartReference: chartReference,
43+
commandLineOptions: i.commandLineOptions,
44+
helmChartClient: i.helmChartClient,
6545
}
6646

67-
chartVersion := i.getChartVersion(charts)
68-
69-
if chartVersion == nil {
70-
71-
if i.commandLineOptions.Version != "" {
72-
return fmt.Errorf("Chart '%s' with version '%s' not found", chartReference, i.commandLineOptions.Version)
73-
} else {
74-
return fmt.Errorf("Chart '%s' not found", chartReference)
75-
}
76-
77-
}
78-
79-
if len(chartVersion.URLs) == 0 {
80-
return fmt.Errorf("Unable to locate Chart URL")
81-
}
82-
83-
chartURL := chartVersion.URLs[0]
84-
85-
values, err := utils.MergeValues(i.commandLineOptions)
86-
87-
if err != nil {
88-
return err
89-
}
90-
91-
release, err := i.helmChartClient.CreateRelease(releaseName, chartURL, values)
92-
93-
if err != nil {
94-
return err
95-
}
96-
97-
fmt.Fprintf(i.commandLineOptions.Streams.Out, "NAME: %s\n", release.Name)
98-
fmt.Fprintf(i.commandLineOptions.Streams.Out, "NAMESPACE: %s\n", release.Namespace)
99-
if !release.Info.LastDeployed.IsZero() {
100-
fmt.Fprintf(i.commandLineOptions.Streams.Out, "LAST DEPLOYED: %s\n", release.Info.LastDeployed.Format(time.ANSIC))
101-
}
102-
fmt.Fprintf(i.commandLineOptions.Streams.Out, "STATUS: %s\n", release.Info.Status.String())
103-
fmt.Fprintf(i.commandLineOptions.Streams.Out, "REVISION: %d\n", release.Version)
104-
105-
return nil
106-
107-
}
108-
109-
func (i *InstallAction) getChartVersion(charts repo.ChartVersions) *repo.ChartVersion {
110-
111-
if len(charts) == 0 {
112-
return nil
113-
}
114-
115-
if i.commandLineOptions.Version == "" {
116-
return charts[0]
117-
}
118-
119-
for _, chart := range charts {
120-
if chart.Version == i.commandLineOptions.Version {
121-
return chart
122-
}
123-
}
124-
125-
return nil
47+
return installChart(helmChartInstall)
12648

12749
}

pkg/action/upgrade.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package action
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/redhat-cop/oc-helm/pkg/client"
58

69
"github.com/redhat-cop/oc-helm/pkg/options"
@@ -35,8 +38,29 @@ func (h *UpgradeAction) BuildHelmChartClient() error {
3538

3639
}
3740

38-
func (i *UpgradeAction) Run(releaseName string, chartReference string) error {
41+
func (u *UpgradeAction) Run(releaseName string, chartReference string) error {
3942

40-
return nil
43+
_, err := u.helmChartClient.GetRelease(releaseName)
44+
45+
helmChartInstall := &HelmChartInstall{
46+
releaseName: releaseName,
47+
chartReference: chartReference,
48+
commandLineOptions: u.commandLineOptions,
49+
helmChartClient: u.helmChartClient,
50+
upgrade: true,
51+
}
52+
53+
// TODO: Change logic to return HelmClientError to Actions level to inspect status code response
54+
if err != nil && u.commandLineOptions.Install && strings.Contains(err.Error(), "release: not found") {
55+
56+
fmt.Fprintf(u.commandLineOptions.Streams.Out, "Release \"%s\" does not exist. Installing it now.\n", releaseName)
57+
58+
helmChartInstall.upgrade = false
59+
60+
} else if err != nil {
61+
return err
62+
}
63+
64+
return installChart(helmChartInstall)
4165

4266
}

pkg/client/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (c *HelmChartClient) History(releaseName string) (*[]release.Release, error
135135

136136
}
137137

138-
func (c *HelmChartClient) CreateRelease(releaseName string, chartUrl string, values map[string]interface{}) (*release.Release, error) {
138+
func (c *HelmChartClient) CreateRelease(releaseName string, chartUrl string, values map[string]interface{}, upgrade bool) (*release.Release, error) {
139139

140140
helmRequest := &types.HelmRequest{
141141
Name: releaseName,
@@ -144,7 +144,14 @@ func (c *HelmChartClient) CreateRelease(releaseName string, chartUrl string, val
144144
Values: values,
145145
}
146146

147-
req, err := c.newRequest("POST", "/api/helm/release", helmRequest)
147+
var req *http.Request
148+
var err error
149+
150+
if upgrade {
151+
req, err = c.newRequest("PUT", "/api/helm/release", helmRequest)
152+
} else {
153+
req, err = c.newRequest("POST", "/api/helm/release", helmRequest)
154+
}
148155

149156
if err != nil {
150157
return nil, err
@@ -160,7 +167,7 @@ func (c *HelmChartClient) CreateRelease(releaseName string, chartUrl string, val
160167
return nil, fmt.Errorf("%s", helmClientError.HelmServerError.Error)
161168
}
162169

163-
return nil, fmt.Errorf("Failed to install release '%s': Status code: %d", releaseName, helmClientError.StatusCode)
170+
return nil, fmt.Errorf("Failed to create release '%s': Status code: %d", releaseName, helmClientError.StatusCode)
164171
}
165172

166173
return &release, nil

pkg/options/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type CommandLineOption struct {
2424
StringValues []string
2525
Values []string
2626
FileValues []string
27+
Install bool
2728
}
2829

2930
func NewCommandLineOption() *CommandLineOption {

0 commit comments

Comments
 (0)