-
Notifications
You must be signed in to change notification settings - Fork 787
/
clusters.go
181 lines (158 loc) · 4.74 KB
/
clusters.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package iks
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
ibmcloud "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/helpers"
"github.com/IBM-Cloud/bluemix-go/session"
"github.com/IBM-Cloud/bluemix-go/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type Clusters interface {
GetClusterConfig(name string, target containerv1.ClusterTargetHeader) (string, error)
}
type clusters struct {
*client.Client
}
// This name is the name known to cruiser
type ClusterConfig struct {
ClusterID string `json:"cluster_id"`
ClusterName string `json:"cluster_name"`
ClusterType string `json:"cluster_type"`
ClusterPayTier string `json:"cluster_pay_tier"`
Datacenter string `json:"datacenter"`
AccountID string `json:"account_id"`
Created string `json:"created"`
}
func newClusterAPI(c *client.Client) Clusters {
return &clusters{
Client: c,
}
}
func ComputeClusterConfigDir(name string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
resultDir := filepath.Join(usr.HomeDir, ".bluemix", "plugins", "container-service", "clusters", name)
return resultDir, nil
}
func (r *clusters) GetClusterConfig(name string, target containerv1.ClusterTargetHeader) (string, error) {
rawURL := fmt.Sprintf("/v1/clusters/%s/config", name)
resultDir, err := ComputeClusterConfigDir(name)
if err != nil {
return "", fmt.Errorf("Error computing directory to download the cluster config")
}
err = os.MkdirAll(resultDir, 0755)
if err != nil {
return "", fmt.Errorf("Error creating directory to download the cluster config")
}
downloadPath := filepath.Join(resultDir, "config.zip")
trace.Logger.Println("Will download the kubeconfig at", downloadPath)
var out *os.File
if out, err = os.Create(downloadPath); err != nil {
return "", err
}
defer out.Close() //nolint:errcheck
defer helpers.RemoveFile(downloadPath) //nolint:errcheck
_, err = r.Client.Get(rawURL, out, target.ToMap())
if err != nil {
return "", err
}
trace.Logger.Println("Downloaded the kubeconfig at", downloadPath)
if err = helpers.Unzip(downloadPath, resultDir); err != nil {
return "", err
}
defer helpers.RemoveFilesWithPattern(resultDir, "[^(.yml)|(.pem)]$") //nolint:errcheck
var kubedir, kubeyml string
files, _ := ioutil.ReadDir(resultDir)
for _, f := range files {
if f.IsDir() && strings.HasPrefix(f.Name(), "kube") {
kubedir = filepath.Join(resultDir, f.Name())
files, _ := ioutil.ReadDir(kubedir)
for _, f := range files {
old := filepath.Join(kubedir, f.Name())
new := filepath.Join(kubedir, "../", f.Name())
if strings.HasSuffix(f.Name(), ".yml") {
kubeyml = new
}
err := os.Rename(old, new)
if err != nil {
return "", fmt.Errorf("Couldn't rename: %q", err)
}
}
break
}
}
if kubedir == "" {
return "", errors.New("Unable to locate kube config in zip archive")
}
return filepath.Abs(kubeyml)
}
func GetClusterName() (string, error) {
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
).ConfigAccess().GetStartingConfig()
if err != nil {
return "", err
}
return config.Contexts[config.CurrentContext].Cluster, nil
}
func GetClusterID() (string, error) {
// we can also get this from kubeconfig
//token :=
c := new(ibmcloud.Config)
accountID, err := ConfigFromJSON(c)
if err != nil {
return "", err
}
s, err := session.New(c)
if err != nil {
return "", err
}
clusterAPI, err := containerv1.New(s)
if err != nil {
return "", err
}
clusterIF := clusterAPI.Clusters()
clusterName, err := GetClusterName()
if err != nil {
return "", err
}
target := containerv1.ClusterTargetHeader{
Region: c.Region,
AccountID: accountID,
}
clusterID, err := clusterIF.Find(clusterName, target)
if err != nil {
return "", err
}
return clusterID.ID, nil
}
func GetKubeClusterID(kubeClient kubernetes.Interface) (string, error) {
clusterConfigCM, err := kubeClient.CoreV1().ConfigMaps("kube-system").Get("cluster-info", metav1.GetOptions{})
if err != nil {
return "", err
}
clusterConfig := &ClusterConfig{}
err = json.Unmarshal(clusterConfigCM.BinaryData["cluster-config.json"], clusterConfig)
return clusterConfig.ClusterID, nil
}
func GetKubeClusterRegion(kubeClient kubernetes.Interface) (string, error) {
clusterConfigCM, err := kubeClient.CoreV1().ConfigMaps("kube-system").Get("crn-info-ibmc", metav1.GetOptions{})
if err != nil {
return "", err
}
return clusterConfigCM.Data["CRN_REGION"], nil
}