From 134fce66bcc7277d25fe143f000ea7482b75c94b Mon Sep 17 00:00:00 2001 From: Walter Medvedeo Date: Tue, 17 Sep 2024 16:13:33 +0200 Subject: [PATCH] kie-kogito-serverless-operator-533: Builder image is not used according to the configuration semantic (#534) (cherry picked from commit 2f3bc4b485d34fde41e62eccda0bbef349b71b0d) --- controllers/platform/platformutils.go | 13 +++++- controllers/platform/platformutils_test.go | 52 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/controllers/platform/platformutils.go b/controllers/platform/platformutils.go index 4daabc0c4..8b68a5dac 100644 --- a/controllers/platform/platformutils.go +++ b/controllers/platform/platformutils.go @@ -26,6 +26,8 @@ import ( "strings" "time" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/cfg" + "github.com/apache/incubator-kie-kogito-serverless-operator/utils" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -158,10 +160,19 @@ func GetRegistryAddress(ctx context.Context) (*string, error) { return nil, nil } -// GetCustomizedBuilderDockerfile gets the Dockerfile as defined in the default platform ConfigMap, apply any custom requirements and return. +// GetCustomizedBuilderDockerfile determines if the default Dockerfile provided by the +// sonataflow-operator-builder-config_v1_configmap.yaml must be customized to use a different builder base image, +// before building a workflow. +// The following ordered criteria are applied: +// 1) if the current platform has a configured platform.Spec.Build.Config.BaseImage, that base image must be used. +// 2) if the current sonataflow-operator-controllers-config.yaml has a configured SonataFlowBaseBuilderImageTag, that +// base image must be used. +// 3) No customization apply. func GetCustomizedBuilderDockerfile(dockerfile string, platform operatorapi.SonataFlowPlatform) string { if len(platform.Spec.Build.Config.BaseImage) > 0 { dockerfile = strings.Replace(dockerfile, GetFromImageTagDockerfile(dockerfile), platform.Spec.Build.Config.BaseImage, 1) + } else if len(cfg.GetCfg().SonataFlowBaseBuilderImageTag) > 0 { + dockerfile = strings.Replace(dockerfile, GetFromImageTagDockerfile(dockerfile), cfg.GetCfg().SonataFlowBaseBuilderImageTag, 1) } return dockerfile } diff --git a/controllers/platform/platformutils_test.go b/controllers/platform/platformutils_test.go index c0dc45cb5..5eeef0327 100644 --- a/controllers/platform/platformutils_test.go +++ b/controllers/platform/platformutils_test.go @@ -24,11 +24,17 @@ import ( "regexp" "testing" + "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/cfg" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/stretchr/testify/assert" "github.com/apache/incubator-kie-kogito-serverless-operator/test" ) +const dockerFile = "FROM docker.io/apache/default-test-kie-sonataflow-builder:main AS builder\n\n# ETC, \n\n# ETC, \n\n# ETC" + func TestSonataFlowBuildController(t *testing.T) { platform := test.GetBasePlatform() dockerfileBytes, err := os.ReadFile("../../test/builder/Dockerfile") @@ -49,3 +55,49 @@ func TestSonataFlowBuildController(t *testing.T) { assert.NoError(t, err) assert.True(t, foundProductized) } + +func TestGetCustomizedBuilderDockerfile_NoBaseImageCustomization(t *testing.T) { + sfp := v1alpha08.SonataFlowPlatform{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: v1alpha08.SonataFlowPlatformSpec{}, + Status: v1alpha08.SonataFlowPlatformStatus{}, + } + customizedDockerfile := GetCustomizedBuilderDockerfile(dockerFile, sfp) + assert.Equal(t, dockerFile, customizedDockerfile) +} + +func TestGetCustomizedBuilderDockerfile_BaseImageCustomizationFromPlatform(t *testing.T) { + sfp := v1alpha08.SonataFlowPlatform{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: v1alpha08.SonataFlowPlatformSpec{ + Build: v1alpha08.BuildPlatformSpec{ + Template: v1alpha08.BuildTemplate{}, + Config: v1alpha08.BuildPlatformConfig{ + BaseImage: "docker.io/apache/platfom-sonataflow-builder:main", + }, + }, + }, + Status: v1alpha08.SonataFlowPlatformStatus{}, + } + + expectedDockerFile := "FROM docker.io/apache/platfom-sonataflow-builder:main AS builder\n\n# ETC, \n\n# ETC, \n\n# ETC" + customizedDockerfile := GetCustomizedBuilderDockerfile(dockerFile, sfp) + assert.Equal(t, expectedDockerFile, customizedDockerfile) +} + +func TestGetCustomizedBuilderDockerfile_BaseImageCustomizationFromControllersConfig(t *testing.T) { + sfp := v1alpha08.SonataFlowPlatform{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: v1alpha08.SonataFlowPlatformSpec{}, + Status: v1alpha08.SonataFlowPlatformStatus{}, + } + + _, err := cfg.InitializeControllersCfgAt("../cfg/testdata/controllers-cfg-test.yaml") + assert.NoError(t, err) + expectedDockerFile := "FROM local/sonataflow-builder:1.0.0 AS builder\n\n# ETC, \n\n# ETC, \n\n# ETC" + customizedDockerfile := GetCustomizedBuilderDockerfile(dockerFile, sfp) + assert.Equal(t, expectedDockerFile, customizedDockerfile) +}