-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
node_authorizer.go
129 lines (108 loc) · 4.34 KB
/
node_authorizer.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"fmt"
"path"
"path/filepath"
"strings"
"time"
"k8s.io/kops/pkg/systemd"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
"github.com/golang/glog"
)
// NodeAuthorizationBuilder is responsible for node authorization
type NodeAuthorizationBuilder struct {
*NodeupModelContext
}
var _ fi.ModelBuilder = &NodeAuthorizationBuilder{}
// Build is responsible for handling the node authorization client
func (b *NodeAuthorizationBuilder) Build(c *fi.ModelBuilderContext) error {
// @check if we are a master and download the certificates for the node-authozier
if b.UseBootstrapTokens() && b.IsMaster {
name := "node-authorizer"
// creates /src/kubernetes/node-authorizer/{tls,tls-key}.pem
if err := b.BuildCertificatePairTask(c, name, name, "tls"); err != nil {
return err
}
// creates /src/kubernetes/node-authorizer/ca.pem
if err := b.BuildCertificateTask(c, fi.CertificateId_CA, filepath.Join(name, "ca.pem")); err != nil {
return err
}
}
authorizerDir := "node-authorizer"
// @check if bootstrap tokens are enabled and download client certificates for nodes
if b.UseBootstrapTokens() && !b.IsMaster {
if err := b.BuildCertificatePairTask(c, "node-authorizer-client", authorizerDir, "tls"); err != nil {
return err
}
if err := b.BuildCertificateTask(c, fi.CertificateId_CA, authorizerDir+"/ca.pem"); err != nil {
return err
}
}
glog.V(3).Infof("bootstrap: %t, node authorization: %t, node authorizer: %t", b.UseBootstrapTokens(),
b.UseNodeAuthorization(), b.UseNodeAuthorizer())
// @check if the NodeAuthorizer provision the client service for nodes
if b.UseNodeAuthorizer() && !b.IsMaster {
na := b.Cluster.Spec.NodeAuthorization.NodeAuthorizer
glog.V(3).Infof("node authorization service is enabled, authorizer: %s", na.Authorizer)
glog.V(3).Infof("node authorization url: %s", na.NodeURL)
// @step: create the systemd unit to run the node authorization client
man := &systemd.Manifest{}
man.Set("Unit", "Description", "Node Authorization Client")
man.Set("Unit", "Documentation", "https://github.com/kubernetes/kops")
man.Set("Unit", "After", "docker.service")
man.Set("Unit", "Before", "kubelet.service")
clientCert := filepath.Join(b.PathSrvKubernetes(), authorizerDir, "tls.pem")
man.Set("Service", "Type", "oneshot")
man.Set("Service", "RemainAfterExit", "yes")
man.Set("Service", "EnvironmentFile", "/etc/environment")
man.Set("Service", "ExecStartPre", "/usr/bin/mkdir -p /var/lib/kubelet")
man.Set("Service", "ExecStartPre", "/usr/bin/docker pull "+na.Image)
man.Set("Service", "ExecStartPre", "/usr/bin/bash -c 'while [ ! -f "+clientCert+" ]; do sleep 5; done; sleep 5'")
interval := 10 * time.Second
timeout := 5 * time.Minute
// @node: using a string array just to make it easier to read
dockerCmd := []string{
"/usr/bin/docker",
"run",
"--rm",
"--net=host",
"--volume=" + path.Dir(b.KubeletBootstrapKubeconfig()) + ":/var/lib/kubelet",
"--volume=" + filepath.Join(b.PathSrvKubernetes(), authorizerDir) + ":/config:ro",
na.Image,
"client",
"--authorizer=" + na.Authorizer,
"--interval=" + interval.String(),
"--kubeapi-url=" + fmt.Sprintf("https://%s", b.Cluster.Spec.MasterInternalName),
"--kubeconfig=" + b.KubeletBootstrapKubeconfig(),
"--node-url=" + na.NodeURL,
"--timeout=" + timeout.String(),
"--tls-client-ca=/config/ca.pem",
"--tls-cert=/config/tls.pem",
"--tls-private-key=/config/tls-key.pem",
}
man.Set("Service", "ExecStart", strings.Join(dockerCmd, " "))
// @step: add the service task
c.AddTask(&nodetasks.Service{
Name: "node-authorizer.service",
Definition: s(man.Render()),
Enabled: fi.Bool(true),
ManageState: fi.Bool(true),
Running: fi.Bool(true),
SmartRestart: fi.Bool(true),
})
}
return nil
}