Skip to content

Commit

Permalink
fix(buildah): Add fullname default baseImage override into the ip for…
Browse files Browse the repository at this point in the history
… buildah
  • Loading branch information
gansheer committed Jun 14, 2023
1 parent d8d3bf6 commit c678e7f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/builder/buildah.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package builder
const BuildahPlatform = "BuildahPlatform"
const BuildahImage = "BuildahImage"
const BuildahDefaultImageName = "quay.io/buildah/stable"
const BuildahDefaultBaseImageName = "docker.io/library/eclipse-temurin:11"

var buildahSupportedOptions = map[string]PublishStrategyOption{
BuildahPlatform: {
Expand Down
8 changes: 8 additions & 0 deletions pkg/platform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ func applyPlatformSpec(source *v1.IntegrationPlatform, target *v1.IntegrationPla
if target.Status.Build.BaseImage == "" {
log.Debugf("Integration Platform %s [%s]: setting base image", target.Name, target.Namespace)
target.Status.Build.BaseImage = source.Status.Build.BaseImage
// Workaround to ensure the default image from buildah is full name. Any baseImage override is in charge of it's validity
if target.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyBuildah && defaults.IsBaseImageDefault() {
target.Status.Build.BaseImage = builder.BuildahDefaultBaseImageName
}
}

if target.Status.Build.Maven.LocalRepository == "" {
Expand Down Expand Up @@ -312,6 +316,10 @@ func setPlatformDefaults(p *v1.IntegrationPlatform, verbose bool) error {
if p.Status.Build.BaseImage == "" {
log.Debugf("Integration Platform %s [%s]: setting base image", p.Name, p.Namespace)
p.Status.Build.BaseImage = defaults.BaseImage()
// Workaround to ensure the default image from buildah is full name. Any baseImage override is in charge of it's validity
if p.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyBuildah && defaults.IsBaseImageDefault() {
p.Status.Build.BaseImage = builder.BuildahDefaultBaseImageName
}
}
if p.Status.Build.Maven.LocalRepository == "" {
log.Debugf("Integration Platform %s [%s]: setting local repository", p.Name, p.Namespace)
Expand Down
82 changes: 82 additions & 0 deletions pkg/platform/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/builder"
"github.com/apache/camel-k/v2/pkg/util/test"
)

Expand Down Expand Up @@ -96,6 +97,87 @@ func TestApplyGlobalPlatformSpec(t *testing.T) {
assert.Equal(t, "global_value2", ip.Status.Build.Maven.Properties["global_prop2"])
}

func TestPlatformBuildahUpdateOverrideLocalPlatformSpec(t *testing.T) {
global := v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
},
Spec: v1.IntegrationPlatformSpec{
Build: v1.IntegrationPlatformBuildSpec{
PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyBuildah,
},
},
}

c, err := test.NewFakeClient(&global)
assert.Nil(t, err)

err = ConfigureDefaults(context.TODO(), c, &global, false)
assert.Nil(t, err)
assert.Equal(t, builder.BuildahDefaultBaseImageName, global.Status.Build.BaseImage)

ip := v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Name: "local-camel-k",
Namespace: "ns",
},
Spec: v1.IntegrationPlatformSpec{
Build: v1.IntegrationPlatformBuildSpec{
PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyBuildah,
BaseImage: "overriden",
},
},
}

ip.ResyncStatusFullConfig()

applyPlatformSpec(&global, &ip)

assert.Equal(t, v1.IntegrationPlatformBuildPublishStrategyBuildah, ip.Status.Build.PublishStrategy)
assert.Equal(t, "overriden", ip.Status.Build.BaseImage)
}

func TestPlatformBuildahUpdateDefaultLocalPlatformSpec(t *testing.T) {

global := v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
},
Spec: v1.IntegrationPlatformSpec{
Build: v1.IntegrationPlatformBuildSpec{
PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyBuildah,
BaseImage: "overriden",
},
},
}

c, err := test.NewFakeClient(&global)
assert.Nil(t, err)

err = ConfigureDefaults(context.TODO(), c, &global, false)
assert.Nil(t, err)
assert.Equal(t, "overriden", global.Status.Build.BaseImage)

ip := v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Name: "local-camel-k",
Namespace: "ns",
},
Spec: v1.IntegrationPlatformSpec{
Build: v1.IntegrationPlatformBuildSpec{
PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyBuildah,
},
},
}

ip.ResyncStatusFullConfig()

applyPlatformSpec(&global, &ip)

assert.Equal(t, v1.IntegrationPlatformBuildPublishStrategyBuildah, ip.Status.Build.PublishStrategy)
assert.Equal(t, builder.BuildahDefaultBaseImageName, ip.Status.Build.BaseImage)
}

func TestRetainLocalPlatformSpec(t *testing.T) {
global := v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/defaults/defaults_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func BaseImage() string {
return envOrDefault(baseImage, "KAMEL_BASE_IMAGE", "RELATED_IMAGE_BASE")
}

func IsBaseImageDefault() bool {
return baseImage == BaseImage()
}

func OperatorImage() string {
return envOrDefault(fmt.Sprintf("%s:%s", ImageName, Version), "KAMEL_OPERATOR_IMAGE", "KAMEL_K_TEST_OPERATOR_CURRENT_IMAGE")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

func TestDefaultBaseImage(t *testing.T) {
assert.NotEmpty(t, BaseImage())
assert.True(t, IsBaseImageDefault())
}

func TestOverriddenBaseImage(t *testing.T) {
Expand All @@ -35,6 +36,7 @@ func TestOverriddenBaseImage(t *testing.T) {
overriddenImage := "xxx"
assert.NoError(t, os.Setenv(env, overriddenImage))
assert.Equal(t, overriddenImage, BaseImage())
assert.False(t, IsBaseImageDefault())
assert.NoError(t, os.Setenv(env, oldEnvVal))
}

Expand Down

0 comments on commit c678e7f

Please sign in to comment.