add env vars to container resource
This commit is contained in:
parent
0d158fecc1
commit
e11d3e06bd
@ -1,2 +1,8 @@
|
|||||||
# decl
|
# decl
|
||||||
|
|
||||||
|
|
||||||
|
# Command-line
|
||||||
|
|
||||||
|
`cli -resource-file decl-runner.yaml`
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@ _ "gopkg.in/yaml.v3"
|
|||||||
_ "os/exec"
|
_ "os/exec"
|
||||||
_ "strings"
|
_ "strings"
|
||||||
"log"
|
"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/container"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
@ -22,6 +24,7 @@ _ "strings"
|
|||||||
|
|
||||||
type ContainerClient interface {
|
type ContainerClient interface {
|
||||||
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
|
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)
|
ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error)
|
||||||
ContainerInspect(context.Context, string) (types.ContainerJSON, error)
|
ContainerInspect(context.Context, string) (types.ContainerJSON, error)
|
||||||
ContainerRemove(context.Context, string, container.RemoveOptions) error
|
ContainerRemove(context.Context, string, container.RemoveOptions) error
|
||||||
@ -34,7 +37,9 @@ type Container struct {
|
|||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
Cmd []string `yaml:"cmd",omitempty`
|
Cmd []string `yaml:"cmd",omitempty`
|
||||||
|
Entrypoint strslice.StrSlice `yaml:"entrypoint",omitempty`
|
||||||
Args []string `yaml:"args",omitempty`
|
Args []string `yaml:"args",omitempty`
|
||||||
|
Environment map[string]string `yaml:"environment"`
|
||||||
Image string `yaml:"image"`
|
Image string `yaml:"image"`
|
||||||
ResolvConfPath string `yaml:"resolvconfpath"`
|
ResolvConfPath string `yaml:"resolvconfpath"`
|
||||||
HostnamePath string `yaml:"hostnamepath"`
|
HostnamePath string `yaml:"hostnamepath"`
|
||||||
@ -49,7 +54,7 @@ type Container struct {
|
|||||||
ProcessLabel string `yaml:"processlabel"`
|
ProcessLabel string `yaml:"processlabel"`
|
||||||
AppArmorProfile string `yaml:"apparmorprofile"`
|
AppArmorProfile string `yaml:"apparmorprofile"`
|
||||||
ExecIDs []string `yaml:"execids"`
|
ExecIDs []string `yaml:"execids"`
|
||||||
HostConfig *container.HostConfig `yaml:"hostconfig"`
|
HostConfig container.HostConfig `yaml:"hostconfig"`
|
||||||
GraphDriver types.GraphDriverData `yaml:"graphdriver"`
|
GraphDriver types.GraphDriverData `yaml:"graphdriver"`
|
||||||
SizeRw *int64 `json:",omitempty"`
|
SizeRw *int64 `json:",omitempty"`
|
||||||
SizeRootFs *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 {
|
func (c *Container) Create(ctx context.Context) error {
|
||||||
|
numberOfEnvironmentVariables := len(c.Environment)
|
||||||
config := &container.Config {
|
config := &container.Config {
|
||||||
Image: c.Image,
|
Image: c.Image,
|
||||||
Cmd: c.Cmd,
|
Cmd: c.Cmd,
|
||||||
|
Entrypoint: c.Entrypoint,
|
||||||
Tty: false,
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -156,6 +178,10 @@ func (c *Container) Create(ctx context.Context) error {
|
|||||||
case <-statusCh:
|
case <-statusCh:
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if startErr := c.apiClient.ContainerStart(ctx, c.Id, types.ContainerStartOptions{}); startErr != nil {
|
||||||
|
return startErr
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,13 @@ func (m *MockContainerClient) ContainerCreate(ctx context.Context, config *conta
|
|||||||
return m.InjectContainerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName)
|
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) {
|
func (m *MockContainerClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
|
||||||
return m.InjectContainerList(ctx, options)
|
return m.InjectContainerList(ctx, options)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user