Skip to content

Commit 1031330

Browse files
[KOGITO-9244] Platform.spec.platform.baseImage does not update the Dockerfile base image (#181)
[KOGITO-9244] Platform.spec.platform.baseImage does not update the Dockerfile base image --------- Signed-off-by: Davide Salerno <dsalerno@redhat.com>
1 parent 589a65e commit 1031330

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

bundle/manifests/sonataflow-operator.clusterserviceversion.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ metadata:
101101
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
102102
repository: https://github.com/kiegroup/kogito-serverless-operator
103103
support: Red Hat
104-
name: sonataflow-operator.vlatest
104+
name: sonataflow-operator.v2.0.0-snapshot
105105
namespace: placeholder
106106
spec:
107107
apiservicedefinitions: {}
@@ -446,7 +446,7 @@ spec:
446446
valueFrom:
447447
fieldRef:
448448
fieldPath: metadata.namespace
449-
image: quay.io/kiegroup/kogito-serverless-operator-nightly:latest
449+
image: quay.io/kiegroup/kogito-serverless-operator-nightly:2.0.0-snapshot
450450
livenessProbe:
451451
httpGet:
452452
path: /healthz
@@ -537,4 +537,4 @@ spec:
537537
minKubeVersion: 1.23.0
538538
provider:
539539
name: Red Hat
540-
version: latest
540+
version: 2.0.0-snapshot

controllers/builder/containerbuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func newContainerBuilderManager(managerContext buildManagerContext, config *rest
9696
}
9797

9898
func (c *containerBuilderManager) getImageBuilderForKaniko(workflowID string, imageNameTag string, workflowDefinition []byte, task *api.KanikoTask) imageBuilder {
99-
containerFile := c.commonConfig.Data[c.commonConfig.Data[configKeyDefaultBuilderResourceName]]
99+
containerFile := platform.GetCustomizedDockerfile(c.commonConfig.Data[c.commonConfig.Data[configKeyDefaultBuilderResourceName]], *c.platform)
100100
ib := NewImageBuilder(workflowID, workflowDefinition, []byte(containerFile))
101101
ib.OnNamespace(c.platform.Namespace)
102102
ib.WithPodMiddleName(workflowID)

controllers/builder/openshiftbuilder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/client-go/rest"
3030
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3131

32+
"github.com/kiegroup/kogito-serverless-operator/controllers/platform"
3233
"github.com/kiegroup/kogito-serverless-operator/workflowproj"
3334

3435
kubeutil "github.com/kiegroup/kogito-serverless-operator/utils/kubernetes"
@@ -143,7 +144,7 @@ func (o *openshiftBuilderManager) Schedule(build *operatorapi.SonataFlowBuild) e
143144

144145
func (o *openshiftBuilderManager) newDefaultBuildConfig(build *operatorapi.SonataFlowBuild) *buildv1.BuildConfig {
145146
optimizationPol := buildv1.ImageOptimizationSkipLayers
146-
dockerFile := o.commonConfig.Data[o.commonConfig.Data[configKeyDefaultBuilderResourceName]]
147+
dockerFile := platform.GetCustomizedDockerfile(o.commonConfig.Data[o.commonConfig.Data[configKeyDefaultBuilderResourceName]], *o.platform)
147148
return &buildv1.BuildConfig{
148149
ObjectMeta: metav1.ObjectMeta{Namespace: build.Namespace, Name: build.Name},
149150
Spec: buildv1.BuildConfigSpec{

controllers/platform/platformutils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package platform
1616

1717
import (
1818
"context"
19+
"regexp"
1920
"runtime"
21+
"strings"
2022
"time"
2123

2224
corev1 "k8s.io/api/core/v1"
@@ -34,6 +36,8 @@ import (
3436
operatorapi "github.com/kiegroup/kogito-serverless-operator/api/v1alpha08"
3537
)
3638

39+
var builderDockerfileFromRE = regexp.MustCompile(`FROM (.*) AS builder`)
40+
3741
// ResourceCustomizer can be used to inject code that changes the objects before they are created.
3842
type ResourceCustomizer func(object ctrl.Object) ctrl.Object
3943

@@ -142,3 +146,11 @@ func GetRegistryAddress(ctx context.Context, c client.Client) (*string, error) {
142146
}
143147
return nil, nil
144148
}
149+
150+
func GetCustomizedDockerfile(dockerfile string, platform operatorapi.SonataFlowPlatform) string {
151+
if platform.Spec.BuildPlatform.BaseImage != "" {
152+
res := builderDockerfileFromRE.FindAllStringSubmatch(dockerfile, 1)
153+
dockerfile = strings.Replace(dockerfile, strings.Trim(res[0][1], " "), platform.Spec.BuildPlatform.BaseImage, 1)
154+
}
155+
return dockerfile
156+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 Red Hat, Inc. and/or its affiliates
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package platform
16+
17+
import (
18+
"os"
19+
"regexp"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
24+
"github.com/kiegroup/kogito-serverless-operator/test"
25+
)
26+
27+
func TestSonataFlowBuildController(t *testing.T) {
28+
platform := test.GetBasePlatform()
29+
dockerfileBytes, err := os.ReadFile("../../test/builder/Dockerfile")
30+
if err != nil {
31+
assert.Fail(t, "Unable to read base Dockerfile")
32+
}
33+
dockerfile := string(dockerfileBytes)
34+
// 1 - Let's verify that the default image is used (for this unit test is quay.io/kiegroup/kogito-swf-builder-nightly:latest)
35+
resDefault := GetCustomizedDockerfile(dockerfile, *platform)
36+
foundDefault, err := regexp.MatchString("FROM quay.io/kiegroup/kogito-swf-builder-nightly:latest AS builder", resDefault)
37+
assert.NoError(t, err)
38+
assert.True(t, foundDefault)
39+
40+
// 2 - Let's try to override using the productized image
41+
platform.Spec.BuildPlatform.BaseImage = "registry.access.redhat.com/openshift-serverless-1-tech-preview/logic-swf-builder-rhel8"
42+
resProductized := GetCustomizedDockerfile(dockerfile, *platform)
43+
foundProductized, err := regexp.MatchString("FROM registry.access.redhat.com/openshift-serverless-1-tech-preview/logic-swf-builder-rhel8 AS builder", resProductized)
44+
assert.NoError(t, err)
45+
assert.True(t, foundProductized)
46+
}

test/yaml.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
sonataFlowPlatformForOpenshift = "sonataflow.org_v1alpha08_sonataflowplatform_openshift.yaml"
4040
sonataFlowBuilderConfig = "sonataflow-operator-builder-config_v1_configmap.yaml"
4141

42+
BuilderDockerfile = "builder_dockerfile.yaml"
43+
4244
configSamplesOneLevelPath = "../config/samples/"
4345
configSamplesTwoLevelPath = "../../config/samples/"
4446
e2eSamples = "test/testdata/"

0 commit comments

Comments
 (0)