-
Notifications
You must be signed in to change notification settings - Fork 31
/
init.go
81 lines (74 loc) · 2.84 KB
/
init.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
package init
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
util2 "github.com/argoproj-labs/argo-dataflow/shared/util"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"k8s.io/utils/strings"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
dfv1 "github.com/argoproj-labs/argo-dataflow/api/v1alpha1"
)
var logger = zap.New()
// due to main container crashing, the init container may be started many times, so each operation we perform should be
// idempontent, i.e. if we copy a file to shared volume, and it already exists, we should ignore that error
func Exec() error {
for _, name := range []string{dfv1.PathKill, dfv1.PathPreStop} {
logger.Info("copying binary", "name", name)
a := filepath.Join("/bin", filepath.Base(name))
src, err := os.Open(a)
if err != nil {
return fmt.Errorf("failed to open %s: %w", a, err)
}
dst, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0o500)
if util2.IgnorePermission(util2.IgnoreExist(err)) != nil {
return fmt.Errorf("failed to open %s: %w", name, err)
} else if err == nil {
if _, err := io.Copy(dst, src); util2.IgnoreExist(err) != nil {
return fmt.Errorf("failed to create input FIFO: %w", err)
}
}
}
spec := dfv1.StepSpec{}
util2.MustUnJSON(os.Getenv(dfv1.EnvStepSpec), &spec)
if spec.GetIn().FIFO {
logger.Info("creating in fifo")
if err := syscall.Mkfifo(dfv1.PathFIFOIn, 0600); util2.IgnoreExist(err) != nil {
return fmt.Errorf("failed to create input FIFO: %w", err)
}
}
logger.Info("creating out fifo")
if err := syscall.Mkfifo(dfv1.PathFIFOOut, 0600); util2.IgnoreExist(err) != nil {
return fmt.Errorf("failed to create output FIFO: %w", err)
}
if g := spec.Git; g != nil {
logger.Info("cloning", "url", g.URL, "checkout", dfv1.PathCheckout)
if _, err := git.PlainClone(dfv1.PathCheckout, false, &git.CloneOptions{
URL: g.URL,
Progress: os.Stdout,
SingleBranch: true, // checkout faster
Depth: 1, // checkout faster
ReferenceName: plumbing.NewBranchReferenceName(g.Branch),
}); IgnoreErrRepositoryAlreadyExists(err) != nil {
return fmt.Errorf("failed to clone repo: %w", err)
}
path := filepath.Join(dfv1.PathCheckout, g.Path)
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("failed to stat %s: %w", path, err)
}
logger.Info("moving checked out code", "path", path, "wd", dfv1.PathWorkingDir)
if err := os.Rename(path, dfv1.PathWorkingDir); util2.IgnoreExist(err) != nil {
return fmt.Errorf("failed to moved checked out path to working dir: %w", err)
}
} else if h := spec.Handler; h != nil {
logger.Info("setting up handler", "runtime", h.Runtime, "code", strings.ShortenString(h.Code, 32)+"...")
if err := ioutil.WriteFile(dfv1.PathHandlerFile, []byte(h.Code), 0600); err != nil {
return fmt.Errorf("failed to create code file: %w", err)
}
}
return nil
}