Skip to content

Commit

Permalink
basic registry pull subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kcq committed Jan 10, 2022
1 parent f24f35d commit f3758b4
Show file tree
Hide file tree
Showing 459 changed files with 72,007 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -32,6 +32,7 @@ require (
github.com/gocolly/colly/v2 v2.0.1
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-containerregistry v0.8.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gorilla/websocket v1.4.2
github.com/gosuri/uilive v0.0.3 // indirect
Expand Down
356 changes: 356 additions & 0 deletions go.sum

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion pkg/app/master/commands/registry/cli.go
Expand Up @@ -27,11 +27,13 @@ func fullCmdName(subCmdName string) string {
}

type PullCommandParams struct {
TargetRef string
SaveToDocker bool
}

func PullCommandFlagValues(ctx *cli.Context) (*PullCommandParams, error) {
values := &PullCommandParams{
TargetRef: ctx.String(commands.FlagTarget),
SaveToDocker: ctx.Bool(FlagSaveToDocker),
}

Expand All @@ -47,9 +49,12 @@ var CLI = &cli.Command{
Name: PullCmdName,
Usage: PullCmdNameUsage,
Flags: []cli.Flag{
commands.Cflag(commands.FlagTarget),
cflag(FlagSaveToDocker),
},
Action: func(ctx *cli.Context) error {
xc := app.NewExecutionContext(fullCmdName(PullCmdName))

gcvalues, err := commands.GlobalFlagValues(ctx)
if err != nil {
return err
Expand All @@ -60,7 +65,16 @@ var CLI = &cli.Command{
return err
}

xc := app.NewExecutionContext(fullCmdName(PullCmdName))
if cparams.TargetRef == "" {
if ctx.Args().Len() < 1 {
xc.Out.Error("param.target", "missing target")
cli.ShowCommandHelp(ctx, Name)
return nil
} else {
cparams.TargetRef = ctx.Args().First()
}
}

OnPullCommand(xc, gcvalues, cparams)
return nil
},
Expand Down
61 changes: 59 additions & 2 deletions pkg/app/master/commands/registry/handler.go
Expand Up @@ -3,6 +3,12 @@ package registry
import (
"fmt"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
gocrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/daemon"
log "github.com/sirupsen/logrus"

"github.com/docker-slim/docker-slim/pkg/app"
"github.com/docker-slim/docker-slim/pkg/app/master/commands"
"github.com/docker-slim/docker-slim/pkg/app/master/docker/dockerclient"
Expand All @@ -12,8 +18,6 @@ import (
"github.com/docker-slim/docker-slim/pkg/util/errutil"
"github.com/docker-slim/docker-slim/pkg/util/fsutil"
v "github.com/docker-slim/docker-slim/pkg/version"

log "github.com/sirupsen/logrus"
)

const appName = commands.AppName
Expand All @@ -33,8 +37,13 @@ func OnPullCommand(

cmdReport := report.NewRegistryCommand(gparams.ReportLocation, gparams.InContainer)
cmdReport.State = command.StateStarted
cmdReport.TargetReference = cparams.TargetRef

xc.Out.State("started")
xc.Out.Info("params",
ovars{
"cmd.params": fmt.Sprintf("%+v", cparams),
})

client, err := dockerclient.New(gparams.ClientConfig)
if err == dockerclient.ErrNoDockerInfo {
Expand Down Expand Up @@ -63,6 +72,24 @@ func OnPullCommand(
version.Print(prefix, logger, client, false, gparams.InContainer, gparams.IsDSImage)
}

//todo: pass a custom client to Pull (based on `client` above)
targetImage, err := crane.Pull(cparams.TargetRef)
errutil.FailOn(err)
outImageInfo(xc, targetImage)

if cparams.SaveToDocker {
xc.Out.State("save.docker.start")

tag, err := name.NewTag(cparams.TargetRef)
errutil.FailOn(err)

rawResponse, err := daemon.Write(tag, targetImage)
errutil.FailOn(err)
logger.Tracef("Image save to Docker response: %v", rawResponse)

xc.Out.State("save.docker.done")
}

xc.Out.State("completed")
cmdReport.State = command.StateCompleted
xc.Out.State("done")
Expand Down Expand Up @@ -194,3 +221,33 @@ func OnCopyCommand(
})
}
}

func outImageInfo(
xc *app.ExecutionContext,
targetImage gocrv1.Image) {
cn, err := targetImage.ConfigName()
errutil.FailOn(err)

d, err := targetImage.Digest()
errutil.FailOn(err)

cf, err := targetImage.ConfigFile()
errutil.FailOn(err)

m, err := targetImage.Manifest()
errutil.FailOn(err)

xc.Out.Info("image.info",
ovars{
"id": fmt.Sprintf("%s:%s", cn.Algorithm, cn.Hex),
"digest": fmt.Sprintf("%s:%s", d.Algorithm, d.Hex),
"architecture": cf.Architecture,
"os": cf.OS,
"manifest.schema": m.SchemaVersion,
"manifest.media_type": m.MediaType,
"manifest.config.media_type": m.Config.MediaType,
"manifest.config.size": fmt.Sprintf("%v", m.Config.Size),
"manifest.config.digest": fmt.Sprintf("%s:%s", m.Config.Digest.Algorithm, m.Config.Digest.Hex),
"manifest.layers.count": fmt.Sprintf("%v", len(m.Layers)),
})
}
93 changes: 93 additions & 0 deletions vendor/github.com/containerd/containerd/errdefs/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions vendor/github.com/containerd/containerd/errdefs/grpc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f3758b4

Please sign in to comment.