-
Notifications
You must be signed in to change notification settings - Fork 372
/
metricserver.go
375 lines (346 loc) · 9 KB
/
metricserver.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright 2022 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"
"math"
"path"
"path/filepath"
"time"
"github.com/k0sproject/k0s/pkg/component"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/templatewriter"
"github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/v1beta1"
"github.com/k0sproject/k0s/pkg/constant"
k8sutil "github.com/k0sproject/k0s/pkg/kubernetes"
)
const metricServerTemplate = `
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
name: system:aggregated-metrics-reader
rules:
- apiGroups:
- metrics.k8s.io
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
- configmaps
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=10250
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
image: {{ .Image }}
imagePullPolicy: {{ .PullPolicy }}
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 10250
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 10
resources:
requests:
memory: {{ .MEMRequest }}
cpu: {{ .CPURequest }}
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: "NoSchedule"
priorityClassName: system-cluster-critical
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 100
`
// MetricServer is the reconciler implementation for metrics server
type MetricServer struct {
log logrus.FieldLogger
K0sVars constant.CfgVars
kubeClientFactory k8sutil.ClientFactoryInterface
clusterConfig *v1beta1.ClusterConfig
tickerDone context.CancelFunc
}
type metricsConfig struct {
Image string
PullPolicy string
CPURequest string
MEMRequest string
}
var _ component.Component = (*MetricServer)(nil)
var _ component.ReconcilerComponent = (*MetricServer)(nil)
// NewMetricServer creates new MetricServer reconciler
func NewMetricServer(k0sVars constant.CfgVars, kubeClientFactory k8sutil.ClientFactoryInterface) *MetricServer {
return &MetricServer{
log: logrus.WithFields(logrus.Fields{"component": "metricServer"}),
K0sVars: k0sVars,
kubeClientFactory: kubeClientFactory,
}
}
// Init does nothing
func (m *MetricServer) Init(_ context.Context) error {
return nil
}
// Run runs the metric server reconciler
func (m *MetricServer) Run(ctx context.Context) error {
ctx, m.tickerDone = context.WithCancel(ctx)
msDir := path.Join(m.K0sVars.ManifestsDir, "metricserver")
err := dir.Init(msDir, constant.ManifestsDirMode)
if err != nil {
return err
}
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
previousConfig := metricsConfig{}
for {
select {
case <-ticker.C:
newConfig, err := m.getConfig(ctx)
if err != nil {
m.log.Warnf("failed to calculate metrics-server config: %s", err.Error())
continue
}
if previousConfig == newConfig {
continue
}
tw := templatewriter.TemplateWriter{
Name: "metricServer",
Template: metricServerTemplate,
Data: newConfig,
Path: filepath.Join(msDir, "metric_server.yaml"),
}
err = tw.Write()
if err != nil {
m.log.Errorf("error writing metric server manifests: %s. will retry", err.Error())
continue
}
previousConfig = newConfig
case <-ctx.Done():
m.log.Info("metric server reconciler done")
return
}
}
}()
return nil
}
// Stop stops the reconciler
func (m *MetricServer) Stop() error {
if m.tickerDone != nil {
m.tickerDone()
}
return nil
}
// Reconcile detects changes in configuration and applies them to the component
func (m *MetricServer) Reconcile(_ context.Context, clusterConfig *v1beta1.ClusterConfig) error {
logrus.Debug("reconcile method called for: MetricServer")
// We just store the last known config, the main reconciler ticker will reconcile config based on number of nodes etc.
m.clusterConfig = clusterConfig
return nil
}
// Healthy is the health-check interface
func (m *MetricServer) Healthy() error { return nil }
// Mostly for calculating the resource needs based on node numbers. From https://github.com/kubernetes-sigs/metrics-server#scaling :
// Starting from v0.5.0 Metrics Server comes with default resource requests that should guarantee good performance for most cluster configurations up to 100 nodes:
// - 100m core of CPU
// - 300MiB of memory
// So that's 10m CPU and 30MiB mem per 10 nodes
func (m *MetricServer) getConfig(ctx context.Context) (metricsConfig, error) {
if m.clusterConfig == nil {
return metricsConfig{}, fmt.Errorf("cluster config not available yet")
}
cfg := metricsConfig{
Image: m.clusterConfig.Spec.Images.MetricsServer.URI(),
PullPolicy: m.clusterConfig.Spec.Images.DefaultPullPolicy,
}
kubeClient, err := m.kubeClientFactory.GetClient()
if err != nil {
return cfg, err
}
nodeList, err := kubeClient.CoreV1().Nodes().List(ctx, v1.ListOptions{})
if err != nil {
return cfg, err
}
scale := math.Ceil(float64(len(nodeList.Items)) / 10.0)
if scale < 1 {
scale = 1
}
memRequest := int(30 * scale)
cpuRequest := int(10 * scale)
cfg.MEMRequest = fmt.Sprintf("%dM", memRequest)
cfg.CPURequest = fmt.Sprintf("%dm", cpuRequest)
return cfg, nil
}