forked from arcalot/arcaflow-engine-deployer-podman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
110 lines (96 loc) · 3.15 KB
/
connector.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
package podman
import (
"context"
"fmt"
"math/rand"
"sync"
"github.com/docker/docker/api/types/container"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/deployer"
args "go.flow.arcalot.io/podmandeployer/internal/argsbuilder"
"go.flow.arcalot.io/podmandeployer/internal/cliwrapper"
"go.flow.arcalot.io/podmandeployer/internal/util"
)
type Connector struct {
containerNamePrefix string
config *Config
logger log.Logger
podmanCliWrapper cliwrapper.CliWrapper
rng *rand.Rand
// Random Number Generator to facilitate the generation
// of random strings for the container name suffix.
rngSeed int64
// The initial integer that is the starting point for a
// random number generator's algorithm.
lock *sync.Mutex
}
func (c *Connector) Deploy(ctx context.Context, image string) (deployer.Plugin, error) {
if err := c.pullImage(ctx, image); err != nil {
return nil, err
}
if c.config.Podman.Path == "" {
c.logger.Errorf("oops, neither podman -> path provided in configuration nor binary found in $PATH")
panic("oops, neither podman -> path provided in configuration nor binary found in $PATH")
}
containerConfig := c.unwrapContainerConfig()
hostConfig := c.unwrapHostConfig()
commandArgs := []string{"run", "-i", "-a", "stdin", "-a", "stdout", "-a", "stderr"}
containerName := c.NextContainerName(c.containerNamePrefix, 10)
args.NewBuilder(&commandArgs).
SetContainerName(containerName).
SetEnv(containerConfig.Env).
SetVolumes(hostConfig.Binds).
SetCgroupNs(string(hostConfig.CgroupnsMode)).
SetNetworkMode(string(hostConfig.NetworkMode))
stdin, stdout, err := c.podmanCliWrapper.Deploy(image, commandArgs, []string{"--atp"})
if err != nil {
return nil, err
}
cliPlugin := CliPlugin{
wrapper: c.podmanCliWrapper,
containerImage: image,
containerName: containerName,
config: c.config,
stdin: stdin,
stdout: stdout,
logger: c.logger,
}
return &cliPlugin, nil
}
func (c *Connector) pullImage(_ context.Context, image string) error {
if c.config.Deployment.ImagePullPolicy == ImagePullPolicyNever {
return nil
}
if c.config.Deployment.ImagePullPolicy == ImagePullPolicyIfNotPresent {
imageExists, err := c.podmanCliWrapper.ImageExists(image)
if err != nil {
return err
}
if *imageExists {
c.logger.Debugf("%s: image already present skipping pull", image)
return nil
}
c.logger.Debugf("Pulling image '%s'", image)
if err := c.podmanCliWrapper.PullImage(image, c.config.Deployment.ImagePlatform); err != nil {
return err
}
}
return nil
}
func (c *Connector) unwrapContainerConfig() container.Config {
if c.config.Deployment.ContainerConfig != nil {
return *c.config.Deployment.ContainerConfig
}
return container.Config{}
}
func (c *Connector) unwrapHostConfig() container.HostConfig {
if c.config.Deployment.HostConfig != nil {
return *c.config.Deployment.HostConfig
}
return container.HostConfig{}
}
func (c *Connector) NextContainerName(containerNamePrefix string, randomStrSize int) string {
c.lock.Lock()
defer c.lock.Unlock()
return fmt.Sprintf("%s_%s", containerNamePrefix, util.GetRandomString(c.rng, randomStrSize))
}