-
Notifications
You must be signed in to change notification settings - Fork 35
/
token.go
53 lines (47 loc) · 1.22 KB
/
token.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
package util
import (
"bytes"
"compress/gzip"
"encoding/base64"
"io"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// joinEncode compresses and base64 encodes a join token
func joinEncode(in io.Reader) (string, error) {
var outBuf bytes.Buffer
gz, err := gzip.NewWriterLevel(&outBuf, gzip.BestCompression)
if err != nil {
return "", err
}
_, err = io.Copy(gz, in)
gzErr := gz.Close()
if err != nil {
return "", err
}
if gzErr != nil {
return "", gzErr
}
return base64.StdEncoding.EncodeToString(outBuf.Bytes()), nil
}
func CreateK0sJoinToken(caCert []byte, token string, joinURL string, userName string) (string, error) {
const k0sContextName = "k0s"
kubeconfig, err := clientcmd.Write(clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{k0sContextName: {
Server: joinURL,
CertificateAuthorityData: caCert,
}},
Contexts: map[string]*clientcmdapi.Context{k0sContextName: {
Cluster: k0sContextName,
AuthInfo: userName,
}},
CurrentContext: k0sContextName,
AuthInfos: map[string]*clientcmdapi.AuthInfo{userName: {
Token: token,
}},
})
if err != nil {
return "", err
}
return joinEncode(bytes.NewReader(kubeconfig))
}