This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_plane.go
192 lines (159 loc) · 4.43 KB
/
control_plane.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
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"crypto"
"crypto/x509"
"fmt"
"github.com/pkg/errors"
certutil "k8s.io/client-go/util/cert"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmcerts "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
pkiutil "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
"net"
)
const (
APIServerDefaultBindAddress = "127.0.0.1"
APIServerDefaultBindPort = 6443
KubeClusterDefaultCIRD = "10.96.0.0/12"
)
func createPKIAssets(cfg *KubeConfig) error {
cfg.initConfiguration.CertificatesDir = cfg.ClusterConfiguration.CertificatesDir
fmt.Printf(
"[kube-certs-gen] Storing certs to: %s\n",
cfg.initConfiguration.CertificatesDir)
// define kubernetes init configuration
cfg.initConfiguration.LocalAPIEndpoint = kubeadmapi.APIEndpoint{
AdvertiseAddress: APIServerDefaultBindAddress,
BindPort: APIServerDefaultBindPort,
}
cfg.initConfiguration.Networking = kubeadmapi.Networking{
ServiceSubnet: KubeClusterDefaultCIRD,
}
// create sa cert and key
err := kubeadmcerts.CreateServiceAccountKeyAndPublicKeyFiles(
cfg.initConfiguration.CertificatesDir)
if err != nil {
return err
}
certList := kubeadmcerts.GetDefaultCertList()
certTree, err := certList.AsMap().CertTree()
if err != nil {
return err
}
if err := createTree(*cfg, certTree); err != nil {
return errors.Wrap(err, "error creating PKI assets")
}
return nil
}
func createTree(kc KubeConfig, certTree kubeadmcerts.CertificateTree) error {
ic := kc.initConfiguration
for ca, leaves := range certTree {
cfg, err := ca.GetConfig(&ic)
if err != nil {
return err
}
var caKey crypto.Signer
caCert, err := pkiutil.TryLoadCertFromDisk(ic.CertificatesDir, ca.BaseName)
if err == nil {
// Cert exists already, make sure it's valid
if !caCert.IsCA {
return errors.Errorf("certificate %q is not a CA", ca.Name)
}
// Try and load a CA Key
caKey, err = pkiutil.TryLoadKeyFromDisk(ic.CertificatesDir, ca.BaseName)
if err != nil {
return err
}
} else {
// CACert doesn't already exist, create a new cert and key.
caCert, caKey, err = pkiutil.NewCertificateAuthority(cfg)
if err != nil {
return err
}
err = writeCertificateAuthorityFilesIfNotExist(
ic.CertificatesDir,
ca.BaseName,
caCert,
caKey,
)
if err != nil {
return err
}
}
for _, leaf := range leaves {
if err := createFromCA(leaf, kc, caCert, caKey); err != nil {
return err
}
}
}
return nil
}
// CreateFromCA makes and writes a certificate using the given CA cert and key.
func createFromCA(k *kubeadmcerts.KubeadmCert, kc KubeConfig, caCert *x509.Certificate, caKey crypto.Signer) error {
ic := kc.initConfiguration
cfg, err := k.GetConfig(&ic)
if err != nil {
return errors.Wrapf(err, "couldn't create %q certificate", k.Name)
}
switch k.Name {
case "apiserver":
err = generateMultipleCertAndKey(k, ic, *cfg, kc.APIServer.Servers, caCert, caKey)
case "etcd-server":
err = generateMultipleCertAndKey(k, ic, *cfg, kc.Etcd.Servers, caCert, caKey)
case "etcd-peer":
err = generateMultipleCertAndKey(k, ic, *cfg, kc.Etcd.Peers, caCert, caKey)
default:
err = generateCertAndKey(k, ic, cfg, caCert, caKey)
}
return err
}
func generateMultipleCertAndKey(
k *kubeadmcerts.KubeadmCert,
ic kubeadmapi.InitConfiguration,
cfg certutil.Config,
servers []Server,
caCert *x509.Certificate,
caKey crypto.Signer) error {
for _, server := range servers {
altNames := cfg.AltNames
altNames.DNSNames = append(altNames.DNSNames, server.Certs.SANs...)
for _, ip := range server.Certs.IPs {
ip := net.ParseIP(ip)
if ip == nil {
return fmt.Errorf("incorrect ip address: %s", ip)
}
altNames.IPs = append(altNames.IPs, ip)
}
cc := cfg
cc.AltNames = altNames
cc.CommonName = server.Name
cc.AltNames.DNSNames[0] = server.Name
k.BaseName = fmt.Sprintf("%s-%s", k.BaseName, server.Name)
if err := generateCertAndKey(k, ic, &cc, caCert, caKey); err != nil {
return err
}
}
return nil
}
func generateCertAndKey(
k *kubeadmcerts.KubeadmCert,
ic kubeadmapi.InitConfiguration,
cfg *certutil.Config,
caCert *x509.Certificate,
caKey crypto.Signer) error {
cert, key, err := pkiutil.NewCertAndKey(caCert, caKey, cfg)
if err != nil {
return err
}
err = writeCertificateFilesIfNotExist(
ic.CertificatesDir,
k.BaseName,
caCert,
cert,
key,
cfg,
)
if err != nil {
return errors.Wrapf(err, "failed to write or validate certificate %q", k.Name)
}
return nil
}