diff --git a/pkg/apis/camel/v1/build_types.go b/pkg/apis/camel/v1/build_types.go index 7c1dc4e43b..f5db1e97fc 100644 --- a/pkg/apis/camel/v1/build_types.go +++ b/pkg/apis/camel/v1/build_types.go @@ -175,7 +175,8 @@ type SpectrumTask struct { // S2iTask is used to configure S2I. type S2iTask struct { - BaseTask `json:",inline"` + BaseTask `json:",inline"` + PublishTask `json:",inline"` // can be useful to share info with other tasks ContextDir string `json:"contextDir,omitempty"` // used by the ImageStream diff --git a/pkg/builder/builder_test.go b/pkg/builder/builder_test.go index 1f8c643b8b..dd12778634 100644 --- a/pkg/builder/builder_test.go +++ b/pkg/builder/builder_test.go @@ -34,7 +34,7 @@ type errorTestSteps struct { Step2 Step } -func TestFailure(t *testing.T) { +func TestBuilderFailure(t *testing.T) { c, err := test.NewFakeClient() require.NoError(t, err) @@ -74,3 +74,99 @@ func TestFailure(t *testing.T) { assert.Equal(t, v1.BuildPhaseFailed, status.Phase) assert.Equal(t, "an error", status.Error) } + +func TestS2IPublishingFailure(t *testing.T) { + c, err := test.NewFakeClient() + require.NoError(t, err) + b := New(c) + build := &v1.Build{ + Spec: v1.BuildSpec{ + Tasks: []v1.Task{ + { + S2i: &v1.S2iTask{ + BaseTask: v1.BaseTask{ + Name: "s2i", + }, + PublishTask: v1.PublishTask{ + BaseImage: "base-image", + }, + }, + }, + }, + }, + Status: v1.BuildStatus{ + RootImage: "root-image", + }, + } + + ctx := cancellable.NewContext() + status := b.Build(build).TaskByName("s2i").Do(ctx) + assert.Equal(t, v1.BuildPhaseFailed, status.Phase) + assert.NotEmpty(t, status.Error) + assert.Equal(t, "base-image", status.BaseImage) + assert.Equal(t, "root-image", status.RootImage) +} + +func TestJibPublishingFailure(t *testing.T) { + c, err := test.NewFakeClient() + require.NoError(t, err) + b := New(c) + build := &v1.Build{ + Spec: v1.BuildSpec{ + Tasks: []v1.Task{ + { + Jib: &v1.JibTask{ + BaseTask: v1.BaseTask{ + Name: "jib", + }, + PublishTask: v1.PublishTask{ + BaseImage: "base-image", + }, + }, + }, + }, + }, + Status: v1.BuildStatus{ + RootImage: "root-image", + }, + } + + ctx := cancellable.NewContext() + status := b.Build(build).TaskByName("jib").Do(ctx) + assert.Equal(t, v1.BuildPhaseFailed, status.Phase) + assert.NotEmpty(t, status.Error) + assert.Equal(t, "base-image", status.BaseImage) + assert.Equal(t, "root-image", status.RootImage) +} + +func TestSpectrumPublishingFailure(t *testing.T) { + c, err := test.NewFakeClient() + require.NoError(t, err) + b := New(c) + build := &v1.Build{ + Spec: v1.BuildSpec{ + Tasks: []v1.Task{ + { + Spectrum: &v1.SpectrumTask{ + BaseTask: v1.BaseTask{ + Name: "spectrum", + }, + PublishTask: v1.PublishTask{ + BaseImage: "base-image", + }, + }, + }, + }, + }, + Status: v1.BuildStatus{ + RootImage: "root-image", + }, + } + + ctx := cancellable.NewContext() + status := b.Build(build).TaskByName("spectrum").Do(ctx) + assert.Equal(t, v1.BuildPhaseFailed, status.Phase) + assert.NotEmpty(t, status.Error) + assert.Equal(t, "base-image", status.BaseImage) + assert.Equal(t, "root-image", status.RootImage) +} diff --git a/pkg/builder/jib.go b/pkg/builder/jib.go index 8285c907ec..1c3255ade8 100644 --- a/pkg/builder/jib.go +++ b/pkg/builder/jib.go @@ -43,18 +43,7 @@ type jibTask struct { var _ Task = &jibTask{} func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { - status := v1.BuildStatus{} - - baseImage := t.build.Status.BaseImage - if baseImage == "" { - baseImage = t.task.BaseImage - } - status.BaseImage = baseImage - rootImage := t.build.Status.RootImage - if rootImage == "" { - rootImage = t.task.BaseImage - } - status.RootImage = rootImage + status := initializeStatusFrom(t.build.Status, t.task.BaseImage) contextDir := t.task.ContextDir if contextDir == "" { @@ -80,14 +69,14 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { if !exists || empty { // this can only indicate that there are no more resources to add to the base image, // because transitive resolution is the same even if spec differs. - log.Infof("No new image to build, reusing existing image %s", baseImage) - status.Image = baseImage - return status + status.Image = status.BaseImage + log.Infof("No new image to build, reusing existing image %s", status.Image) + return *status } mavenDir := strings.ReplaceAll(contextDir, ContextDir, "maven") log.Debugf("Registry address: %s", t.task.Registry.Address) - log.Debugf("Base image: %s", baseImage) + log.Debugf("Base image: %s", status.BaseImage) registryConfigDir := "" if t.task.Registry.Secret != "" { @@ -109,7 +98,7 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { mavenArgs = append(mavenArgs, strings.Split(string(mavenCommand), " ")...) mavenArgs = append(mavenArgs, "-P", "jib") mavenArgs = append(mavenArgs, jib.JibMavenToImageParam+t.task.Image) - mavenArgs = append(mavenArgs, jib.JibMavenFromImageParam+baseImage) + mavenArgs = append(mavenArgs, jib.JibMavenFromImageParam+status.BaseImage) mavenArgs = append(mavenArgs, jib.JibMavenBaseImageCache+mavenDir+"/jib") if t.task.Configuration.ImagePlatforms != nil { platforms := strings.Join(t.task.Configuration.ImagePlatforms, ",") @@ -154,7 +143,7 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus { } } - return status + return *status } func cleanRegistryConfig(registryConfigDir string) error { diff --git a/pkg/builder/s2i.go b/pkg/builder/s2i.go index 718e9eb05b..e88387bfaf 100644 --- a/pkg/builder/s2i.go +++ b/pkg/builder/s2i.go @@ -59,7 +59,7 @@ type s2iTask struct { var _ Task = &s2iTask{} func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus { - status := v1.BuildStatus{} + status := initializeStatusFrom(t.build.Status, t.task.BaseImage) bc := &buildv1.BuildConfig{ TypeMeta: metav1.TypeMeta{ @@ -220,7 +220,7 @@ func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus { return status.Failed(err) } - return status + return *status } func (t *s2iTask) getControllerReference() metav1.Object { diff --git a/pkg/builder/spectrum.go b/pkg/builder/spectrum.go index dbe70d7b5f..ffc9ca8818 100644 --- a/pkg/builder/spectrum.go +++ b/pkg/builder/spectrum.go @@ -44,18 +44,7 @@ type spectrumTask struct { var _ Task = &spectrumTask{} func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus { - status := v1.BuildStatus{} - - baseImage := t.build.Status.BaseImage - if baseImage == "" { - baseImage = t.task.BaseImage - } - status.BaseImage = baseImage - rootImage := t.build.Status.RootImage - if rootImage == "" { - rootImage = t.task.BaseImage - } - status.RootImage = rootImage + status := initializeStatusFrom(t.build.Status, t.task.BaseImage) contextDir := t.task.ContextDir if contextDir == "" { @@ -83,17 +72,17 @@ func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus { if !exists || empty { // this can only indicate that there are no more resources to add to the base image, // because transitive resolution is the same even if spec differs. - log.Infof("No new image to build, reusing existing image %s", baseImage) - status.Image = baseImage - return status + status.Image = status.BaseImage + log.Infof("No new image to build, reusing existing image %s", status.Image) + return *status } pullInsecure := t.task.Registry.Insecure // incremental build case log.Debugf("Registry address: %s", t.task.Registry.Address) - log.Debugf("Base image: %s", baseImage) + log.Debugf("Base image: %s", status.BaseImage) - if !strings.HasPrefix(baseImage, t.task.Registry.Address) { + if !strings.HasPrefix(status.BaseImage, t.task.Registry.Address) { if pullInsecure { log.Info("Assuming secure pull because the registry for the base image and the main registry are different") pullInsecure = false @@ -122,7 +111,7 @@ func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus { PushInsecure: t.task.Registry.Insecure, PullConfigDir: registryConfigDir, PushConfigDir: registryConfigDir, - Base: baseImage, + Base: status.BaseImage, Target: t.task.Image, Stdout: newStdW, Stderr: newStdW, @@ -149,7 +138,7 @@ func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus { } } - return status + return *status } func readSpectrumLogs(newStdOut io.Reader) { diff --git a/pkg/builder/util.go b/pkg/builder/util.go index aad11bc3a0..721306f6f8 100644 --- a/pkg/builder/util.go +++ b/pkg/builder/util.go @@ -30,3 +30,20 @@ func artifactIDs(artifacts []v1.Artifact) []string { return result } + +// initializeStatusFrom helps creating a BuildStatus from scratch filling with base and root images. +func initializeStatusFrom(buildStatus v1.BuildStatus, taskBaseImage string) *v1.BuildStatus { + status := v1.BuildStatus{} + baseImage := buildStatus.BaseImage + if baseImage == "" { + baseImage = taskBaseImage + } + status.BaseImage = baseImage + rootImage := buildStatus.RootImage + if rootImage == "" { + rootImage = taskBaseImage + } + status.RootImage = rootImage + + return &status +} diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go index d3c20dc083..fb92721128 100644 --- a/pkg/trait/builder.go +++ b/pkg/trait/builder.go @@ -292,6 +292,10 @@ func (t *builderTrait) Apply(e *Environment) error { Name: "s2i", Configuration: *taskConfOrDefault(tasksConf, "s2i"), }, + PublishTask: v1.PublishTask{ + BaseImage: t.getBaseImage(e), + Image: imageName, + }, Tag: e.IntegrationKit.ResourceVersion, }}) } diff --git a/pkg/trait/builder_test.go b/pkg/trait/builder_test.go index dd831b6409..fc6b18b56b 100644 --- a/pkg/trait/builder_test.go +++ b/pkg/trait/builder_test.go @@ -90,6 +90,25 @@ func TestS2IBuilderTrait(t *testing.T) { assert.NotNil(t, env.Pipeline[0].Builder) assert.NotNil(t, env.Pipeline[1].Package) assert.NotNil(t, env.Pipeline[2].S2i) + assert.Equal(t, "root-jdk-image", env.Pipeline[2].S2i.BaseImage) + assert.Empty(t, env.Pipeline[2].S2i.Registry) +} + +func TestJibBuilderTrait(t *testing.T) { + env := createBuilderTestEnv(v1.IntegrationPlatformClusterOpenShift, v1.IntegrationPlatformBuildPublishStrategyJib, v1.BuildStrategyRoutine) + conditions, err := NewBuilderTestCatalog().apply(env) + + require.NoError(t, err) + assert.NotEmpty(t, conditions) + assert.NotEmpty(t, env.ExecutedTraits) + assert.NotNil(t, env.GetTrait("builder")) + assert.NotEmpty(t, env.Pipeline) + assert.Len(t, env.Pipeline, 3) + assert.NotNil(t, env.Pipeline[0].Builder) + assert.NotNil(t, env.Pipeline[1].Package) + assert.NotNil(t, env.Pipeline[2].Jib) + assert.Equal(t, "root-jdk-image", env.Pipeline[2].Jib.BaseImage) + assert.NotEmpty(t, env.Pipeline[2].Jib.Registry) } func createBuilderTestEnv(cluster v1.IntegrationPlatformCluster, strategy v1.IntegrationPlatformBuildPublishStrategy, buildStrategy v1.BuildStrategy) *Environment { @@ -134,6 +153,7 @@ func createBuilderTestEnv(cluster v1.IntegrationPlatformCluster, strategy v1.Int BuildConfiguration: v1.BuildConfiguration{ Strategy: buildStrategy, }, + BaseImage: "root-jdk-image", }, }, Status: v1.IntegrationPlatformStatus{