Skip to content

Commit

Permalink
kie-kogito-serverless-operator-533: Builder image is not used accordi…
Browse files Browse the repository at this point in the history
…ng to the configuration semantic (apache#534)

(cherry picked from commit 2f3bc4b)
  • Loading branch information
wmedvede committed Sep 17, 2024
1 parent d556021 commit 134fce6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
13 changes: 12 additions & 1 deletion controllers/platform/platformutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
52 changes: 52 additions & 0 deletions controllers/platform/platformutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}

0 comments on commit 134fce6

Please sign in to comment.