/
konnectivity.go
337 lines (302 loc) · 9.57 KB
/
konnectivity.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright 2021 k0s 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 controller
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/k0sproject/k0s/internal/util"
config "github.com/k0sproject/k0s/pkg/apis/v1beta1"
"github.com/k0sproject/k0s/pkg/assets"
"github.com/k0sproject/k0s/pkg/constant"
"github.com/k0sproject/k0s/pkg/kubernetes"
k8sutil "github.com/k0sproject/k0s/pkg/kubernetes"
"github.com/k0sproject/k0s/pkg/supervisor"
)
// Konnectivity implements the component interface of konnectivity server
type Konnectivity struct {
ClusterConfig *config.ClusterConfig
K0sVars constant.CfgVars
LogLevel string
supervisor *supervisor.Supervisor
uid int
// used for lease lock
KubeClientFactory k8sutil.ClientFactory
serverCount int
serverCountChan chan int
stopCtx context.Context
stopFunc context.CancelFunc
log *logrus.Entry
}
// Init ...
func (k *Konnectivity) Init() error {
var err error
k.uid, err = util.GetUID(constant.KonnectivityServerUser)
if err != nil {
logrus.Warning(fmt.Errorf("Running konnectivity as root: %v", err))
}
err = util.InitDirectory(k.K0sVars.KonnectivitySocketDir, 0755)
if err != nil {
return fmt.Errorf("failed to initialize directory %s: %v", k.K0sVars.KonnectivitySocketDir, err)
}
err = os.Chown(k.K0sVars.KonnectivitySocketDir, k.uid, -1)
if err != nil && os.Geteuid() == 0 {
return fmt.Errorf("failed to chown %s: %v", k.K0sVars.KonnectivitySocketDir, err)
}
k.log = logrus.WithFields(logrus.Fields{"component": "konnectivity"})
return assets.Stage(k.K0sVars.BinDir, "konnectivity-server", constant.BinDirMode)
}
// Run ..
func (k *Konnectivity) Run() error {
// Buffered chan to send updates for the count of servers
k.serverCountChan = make(chan int, 1)
k.stopCtx, k.stopFunc = context.WithCancel(context.Background())
go k.runServer()
if k.ClusterConfig.Spec.API.ExternalAddress != "" {
go k.runLeaseCounter()
} else {
// It's a buffered channel so once we start the runServer routine it'll pick this up and just sees it never changing
k.serverCountChan <- 1
}
return k.writeKonnectivityAgent()
}
func (k *Konnectivity) defaultArgs() util.MappedArgs {
serverID, err := util.MachineID()
if err != nil {
logrus.Errorf("failed to fetch server ID for konnectivity-server")
}
return util.MappedArgs{
"--uds-name": filepath.Join(k.K0sVars.KonnectivitySocketDir, "konnectivity-server.sock"),
"--cluster-cert": filepath.Join(k.K0sVars.CertRootDir, "server.crt"),
"--cluster-key": filepath.Join(k.K0sVars.CertRootDir, "server.key"),
"--kubeconfig": k.K0sVars.KonnectivityKubeConfigPath,
"--mode": "grpc",
"--server-port": "0",
"--agent-port": fmt.Sprintf("%d", k.ClusterConfig.Spec.Konnectivity.AgentPort),
"--admin-port": fmt.Sprintf("%d", k.ClusterConfig.Spec.Konnectivity.AdminPort),
"--agent-namespace": "kube-system",
"--agent-service-account": "konnectivity-agent",
"--authentication-audience": "system:konnectivity-server",
"--logtostderr": "true",
"--stderrthreshold": "1",
"--v": k.LogLevel,
"--enable-profiling": "false",
"--server-id": serverID,
}
}
// runs the supervisor and restarts if the calculated server count changes
func (k *Konnectivity) runServer() {
for {
select {
case <-k.stopCtx.Done():
return
case count := <-k.serverCountChan:
// restart only if the count actually changes
if count != k.serverCount {
// Stop supervisor
if k.supervisor != nil {
if err := k.supervisor.Stop(); err != nil {
logrus.Errorf("failed to stop supervisor: %s", err)
// TODO Should we just return? That means other part will continue to run but the server is never properly restarted
}
}
args := k.defaultArgs()
args["--server-count"] = strconv.Itoa(count)
k.supervisor = &supervisor.Supervisor{
Name: "konnectivity",
BinPath: assets.BinPath("konnectivity-server", k.K0sVars.BinDir),
DataDir: k.K0sVars.DataDir,
RunDir: k.K0sVars.RunDir,
Args: args.ToArgs(),
UID: k.uid,
}
err := k.supervisor.Supervise()
if err != nil {
logrus.Errorf("failed to start konnectivity supervisor: %s", err)
k.supervisor = nil // not to make the next loop to try to stop it first
continue
}
k.serverCount = count
}
}
}
}
// Stop stops
func (k *Konnectivity) Stop() error {
if k.stopFunc != nil {
k.stopFunc()
}
if k.supervisor == nil {
return nil
}
return k.supervisor.Stop()
}
type konnectivityAgentConfig struct {
APIAddress string
AgentPort int64
Image string
PullPolicy string
}
func (k *Konnectivity) writeKonnectivityAgent() error {
konnectivityDir := filepath.Join(k.K0sVars.ManifestsDir, "konnectivity")
err := util.InitDirectory(konnectivityDir, constant.ManifestsDirMode)
if err != nil {
return err
}
tw := util.TemplateWriter{
Name: "konnectivity-agent",
Template: konnectivityAgentTemplate,
Data: konnectivityAgentConfig{
APIAddress: k.ClusterConfig.Spec.API.APIAddress(),
AgentPort: k.ClusterConfig.Spec.Konnectivity.AgentPort,
Image: k.ClusterConfig.Spec.Images.Konnectivity.URI(),
PullPolicy: k.ClusterConfig.Spec.Images.DefaultPullPolicy,
},
Path: filepath.Join(konnectivityDir, "konnectivity-agent.yaml"),
}
err = tw.Write()
if err != nil {
return fmt.Errorf("failed to write konnectivity agent manifest: %v", err)
}
return nil
}
func (k *Konnectivity) runLeaseCounter() {
logrus.Infof("starting to count controller lease holders every 10 secs")
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-k.stopCtx.Done():
return
case <-ticker.C:
count, err := k.countLeaseHolders()
if err != nil {
logrus.Errorf("failed to count controller leases: %s", err)
continue
}
k.serverCountChan <- count
}
}
}
func (k *Konnectivity) countLeaseHolders() (int, error) {
client, err := k.KubeClientFactory.GetClient()
if err != nil {
return 0, err
}
ctx, cancel := context.WithTimeout(k.stopCtx, 5*time.Second)
defer cancel()
count := 0
leases, err := client.CoordinationV1().Leases("kube-node-lease").List(ctx, v1.ListOptions{})
if err != nil {
return 0, err
}
for _, l := range leases.Items {
if strings.HasPrefix(l.ObjectMeta.Name, "k0s-ctrl") {
if kubernetes.IsValidLease(l) {
count++
}
}
}
return count, nil
}
const konnectivityAgentTemplate = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:konnectivity-server
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: system:konnectivity-server
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: konnectivity-agent
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: apps/v1
# Alternatively, you can deploy the agents as Deployments. It is not necessary
# to have an agent on each node.
kind: DaemonSet
metadata:
labels:
addonmanager.kubernetes.io/mode: Reconcile
k8s-app: konnectivity-agent
namespace: kube-system
name: konnectivity-agent
spec:
selector:
matchLabels:
k8s-app: konnectivity-agent
template:
metadata:
labels:
k8s-app: konnectivity-agent
spec:
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
containers:
- image: {{ .Image }}
imagePullPolicy: {{ .PullPolicy }}
name: konnectivity-agent
command: ["/proxy-agent"]
args: [
"--logtostderr=true",
"--ca-cert=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
# Since the konnectivity server runs with hostNetwork=true,
# this is the IP address of the master machine.
"--proxy-server-host={{ .APIAddress }}",
"--proxy-server-port={{ .AgentPort }}",
"--service-account-token-path=/var/run/secrets/tokens/konnectivity-agent-token"
]
volumeMounts:
- mountPath: /var/run/secrets/tokens
name: konnectivity-agent-token
livenessProbe:
httpGet:
port: 8093
path: /healthz
initialDelaySeconds: 15
timeoutSeconds: 15
serviceAccountName: konnectivity-agent
volumes:
- name: konnectivity-agent-token
projected:
sources:
- serviceAccountToken:
path: konnectivity-agent-token
audience: system:konnectivity-server
`
// Healthy is a no-op check
func (k *Konnectivity) Healthy() error { return nil }