forked from argoproj/argo-workflows
/
kubelet.go
107 lines (96 loc) · 2.83 KB
/
kubelet.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
package kubelet
import (
"compress/gzip"
"os"
"syscall"
"time"
"github.com/argoproj/argo/errors"
log "github.com/sirupsen/logrus"
)
// killGracePeriod is the time in seconds after sending SIGTERM before
// forcefully killing the sidecar with SIGKILL (value matches k8s)
const killGracePeriod = 10
type KubeletExecutor struct {
cli *kubeletClient
}
func NewKubeletExecutor() (*KubeletExecutor, error) {
log.Infof("Creating a kubelet executor")
cli, err := newKubeletClient()
if err != nil {
return nil, errors.InternalWrapError(err)
}
return &KubeletExecutor{
cli: cli,
}, nil
}
func (k *KubeletExecutor) GetFileContents(containerID string, sourcePath string) (string, error) {
b, err := k.cli.GetFileContents(containerID, sourcePath)
if err != nil {
return "", err
}
return b.String(), nil
}
func (k *KubeletExecutor) CopyFile(containerID string, sourcePath string, destPath string) error {
log.Infof("Archiving %s:%s to %s", containerID, sourcePath, destPath)
b, err := k.cli.CreateArchive(containerID, sourcePath)
if err != nil {
return err
}
f, err := os.OpenFile(destPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
return err
}
w := gzip.NewWriter(f)
_, err = w.Write(b.Bytes())
if err != nil {
return err
}
err = w.Flush()
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return f.Close()
}
// GetOutput returns the entirety of the container output as a string
// Used to capturing script results as an output parameter
func (k *KubeletExecutor) GetOutput(containerID string) (string, error) {
return k.cli.GetContainerLogs(containerID)
}
// Logs copies logs to a given path
func (k *KubeletExecutor) Logs(containerID, path string) error {
return k.cli.SaveLogsToFile(containerID, path)
}
// Wait for the container to complete
func (k *KubeletExecutor) Wait(containerID string) error {
return k.cli.WaitForTermination(containerID, 0)
}
// Kill kills a list of containerIDs first with a SIGTERM then with a SIGKILL after a grace period
func (k *KubeletExecutor) Kill(containerIDs []string) error {
for _, containerID := range containerIDs {
log.Infof("SIGTERM containerID %q: %s", containerID, syscall.SIGTERM.String())
err := k.cli.TerminatePodWithContainerID(containerID, syscall.SIGTERM)
if err != nil {
return err
}
err = k.cli.WaitForTermination(containerID, time.Second*killGracePeriod)
if err == nil {
log.Infof("ContainerID %q successfully killed", containerID)
continue
}
log.Infof("SIGKILL containerID %q: %s", containerID, syscall.SIGKILL.String())
err = k.cli.TerminatePodWithContainerID(containerID, syscall.SIGKILL)
if err != nil {
return err
}
err = k.cli.WaitForTermination(containerID, time.Second*killGracePeriod)
if err != nil {
return err
}
log.Infof("ContainerID %q successfully killed", containerID)
}
return nil
}