From 45ca4503048f67c4e0b07785a45e3e345e0cc2a0 Mon Sep 17 00:00:00 2001 From: Thomas Diesler Date: Tue, 26 Mar 2024 18:24:25 +0100 Subject: [PATCH] (fix#5280): Basic integration fails on ARM64 --- pkg/builder/jib.go | 31 +++++++++++++++++++++++++++++++ pkg/util/jib/configuration.go | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/builder/jib.go b/pkg/builder/jib.go index 264c95a085..70453982dd 100644 --- a/pkg/builder/jib.go +++ b/pkg/builder/jib.go @@ -19,6 +19,7 @@ package builder import ( "context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os" "os/exec" "path/filepath" @@ -107,12 +108,20 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { return status.Failed(err) } + arch, err := t.GetControllerArchitecture(ctx, t.c) + if err != nil { + return status.Failed(err) + } + mavenArgs := make([]string, 0) mavenArgs = append(mavenArgs, jib.JibMavenGoal) mavenArgs = append(mavenArgs, strings.Split(string(mavenCommand), " ")...) mavenArgs = append(mavenArgs, "-P", "jib") mavenArgs = append(mavenArgs, jib.JibMavenToImageParam+t.task.Image) mavenArgs = append(mavenArgs, jib.JibMavenFromImageParam+baseImage) + if arch == "amd64" || arch == "arm64" { + mavenArgs = append(mavenArgs, jib.JibMavenFromPlatforms+"linux/"+arch) + } if t.task.Registry.Insecure { mavenArgs = append(mavenArgs, jib.JibMavenInsecureRegistries+"true") } @@ -142,3 +151,25 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { return status } + +func (t *jibTask) GetControllerArchitecture(ctx context.Context, c client.Client) (string, error) { + + podName := os.Getenv("POD_NAME") + namespace, _ := c.GetCurrentNamespace("") + log.Infof("Fetching image architecture for pod %s in namespace %s", podName, namespace) + + // Get the pod by name + pod, err := c.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{}) + if err != nil { + panic(err.Error()) + } + + // Note, it might be better to inspect the image metadata + // by querying the registry with the image id + image := pod.Spec.Containers[0].Image + if strings.HasSuffix(image, "-amd64") || strings.HasSuffix(image, "-arm64") { + return image[len(image)-5:], nil + } + + return "", nil +} diff --git a/pkg/util/jib/configuration.go b/pkg/util/jib/configuration.go index ac54676e4c..196fd85194 100644 --- a/pkg/util/jib/configuration.go +++ b/pkg/util/jib/configuration.go @@ -36,9 +36,10 @@ import ( const JibMavenGoal = "jib:build" const JibMavenToImageParam = "-Djib.to.image=" const JibMavenFromImageParam = "-Djib.from.image=" +const JibMavenFromPlatforms = "-Djib.from.platforms=" const JibMavenInsecureRegistries = "-Djib.allowInsecureRegistries=" const JibDigestFile = "target/jib-image.digest" -const JibMavenPluginVersionDefault = "3.3.2" +const JibMavenPluginVersionDefault = "3.4.1" const JibLayerFilterExtensionMavenVersionDefault = "0.3.0" type JibBuild struct {