Repository navigation
Expand file tree
/
Copy pathcluster.go
More file actions
109 lines (92 loc) · 3.65 KB
/
Copy pathcluster.go
File metadata and controls
109 lines (92 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package utils
import (
"fmt"
"net/url"
"regexp"
logger "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"github.com/iamkirkbater/backplane-cli/pkg/cli/config"
)
type BackplaneCluster struct {
ClusterID string
ClusterURL string // for e.g. https://api-backplane.apps.com/backplane/cluster/<cluster-id>/
BackplaneHost string // for e.g. https://api-backplane.apps.com
}
type ClusterUtils interface {
GetClusterIDAndHostFromClusterURL(clusterURL string) (string, string, error)
GetBackplaneClusterFromConfig() (BackplaneCluster, error)
GetBackplaneClusterFromClusterKey(clusterKey string) (BackplaneCluster, error)
GetBackplaneCluster(params ...string) (BackplaneCluster, error)
}
type DefaultClusterUtilsImpl struct{}
var (
DefaultClusterUtils ClusterUtils = &DefaultClusterUtilsImpl{}
)
// GetClusterIDAndHostFromClusterURL with Cluster URL format: https://api-backplane.apps.com/backplane/cluster/<cluster-id>/
func (s *DefaultClusterUtilsImpl) GetClusterIDAndHostFromClusterURL(clusterURL string) (string, string, error) {
parsedURL, err := url.Parse(clusterURL)
if err != nil {
return "", "", err
}
backplaneHost := "https://" + parsedURL.Host
re := regexp.MustCompile(ClusterIDRegexp)
matches := re.FindStringSubmatch(parsedURL.Path)
if len(matches) < 2 {
return "", backplaneHost, fmt.Errorf("couldn't find cluster-id from the backplane cluster url")
}
clusterID := matches[1] // first capturing group
return clusterID, backplaneHost, nil
}
// GetBackplaneClusterFromConfig get the backplane cluster from config file
func (s *DefaultClusterUtilsImpl) GetBackplaneClusterFromConfig() (BackplaneCluster, error) {
logger.Debugln("Finding target cluster from kube config")
cfg, err := clientcmd.BuildConfigFromFlags("", clientcmd.NewDefaultPathOptions().GetDefaultFilename())
if err != nil {
return BackplaneCluster{}, err
}
clusterID, backplaneHost, err := s.GetClusterIDAndHostFromClusterURL(cfg.Host)
if err != nil {
return BackplaneCluster{}, err
}
cluster := BackplaneCluster{
ClusterID: clusterID,
BackplaneHost: backplaneHost,
ClusterURL: cfg.Host,
}
logger.WithFields(logger.Fields{
"ClusterID": cluster.ClusterID,
"BackplaneHost": cluster.BackplaneHost,
"ClusterURL": cluster.ClusterURL}).Debugln("Found target cluster")
return cluster, nil
}
// GetBackplaneClusterFromClusterKey get the backplane cluster from the given cluster
func (s *DefaultClusterUtilsImpl) GetBackplaneClusterFromClusterKey(clusterKey string) (BackplaneCluster, error) {
logger.WithField("SearchKey", clusterKey).Debugln("Finding target cluster")
clusterID, clusterName, err := DefaultOCMInterface.GetTargetCluster(clusterKey)
if err != nil {
return BackplaneCluster{}, err
}
bpConfig, err := config.GetBackplaneConfiguration()
backplaneURL := bpConfig.URL
if err != nil {
return BackplaneCluster{}, err
}
cluster := BackplaneCluster{
ClusterID: clusterID,
BackplaneHost: backplaneURL,
ClusterURL: fmt.Sprintf("%s/backplane/cluster/%s", backplaneURL, clusterID),
}
logger.WithFields(logger.Fields{
"ClusterID": cluster.ClusterID,
"BackplaneHost": cluster.BackplaneHost,
"ClusterURL": cluster.ClusterURL,
"Name": clusterName}).Debugln("Found target cluster")
return cluster, nil
}
// GetBackplaneCluster returns BackplaneCluster, if clusterKey is present it will try to search for cluster otherwise it will load cluster from the kube config file.
func (s *DefaultClusterUtilsImpl) GetBackplaneCluster(params ...string) (BackplaneCluster, error) {
if len(params) > 0 && params[0] != "" {
return s.GetBackplaneClusterFromClusterKey(params[0])
}
return s.GetBackplaneClusterFromConfig()
}