Skip to content

Commit

Permalink
feat(core): Support S2I for builder image generation
Browse files Browse the repository at this point in the history
* Add initialize builder image on catalog with imagestream and buildconfig resource who's owner is the CamelCatalog
* Light refactoring of S2I code

Ref apache#4297
  • Loading branch information
gansheer committed May 26, 2023
1 parent 1beec1a commit 81c4d80
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 58 deletions.
44 changes: 3 additions & 41 deletions pkg/builder/s2i.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"os"
"path/filepath"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -49,6 +48,7 @@ import (
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/log"
"github.com/apache/camel-k/v2/pkg/util/s2i"
)

type s2iTask struct {
Expand Down Expand Up @@ -203,11 +203,11 @@ func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus {
return fmt.Errorf("cannot unmarshal instantiated binary response: %w", err)
}

err = t.waitForS2iBuildCompletion(ctx, t.c, &s2iBuild)
err = s2i.WaitForS2iBuildCompletion(ctx, t.c, &s2iBuild)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
// nolint: contextcheck
if err := t.cancelBuild(context.Background(), &s2iBuild); err != nil {
if err := s2i.CancelBuild(context.Background(), t.c, &s2iBuild); err != nil {
log.Errorf(err, "cannot cancel s2i Build: %s/%s", s2iBuild.Namespace, s2iBuild.Name)
}
}
Expand Down Expand Up @@ -255,44 +255,6 @@ func (t *s2iTask) getControllerReference() metav1.Object {
return owner
}

func (t *s2iTask) waitForS2iBuildCompletion(ctx context.Context, c client.Client, build *buildv1.Build) error {
key := ctrl.ObjectKeyFromObject(build)
for {
select {

case <-ctx.Done():
return ctx.Err()

case <-time.After(1 * time.Second):
err := c.Get(ctx, key, build)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return err
}

if build.Status.Phase == buildv1.BuildPhaseComplete {
return nil
} else if build.Status.Phase == buildv1.BuildPhaseCancelled ||
build.Status.Phase == buildv1.BuildPhaseFailed ||
build.Status.Phase == buildv1.BuildPhaseError {
return errors.New("build failed")
}
}
}
}

func (t *s2iTask) cancelBuild(ctx context.Context, build *buildv1.Build) error {
target := build.DeepCopy()
target.Status.Cancelled = true
if err := t.c.Patch(ctx, target, ctrl.MergeFrom(build)); err != nil {
return err
}
*build = *target
return nil
}

func tarDir(src string, writers ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/build/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func addBuildTaskToPod(build *v1.Build, taskName string, pod *corev1.Pod) {
)
}

var envVars []corev1.EnvVar = proxyFromEnvironment()
envVars = append(envVars, corev1.EnvVar{
Name: "HOME",
Value: filepath.Join(builderDir, build.Name),
})

container := corev1.Container{
Name: taskName,
Image: build.BuilderConfiguration().ToolImage,
Expand All @@ -287,7 +293,7 @@ func addBuildTaskToPod(build *v1.Build, taskName string, pod *corev1.Pod) {
taskName,
},
WorkingDir: filepath.Join(builderDir, build.Name),
Env: proxyFromEnvironment(),
Env: envVars,
}

configureResources(build, &container)
Expand Down
Loading

0 comments on commit 81c4d80

Please sign in to comment.