Skip to content

Commit

Permalink
fix(builder): add root and base image to S2I report
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed May 28, 2024
1 parent bfcd9fb commit 9de2092
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 41 deletions.
3 changes: 2 additions & 1 deletion pkg/apis/camel/v1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 97 additions & 1 deletion pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
25 changes: 7 additions & 18 deletions pkg/builder/jib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 != "" {
Expand All @@ -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, ",")
Expand Down Expand Up @@ -154,7 +143,7 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus {
}
}

return status
return *status
}

func cleanRegistryConfig(registryConfigDir string) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/s2i.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 8 additions & 19 deletions pkg/builder/spectrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -149,7 +138,7 @@ func (t *spectrumTask) Do(ctx context.Context) v1.BuildStatus {
}
}

return status
return *status
}

func readSpectrumLogs(newStdOut io.Reader) {
Expand Down
17 changes: 17 additions & 0 deletions pkg/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/trait/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}})
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/trait/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -134,6 +153,7 @@ func createBuilderTestEnv(cluster v1.IntegrationPlatformCluster, strategy v1.Int
BuildConfiguration: v1.BuildConfiguration{
Strategy: buildStrategy,
},
BaseImage: "root-jdk-image",
},
},
Status: v1.IntegrationPlatformStatus{
Expand Down

0 comments on commit 9de2092

Please sign in to comment.