Skip to content

Commit

Permalink
Issue-434: Forcing pulling images from BC when latest is set (apache#435
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ricardozanini authored and rgdoliveira committed Apr 16, 2024
1 parent 70790f8 commit f4491e6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/check-container-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:

env:
REGISTRY_STORAGE_DELETE_ENABLED : true
GO_VERSION: 1.21

jobs:
unit-tests:
Expand All @@ -31,7 +32,8 @@ jobs:
- name: Setup golang
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: ${{ env.GO_VERSION }}
cache: true
- name: Checkout code
uses: actions/checkout@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions controllers/builder/openshiftbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (o *openshiftBuilderManager) Schedule(build *operatorapi.SonataFlowBuild) e
func (o *openshiftBuilderManager) newDefaultBuildConfig(build *operatorapi.SonataFlowBuild, workflow *operatorapi.SonataFlow) *buildv1.BuildConfig {
optimizationPol := buildv1.ImageOptimizationSkipLayers
dockerFile := platform.GetCustomizedBuilderDockerfile(o.builderConfigMap.Data[defaultBuilderResourceName], *o.platform)
forcePull := kubeutil.GetImageTag(platform.GetFromImageTagDockerfile(dockerFile)) == "latest"
return &buildv1.BuildConfig{
ObjectMeta: metav1.ObjectMeta{Namespace: build.Namespace, Name: build.Name},
Spec: buildv1.BuildConfigSpec{
Expand All @@ -168,6 +169,7 @@ func (o *openshiftBuilderManager) newDefaultBuildConfig(build *operatorapi.Sonat
ImageOptimizationPolicy: &optimizationPol,
BuildArgs: build.Spec.BuildArgs,
Env: build.Spec.Envs,
ForcePull: forcePull,
},
},
Output: buildv1.BuildOutput{
Expand Down
33 changes: 33 additions & 0 deletions controllers/builder/openshiftbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,36 @@ func Test_openshiftbuilder_externalCMs(t *testing.T) {

assert.Len(t, bc.Spec.Source.ConfigMaps, 1)
}

func Test_openshiftbuilder_forcePull(t *testing.T) {
// Setup
ns := t.Name()
workflow := test.GetBaseSonataFlow(ns)
platform := test.GetBasePlatformInReadyPhase(t.Name())
config := test.GetSonataFlowBuilderConfig(ns)
namespacedName := types.NamespacedName{Namespace: workflow.Namespace, Name: workflow.Name}
client := test.NewKogitoClientBuilderWithOpenShift().WithRuntimeObjects(workflow, platform, config).Build()
buildClient := buildfake.NewSimpleClientset().BuildV1()
managerContext := buildManagerContext{
ctx: context.TODO(),
client: client,
platform: platform,
builderConfigMap: config,
}

buildManager := newOpenShiftBuilderManagerWithClient(managerContext, buildClient)
// End Setup

// Schedule a build
kogitoBuildManager := NewSonataFlowBuildManager(context.TODO(), client)
kbuild, err := kogitoBuildManager.GetOrCreateBuild(workflow)
assert.NoError(t, err)
assert.NotNil(t, kbuild)
assert.NoError(t, buildManager.Schedule(kbuild))

bc := &buildv1.BuildConfig{}
assert.NoError(t, client.Get(context.TODO(), namespacedName, bc))

// verify if we set force pull to BC
assert.True(t, bc.Spec.Strategy.DockerStrategy.ForcePull)
}
8 changes: 6 additions & 2 deletions controllers/platform/platformutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ func GetRegistryAddress(ctx context.Context, c client.Client) (*string, error) {
// GetCustomizedBuilderDockerfile gets the Dockerfile as defined in the default platform ConfigMap, apply any custom requirements and return.
func GetCustomizedBuilderDockerfile(dockerfile string, platform operatorapi.SonataFlowPlatform) string {
if len(platform.Spec.Build.Config.BaseImage) > 0 {
res := builderDockerfileFromRE.FindAllStringSubmatch(dockerfile, 1)
dockerfile = strings.Replace(dockerfile, strings.Trim(res[0][1], " "), platform.Spec.Build.Config.BaseImage, 1)
dockerfile = strings.Replace(dockerfile, GetFromImageTagDockerfile(dockerfile), platform.Spec.Build.Config.BaseImage, 1)
}
return dockerfile
}

func GetFromImageTagDockerfile(dockerfile string) string {
res := builderDockerfileFromRE.FindAllStringSubmatch(dockerfile, 1)
return strings.Trim(res[0][1], " ")
}
16 changes: 14 additions & 2 deletions utils/kubernetes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// GetImagePullPolicy gets the default corev1.PullPolicy depending on the image tag specified.
// It follows the conventions of docker client and OpenShift. If no tag specified, it assumes latest.
// Returns PullAlways if latest tag, empty otherwise to let the cluster figure it out.
// See: https://kubernetes.io/docs/concepts/containers/images/#updating-images
func GetImagePullPolicy(imageTag string) corev1.PullPolicy {
if len(imageTag) == 0 {
return ""
Expand All @@ -31,9 +32,20 @@ func GetImagePullPolicy(imageTag string) corev1.PullPolicy {
if idx < 0 {
return corev1.PullAlways
}
tag := imageTag[idx+1:]
if tag == "latest" {
if GetImageTag(imageTag) == "latest" {
return corev1.PullAlways
}
return ""
}

// GetImageTag gets the tag after `:` in an image tag or empty if not found.
func GetImageTag(imageTag string) string {
if len(imageTag) == 0 {
return ""
}
idx := strings.LastIndex(imageTag, ":")
if idx < 0 {
return ""
}
return imageTag[idx+1:]
}
1 change: 1 addition & 0 deletions utils/kubernetes/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestGetImagePullPolicy(t *testing.T) {
{"Long name with tag", args{"gcr.io/knative-releases/knative.dev/eventing/cmd/event_display:1.2"}, ""},
{"Empty tag", args{""}, ""},
{"Messy tag", args{":"}, ""},
{"Sha tag", args{"ubuntu@sha256:3235326357dfb65f1781dbc4df3b834546d8bf914e82cce58e6e6b676e23ce8f"}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions workflowproj/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/apache/incubator-kie-kogito-serverless-operator/workflowproj

go 1.21

toolchain go1.21.6

// Internal dependencies
replace github.com/apache/incubator-kie-kogito-serverless-operator/api v0.0.0 => ../api

Expand Down

0 comments on commit f4491e6

Please sign in to comment.