From e11d3e06bd342798d3ecc4e4925242b2733a73d5 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Thu, 21 Mar 2024 15:28:29 -0700 Subject: [PATCH] add env vars to container resource --- README.md | 6 ++++++ internal/resource/container.go | 30 ++++++++++++++++++++++++++++-- tests/mocks/container.go | 7 +++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd151a8..142cbe1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # decl + +# Command-line + +`cli -resource-file decl-runner.yaml` + + diff --git a/internal/resource/container.go b/internal/resource/container.go index 92c46d9..2c593cf 100644 --- a/internal/resource/container.go +++ b/internal/resource/container.go @@ -9,6 +9,8 @@ _ "gopkg.in/yaml.v3" _ "os/exec" _ "strings" "log" + "github.com/docker/docker/api/types/strslice" + "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types" @@ -22,6 +24,7 @@ _ "strings" type ContainerClient interface { ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) + ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error) ContainerInspect(context.Context, string) (types.ContainerJSON, error) ContainerRemove(context.Context, string, container.RemoveOptions) error @@ -34,7 +37,9 @@ type Container struct { Name string `yaml:"name"` Path string `yaml:"path"` Cmd []string `yaml:"cmd",omitempty` + Entrypoint strslice.StrSlice `yaml:"entrypoint",omitempty` Args []string `yaml:"args",omitempty` + Environment map[string]string `yaml:"environment"` Image string `yaml:"image"` ResolvConfPath string `yaml:"resolvconfpath"` HostnamePath string `yaml:"hostnamepath"` @@ -49,7 +54,7 @@ type Container struct { ProcessLabel string `yaml:"processlabel"` AppArmorProfile string `yaml:"apparmorprofile"` ExecIDs []string `yaml:"execids"` - HostConfig *container.HostConfig `yaml:"hostconfig"` + HostConfig container.HostConfig `yaml:"hostconfig"` GraphDriver types.GraphDriverData `yaml:"graphdriver"` SizeRw *int64 `json:",omitempty"` SizeRootFs *int64 `json:",omitempty"` @@ -135,12 +140,29 @@ func (c *Container) LoadDecl(yamlFileResourceDeclaration string) error { */ func (c *Container) Create(ctx context.Context) error { + numberOfEnvironmentVariables := len(c.Environment) config := &container.Config { Image: c.Image, Cmd: c.Cmd, + Entrypoint: c.Entrypoint, Tty: false, } - resp, err := c.apiClient.ContainerCreate(ctx, config, nil, nil, nil, c.Name) + + config.Env = make([]string, numberOfEnvironmentVariables) + index := 0 + for k,v := range c.Environment { + config.Env[index] = k + "=" + v + index++ + } + for i := range c.HostConfig.Mounts { + if c.HostConfig.Mounts[i].Type == mount.TypeBind { + if mountSourceAbsolutePath,e := filepath.Abs(c.HostConfig.Mounts[i].Source); e == nil { + c.HostConfig.Mounts[i].Source = mountSourceAbsolutePath + } + } + } + + resp, err := c.apiClient.ContainerCreate(ctx, config, &c.HostConfig, nil, nil, c.Name) if err != nil { panic(err) } @@ -156,6 +178,10 @@ func (c *Container) Create(ctx context.Context) error { case <-statusCh: } */ + + if startErr := c.apiClient.ContainerStart(ctx, c.Id, types.ContainerStartOptions{}); startErr != nil { + return startErr + } return err } diff --git a/tests/mocks/container.go b/tests/mocks/container.go index 39d54cb..6097101 100644 --- a/tests/mocks/container.go +++ b/tests/mocks/container.go @@ -21,6 +21,13 @@ func (m *MockContainerClient) ContainerCreate(ctx context.Context, config *conta return m.InjectContainerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName) } +func (m *MockContainerClient) (ctx context.Context, containerID string, options container.StartOptions) error { + if m.InjectContainerStart == nil { + return nil + } + return m.InjectContainerStart(ctx, containerID, options) +} + func (m *MockContainerClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) { return m.InjectContainerList(ctx, options) }