|
1 | 1 | package action |
2 | 2 |
|
| 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 | + |
3 | 12 | 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 |
6 | 94 | } |
0 commit comments