Skip to content

Commit

Permalink
docker and podman engines for imagebuild
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Quest <kcq.public@gmail.com>
  • Loading branch information
kcq committed Sep 24, 2024
1 parent ebeaf27 commit 255f553
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 114 deletions.
1 change: 0 additions & 1 deletion pkg/app/master/command/build/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ func GetContainerBuildOptions(ctx *cli.Context) (*config.ContainerBuildOptions,
cbo.ExtraHosts = strings.Join(hosts, ",")

cbo.BuildArgs = command.ParseKVParams(ctx.StringSlice(FlagCBOBuildArg))

kvLabels := command.ParseKVParams(ctx.StringSlice(FlagCBOLabel))
for _, kv := range kvLabels {
cbo.Labels[kv.Name] = kv.Value
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/master/command/build/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ func buildFatImage(
"context": cbOpts.DockerfileContext,
})

fatBuilder, err := standardbuilder.New(client, doShowBuildLogs)
crtClient := dockercrtclient.NewBuilder(client, doShowBuildLogs)
fatBuilder, err := standardbuilder.New(crtClient)

options := imagebuilder.DockerfileBuildOptions{
Dockerfile: cbOpts.Dockerfile,
BuildContext: cbOpts.DockerfileContext,
Expand Down
38 changes: 25 additions & 13 deletions pkg/app/master/command/imagebuild/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/mintoolkit/mint/pkg/app"
"github.com/mintoolkit/mint/pkg/app/master/command"
cmd "github.com/mintoolkit/mint/pkg/command"
"github.com/mintoolkit/mint/pkg/imagebuilder"
"github.com/mintoolkit/mint/pkg/util/fsutil"
)

Expand All @@ -20,17 +21,18 @@ const (
)

type CommandParams struct {
Engine string `json:"engine,omitempty"`
EngineEndpoint string `json:"engine_endpoint,omitempty"`
EngineToken string `json:"engine_token,omitempty"`
EngineNamespace string `json:"engine_namespace,omitempty"`
ImageName string `json:"image_name,omitempty"`
ImageArchiveFile string `json:"image_archive_file,omitempty"`
Runtime string `json:"runtime,omitempty"` //runtime where to load the created image
Dockerfile string `json:"dockerfile,omitempty"`
ContextDir string `json:"context_dir,omitempty"`
BuildArgs []string `json:"build_args,omitempty"`
Architecture string `json:"architecture,omitempty"`
Engine string `json:"engine,omitempty"`
EngineEndpoint string `json:"engine_endpoint,omitempty"`
EngineToken string `json:"engine_token,omitempty"`
EngineNamespace string `json:"engine_namespace,omitempty"`
ImageName string `json:"image_name,omitempty"`
ImageArchiveFile string `json:"image_archive_file,omitempty"`
Runtime string `json:"runtime,omitempty"` //runtime where to load the created image
Dockerfile string `json:"dockerfile,omitempty"`
ContextDir string `json:"context_dir,omitempty"`
BuildArgs []imagebuilder.NVParam `json:"build_args,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Architecture string `json:"architecture,omitempty"`
}

var ImageBuildFlags = useAllFlags()
Expand Down Expand Up @@ -60,11 +62,21 @@ var CLI = &cli.Command{
ImageArchiveFile: ctx.String(FlagImageArchiveFile),
Dockerfile: ctx.String(FlagDockerfile),
ContextDir: ctx.String(FlagContextDir),
BuildArgs: ctx.StringSlice(FlagBuildArg),
Runtime: ctx.String(FlagRuntimeLoad),
Architecture: ctx.String(FlagArchitecture),
}

cboBuildArgs := command.ParseKVParams(ctx.StringSlice(FlagBuildArg))
for _, val := range cboBuildArgs {
cparams.BuildArgs = append(cparams.BuildArgs,
imagebuilder.NVParam{Name: val.Name, Value: val.Value})
}

kvLabels := command.ParseKVParams(ctx.StringSlice(FlagLabel))
for _, kv := range kvLabels {
cparams.Labels[kv.Name] = kv.Value
}

engineProps, found := BuildEngines[cparams.Engine]
if !found {
return command.ErrBadParamValue
Expand All @@ -83,7 +95,7 @@ var CLI = &cli.Command{
}

if cparams.Architecture == "" {
cparams.Architecture = DefaultBuildArch
cparams.Architecture = GetDefaultBuildArch()
}

if !IsArchValue(cparams.Architecture) {
Expand Down
25 changes: 23 additions & 2 deletions pkg/app/master/command/imagebuild/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package imagebuild

import (
"runtime"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -37,6 +39,9 @@ const (
FlagBuildArg = "build-arg"
FlagBuildArgUsage = "build time variable (ARG)"

FlagLabel = "label"
FlagLabelUsage = "image label to add"

FlagArchitecture = "architecture"
FlagArchitectureUsage = "build architecture"
)
Expand Down Expand Up @@ -65,11 +70,21 @@ const (
Amd64Arch = "amd64"
Arm64Arch = "arm64"

DefaultBuildArch = Amd64Arch
DefaultRuntimeLoad = NoneRuntimeLoad
DefaultEngineName = DockerBuildEngine
)

func GetDefaultBuildArch() string {
switch runtime.GOARCH {
case Amd64Arch:
return Amd64Arch
case Arm64Arch:
return Arm64Arch
default:
return Amd64Arch
}
}

type BuildEngineProps struct {
Info string
TokenRequired bool
Expand Down Expand Up @@ -167,6 +182,12 @@ var Flags = map[string]cli.Flag{
Usage: FlagBuildArgUsage,
EnvVars: []string{"DSLIM_IMAGEBUILD_BUILD_ARGS"},
},
FlagLabel: &cli.StringSliceFlag{
Name: FlagLabel,
Value: cli.NewStringSlice(""),
Usage: FlagLabelUsage,
EnvVars: []string{"DSLIM_IMAGEBUILD_LABELS"},
},
FlagRuntimeLoad: &cli.StringFlag{
Name: FlagRuntimeLoad,
Value: DefaultRuntimeLoad,
Expand All @@ -175,7 +196,7 @@ var Flags = map[string]cli.Flag{
},
FlagArchitecture: &cli.StringFlag{
Name: FlagArchitecture,
Value: DefaultBuildArch,
Value: GetDefaultBuildArch(),
Usage: FlagArchitectureUsage,
EnvVars: []string{"DSLIM_IMAGEBUILD_ARCH"},
},
Expand Down
27 changes: 18 additions & 9 deletions pkg/app/master/command/imagebuild/handle_engine_buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"

Expand All @@ -28,6 +27,7 @@ func HandleBuildkitEngine(
cparams *CommandParams) {
logger.Trace("HandleBuildkitEngine.call")
defer logger.Trace("HandleBuildkitEngine.exit")
xc.Out.State("buildkit.engine.image.build.started")
ctx := context.Background()

logger.Trace("buildkit.client.New")
Expand All @@ -36,6 +36,8 @@ func HandleBuildkitEngine(

berr = buildkitBuildImage(logger, xc, cparams, ctx, bclient)
xc.FailOn(berr)

xc.Out.State("buildkit.engine.image.build.completed")
}

func buildkitBuildImage(
Expand Down Expand Up @@ -92,19 +94,26 @@ func buildkitBuildImage(
},
}

for _, kvStr := range cparams.BuildArgs {
kv := strings.SplitN(kvStr, "=", 2)
if len(kv) != 2 {
logger.Debugf("malformed build arg: %s", kvStr)
continue
}
for _, kv := range cparams.BuildArgs {
/*
kv := strings.SplitN(kvStr, "=", 2)
if len(kv) != 2 {
logger.Debugf("malformed build arg: %s", kvStr)
continue
}
opts.FrontendAttrs["build-arg:"+kv[0]] = kv[1]
*/

opts.FrontendAttrs["build-arg:"+kv.Name] = kv.Value
}

opts.FrontendAttrs["build-arg:"+kv[0]] = kv[1]
for k, v := range cparams.Labels {
opts.FrontendAttrs["label:"+k] = v
}

var res *client.SolveResponse
eg.Go(func() error {
//res, err = bclient.Build(ctx, opts, "", dockerfile.Build, ch)
res, err = bclient.Solve(ctx, nil, opts, ch)
return err
})
Expand Down
8 changes: 5 additions & 3 deletions pkg/app/master/command/imagebuild/handle_engine_depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func HandleDepotEngine(
cparams *CommandParams) {
logger.Trace("HandleDepotEngine.call")
defer logger.Trace("HandleDepotEngine.exit")
xc.Out.State("depot.engine.image.build.started")
ctx := context.Background()

var doLoad bool
Expand Down Expand Up @@ -51,9 +52,8 @@ func HandleDepotEngine(

logger.Trace("depot.build.NewBuild")
build, err := build.NewBuild(ctx, req, cparams.EngineToken)
if err != nil {
panic(err)
}
xc.FailOn(err)

logger.Tracef("depot.build.NewBuild -> id=%s buildURL='%s' useLocalRegistry=%v proxyImage=%s",
build.ID, build.BuildURL, build.UseLocalRegistry, build.ProxyImage)

Expand Down Expand Up @@ -85,4 +85,6 @@ func HandleDepotEngine(
if berr != nil {
return
}

xc.Out.State("depot.engine.image.build.completed")
}
51 changes: 50 additions & 1 deletion pkg/app/master/command/imagebuild/handle_engine_docker.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package imagebuild

import (
"fmt"
"os"

dockerapi "github.com/fsouza/go-dockerclient"
log "github.com/sirupsen/logrus"

"github.com/mintoolkit/mint/pkg/app"
"github.com/mintoolkit/mint/pkg/app/master/command"
//"github.com/mintoolkit/mint/pkg/util/jsonutil"
"github.com/mintoolkit/mint/pkg/crt/docker/dockercrtclient"
"github.com/mintoolkit/mint/pkg/imagebuilder"
"github.com/mintoolkit/mint/pkg/imagebuilder/standardbuilder"
"github.com/mintoolkit/mint/pkg/util/fsutil"
v "github.com/mintoolkit/mint/pkg/version"
)

// HandleDockerEngine implements support for the Docker container build engine
Expand All @@ -18,4 +25,46 @@ func HandleDockerEngine(
client *dockerapi.Client) {
logger.Trace("HandleDockerEngine.call")
defer logger.Trace("HandleDockerEngine.exit")
xc.Out.State("docker.engine.image.build.started")

doShowBuildLogs := true
crtClient := dockercrtclient.NewBuilder(client, doShowBuildLogs)
builder, err := standardbuilder.New(crtClient)

//note: need to also "save" the created image if it needs to be loaded in a different runtime
options := imagebuilder.DockerfileBuildOptions{
OutputStream: os.Stdout, //doShowBuildLogs
Dockerfile: cparams.Dockerfile,
BuildContext: cparams.ContextDir,
ImagePath: cparams.ImageName,
BuildArgs: cparams.BuildArgs,
Labels: cparams.Labels,
}

if cparams.Architecture != "" {
options.Platforms = []string{
fmt.Sprintf("linux/%s", cparams.Architecture),
}
}

err = builder.Build(options)
if err != nil {
xc.Out.Info("build.error",
ovars{
"status": "docker.engine.image.build.error",
"value": err,
})

exitCode := 721
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})

xc.Exit(exitCode)
}

xc.Out.State("docker.engine.image.build.completed")
}
50 changes: 49 additions & 1 deletion pkg/app/master/command/imagebuild/handle_engine_podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package imagebuild

import (
"context"
"fmt"
"os"

log "github.com/sirupsen/logrus"

"github.com/mintoolkit/mint/pkg/app"
"github.com/mintoolkit/mint/pkg/app/master/command"
//"github.com/mintoolkit/mint/pkg/util/jsonutil"
"github.com/mintoolkit/mint/pkg/crt/podman/podmancrtclient"
"github.com/mintoolkit/mint/pkg/imagebuilder"
"github.com/mintoolkit/mint/pkg/imagebuilder/standardbuilder"
"github.com/mintoolkit/mint/pkg/util/fsutil"
v "github.com/mintoolkit/mint/pkg/version"
)

// HandlePodmanEngine implements support for the Podman container build engine
Expand All @@ -19,4 +25,46 @@ func HandlePodmanEngine(
client context.Context) {
logger.Trace("HandlePodmanEngine.call")
defer logger.Trace("HandlePodmanEngine.exit")
xc.Out.State("podman.engine.image.build.started")

doShowBuildLogs := true
crtClient := podmancrtclient.NewBuilder(client, doShowBuildLogs)
builder, err := standardbuilder.New(crtClient)

//note: need to also "save" the created image if it needs to be loaded in a different runtime
options := imagebuilder.DockerfileBuildOptions{
OutputStream: os.Stdout, //doShowBuildLogs
Dockerfile: cparams.Dockerfile,
BuildContext: cparams.ContextDir,
ImagePath: cparams.ImageName,
BuildArgs: cparams.BuildArgs,
Labels: cparams.Labels,
}

if cparams.Architecture != "" {
options.Platforms = []string{
fmt.Sprintf("linux/%s", cparams.Architecture),
}
}

err = builder.Build(options)
if err != nil {
xc.Out.Info("build.error",
ovars{
"status": "podman.engine.image.build.error",
"value": err,
})

exitCode := 721
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})

xc.Exit(exitCode)
}

xc.Out.State("podman.engine.image.build.completed")
}
Loading

0 comments on commit 255f553

Please sign in to comment.