This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcomponent.go
More file actions
340 lines (291 loc) · 9.93 KB
/
component.go
File metadata and controls
340 lines (291 loc) · 9.93 KB
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
// Copyright 2020 The Lokomotive 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 clusterautoscaler
import (
"encoding/base64"
"fmt"
"os"
"strings"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
)
const name = "cluster-autoscaler"
const chartValuesTmpl = `
cloudProvider: {{ .Provider }}
image:
tag: v1.17.0
nodeSelector:
node.kubernetes.io/controller: "true"
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Exists
rbac:
create: true
cloudConfigPath: /config
packetClusterName: {{ .ClusterName }}
packetAuthToken: {{ .Packet.AuthToken }}
packetCloudInit: {{ .Packet.UserData }}
packetProjectID: {{ .Packet.ProjectID }}
packetFacility: {{ .Packet.Facility }}
packetOSChannel: {{ .Packet.WorkerChannel }}
packetNodeType: {{ .Packet.WorkerType }}
autoscalingGroups:
- name: {{ .WorkerPool }}
maxSize: {{ .MaxWorkers }}
minSize: {{ .MinWorkers }}
extraArgs:
scale-down-unneeded-time: {{ .ScaleDownUnneededTime }}
scale-down-delay-after-add: {{ .ScaleDownDelayAfterAdd }}
scale-down-unready-time: {{ .ScaleDownUnreadyTime }}
podDisruptionBudget: []
kubeTargetVersionOverride: v1.17.2
{{ if .ServiceMonitor }}
serviceMonitor:
enabled: true
namespace: {{ .Namespace }}
selector:
release: prometheus-operator
{{ end }}
`
func init() {
components.Register(name, newComponent())
}
type component struct {
// required parameters
Provider string `hcl:"provider,optional"`
WorkerPool string `hcl:"worker_pool,optional"`
ClusterName string `hcl:"cluster_name,optional"`
// optional parameters
Namespace string `hcl:"namespace,optional"`
MinWorkers int `hcl:"min_workers,optional"`
MaxWorkers int `hcl:"max_workers,optional"`
ScaleDownUnneededTime time.Duration
ScaleDownUnneededTimeRaw string `hcl:"scale_down_unneeded_time,optional"`
ScaleDownDelayAfterAdd time.Duration
ScaleDownDelayAfterAddRaw string `hcl:"scale_down_delay_after_add,optional"`
ScaleDownUnreadyTime time.Duration
ScaleDownUnreadyTimeRaw string `hcl:"scale_down_unready_time,optional"`
// Prometheus Operator related parameters.
ServiceMonitor bool `hcl:"service_monitor,optional"`
// Packet-specific parameters
Packet *packetConfiguration `hcl:"packet,block"`
}
type packetConfiguration struct {
// required parameters
ProjectID string `hcl:"project_id,optional"`
Facility string `hcl:"facility,optional"`
// optional parameters
WorkerType string `hcl:"worker_type,optional"`
WorkerChannel string `hcl:"worker_channel,optional"`
UserData string
AuthToken string
}
func newComponent() *component {
c := &component{
Provider: "packet",
Namespace: "kube-system",
MinWorkers: 1,
MaxWorkers: 4,
ScaleDownUnneededTime: 10 * time.Minute,
ScaleDownDelayAfterAdd: 10 * time.Minute,
ScaleDownUnreadyTime: 20 * time.Minute,
ServiceMonitor: false,
}
switch c.Provider {
case "packet":
c.Packet = &packetConfiguration{
WorkerType: "c3.small.x86",
WorkerChannel: "stable",
}
}
return c
}
// getWorkerUserdata finds a worker from clusterName in facility given a list
// of devices in a project and returns its user data. If two devices with the
// same name are found it returns an error.
func getWorkerUserdata(clusterName, facility string, devices []packngo.Device) (string, error) {
var userData string
deviceSet := make(map[string]struct{})
for _, d := range devices {
if d.Facility.Code != facility {
continue
}
if _, ok := deviceSet[d.Hostname]; !ok {
deviceSet[d.Hostname] = struct{}{}
} else {
return "", fmt.Errorf("having two devices with the same name (%q) in the same facility is not supported", d.Hostname)
}
// if device hostname contains the cluster name and "worker", we want
// its user data
if strings.Contains(d.Hostname, clusterName) &&
strings.Contains(d.Hostname, "worker") {
userData = base64.StdEncoding.EncodeToString([]byte(d.UserData))
}
}
if userData == "" {
return "", fmt.Errorf("cluster %q must have at least one worker node but no worker was found", clusterName)
}
return userData, nil
}
// parseDurations takes the raw string time parameters from component and sets
// parsed time.Duration parameters.
func (c *component) parseDurations() hcl.Diagnostics {
var (
err error
diagnostics hcl.Diagnostics
)
if c.ScaleDownUnneededTimeRaw != "" {
c.ScaleDownUnneededTime, err = time.ParseDuration(c.ScaleDownUnneededTimeRaw)
if err != nil {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "error parsing 'scale_down_unneeded_time'",
Detail: fmt.Sprintf("error parsing 'scale_down_unneeded_time': %v", err),
})
}
}
if c.ScaleDownDelayAfterAddRaw != "" {
c.ScaleDownDelayAfterAdd, err = time.ParseDuration(c.ScaleDownDelayAfterAddRaw)
if err != nil {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "error parsing 'scale_down_delay_after_add'",
Detail: fmt.Sprintf("error parsing 'scale_down_delay_after_add': %v", err),
})
}
}
if c.ScaleDownUnreadyTimeRaw != "" {
c.ScaleDownUnreadyTime, err = time.ParseDuration(c.ScaleDownUnreadyTimeRaw)
if err != nil {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "error parsing 'scale_down_unready_time'",
Detail: fmt.Sprintf("error parsing 'scale_down_unready_time': %v", err),
})
}
}
return diagnostics
}
func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics {
diagnostics := hcl.Diagnostics{}
// If config is not defined at all, replace it with just empty struct, so we can
// deserialize it and proceed
if configBody == nil {
// Perhaps we can skip this error?
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "component requires configuration",
Detail: "component has required fields in its configuration, so configuration block must be created",
})
emptyConfig := hcl.EmptyBody()
configBody = &emptyConfig
}
if err := gohcl.DecodeBody(*configBody, evalContext, c); err != nil {
diagnostics = append(diagnostics, err...)
}
// work around HCL not supporting time.Duration values
diagnostics = append(diagnostics, c.parseDurations()...)
switch c.Provider {
case "packet":
diagnostics = c.validatePacket(diagnostics)
default:
// Slice can't be constant, so just use a variable
supportedProviders := []string{"packet"}
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Make sure to set provider to one of supported values",
Detail: fmt.Sprintf("provider must be one of: '%s'", strings.Join(supportedProviders[:], "', '")),
})
}
if c.WorkerPool == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'worker_pool' must be set",
Detail: "'worker_pool' must be set but it was not found",
})
}
if c.ClusterName == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'cluster_name' must be set",
Detail: "'cluster_name' must be set but it was not found",
})
}
return diagnostics
}
func (c *component) validatePacket(diagnostics hcl.Diagnostics) hcl.Diagnostics {
if c.Packet == nil {
c.Packet = &packetConfiguration{}
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'packet' block must exist",
Detail: "When using Packet provider, 'packet' block must exist",
})
}
if c.Packet.ProjectID == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'project_id' must be set",
Detail: "When using Packet provider, 'project_id' must be set but it was not found",
})
}
if c.Packet.Facility == "" {
diagnostics = append(diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "'facility' must be set",
Detail: "When using Packet provider, 'facility' must be set but it was not found",
})
}
return diagnostics
}
func (c *component) RenderManifests() (map[string]string, error) {
helmChart, err := util.LoadChartFromAssets(fmt.Sprintf("/components/%s", name))
if err != nil {
return nil, errors.Wrap(err, "load chart from assets")
}
if c.Provider == "packet" {
cl, err := packngo.NewClient()
if err != nil {
return nil, errors.Wrap(err, "create packet API client")
}
devices, _, err := cl.Devices.List(c.Packet.ProjectID, nil)
if err != nil {
return nil, errors.Wrapf(err, "listing devices in project %q", c.Packet.ProjectID)
}
userData, err := getWorkerUserdata(c.ClusterName, c.Packet.Facility, devices)
if err != nil {
return nil, errors.Wrapf(err, "getting worker data for cluster %q", c.ClusterName)
}
c.Packet.UserData = userData
c.Packet.AuthToken = base64.StdEncoding.EncodeToString([]byte(os.Getenv("PACKET_AUTH_TOKEN")))
}
values, err := template.Render(chartValuesTmpl, c)
if err != nil {
return nil, errors.Wrap(err, "render chart values template")
}
return util.RenderChart(helmChart, name, c.Namespace, values)
}
func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: c.Namespace,
}
}