Skip to content

Commit 9963bf0

Browse files
committed
Added console discovery and heavy refactoring of actions
1 parent bce8bfb commit 9963bf0

19 files changed

Lines changed: 303 additions & 256 deletions

cmd/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func newHistoryCmd(commandLineOptions *options.CommandLineOption) *cobra.Command
4242
},
4343
RunE: func(cmd *cobra.Command, args []string) error {
4444

45-
err := action.BuildHelmChartClient()
45+
err := action.Setup()
4646

4747
if err != nil {
4848
return err

cmd/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newIndexCmd(commandLineOptions *options.CommandLineOption) *cobra.Command {
3232
Long: "Index of Available Charts Defined in the Cluster",
3333
RunE: func(cmd *cobra.Command, args []string) error {
3434

35-
err := action.BuildHelmChartClient()
35+
err := action.Setup()
3636

3737
if err != nil {
3838
return err

cmd/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func newInstallCmd(commandLineOptions *options.CommandLineOption) *cobra.Command
4949
},
5050
RunE: func(cmd *cobra.Command, args []string) error {
5151

52-
err := action.BuildHelmChartClient()
52+
err := action.Setup()
5353

5454
if err != nil {
5555
return err

cmd/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func newListCmd(commandLineOptions *options.CommandLineOption) *cobra.Command {
3333
Long: `List installed charts`,
3434
RunE: func(cmd *cobra.Command, args []string) error {
3535

36-
err := action.BuildHelmChartClient()
36+
err := action.Setup()
3737

3838
if err != nil {
3939
return err

cmd/rollback.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newRollbackCmd(commandLineOptions *options.CommandLineOption) *cobra.Comman
5353

5454
}
5555

56-
err := action.BuildHelmChartClient()
56+
err := action.Setup()
5757

5858
if err != nil {
5959
return err

cmd/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func newUninstallCmd(commandLineOptions *options.CommandLineOption) *cobra.Comma
4242
},
4343
RunE: func(cmd *cobra.Command, args []string) error {
4444

45-
err := action.BuildHelmChartClient()
45+
err := action.Setup()
4646

4747
if err != nil {
4848
return err

cmd/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func newUpgradeCmd(commandLineOptions *options.CommandLineOption) *cobra.Command
4949
},
5050
RunE: func(cmd *cobra.Command, args []string) error {
5151

52-
err := action.BuildHelmChartClient()
52+
err := action.Setup()
5353

5454
if err != nil {
5555
return err

pkg/action/action.go

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

3+
import (
4+
"fmt"
5+
"net/url"
6+
"strings"
7+
8+
"github.com/redhat-cop/oc-helm/pkg/client"
9+
"github.com/redhat-cop/oc-helm/pkg/options"
10+
)
11+
312
type Action interface {
4-
Run() error
5-
BuildHelmChartClient() error
13+
Setup() error
14+
}
15+
16+
type baseAction struct {
17+
commandLineOptions *options.CommandLineOption
18+
helmChartClient *client.HelmChartClient
19+
openshiftClient *client.OpenShiftClient
20+
}
21+
22+
func (b *baseAction) Setup() error {
23+
24+
if err := b.commandLineOptions.Process(); err != nil {
25+
return err
26+
}
27+
28+
// Attempt to disover console hostname. Otherwise fallback to console-openshift-console.apps
29+
if b.commandLineOptions.ConsoleHostname == "" {
30+
31+
openShiftClient, err := client.NewOpenShiftClient(b.commandLineOptions)
32+
33+
if err != nil {
34+
return err
35+
}
36+
37+
consoleURLConfigMap, err := openShiftClient.DiscoverConsoleURL()
38+
39+
if err != nil {
40+
return err
41+
}
42+
43+
consoleURL, err := extractConsoleURLFromConfigMap(consoleURLConfigMap)
44+
45+
if err == nil {
46+
b.commandLineOptions.ConsoleHostname = consoleURL.Hostname()
47+
} else {
48+
49+
serverURL, err := url.Parse(b.commandLineOptions.Server)
50+
51+
if err != nil {
52+
return err
53+
}
54+
55+
b.commandLineOptions.ConsoleHostname = strings.Replace(serverURL.Hostname(), "api", "console-openshift-console.apps", 1)
56+
}
57+
58+
}
59+
60+
helmChartClient, err := client.NewHelmChartClient(b.commandLineOptions)
61+
62+
if err != nil {
63+
return err
64+
}
65+
66+
b.helmChartClient = helmChartClient
67+
68+
return nil
69+
}
70+
71+
func extractConsoleURLFromConfigMap(configMap map[string]interface{}) (*url.URL, error) {
72+
73+
var consoleURL string
74+
75+
if configMap != nil {
76+
data := configMap["data"].(map[string]interface{})
77+
78+
if data["consoleURL"] != nil {
79+
consoleURL = data["consoleURL"].(string)
80+
}
81+
}
82+
83+
if consoleURL == "" {
84+
return nil, fmt.Errorf("Unable to determine Console URL from ConfigMap")
85+
}
86+
87+
parsedConsoleURL, err := url.Parse(consoleURL)
88+
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
return parsedConsoleURL, nil
694
}

pkg/action/history.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,19 @@ import (
55
"text/tabwriter"
66
"time"
77

8-
"github.com/redhat-cop/oc-helm/pkg/client"
9-
108
"github.com/redhat-cop/oc-helm/pkg/options"
119
)
1210

1311
type HistoryAction struct {
14-
commandLineOptions *options.CommandLineOption
15-
helmChartClient *client.HelmChartClient
12+
baseAction
1613
}
1714

1815
func NewHistoryAction(commandLineOptions *options.CommandLineOption) *HistoryAction {
1916
return &HistoryAction{
20-
commandLineOptions: commandLineOptions,
21-
}
22-
}
23-
24-
func (h *HistoryAction) BuildHelmChartClient() error {
25-
26-
if err := h.commandLineOptions.Process(); err != nil {
27-
return err
17+
baseAction: baseAction{
18+
commandLineOptions: commandLineOptions,
19+
},
2820
}
29-
30-
helmChartClient, err := client.NewHelmChartClient(h.commandLineOptions)
31-
32-
if err != nil {
33-
return err
34-
}
35-
36-
h.helmChartClient = helmChartClient
37-
38-
return nil
39-
4021
}
4122

4223
func (h *HistoryAction) Run(releaseName string) error {

pkg/action/index.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"sort"
66
"text/tabwriter"
77

8-
"github.com/redhat-cop/oc-helm/pkg/client"
98
"github.com/redhat-cop/oc-helm/pkg/types"
109
"github.com/redhat-cop/oc-helm/pkg/utils"
1110

@@ -15,34 +14,17 @@ import (
1514
)
1615

1716
type IndexAction struct {
18-
commandLineOptions *options.CommandLineOption
19-
helmChartClient *client.HelmChartClient
17+
baseAction
2018
}
2119

22-
func NewIndexAction(commandLineOptions *options.CommandLineOption) Action {
20+
func NewIndexAction(commandLineOptions *options.CommandLineOption) *IndexAction {
2321
return &IndexAction{
24-
commandLineOptions: commandLineOptions,
22+
baseAction: baseAction{
23+
commandLineOptions: commandLineOptions,
24+
},
2525
}
2626
}
2727

28-
func (i *IndexAction) BuildHelmChartClient() error {
29-
30-
if err := i.commandLineOptions.Process(); err != nil {
31-
return err
32-
}
33-
34-
helmChartClient, err := client.NewHelmChartClient(i.commandLineOptions)
35-
36-
if err != nil {
37-
return err
38-
}
39-
40-
i.helmChartClient = helmChartClient
41-
42-
return nil
43-
44-
}
45-
4628
func (i *IndexAction) Run() error {
4729

4830
index, err := i.helmChartClient.GetIndex()

0 commit comments

Comments
 (0)