-
-
Notifications
You must be signed in to change notification settings - Fork 503
feat: add ability to configure platforms of the deployed images #1603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,3 @@ qodana.yaml | |
|
||
# Pipenv | ||
Pipfile* | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,19 +27,21 @@ import ( | |||||
"fmt" | ||||||
"io" | ||||||
|
||||||
"github.com/containerd/platforms" | ||||||
"github.com/docker/docker/api/types" | ||||||
"github.com/docker/docker/api/types/container" | ||||||
"github.com/docker/docker/api/types/filters" | ||||||
dockerimage "github.com/docker/docker/api/types/image" | ||||||
"github.com/docker/docker/client" | ||||||
l "github.com/k3d-io/k3d/v5/pkg/logger" | ||||||
k3d "github.com/k3d-io/k3d/v5/pkg/types" | ||||||
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||||||
"github.com/sirupsen/logrus" | ||||||
) | ||||||
|
||||||
// createContainer creates a new docker container from translated specs | ||||||
func createContainer(ctx context.Context, dockerNode *NodeInDocker, name string) (string, error) { | ||||||
l.Log().Tracef("Creating docker container with translated config\n%+v\n", dockerNode) | ||||||
l.Log().Tracef("Creating docker container with translated config: %s\n%+v\n", name, dockerNode) | ||||||
|
||||||
// initialize docker client | ||||||
docker, err := GetDockerClient() | ||||||
|
@@ -51,10 +53,10 @@ func createContainer(ctx context.Context, dockerNode *NodeInDocker, name string) | |||||
// create container | ||||||
var resp container.CreateResponse | ||||||
for { | ||||||
resp, err = docker.ContainerCreate(ctx, &dockerNode.ContainerConfig, &dockerNode.HostConfig, &dockerNode.NetworkingConfig, nil, name) | ||||||
resp, err = docker.ContainerCreate(ctx, &dockerNode.ContainerConfig, &dockerNode.HostConfig, &dockerNode.NetworkingConfig, dockerNode.PlatformConfig, name) | ||||||
if err != nil { | ||||||
if client.IsErrNotFound(err) { | ||||||
if err := pullImage(ctx, docker, dockerNode.ContainerConfig.Image); err != nil { | ||||||
if err := pullImage(ctx, docker, dockerNode.ContainerConfig.Image, dockerNode.PlatformConfig); err != nil { | ||||||
return "", fmt.Errorf("docker failed to pull image '%s': %w", dockerNode.ContainerConfig.Image, err) | ||||||
} | ||||||
continue | ||||||
|
@@ -105,8 +107,15 @@ func removeContainer(ctx context.Context, ID string) error { | |||||
} | ||||||
|
||||||
// pullImage pulls a container image and outputs progress if --verbose flag is set | ||||||
func pullImage(ctx context.Context, docker client.APIClient, image string) error { | ||||||
resp, err := docker.ImagePull(ctx, image, dockerimage.PullOptions{}) | ||||||
func pullImage(ctx context.Context, docker client.APIClient, image string, platform *ocispecv1.Platform) error { | ||||||
opts := dockerimage.PullOptions{} | ||||||
|
||||||
if platform != nil { | ||||||
// if a platform is specified, use it to pull the image | ||||||
opts.Platform = platforms.Format(*platform) | ||||||
} | ||||||
|
||||||
resp, err := docker.ImagePull(ctx, image, opts) | ||||||
if err != nil { | ||||||
return fmt.Errorf("docker failed to pull the image '%s': %w", image, err) | ||||||
} | ||||||
|
@@ -168,7 +177,7 @@ func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, er | |||||
|
||||||
// executes an arbitrary command in a container while returning its exit code. | ||||||
// useful to check something in docker env | ||||||
func executeCheckInContainer(ctx context.Context, image string, cmd []string) (int64, error) { | ||||||
func executeCheckInContainer(ctx context.Context, image string, platform *ocispecv1.Platform, cmd []string) (int64, error) { | ||||||
docker, err := GetDockerClient() | ||||||
if err != nil { | ||||||
return -1, fmt.Errorf("failed to create docker client: %w", err) | ||||||
|
@@ -186,7 +195,7 @@ func executeCheckInContainer(ctx context.Context, image string, cmd []string) (i | |||||
}, nil, nil, nil, "") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The container creation in executeCheckInContainer doesn't use the platform parameter. The platform should be passed as the 4th argument to ContainerCreate to ensure the correct platform is used when creating the temporary container.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
if err != nil { | ||||||
if client.IsErrNotFound(err) { | ||||||
if err := pullImage(ctx, docker, image); err != nil { | ||||||
if err := pullImage(ctx, docker, image, platform); err != nil { | ||||||
return -1, fmt.Errorf("docker failed to pull image '%s': %w", image, err) | ||||||
} | ||||||
continue | ||||||
|
@@ -219,11 +228,11 @@ func executeCheckInContainer(ctx context.Context, image string, cmd []string) (i | |||||
} | ||||||
|
||||||
// CheckIfDirectoryExists checks for the existence of a given path inside the docker environment | ||||||
func CheckIfDirectoryExists(ctx context.Context, image string, dir string) (bool, error) { | ||||||
func CheckIfDirectoryExists(ctx context.Context, image string, platform *ocispecv1.Platform, dir string) (bool, error) { | ||||||
l.Log().Tracef("checking if dir %s exists in docker environment...", dir) | ||||||
shellCmd := fmt.Sprintf("[ -d \"%s\" ] && exit 0 || exit 1", dir) | ||||||
cmd := []string{"sh", "-c", shellCmd} | ||||||
exitCode, err := executeCheckInContainer(ctx, image, cmd) | ||||||
exitCode, err := executeCheckInContainer(ctx, image, platform, cmd) | ||||||
l.Log().Tracef("check dir container returned %d exit code", exitCode) | ||||||
return exitCode == 0, err | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,14 +7,14 @@ LOGFILE="/var/log/k3d-entrypoints_$(date "+%y%m%d%H%M%S").log" | |||||
|
||||||
touch "$LOGFILE" | ||||||
|
||||||
echo "[$(date -Iseconds)] Running k3d entrypoints..." >> "$LOGFILE" | ||||||
echo "[$(date -Iseconds)] Running k3d entrypoints..." | tee -a "$LOGFILE" | ||||||
|
||||||
for entrypoint in /bin/k3d-entrypoint-*.sh ; do | ||||||
echo "[$(date -Iseconds)] Running $entrypoint" >> "$LOGFILE" | ||||||
"$entrypoint" >> "$LOGFILE" 2>&1 || exit 1 | ||||||
for entrypoint in /bin/k3d-entrypoint-*.sh; do | ||||||
echo "[$(date -Iseconds)] Running $entrypoint" | tee -a "$LOGFILE" | ||||||
eval "$entrypoint" 2>&1 | tee "$LOGFILE" || exit 1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using eval on the entrypoint script path is unsafe and unnecessary. This should be a direct execution like the original code:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
done | ||||||
|
||||||
echo "[$(date -Iseconds)] Finished k3d entrypoint scripts!" >> "$LOGFILE" | ||||||
echo "[$(date -Iseconds)] Finished k3d entrypoint scripts!" | tee -a "$LOGFILE" | ||||||
|
||||||
/bin/k3s "$@" & | ||||||
k3s_pid=$! | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a spelling error in the error message. 'RuntimePlatformmapping' should be 'Runtime platform mapping' with proper spacing.
Copilot uses AI. Check for mistakes.