-
Notifications
You must be signed in to change notification settings - Fork 90
/
cluster.go
70 lines (58 loc) · 2.07 KB
/
cluster.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
package k8sutil
import (
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
bootstraputil "k8s.io/cluster-bootstrap/token/util"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
tokenphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/node"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/copycerts"
kubeadmconfig "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
)
// GenerateBootstrapToken will generate a node join token for kubeadm.
// ttl defines the time to live for this token.
func GenerateBootstrapToken(client kubernetes.Interface, ttl time.Duration) (string, error) {
token, err := bootstraputil.GenerateBootstrapToken()
if err != nil {
return "", errors.Wrap(err, "generate kubeadm token")
}
bts, err := kubeadm.NewBootstrapTokenString(token)
if err != nil {
return "", errors.Wrap(err, "new kubeadm token string")
}
duration := &metav1.Duration{Duration: ttl}
if err := tokenphase.UpdateOrCreateTokens(client, false, []kubeadm.BootstrapToken{
{
Token: bts,
TTL: duration,
Usages: []string{"authentication", "signing"},
Groups: []string{kubeadmconstants.NodeBootstrapTokenAuthGroup},
},
}); err != nil {
return "", errors.Wrap(err, "create kubeadm token")
}
return token, nil
}
func UploadCertsWithNewKey() (string, error) {
client, err := GetClientset()
if err != nil {
return "", errors.Wrap(err, "failed to create clientset")
}
config, err := kubeadmconfig.DefaultedInitConfiguration(&kubeadmapiv1beta2.InitConfiguration{}, &kubeadmapiv1beta2.ClusterConfiguration{})
if err != nil {
return "", errors.Wrap(err, "failed to get kotsadm config")
}
key, err := copycerts.CreateCertificateKey()
if err != nil {
return "", errors.Wrap(err, "failed to generate key")
}
config.CertificateKey = key
err = copycerts.UploadCerts(client, config, key)
if err != nil {
return "", errors.Wrap(err, "failed to upload cert")
}
return key, nil
}