forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emissary.go
168 lines (145 loc) · 5.07 KB
/
emissary.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
package emissary
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"
"time"
log "github.com/sirupsen/logrus"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/executor"
)
/*
This executor works very differently to the others. It mounts an empty-dir on all containers at `/var/run/argo`. The main container command is replaces by a new binary `argoexec` which starts the original command in a sub-process and when it is finished, captures the outputs:
The init container creates these files:
* `/var/run/argo/argoexec` The binary, copied from the `argoexec` image.
* `/var/run/argo/template` A JSON encoding of the template.
In the main container, the emissary creates these files:
* `/var/run/argo/ctr/${containerName}/exitcode` The container exit code.
* `/var/run/argo/ctr/${containerName}/stderr` A copy of stderr (if needed).
* `/var/run/argo/ctr/${containerName}/stdout` A copy of stdout (if needed).
If the container is named `main` it also copies base-layer artifacts to the shared volume:
* `/var/run/argo/outputs/parameters/${path}` All output parameters are copied here, e.g. `/tmp/message` is moved to `/var/run/argo/outputs/parameters/tmp/message`.
* `/var/run/argo/outputs/artifacts/${path}.tgz` All output artifacts are copied here, e.g. `/tmp/message` is moved to /var/run/argo/outputs/artifacts/tmp/message.tgz`.
The wait container can create one file itself, used for terminating the sub-process:
* `/var/run/argo/ctr/${containerName}/signal` The emissary binary listens to changes in this file, and signals the sub-process with the value found in this file.
*/
type emissary struct{}
func New() (executor.ContainerRuntimeExecutor, error) {
return &emissary{}, nil
}
func (e *emissary) Init(t wfv1.Template) error {
if err := copyBinary(); err != nil {
return err
}
if err := e.writeTemplate(t); err != nil {
return err
}
return nil
}
func (e emissary) writeTemplate(t wfv1.Template) error {
data, err := json.Marshal(t)
if err != nil {
return err
}
return ioutil.WriteFile("/var/run/argo/template", data, 0o400) // chmod -r--------
}
func (e emissary) GetFileContents(_ string, sourcePath string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join("/var/run/argo/outputs/parameters", sourcePath))
return string(data), err
}
func (e emissary) CopyFile(_ string, sourcePath string, destPath string, _ int) error {
// this implementation is very different, because we expect the emissary binary has already compressed the file
// so no compression can or needs to be implemented here
// TODO - warn the user we ignored compression?
sourceFile := filepath.Join("/var/run/argo/outputs/artifacts", sourcePath+".tgz")
log.Infof("%s -> %s", sourceFile, destPath)
src, err := os.Open(sourceFile)
if err != nil {
return err
}
defer func() { _ = src.Close() }()
dst, err := os.Create(destPath)
if err != nil {
return err
}
defer func() { _ = dst.Close() }()
_, err = io.Copy(dst, src)
if err := dst.Close(); err != nil {
return err
}
return err
}
func (e emissary) GetOutputStream(_ context.Context, containerName string, combinedOutput bool) (io.ReadCloser, error) {
names := []string{"stdout"}
if combinedOutput {
names = append(names, "stderr")
}
var files []io.ReadCloser
for _, name := range names {
f, err := os.Open("/var/run/argo/ctr/" + containerName + "/" + name)
if os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
}
files = append(files, f)
}
return newMultiReaderCloser(files...), nil
}
func (e emissary) Wait(ctx context.Context, containerNames []string) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if e.isComplete(containerNames) {
return nil
}
time.Sleep(time.Second)
}
}
}
func (e emissary) isComplete(containerNames []string) bool {
for _, containerName := range containerNames {
_, err := os.Stat("/var/run/argo/ctr/" + containerName + "/exitcode")
if os.IsNotExist(err) {
return false
}
}
return true
}
func (e emissary) Kill(ctx context.Context, containerNames []string, terminationGracePeriodDuration time.Duration) error {
for _, containerName := range containerNames {
if err := ioutil.WriteFile("/var/run/argo/ctr/"+containerName+"/signal", []byte(strconv.Itoa(int(syscall.SIGTERM))), 0o600); err != nil {
return err
}
}
ctx, cancel := context.WithTimeout(ctx, terminationGracePeriodDuration)
defer cancel()
err := e.Wait(ctx, containerNames)
if err != context.Canceled {
return err
}
for _, containerName := range containerNames {
if err := ioutil.WriteFile("/var/run/argo/ctr/"+containerName+"/signal", []byte(strconv.Itoa(int(syscall.SIGKILL))), 0o600); err != nil {
return err
}
}
return e.Wait(ctx, containerNames)
}
func (e emissary) ListContainerNames(ctx context.Context) ([]string, error) {
var containerNames []string
dir, err := ioutil.ReadDir("/var/run/argo/ctr")
if err != nil {
return nil, err
}
for _, n := range dir {
containerNames = append(containerNames, n.Name())
}
return containerNames, nil
}