/
kubeconfig.go
152 lines (123 loc) · 3.61 KB
/
kubeconfig.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
package aws
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var kubeClient *kubernetes.Clientset
var cacheClientsetOnce sync.Once
var cacheConfigPathOnce sync.Once
var (
rootPath = "/opt/datacol"
kcpath = filepath.Join(rootPath, "kubeconfig")
pemPathRE = filepath.Join(rootPath, "%s.pem")
privateIpAttr = "MasterPrivateIp"
bastionIpAttr = "BastionHostPublicIp"
scpCmd = "scp -i %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@%s:~/kubeconfig %s"
s3StateStoreMatcher = regexp.MustCompile(`s3://(\S+)/cluster-info.yaml`)
)
func (p *AwsCloud) kubeClient() *kubernetes.Clientset {
cacheConfigPathOnce.Do(func() {
p.K8sConfigPath()
})
cacheClientsetOnce.Do(func() {
kube, err := getKubeClientSet(p.DeploymentName)
if err != nil {
log.Fatal(err)
}
kubeClient = kube
})
return kubeClient
}
// fetchClusterInfoFromS3 tries to fetch cluster state from s3. Currently not being used
// since the cluster-info.yaml doesn't contain user credentials
func (p *AwsCloud) fetchClusterInfoFromS3(path string) (cached bool, err error) {
value, err := p.stackOutputValue("JoinNodes")
log.Warn(err)
if value != "" {
log.Debugf("Found JoinNodes Output: %s", value)
if match := s3StateStoreMatcher.FindStringSubmatch(value); len(match) == 2 {
s3bucket := match[1]
buff := &aws.WriteAtBuffer{}
s3dl := s3manager.NewDownloader(session.New())
if _, err = s3dl.Download(buff, &s3.GetObjectInput{
Bucket: aws.String(s3bucket),
Key: aws.String("cluster-info.yaml"),
}); err != nil {
return
}
err = ioutil.WriteFile(path, buff.Bytes(), 0644)
if err == nil {
cached = true
}
}
}
return
}
func (p *AwsCloud) K8sConfigPath() (string, error) {
if _, err := os.Stat(kcpath); err != nil {
if os.IsNotExist(err) {
// if fetched, err := p.fetchClusterInfoFromS3(kcpath); fetched {
// log.Infof("cached the %s from s3 state store. err:%v", kcpath, err)
// return kcpath, nil
// }
ipAddr, err := p.masterPrivateIp()
if err != nil {
return ipAddr, err
}
keyname := fmt.Sprintf(pemPathRE, os.Getenv("DATACOL_KEY_NAME"))
cmd := fmt.Sprintf(scpCmd, keyname, ipAddr, kcpath)
log.Debugf("Executing %s", cmd)
if _, err := exec.Command("/bin/sh", "-c", cmd).Output(); err != nil {
return "", err
}
} else {
return kcpath, err
}
}
return kcpath, nil
}
func (p *AwsCloud) bastionHostIp() (string, error) {
return p.stackOutputValue(bastionIpAttr)
}
func (p *AwsCloud) stackOutputValue(attr string) (string, error) {
s, err := p.describeStack("")
if err != nil {
return "", err
}
for _, o := range s.Outputs {
if o.OutputKey != nil && attr == *o.OutputKey {
return *o.OutputValue, nil
}
}
return "", fmt.Errorf("unable to find %s from stack output", attr)
}
func (p *AwsCloud) masterPrivateIp() (string, error) {
return p.stackOutputValue(privateIpAttr)
}
func getKubeClientSet(name string) (*kubernetes.Clientset, error) {
config, err := getKubeClientConfig(name)
if err != nil {
return nil, err
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("cluster connection %v", err)
}
return c, nil
}
func getKubeClientConfig(name string) (*rest.Config, error) {
return clientcmd.BuildConfigFromFlags("", kcpath)
}