forked from kubernetes/kops
/
create_kubecfg.go
120 lines (97 loc) · 2.25 KB
/
create_kubecfg.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
package kutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
)
type CreateKubecfg struct {
ClusterName string
KeyStore fi.CAStore
SecretStore fi.SecretStore
MasterPublicName string
tmpdir string
}
func (c *CreateKubecfg) WriteKubecfg() error {
if c.tmpdir == "" {
tmpdir, err := ioutil.TempDir("", "k8s")
if err != nil {
return fmt.Errorf("error creating temporary directory: %v", err)
}
c.tmpdir = tmpdir
}
b := &KubeconfigBuilder{}
b.Init()
b.Context = c.ClusterName
var err error
if b.CACert, err = c.copyCertificate(fi.CertificateId_CA); err != nil {
return err
}
if b.KubecfgCert, err = c.copyCertificate("kubecfg"); err != nil {
return err
}
if b.KubecfgKey, err = c.copyPrivateKey("kubecfg"); err != nil {
return err
}
b.KubeMasterIP = c.MasterPublicName
{
secret, err := c.SecretStore.FindSecret("kube")
if err != nil {
return err
}
if secret != nil {
b.KubeUser = "admin"
b.KubePassword = string(secret.Data)
}
}
err = b.WriteKubecfg()
if err != nil {
return err
}
return nil
}
func (c *CreateKubecfg) Close() {
if c.tmpdir != "" {
err := os.RemoveAll(c.tmpdir)
if err != nil {
glog.Warningf("error deleting tempdir %q: %v", c.tmpdir, err)
} else {
c.tmpdir = ""
}
}
}
func (c *CreateKubecfg) copyCertificate(id string) (string, error) {
p := path.Join(c.tmpdir, id+".crt")
cert, err := c.KeyStore.Cert(id)
if err != nil {
return "", fmt.Errorf("error fetching certificate %q: %v", id, err)
}
_, err = writeFile(p, cert)
if err != nil {
return "", fmt.Errorf("error writing certificate %q: %v", id, err)
}
return p, nil
}
func (c *CreateKubecfg) copyPrivateKey(id string) (string, error) {
p := path.Join(c.tmpdir, id+".key")
cert, err := c.KeyStore.PrivateKey(id)
if err != nil {
return "", fmt.Errorf("error fetching private key %q: %v", id, err)
}
_, err = writeFile(p, cert)
if err != nil {
return "", fmt.Errorf("error writing private key %q: %v", id, err)
}
return p, nil
}
func writeFile(dst string, src io.WriterTo) (int64, error) {
f, err := os.Create(dst)
if err != nil {
return 0, fmt.Errorf("error creating file %q: %v", dst, err)
}
defer fi.SafeClose(f)
return src.WriteTo(f)
}