Skip to content

Commit

Permalink
fix: Debug sidecar to use the same platform as the target
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Jan 19, 2023
1 parent 71ec8ee commit 566128e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
7 changes: 7 additions & 0 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type options struct {
quiet bool

runtime string
platform string
namespace string
}

Expand Down Expand Up @@ -160,6 +161,12 @@ func NewCommand(cli cliutil.CLI) *cobra.Command {
"",
`Runtime address ("/var/run/docker.sock" | "/run/containerd/containerd.sock" | "https://<kube-api-addr>:8433/...)`,
)
flags.StringVar(
&opts.platform,
"platform",
"",
`Platform (e.g., linux/amd64, linux/arm64) of the target container (for some runtimes it's hard to detect it automatically, but the debug sidecar must be of the same platform as the target)`,
)

return cmd
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/exec/exec_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/platforms"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -79,7 +80,16 @@ func runDebuggerContainerd(ctx context.Context, cli cliutil.CLI, opts *options)
}

cli.PrintAux("Pulling debugger image...\n")
image, err := client.ImagePullEx(ctx, opts.image)
image, err := client.ImagePullEx(
ctx,
opts.image,
func() string {
if len(opts.platform) == 0 {
return platforms.Format(platforms.DefaultSpec())
}
return opts.platform
}(),
)
if err != nil {
return errCannotPull(opts.image, err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/exec/exec_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ func runDebuggerDocker(ctx context.Context, cli cliutil.CLI, opts *options) erro
}

cli.PrintAux("Pulling debugger image...\n")
if err := client.ImagePullEx(ctx, opts.image, types.ImagePullOptions{}); err != nil {
if err := client.ImagePullEx(ctx, opts.image, types.ImagePullOptions{
Platform: func() string {
if len(opts.platform) == 0 {
return target.Platform
}
return opts.platform
}(),
}); err != nil {
return errCannotPull(opts.image, err)
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/portforward/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func runPortForward(ctx context.Context, cli cliutil.CLI, opts *options) error {

// TODO: Pull only if not present locally.
cli.PrintAux("Pulling forwarder image...\n")
if err := client.ImagePullEx(ctx, forwarderImage, types.ImagePullOptions{}); err != nil {
if err := client.ImagePullEx(ctx, forwarderImage, types.ImagePullOptions{
// Platform: ... TODO: Test if an arm64 sidecar can be attached to an amd64 target and vice versa.
}); err != nil {
return fmt.Errorf("cannot pull forwarder image %q: %w", forwarderImage, err)
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (c *Client) ContainerRemoveEx(
func (c *Client) ImagePullEx(
ctx context.Context,
ref string,
platform string,
) (containerd.Image, error) {
if !strings.Contains(ref, ":") {
ref = ref + ":latest"
Expand All @@ -102,7 +103,12 @@ func (c *Client) ImagePullEx(
close(progressCh)
}()

image, err := c.Pull(ctx, ref, containerd.WithPullUnpack)
image, err := c.Pull(
ctx,
ref,
containerd.WithPullUnpack,
containerd.WithPlatform(platform),
)
stopProgress()
if err != nil {
return image, err
Expand Down

0 comments on commit 566128e

Please sign in to comment.