-
Notifications
You must be signed in to change notification settings - Fork 14
/
apiserver_boot.go
317 lines (281 loc) · 8.5 KB
/
apiserver_boot.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package k8s
import (
"context"
"crypto/md5"
"fmt"
"strings"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op"
"github.com/cybozu-go/cke/op/common"
)
var (
// admissionPlugins is the recommended list of admission plugins.
// https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#is-there-a-recommended-set-of-admission-controllers-to-use
admissionPlugins = []string{
"NamespaceLifecycle",
"LimitRanger",
"ServiceAccount",
"Priority",
"DefaultTolerationSeconds",
"DefaultStorageClass",
"PersistentVolumeClaimResize",
"MutatingAdmissionWebhook",
"ValidatingAdmissionWebhook",
"ResourceQuota",
"StorageObjectInUseProtection",
// NodeRestriction is not in the list above, but added to restrict kubelet privilege.
"NodeRestriction",
}
)
const auditPolicyBasePath = "/etc/kubernetes/apiserver/audit-policy-%x.yaml"
type apiServerBootOp struct {
nodes []*cke.Node
cps []*cke.Node
serviceSubnet string
domain string
params cke.APIServerParams
step int
files *common.FilesBuilder
}
// APIServerBootOp returns an Operator to bootstrap kube-apiserver
func APIServerBootOp(nodes, cps []*cke.Node, serviceSubnet, domain string, params cke.APIServerParams) cke.Operator {
return &apiServerBootOp{
nodes: nodes,
cps: cps,
serviceSubnet: serviceSubnet,
domain: domain,
params: params,
files: common.NewFilesBuilder(nodes),
}
}
func (o *apiServerBootOp) Name() string {
return "kube-apiserver-bootstrap"
}
func (o *apiServerBootOp) NextCommand() cke.Commander {
switch o.step {
case 0:
o.step++
return common.ImagePullCommand(o.nodes, cke.KubernetesImage)
case 1:
o.step++
return common.MakeDirsCommandWithMode(o.nodes, []string{encryptionConfigDir}, "700")
case 2:
o.step++
return prepareAPIServerFilesCommand{o.files, o.serviceSubnet, o.domain, o.params}
case 3:
o.step++
return o.files
case 4:
o.step++
opts := []string{
"--mount", "type=tmpfs,dst=/run/kubernetes",
}
paramsMap := make(map[string]cke.ServiceParams)
for _, n := range o.nodes {
paramsMap[n.Address] = APIServerParams(o.cps, n.Address, o.serviceSubnet, o.params.AuditLogEnabled, o.params.AuditLogPolicy)
}
return common.RunContainerCommand(o.nodes, op.KubeAPIServerContainerName, cke.KubernetesImage,
common.WithOpts(opts),
common.WithParamsMap(paramsMap),
common.WithExtra(o.params.ServiceParams))
default:
return nil
}
}
func (o *apiServerBootOp) Targets() []string {
ips := make([]string, len(o.nodes))
for i, n := range o.nodes {
ips[i] = n.Address
}
return ips
}
type prepareAPIServerFilesCommand struct {
files *common.FilesBuilder
serviceSubnet string
domain string
params cke.APIServerParams
}
func (c prepareAPIServerFilesCommand) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
storage := inf.Storage()
// server (and client) certs of API server.
f := func(ctx context.Context, n *cke.Node) (cert, key []byte, err error) {
c, k, e := cke.KubernetesCA{}.IssueForAPIServer(ctx, inf, n, c.serviceSubnet, c.domain)
if e != nil {
return nil, nil, e
}
return []byte(c), []byte(k), nil
}
err := c.files.AddKeyPair(ctx, op.K8sPKIPath("apiserver"), f)
if err != nil {
return err
}
// client certs for etcd auth.
f = func(ctx context.Context, n *cke.Node) (cert, key []byte, err error) {
c, k, e := cke.EtcdCA{}.IssueForAPIServer(ctx, inf, n)
if e != nil {
return nil, nil, e
}
return []byte(c), []byte(k), nil
}
err = c.files.AddKeyPair(ctx, op.K8sPKIPath("apiserver-etcd-client"), f)
if err != nil {
return err
}
// CA of k8s cluster.
ca, err := storage.GetCACertificate(ctx, "kubernetes")
if err != nil {
return err
}
caData := []byte(ca)
g := func(ctx context.Context, n *cke.Node) ([]byte, error) {
return caData, nil
}
err = c.files.AddFile(ctx, op.K8sPKIPath("ca.crt"), g)
if err != nil {
return err
}
// CA of etcd server.
etcdCA, err := storage.GetCACertificate(ctx, "server")
if err != nil {
return err
}
etcdCAData := []byte(etcdCA)
g = func(ctx context.Context, n *cke.Node) ([]byte, error) {
return etcdCAData, nil
}
err = c.files.AddFile(ctx, op.K8sPKIPath("etcd-ca.crt"), g)
if err != nil {
return err
}
// ServiceAccount cert.
saCert, err := storage.GetServiceAccountCert(ctx)
if err != nil {
return err
}
saCertData := []byte(saCert)
g = func(ctx context.Context, n *cke.Node) ([]byte, error) {
return saCertData, nil
}
err = c.files.AddFile(ctx, op.K8sPKIPath("service-account.crt"), g)
if err != nil {
return err
}
// Aggregation cert.
agCert, err := storage.GetCACertificate(ctx, "kubernetes-aggregation")
if err != nil {
return err
}
agCertData := []byte(agCert)
g = func(ctx context.Context, n *cke.Node) ([]byte, error) {
return agCertData, nil
}
err = c.files.AddFile(ctx, op.K8sPKIPath("aggregation-ca.crt"), g)
if err != nil {
return err
}
// client certs for Aggregation
f = func(ctx context.Context, n *cke.Node) (cert, key []byte, err error) {
c, k, e := cke.AggregationCA{}.IssueClientCertificate(ctx, inf)
if e != nil {
return nil, nil, e
}
return []byte(c), []byte(k), nil
}
err = c.files.AddKeyPair(ctx, op.K8sPKIPath("aggregation"), f)
if err != nil {
return err
}
// EncryptionConfiguration
enccfg, err := getEncryptionConfiguration(ctx, inf)
if err != nil {
return err
}
enccfgData, err := encodeToYAML(enccfg)
if err != nil {
return err
}
err = c.files.AddFile(ctx, encryptionConfigFile, func(ctx context.Context, node *cke.Node) ([]byte, error) {
return enccfgData, nil
})
if err != nil {
return err
}
// audit log policy
if c.params.AuditLogEnabled {
return c.files.AddFile(ctx, auditPolicyFilePath(c.params.AuditLogPolicy), func(context.Context, *cke.Node) ([]byte, error) {
return []byte(c.params.AuditLogPolicy), nil
})
}
return nil
}
func (c prepareAPIServerFilesCommand) Command() cke.Command {
return cke.Command{
Name: "prepare-apiserver-files",
}
}
func auditPolicyFilePath(policy string) string {
return fmt.Sprintf(auditPolicyBasePath, md5.Sum([]byte(policy)))
}
// APIServerParams returns parameters for API server.
func APIServerParams(controlPlanes []*cke.Node, advertiseAddress, serviceSubnet string, auditLogEnabeled bool, auditLogPolicy string) cke.ServiceParams {
args := []string{
"kube-apiserver",
"--allow-privileged",
"--etcd-servers=https://127.0.0.1:12379",
"--etcd-cafile=" + op.K8sPKIPath("etcd-ca.crt"),
"--etcd-certfile=" + op.K8sPKIPath("apiserver-etcd-client.crt"),
"--etcd-keyfile=" + op.K8sPKIPath("apiserver-etcd-client.key"),
// disable compaction by apisever as it cannot do it.
"--etcd-compaction-interval=0",
"--bind-address=0.0.0.0",
"--insecure-port=0",
"--client-ca-file=" + op.K8sPKIPath("ca.crt"),
"--tls-cert-file=" + op.K8sPKIPath("apiserver.crt"),
"--tls-private-key-file=" + op.K8sPKIPath("apiserver.key"),
"--kubelet-certificate-authority=" + op.K8sPKIPath("ca.crt"),
"--kubelet-client-certificate=" + op.K8sPKIPath("apiserver.crt"),
"--kubelet-client-key=" + op.K8sPKIPath("apiserver.key"),
"--kubelet-https=true",
"--enable-admission-plugins=" + strings.Join(admissionPlugins, ","),
// for service accounts
"--service-account-key-file=" + op.K8sPKIPath("service-account.crt"),
"--service-account-lookup",
// for aggregation
"--requestheader-client-ca-file=" + op.K8sPKIPath("aggregation-ca.crt"),
"--requestheader-allowed-names=" + cke.CNAPIServer,
"--requestheader-extra-headers-prefix=X-Remote-Extra-",
"--requestheader-group-headers=X-Remote-Group",
"--requestheader-username-headers=X-Remote-User",
"--proxy-client-cert-file=" + op.K8sPKIPath("aggregation.crt"),
"--proxy-client-key-file=" + op.K8sPKIPath("aggregation.key"),
"--authorization-mode=Node,RBAC",
"--advertise-address=" + advertiseAddress,
// See https://github.com/cybozu-go/neco/issues/397
"--endpoint-reconciler-type=none",
"--service-cluster-ip-range=" + serviceSubnet,
"--encryption-provider-config=" + encryptionConfigFile,
}
if auditLogEnabeled {
args = append(args, "--audit-log-path=-")
args = append(args, "--audit-policy-file="+auditPolicyFilePath(auditLogPolicy))
}
return cke.ServiceParams{
ExtraArguments: args,
ExtraBinds: []cke.Mount{
{
Source: "/etc/machine-id",
Destination: "/etc/machine-id",
ReadOnly: true,
Propagation: "",
Label: "",
},
{
Source: "/etc/kubernetes",
Destination: "/etc/kubernetes",
ReadOnly: true,
Propagation: "",
Label: cke.LabelShared,
},
},
}
}