-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathkubectltask.go
More file actions
395 lines (330 loc) · 11.4 KB
/
kubectltask.go
File metadata and controls
395 lines (330 loc) · 11.4 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* === This file is part of ALICE O² ===
*
* Copyright 2018-2025 CERN and copyright holders of ALICE O².
* Author: Michal Tichak <michal.tichak@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package executable
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/AliceO2Group/Control/core/controlcommands"
"github.com/AliceO2Group/Control/executor/executorcmd"
mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/sirupsen/logrus"
)
const (
KUBECTL string = "kubectl"
APPLY string = "apply"
DELETE string = "delete"
PATCH string = "patch"
GET string = "get"
TASK string = "task"
CREATE string = "create"
// TRANSITION_TIMEOUT = 10 * time.Second // inside controllable task
)
type KubectlTask struct {
taskBase
rpc *executorcmd.RpcClient
configYaml string
running bool
latestStatus atomic.Value
}
func GetUserInfo(username string) (uid, gid int64, supplemental []int64, err error) {
u, err := user.Lookup(username)
if err != nil {
return 0, 0, nil, err
}
// Convert UID
uidInt, _ := strconv.ParseInt(u.Uid, 10, 64)
// Convert Primary GID
gidInt, _ := strconv.ParseInt(u.Gid, 10, 64)
// Get Supplemental Groups (e.g., wheel, pda)
groupStrings, _ := u.GroupIds()
var supplementalInts []int64
for _, g := range groupStrings {
gInt, _ := strconv.ParseInt(g, 10, 64)
// Avoid adding the primary GID to the supplemental list
if gInt != gidInt {
supplementalInts = append(supplementalInts, gInt)
}
}
return uidInt, gidInt, supplementalInts, nil
}
func (task *KubectlTask) Launch() error {
if len(task.Tci.Arguments) == 0 {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
}).
Error("no arguments in kubectl task. We need to have at least manifest location as the last argument")
return errors.New("no arguments for kubectl task. Location for kubernetes manifest needed")
}
task.configYaml = task.Tci.Arguments[0]
// Read the template file
content, err := os.ReadFile(task.configYaml)
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"file": task.configYaml,
}).WithError(err).Error("failed to read kubectl config file")
return err
}
// Set the AliECS environment variables in the local process
// so os.ExpandEnv can find them
for _, envVar := range task.Tci.Env {
parts := strings.SplitN(envVar, "=", 2)
if len(parts) == 2 {
os.Setenv(parts[0], parts[1])
}
}
// Set arguments into the KUBE_ARGUMENTS os env leaving the kubemanifest file
arguments := task.Tci.Arguments[1:]
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"args": arguments,
}).Info("setting arguments as a KUBE_ARGUMENTS env var")
os.Setenv("KUBE_ARGUMENTS", strings.Join(arguments, " "))
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": *task.Tci.Value,
}).Info("setting command as a KUBE_COMMAND env var")
os.Setenv("KUBE_COMMAND", *task.Tci.Value)
if uid, gid, supplementalIds, err := GetUserInfo("flp"); err == nil {
os.Setenv("FLP_UID", strconv.FormatInt(uid, 10))
os.Setenv("FLP_GID", strconv.FormatInt(gid, 10))
var strIds []string
for _, id := range supplementalIds {
strIds = append(strIds, strconv.FormatInt(id, 10))
}
supplementalString := "[" + strings.Join(strIds, ", ") + "]"
os.Setenv("FLP_SUPPLEMENTAL_GORUPS", supplementalString)
} else {
log.Error("we cannot run kubectl task as flp user because we didn't find user details")
}
// Replace ${VAR} placeholders with actual values
expandedYaml := os.ExpandEnv(string(content))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Apply via Stdin (-)
command := exec.CommandContext(ctx, KUBECTL, APPLY, "-f", "-")
command.Stdin = strings.NewReader(expandedYaml)
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).Info("Starting kubectl apply via Stdin")
var stdoutBuf bytes.Buffer
var stderrBuf bytes.Buffer
command.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
command.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
err = command.Run()
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
}).WithError(err).Errorf("kubectl apply failed stderr: %s , stdin: %s", stderrBuf.String(), stdoutBuf.String())
return err
}
task.latestStatus.Store("")
task.running = true
go task.eventLoop()
return nil
}
func (task *KubectlTask) Kill() error {
task.running = false
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
command := exec.CommandContext(ctx, KUBECTL, DELETE, "-f", task.configYaml)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
}).WithError(err).Error("kubectl delete failed")
return err
}
task.sendStatus(task.knownEnvironmentId, mesos.TASK_FINISHED, "")
return nil
}
func (task *KubectlTask) Transition(transition *executorcmd.ExecutorCommand_Transition) *controlcommands.MesosCommandResponse_Transition {
// kubectl patch -f exampletask.yaml --type='json' -p='[{"op": "replace", "path": "/spec/state", "value": "running"}]'
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Extract the transition arguments (the 'Mix') to pipe them to Kubernetes
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"args": transition.Arguments,
}).Info("Patching transition arguments to Kubernetes")
argsJSON, err := json.Marshal(transition.Arguments)
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
}).WithError(err).Error("failed to marshal transition arguments")
return transition.PrepareResponse(err, transition.Source, task.ti.TaskID.Value)
}
transitionJSON := fmt.Sprintf(`[
{"op": "add", "path": "/spec/state", "value": "%s"},
{"op": "add", "path": "/spec/arguments", "value": %s}
]`, strings.ToLower(transition.Destination), string(argsJSON))
command := exec.CommandContext(ctx, KUBECTL, PATCH, "-f", task.configYaml, "--type=json", "-p", transitionJSON)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).Info("Starting kubectl patch")
statusBeforeTransition := task.latestStatus.Load().(string)
err = command.Run()
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).WithError(err).Error("kubectl patch failed")
return transition.PrepareResponse(err, transition.Source, task.ti.TaskID.Value)
}
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).Info("kubectl patch suceeded, waiting for actual status change")
actualStatus := ""
timeout := time.After(TRANSITION_TIMEOUT)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
loop:
for {
select {
case <-ticker.C:
actualStatus = task.latestStatus.Load().(string)
if actualStatus != statusBeforeTransition {
break loop
}
case <-timeout:
return transition.PrepareResponse(errors.New("timeout waiting for status change"), statusBeforeTransition, task.ti.TaskID.Value)
}
}
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).Infof("status changed from %s to %s", statusBeforeTransition, actualStatus)
// TODO: I am not sure what PID should I put here
return transition.PrepareResponse(nil, actualStatus, task.ti.TaskID.Value)
}
func (task *KubectlTask) getTaskStatus() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// command := exec.CommandContext(ctx, KUBECTL, GET, TASK, task.ti.Name, "-o", "jsonpath={.status.state}")
command := exec.CommandContext(ctx, KUBECTL, GET, "-f", task.configYaml, "-o", "jsonpath={.status.state}")
var stdoutBuf bytes.Buffer
command.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
command.Stderr = os.Stderr
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).Debug("Starting kubectl get task")
err := command.Run()
if err != nil {
log.WithFields(logrus.Fields{
"controlmode": task.Tci.ControlMode,
"name": task.ti.Name,
"command": command,
}).WithError(err).Error("kubectl get task failed")
return "", err
}
// no newlines
return strings.TrimSpace(stdoutBuf.String()), nil
}
func (task *KubectlTask) eventLoop() {
errorCount := 0
maxErrors := 5
for task.running {
time.Sleep(5 * time.Second)
status, err := task.getTaskStatus()
if err != nil {
errorCount += 1
if errorCount < maxErrors {
log.WithError(err).Warnf("failed to get Task Status, retrying %d/%d", errorCount, maxErrors)
continue
}
log.WithError(err).Errorf("failed to get Task Status, sending TASK_FAILED and breaking from the eventLoop")
task.sendStatus(task.knownEnvironmentId, mesos.TASK_FAILED, "couldn't get task status via kubectl")
task.running = false
// TODO: remove when debugging done
// _ = task.Kill()
break
}
status = strings.ToUpper(status)
if task.latestStatus.Load().(string) == status {
continue
}
task.latestStatus.Store(status)
var state mesos.TaskState
switch status {
case "CONFIGURED", "RUNNING", "STANDBY":
state = mesos.TASK_RUNNING
case "ERROR":
state = mesos.TASK_FAILED
log.WithError(err).Error("Received error from kubectl task, terminating everything and sending update")
task.running = false
// TODO: remove when debugging done
// _ = task.Kill()
//
default:
log.Errorf("received different status than expected: %s", status)
continue
}
log.Debugf("sending new status from kubectl task %s", status)
task.sendStatus(task.knownEnvironmentId, state, "")
}
}
func (task *KubectlTask) UnmarshalTransition(data []byte) (cmd *executorcmd.ExecutorCommand_Transition, err error) {
cmd = new(executorcmd.ExecutorCommand_Transition)
if task.rpc != nil {
cmd.Transitioner = task.rpc.Transitioner
}
err = json.Unmarshal(data, cmd)
if err != nil {
cmd = nil
}
return
}