Skip to content

Commit

Permalink
fix: Fix import image-bundle command (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Mar 31, 2022
1 parent 2c8311e commit e15f61c
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 39 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ linters-settings:
sections:
- Standard
- Default
- Prefix(github.com/mesosphere)
- Prefix(github.com/mesosphere/mindthegap)
gocritic:
enabled-tags:
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
golangci-lint 1.44.2
goreleaser 1.6.3
golang 1.17.8
golang 1.17.8 # FREEZE until golangci-lint fully supports go 1.18
pre-commit 2.17.0
upx 3.96
3 changes: 2 additions & 1 deletion cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package create

import (
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cmd/create/imagebundle"
)

Expand Down
4 changes: 3 additions & 1 deletion cmd/create/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"path/filepath"

"github.com/distribution/distribution/v3/manifest/manifestlist"
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/archive"
"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/config"
Expand Down Expand Up @@ -226,6 +227,7 @@ func NewCommand(out output.Output) *cobra.Command {
skopeo.OS(p.os),
skopeo.Arch(p.arch),
skopeo.Variant(p.variant),
skopeo.PreserveDigests(),
)...,
)
if err != nil {
Expand Down
39 changes: 36 additions & 3 deletions cmd/importcmd/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"os"
"path/filepath"

"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/containerd"
"github.com/mesosphere/mindthegap/docker/registry"
"github.com/mesosphere/mindthegap/skopeo"
)

func NewCommand(out output.Output) *cobra.Command {
Expand Down Expand Up @@ -74,21 +76,52 @@ func NewCommand(out output.Output) *cobra.Command {
}()
out.EndOperation(true)

skopeoRunner, skopeoCleanup := skopeo.NewRunner()
cleaner.AddCleanupFn(func() { _ = skopeoCleanup() })

ociExportsTempDir, err := os.MkdirTemp("", ".oci-exports-*")
if err != nil {
return fmt.Errorf("failed to create temporary directory for OCI exports: %w", err)
}
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(ociExportsTempDir) })

for registryName, registryConfig := range cfg {
for imageName, imageTags := range registryConfig.Images {
for _, imageTag := range imageTags {
srcImageName := fmt.Sprintf("%s/%s:%s", reg.Address(), imageName, imageTag)
destImageName := fmt.Sprintf("%s/%s:%s", registryName, imageName, imageTag)

out.StartOperation(fmt.Sprintf("Importing %s", destImageName))
ctrOutput, err := containerd.ImportImage(
context.TODO(), srcImageName, destImageName, containerdNamespace,

exportTarball := filepath.Join(ociExportsTempDir, "oci-export.tar")

skopeoStdout, skopeoStderr, err := skopeoRunner.Copy(context.TODO(),
fmt.Sprintf("docker://%s", srcImageName),
fmt.Sprintf("oci-archive:%s:%s", exportTarball, destImageName),
skopeo.All(),
skopeo.DisableSrcTLSVerify(),
)
if err != nil {
out.EndOperation(false)
out.Infof("---skopeo stdout---:\n%s", skopeoStdout)
out.Infof("---skopeo stderr---:\n%s", skopeoStderr)
return err
}
out.V(4).Infof("---skopeo stdout---:\n%s", skopeoStdout)
out.V(4).Infof("---skopeo stderr---:\n%s", skopeoStderr)

ctrOutput, err := containerd.ImportImageArchive(
context.TODO(), exportTarball, containerdNamespace,
)
if err != nil {
out.Info(string(ctrOutput))
out.EndOperation(false)
return err
}
out.V(4).Info(string(ctrOutput))

_ = os.Remove(exportTarball)

out.EndOperation(true)
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/importcmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package importcmd

import (
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cmd/importcmd/imagebundle"
)

Expand Down
7 changes: 5 additions & 2 deletions cmd/push/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"os"
"path/filepath"

"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/ecr"
Expand Down Expand Up @@ -82,7 +83,9 @@ func NewCommand(out output.Output) *cobra.Command {
skopeoRunner, skopeoCleanup := skopeo.NewRunner()
cleaner.AddCleanupFn(func() { _ = skopeoCleanup() })

var skopeoOpts []skopeo.SkopeoOption
skopeoOpts := []skopeo.SkopeoOption{
skopeo.PreserveDigests(),
}
if destRegistryUsername != "" && destRegistryPassword != "" {
skopeoOpts = append(
skopeoOpts,
Expand Down
3 changes: 2 additions & 1 deletion cmd/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package push

import (
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cmd/push/imagebundle"
)

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"io"
"os"

"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/cmd/root"
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/mindthegap/cmd/create"
"github.com/mesosphere/mindthegap/cmd/importcmd"
Expand Down
3 changes: 2 additions & 1 deletion cmd/serve/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"fmt"
"os"

"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/docker/registry"
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package serve

import (
"github.com/mesosphere/dkp-cli-runtime/core/output"
"github.com/spf13/cobra"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cmd/serve/imagebundle"
)

Expand Down
30 changes: 6 additions & 24 deletions containerd/ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,19 @@ import (

type CtrOption func() string

func ImportImage(ctx context.Context, src, tag, containerdNamespace string) ([]byte, error) {
func ImportImageArchive(
ctx context.Context,
archivePath, containerdNamespace string,
) ([]byte, error) {
baseArgs := []string{"-n", containerdNamespace}
//nolint:gosec // Args are fine.
cmd := exec.CommandContext(
ctx,
"ctr",
append(baseArgs, []string{"images", "pull", "--plain-http", src}...)...)
append(baseArgs, []string{"images", "import", "--no-unpack", archivePath}...)...)
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
return cmdOutput, fmt.Errorf("failed to pull image from temporary docker registry: %w", err)
}

if tag != "" {
//nolint:gosec // Args are fine.
cmd = exec.CommandContext(
ctx,
"ctr",
append(baseArgs, []string{"images", "tag", "--force", src, tag}...)...)
tagOutput, err := cmd.CombinedOutput()
cmdOutput = append(cmdOutput, tagOutput...)
if err != nil {
return cmdOutput, fmt.Errorf("failed to tag image: %w", err)
}

//nolint:gosec // Args are fine.
cmd = exec.CommandContext(ctx, "ctr", append(baseArgs, []string{"images", "rm", src}...)...)
rmOutput, err := cmd.CombinedOutput()
cmdOutput = append(cmdOutput, rmOutput...)
if err != nil {
return cmdOutput, fmt.Errorf("failed to tag image: %w", err)
}
return cmdOutput, fmt.Errorf("failed to import image(s) from image archive: %w", err)
}

return cmdOutput, nil
Expand Down
16 changes: 14 additions & 2 deletions skopeo/skopeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func All() SkopeoOption {
}
}

func IndexOnly() SkopeoOption {
return func() string {
return "--multi-arch=index-only"
}
}

func SrcCredentials(username, password string) SkopeoOption {
return func() string {
return fmt.Sprintf("--src-creds=%s:%s", username, password)
Expand All @@ -89,6 +95,12 @@ func DestCredentials(username, password string) SkopeoOption {
}
}

func PreserveDigests() SkopeoOption {
return func() string {
return "--preserve-digests"
}
}

type Runner struct {
unpacked sync.Once
unpackedSkopeoPath string
Expand Down Expand Up @@ -138,7 +150,6 @@ func (r *Runner) Copy(
copyArgs := []string{
"copy",
"--policy", r.unpackedSkopeoPolicyPath,
"--preserve-digests",
src,
dest,
}
Expand Down Expand Up @@ -241,7 +252,8 @@ func (r *Runner) CopyManifest(
ctx,
"dir:"+td,
dest,
append(opts, func() string { return "--multi-arch=index-only" })...)
append(opts, PreserveDigests(), IndexOnly())...,
)
}

func (r *Runner) run(
Expand Down

0 comments on commit e15f61c

Please sign in to comment.