Skip to content

image build support #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ To change the default container runtime:

```yaml
# ...
container:
runtime: kubernetes
runtime:
container:
default_runtime: kubernetes
# ...
```

Expand Down
90 changes: 60 additions & 30 deletions pkg/action/runtime.container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
containerFlagRebuildImage = "rebuild-image"
containerFlagEntrypoint = "entrypoint"
containerFlagExec = "exec"
containerRegistryURL = "registry-url"
containerRegistryType = "registry-type"
)

type runtimeContainer struct {
Expand All @@ -50,15 +52,20 @@ type runtimeContainer struct {
nameprv ContainerNameProvider

// Runtime flags
isSetRemote bool
copyBack bool
removeImg bool
noCache bool
rebuildImage bool
entrypoint string
entrypointSet bool
exec bool
volumeFlags string
// @todo need to think about a better way to handle this.
runtimeFlags driver.RuntimeFlags
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Driver must not know about runtime flags. You can save flags in the struct, but move it back to runtime.


//isSetRemote bool
//copyBack bool
//removeImg bool
//noCache bool
//rebuildImage bool
//entrypoint string
//entrypointSet bool
//exec bool
//volumeFlags string
//registryURL string
//registryType string
}

// ContainerNameProvider provides an ability to generate a random container name
Expand Down Expand Up @@ -156,6 +163,19 @@ func (c *runtimeContainer) GetFlags() *FlagsGroup {
Type: jsonschema.Boolean,
Default: false,
},
&DefParameter{
Name: containerRegistryURL,
Title: "k8s Images registry URL",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Kubernetes for all frontend strings. k8s is mostly for development. It seems we need to be able to specify registry in the global config. We may as well have multiple registries.

Type: jsonschema.String,
Default: "localhost:5000",
},
&DefParameter{
Name: containerRegistryType,
Title: "k8s Images registry type",
Type: jsonschema.String,
Enum: []any{driver.RegistryNone, driver.RegistryLocal, driver.RegistryRemote},
Default: driver.RegistryLocal,
},
}

flags.AddDefinitions(definitions)
Expand Down Expand Up @@ -184,32 +204,40 @@ func (c *runtimeContainer) SetFlags(input *Input) error {
flags := input.GroupFlags(c.flags.GetName())

if v, ok := flags[containerFlagRemote]; ok {
c.isSetRemote = v.(bool)
c.runtimeFlags.IsSetRemote = v.(bool)
}

if v, ok := flags[containerFlagCopyBack]; ok {
c.copyBack = v.(bool)
c.runtimeFlags.CopyBack = v.(bool)
}

if r, ok := flags[containerFlagRemoveImage]; ok {
c.removeImg = r.(bool)
c.runtimeFlags.RemoveImg = r.(bool)
}

if nc, ok := flags[containerFlagNoCache]; ok {
c.noCache = nc.(bool)
c.runtimeFlags.NoCache = nc.(bool)
}

if rb, ok := flags[containerFlagRebuildImage]; ok {
c.rebuildImage = rb.(bool)
c.runtimeFlags.RebuildImage = rb.(bool)
}

if e, ok := flags[containerFlagEntrypoint]; ok && e != "" {
c.entrypointSet = true
c.entrypoint = e.(string)
c.runtimeFlags.EntrypointSet = true
c.runtimeFlags.Entrypoint = e.(string)
}

if ex, ok := flags[containerFlagExec]; ok {
c.exec = ex.(bool)
c.runtimeFlags.Exec = ex.(bool)
}

if rt, ok := flags[containerRegistryType]; ok {
c.runtimeFlags.RegistryType = rt.(string)
}

if rurl, ok := flags[containerRegistryURL]; ok {
c.runtimeFlags.RegistryURL = rurl.(string)
}

return nil
Expand Down Expand Up @@ -241,15 +269,17 @@ func (c *runtimeContainer) Init(ctx context.Context, _ *Action) (err error) {
if !c.isRemote() && c.isSELinuxEnabled(ctx) {
// Check SELinux settings to allow reading the FS inside a container.
// Use the lowercase z flag to allow concurrent actions access to the FS.
c.volumeFlags += ":z"
c.runtimeFlags.VolumeFlags += ":z"
launchr.Term().Warning().Printfln(
"SELinux is detected. The volumes will be mounted with the %q flags, which will relabel your files.\n"+
"This process may take time or potentially break existing permissions.",
c.volumeFlags,
c.runtimeFlags.VolumeFlags,
)
c.Log().Warn("using selinux flags", "flags", c.volumeFlags)
c.Log().Warn("using selinux flags", "flags", c.runtimeFlags.VolumeFlags)
}

c.crt.SetRuntimeFlags(c.runtimeFlags)

return nil
}

Expand Down Expand Up @@ -298,7 +328,7 @@ func (c *runtimeContainer) Execute(ctx context.Context, a *Action) (err error) {

// Remove the used image if it was specified.
defer func() {
if !c.removeImg {
if !c.runtimeFlags.RemoveImg {
return
}
log.Debug("removing container image after run")
Expand Down Expand Up @@ -405,7 +435,7 @@ func (c *runtimeContainer) imageRemove(ctx context.Context, a *Action) error {

func (c *runtimeContainer) isRebuildRequired(bi *driver.BuildDefinition) (bool, error) {
// @todo test image cache resolution somehow.
if c.imgccres == nil || bi == nil || !c.rebuildImage {
if c.imgccres == nil || bi == nil || !c.runtimeFlags.RebuildImage {
return false, nil
}

Expand Down Expand Up @@ -454,7 +484,7 @@ func (c *runtimeContainer) imageEnsure(ctx context.Context, a *Action) error {
status, err := crt.ImageEnsure(ctx, driver.ImageOptions{
Name: image,
Build: buildInfo,
NoCache: c.noCache,
NoCache: c.runtimeFlags.NoCache,
ForceRebuild: forceRebuild,
})
if err != nil {
Expand Down Expand Up @@ -521,13 +551,13 @@ func (c *runtimeContainer) createContainerDef(a *Action, cname string) driver.Co

// Override an entrypoint if it was set in flags.
var entrypoint []string
if c.entrypointSet {
entrypoint = []string{c.entrypoint}
if c.runtimeFlags.EntrypointSet {
entrypoint = []string{c.runtimeFlags.Entrypoint}
}

// Override Command with exec command.
cmd := runDef.Container.Command
if c.exec {
if c.runtimeFlags.Exec {
cmd = a.Input().ArgsPositional()
}

Expand Down Expand Up @@ -556,8 +586,8 @@ func (c *runtimeContainer) createContainerDef(a *Action, cname string) driver.Co
)
} else {
createOpts.Binds = []string{
launchr.MustAbs(a.WorkDir()) + ":" + containerHostMount + c.volumeFlags,
launchr.MustAbs(a.Dir()) + ":" + containerActionMount + c.volumeFlags,
launchr.MustAbs(a.WorkDir()) + ":" + containerHostMount + c.runtimeFlags.VolumeFlags,
launchr.MustAbs(a.Dir()) + ":" + containerActionMount + c.runtimeFlags.VolumeFlags,
}
}
return createOpts
Expand Down Expand Up @@ -591,7 +621,7 @@ func (c *runtimeContainer) copyAllToContainer(ctx context.Context, cid string, a
}

func (c *runtimeContainer) copyAllFromContainer(ctx context.Context, cid string, a *Action) (err error) {
if !c.isRemote() || !c.copyBack {
if !c.isRemote() || !c.runtimeFlags.CopyBack {
return nil
}
// @todo it's a bad implementation considering consequential runs, need to find a better way to sync with remote.
Expand Down Expand Up @@ -670,5 +700,5 @@ func (c *runtimeContainer) isSELinuxEnabled(ctx context.Context) bool {
}

func (c *runtimeContainer) isRemote() bool {
return c.isRemoteRuntime || c.isSetRemote
return c.isRemoteRuntime || c.runtimeFlags.IsSetRemote
}
8 changes: 5 additions & 3 deletions pkg/action/runtime.container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,11 @@ func Test_ContainerExec_createContainerDef(t *testing.T) {
input.SetValidated(true)
_ = a.SetInput(input)
r.isRemoteRuntime = true
r.entrypointSet = true
r.entrypoint = "/my/entrypoint"
r.exec = true
r.runtimeFlags = driver.RuntimeFlags{
EntrypointSet: true,
Entrypoint: "/my/entrypoint",
Exec: true,
}
return a
},
driver.ContainerDefinition{
Expand Down
4 changes: 4 additions & 0 deletions pkg/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewDockerRuntime() (ContainerRunner, error) {
return &dockerRuntime{cli: c}, nil
}

func (d *dockerRuntime) SetRuntimeFlags(_ RuntimeFlags) {

}

func (d *dockerRuntime) Info(ctx context.Context) (SystemInfo, error) {
if d.info.ID != "" {
return d.info, nil
Expand Down
Loading