Skip to content

Commit 14093d8

Browse files
committed
registry ops refactoring and adding ability to push newly built images by the imagebuild command
Signed-off-by: Kyle Quest <kcq.public@gmail.com>
1 parent 4993c73 commit 14093d8

File tree

14 files changed

+202
-127
lines changed

14 files changed

+202
-127
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ Flags:
636636
- `--base-tar` - `simple` build engine: base image from a local tar file
637637
- `--base-with-certs` - `simple` build engine: boolean flat to use the static-debian12 distroless base image - contains only certs and timezone info
638638
- `--exe-path` - `simple` build engine: local (linux) executable file that will be used as the entrypoint for the new image (added to the selected base image or scratch image if no base image is provided)
639+
- `--registry-push` - Push the built image to a container registry
640+
- `--use-docker-credentials` - Use the registry credentials from the default Docker config file (defaults to true).
641+
- `--account` - Registry credentials account.
642+
- `--secret` - Registry credentials secret.
639643

640644

641645
Examples:
@@ -717,7 +721,7 @@ There's also a placeholder for `copy`, but it doesn't do anything yet. Great opp
717721

718722
Shared Command Level Flags:
719723

720-
- `--use-docker-credentials` - Use the registry credentials from the default Docker config file.
724+
- `--use-docker-credentials` - Use the registry credentials from the default Docker config file (defaults to true).
721725
- `--account` - Registry credentials account.
722726
- `--secret` - Registry credentials secret.
723727

pkg/app/master/command/imagebuild/cli.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/mintoolkit/mint/pkg/app"
1313
"github.com/mintoolkit/mint/pkg/app/master/command"
14+
registrycmd "github.com/mintoolkit/mint/pkg/app/master/command/registry"
1415
cmd "github.com/mintoolkit/mint/pkg/command"
1516
"github.com/mintoolkit/mint/pkg/imagebuilder"
1617
"github.com/mintoolkit/mint/pkg/util/fsutil"
@@ -30,6 +31,7 @@ type CommandParams struct {
3031
ImageName string `json:"image_name,omitempty"`
3132
ImageArchiveFile string `json:"image_archive_file,omitempty"`
3233
LoadRuntimes []string `json:"runtime,omitempty"` //runtime where to load the created image
34+
RegistryPush bool `json:"registry_push,omitempty"`
3335
Dockerfile string `json:"dockerfile,omitempty"`
3436
ContextDir string `json:"context_dir,omitempty"`
3537
BuildArgs []imagebuilder.NVParam `json:"build_args,omitempty"`
@@ -39,6 +41,9 @@ type CommandParams struct {
3941
BaseImageTar string `json:"base_image_tar,omitempty"`
4042
BaseImageWithCerts bool `json:"base_image_with_certs,omitempty"`
4143
ExePath string `json:"exe_path,omitempty"`
44+
UseDockerCreds bool `json:"use_docker_creds,omitempty"`
45+
CredsAccount string `json:"creds_account,omitempty"`
46+
CredsSecret string `json:"creds_secret,omitempty"`
4247
}
4348

4449
var ImageBuildFlags = useAllFlags()
@@ -47,7 +52,11 @@ var CLI = &cli.Command{
4752
Name: Name,
4853
Aliases: []string{Alias},
4954
Usage: Usage,
50-
Flags: ImageBuildFlags,
55+
Flags: append(ImageBuildFlags,
56+
registrycmd.Cflag(registrycmd.FlagUseDockerCreds),
57+
registrycmd.Cflag(registrycmd.FlagCredsAccount),
58+
registrycmd.Cflag(registrycmd.FlagCredsSecret),
59+
),
5160
Action: func(ctx *cli.Context) error {
5261
logger := log.WithFields(log.Fields{"app": command.AppName, "cmd": Name, "op": "cli.Action"})
5362

@@ -77,6 +86,10 @@ var CLI = &cli.Command{
7786
BaseImageWithCerts: ctx.Bool(FlagBaseWithCerts),
7887
ExePath: ctx.String(FlagExePath),
7988
Labels: map[string]string{},
89+
RegistryPush: ctx.Bool(FlagRegistryPush),
90+
UseDockerCreds: ctx.Bool(registrycmd.FlagUseDockerCreds),
91+
CredsAccount: ctx.String(registrycmd.FlagCredsAccount),
92+
CredsSecret: ctx.String(registrycmd.FlagCredsSecret),
8093
}
8194

8295
loadRuntimeSet := map[string]struct{}{}

pkg/app/master/command/imagebuild/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99

1010
// ImageBuild command flag names and usage descriptions
1111
const (
12+
FlagRegistryPush = "registry-push"
13+
FlagRegistryPushUsage = "Push the built image to a container registry"
14+
1215
FlagRuntimeLoad = "runtime-load"
1316
FlagRuntimeLoadUsage = "container runtime where to load to created image"
1417

@@ -248,6 +251,12 @@ var Flags = map[string]cli.Flag{
248251
Usage: FlagExePathUsage,
249252
EnvVars: []string{"DSLIM_IMAGEBUILD_EXE_PATH"},
250253
},
254+
FlagRegistryPush: &cli.BoolFlag{
255+
Name: FlagRegistryPush,
256+
Value: false,
257+
Usage: FlagRegistryPushUsage,
258+
EnvVars: []string{"DSLIM_IMAGEBUILD_REGISTRY_PUSH"},
259+
},
251260
}
252261

253262
func cflag(name string) cli.Flag {

pkg/app/master/command/imagebuild/handler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66

77
docker "github.com/fsouza/go-dockerclient"
8+
"github.com/google/go-containerregistry/pkg/name"
9+
"github.com/google/go-containerregistry/pkg/v1/remote"
810
log "github.com/sirupsen/logrus"
911

1012
"github.com/mintoolkit/mint/pkg/app"
@@ -17,6 +19,7 @@ import (
1719
"github.com/mintoolkit/mint/pkg/crt/docker/dockercrtclient"
1820
"github.com/mintoolkit/mint/pkg/crt/podman/podmancrtclient"
1921

22+
"github.com/mintoolkit/mint/pkg/app/master/registry"
2023
"github.com/mintoolkit/mint/pkg/report"
2124
"github.com/mintoolkit/mint/pkg/util/fsutil"
2225
"github.com/mintoolkit/mint/pkg/util/jsonutil"
@@ -182,6 +185,26 @@ func OnCommand(
182185
xc.Out.Info("runtime.load.image.none")
183186
}
184187

188+
if cparams.RegistryPush {
189+
remoteOpts := []remote.Option{
190+
remote.WithContext(context.Background()),
191+
}
192+
remoteOpts, err = registry.ConfigureAuth(
193+
cparams.UseDockerCreds,
194+
cparams.CredsAccount,
195+
cparams.CredsSecret,
196+
remoteOpts)
197+
198+
xc.FailOn(err)
199+
200+
nameOpts := []name.Option{
201+
name.WeakValidation,
202+
name.Insecure,
203+
}
204+
err = registry.PushImageFromTar(logger, cparams.ImageArchiveFile, cparams.ImageName, nameOpts, remoteOpts)
205+
xc.FailOn(err)
206+
}
207+
185208
xc.Out.State(cmd.StateCompleted)
186209
cmdReport.State = cmd.StateCompleted
187210

pkg/app/master/command/imagebuild/prompt.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/c-bata/go-prompt"
55

66
"github.com/mintoolkit/mint/pkg/app/master/command"
7+
registrycmd "github.com/mintoolkit/mint/pkg/app/master/command/registry"
78
)
89

910
var CommandSuggestion = prompt.Suggest{
@@ -46,6 +47,7 @@ var CommandFlagSuggestions = &command.FlagSuggestions{
4647
Names: []prompt.Suggest{
4748
{Text: command.FullFlagName(FlagEngine), Description: FlagEngineUsage},
4849
{Text: command.FullFlagName(FlagRuntimeLoad), Description: FlagRuntimeLoadUsage},
50+
{Text: command.FullFlagName(FlagRegistryPush), Description: FlagRegistryPushUsage},
4951
{Text: command.FullFlagName(FlagEngineEndpoint), Description: FlagEngineEndpointUsage},
5052
{Text: command.FullFlagName(FlagEngineToken), Description: FlagEngineTokenUsage},
5153
{Text: command.FullFlagName(FlagEngineNamespace), Description: FlagEngineNamespaceUsage},
@@ -60,14 +62,19 @@ var CommandFlagSuggestions = &command.FlagSuggestions{
6062
{Text: command.FullFlagName(FlagBaseTar), Description: FlagBaseTarUsage},
6163
{Text: command.FullFlagName(FlagBaseWithCerts), Description: FlagBaseWithCertsUsage},
6264
{Text: command.FullFlagName(FlagExePath), Description: FlagExePathUsage},
65+
{Text: command.FullFlagName(registrycmd.FlagUseDockerCreds), Description: registrycmd.FlagUseDockerCredsUsage},
66+
{Text: command.FullFlagName(registrycmd.FlagCredsAccount), Description: registrycmd.FlagCredsAccountUsage},
67+
{Text: command.FullFlagName(registrycmd.FlagCredsSecret), Description: registrycmd.FlagCredsSecretUsage},
6368
},
6469
Values: map[string]command.CompleteValue{
65-
command.FullFlagName(FlagEngine): completeBuildEngine,
66-
command.FullFlagName(FlagRuntimeLoad): completeRuntimeLoad,
67-
command.FullFlagName(FlagArchitecture): completeArchitecture,
68-
command.FullFlagName(FlagContextDir): command.CompleteDir,
69-
command.FullFlagName(FlagBaseTar): command.CompleteFile,
70-
command.FullFlagName(FlagBaseWithCerts): command.CompleteBool,
71-
command.FullFlagName(FlagExePath): command.CompleteFile,
70+
command.FullFlagName(FlagEngine): completeBuildEngine,
71+
command.FullFlagName(FlagRuntimeLoad): completeRuntimeLoad,
72+
command.FullFlagName(FlagRegistryPush): command.CompleteBool,
73+
command.FullFlagName(FlagArchitecture): completeArchitecture,
74+
command.FullFlagName(FlagContextDir): command.CompleteDir,
75+
command.FullFlagName(FlagBaseTar): command.CompleteFile,
76+
command.FullFlagName(FlagBaseWithCerts): command.CompleteBool,
77+
command.FullFlagName(FlagExePath): command.CompleteFile,
78+
command.FullFlagName(registrycmd.FlagUseDockerCreds): command.CompleteTBool,
7279
},
7380
}

pkg/app/master/command/registry/auth.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

pkg/app/master/command/registry/cli.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,17 @@ var CLI = &cli.Command{
180180
Aliases: []string{Alias},
181181
Usage: Usage,
182182
Flags: []cli.Flag{
183-
cflag(FlagUseDockerCreds),
184-
cflag(FlagCredsAccount),
185-
cflag(FlagCredsSecret),
183+
Cflag(FlagUseDockerCreds),
184+
Cflag(FlagCredsAccount),
185+
Cflag(FlagCredsSecret),
186186
},
187187
Subcommands: []*cli.Command{
188188
{
189189
Name: PullCmdName,
190190
Usage: PullCmdNameUsage,
191191
Flags: []cli.Flag{
192192
command.Cflag(command.FlagTarget),
193-
cflag(FlagSaveToDocker),
193+
Cflag(FlagSaveToDocker),
194194
},
195195
Action: func(ctx *cli.Context) error {
196196
gcvalues := command.GlobalFlagValues(ctx)
@@ -222,10 +222,10 @@ var CLI = &cli.Command{
222222
Name: PushCmdName,
223223
Usage: PushCmdNameUsage,
224224
Flags: []cli.Flag{
225-
cflag(FlagAs),
226-
cflag(FlagDocker),
227-
cflag(FlagTar),
228-
cflag(FlagOCI),
225+
Cflag(FlagAs),
226+
Cflag(FlagDocker),
227+
Cflag(FlagTar),
228+
Cflag(FlagOCI),
229229
},
230230
Action: func(ctx *cli.Context) error {
231231
gcvalues := command.GlobalFlagValues(ctx)
@@ -272,11 +272,11 @@ var CLI = &cli.Command{
272272
Name: ImageIndexCreateCmdName,
273273
Usage: ImageIndexCreateCmdNameUsage,
274274
Flags: []cli.Flag{
275-
cflag(FlagImageIndexName),
276-
cflag(FlagImageName),
277-
cflag(FlagAsManifestList),
278-
cflag(FlagInsecureRefs),
279-
cflag(FlagDumpRawManifest),
275+
Cflag(FlagImageIndexName),
276+
Cflag(FlagImageName),
277+
Cflag(FlagAsManifestList),
278+
Cflag(FlagInsecureRefs),
279+
Cflag(FlagDumpRawManifest),
280280
},
281281
Action: func(ctx *cli.Context) error {
282282
gcvalues := command.GlobalFlagValues(ctx)
@@ -298,13 +298,13 @@ var CLI = &cli.Command{
298298
Name: ServerCmdName,
299299
Usage: ServerCmdNameUsage,
300300
Flags: []cli.Flag{
301-
cflag(FlagDomain),
302-
cflag(FlagAddress),
303-
cflag(FlagPort),
304-
cflag(FlagHTTPS),
305-
cflag(FlagCertPath),
306-
cflag(FlagKeyPath),
307-
cflag(FlagReferrersAPI),
301+
Cflag(FlagDomain),
302+
Cflag(FlagAddress),
303+
Cflag(FlagPort),
304+
Cflag(FlagHTTPS),
305+
Cflag(FlagCertPath),
306+
Cflag(FlagKeyPath),
307+
Cflag(FlagReferrersAPI),
308308
},
309309
Action: func(ctx *cli.Context) error {
310310
gcvalues := command.GlobalFlagValues(ctx)

pkg/app/master/command/registry/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const (
8585
var Flags = map[string]cli.Flag{
8686
FlagUseDockerCreds: &cli.BoolFlag{
8787
Name: FlagUseDockerCreds,
88-
Value: false, //defaults to false
88+
Value: true, //defaults to true (now)
8989
Usage: FlagUseDockerCredsUsage,
9090
EnvVars: []string{"DSLIM_REG_DOCKER_CREDS"},
9191
},
@@ -221,7 +221,7 @@ var Flags = map[string]cli.Flag{
221221
},
222222
}
223223

224-
func cflag(name string) cli.Flag {
224+
func Cflag(name string) cli.Flag {
225225
cf, ok := Flags[name]
226226
if !ok {
227227
log.Fatalf("unknown flag='%s'", name)

pkg/app/master/command/registry/handler_image_index.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/mintoolkit/mint/pkg/app"
2020
"github.com/mintoolkit/mint/pkg/app/master/command"
21+
"github.com/mintoolkit/mint/pkg/app/master/registry"
2122
"github.com/mintoolkit/mint/pkg/app/master/version"
2223
cmd "github.com/mintoolkit/mint/pkg/command"
2324
"github.com/mintoolkit/mint/pkg/crt/docker/dockerclient"
@@ -83,8 +84,11 @@ func OnImageIndexCreateCommand(
8384
remoteOpts := []remote.Option{
8485
remote.WithContext(context.Background()),
8586
}
86-
87-
remoteOpts, err = ConfigureAuth(cparams.CommonCommandParams, remoteOpts)
87+
remoteOpts, err = registry.ConfigureAuth(
88+
cparams.CommonCommandParams.UseDockerCreds,
89+
cparams.CommonCommandParams.CredsAccount,
90+
cparams.CommonCommandParams.CredsSecret,
91+
remoteOpts)
8892
xc.FailOn(err)
8993

9094
nameOpts := []name.Option{

pkg/app/master/command/registry/handler_pull.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/mintoolkit/mint/pkg/app"
1414
"github.com/mintoolkit/mint/pkg/app/master/command"
15+
"github.com/mintoolkit/mint/pkg/app/master/registry"
1516
"github.com/mintoolkit/mint/pkg/app/master/version"
1617
cmd "github.com/mintoolkit/mint/pkg/command"
1718
"github.com/mintoolkit/mint/pkg/crt/docker/dockerclient"
@@ -78,7 +79,11 @@ func OnPullCommand(
7879
remoteOpts := []remote.Option{
7980
remote.WithContext(context.Background()),
8081
}
81-
remoteOpts, err = ConfigureAuth(cparams.CommonCommandParams, remoteOpts)
82+
remoteOpts, err = registry.ConfigureAuth(
83+
cparams.CommonCommandParams.UseDockerCreds,
84+
cparams.CommonCommandParams.CredsAccount,
85+
cparams.CommonCommandParams.CredsSecret,
86+
remoteOpts)
8287
xc.FailOn(err)
8388

8489
nameOpts := []name.Option{

0 commit comments

Comments
 (0)