You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2019/12/18 12:34:47 UTC

[camel-k] 02/13: feat(build): Better handle tasks build directory sharing

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit b424b959335ed43f7cb570c37065500cd869ff6c
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Mon Dec 16 12:20:50 2019 +0100

    feat(build): Better handle tasks build directory sharing
---
 pkg/builder/builder.go               | 26 +++++++++++++-------------
 pkg/builder/builder_steps.go         |  2 +-
 pkg/controller/build/schedule_pod.go |  4 ++--
 pkg/trait/builder.go                 | 21 ++++++++++-----------
 4 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go
index 2683386..4cf5c3d 100644
--- a/pkg/builder/builder.go
+++ b/pkg/builder/builder.go
@@ -51,24 +51,24 @@ func New(c client.Client) Builder {
 func (b *defaultBuilder) Run(build v1alpha1.BuilderTask) v1alpha1.BuildStatus {
 	result := v1alpha1.BuildStatus{}
 
-	// create tmp path
-	buildDir := build.BuildDir
-	if buildDir == "" {
-		buildDir = os.TempDir()
-	}
-	builderPath, err := ioutil.TempDir(buildDir, "builder-")
-	if err != nil {
-		log.Error(err, "Unexpected error while creating a temporary dir")
+	var buildDir string
+	if build.BuildDir == "" {
+		tmpDir, err := ioutil.TempDir(os.TempDir(), "builder-")
+		if err != nil {
+			log.Error(err, "Unexpected error while creating a temporary dir")
 
-		result.Phase = v1alpha1.BuildPhaseFailed
-		result.Error = err.Error()
+			result.Phase = v1alpha1.BuildPhaseFailed
+			result.Error = err.Error()
+		}
+		buildDir = tmpDir
+		defer os.RemoveAll(buildDir)
+	} else {
+		buildDir = build.BuildDir
 	}
 
-	defer os.RemoveAll(builderPath)
-
 	c := Context{
 		Client:    b.client,
-		Path:      builderPath,
+		Path:      buildDir,
 		Namespace: build.Meta.Namespace,
 		Build:     build,
 		BaseImage: build.BaseImage,
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index 4b84f9f..429d9b8 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -297,7 +297,7 @@ func packager(ctx *Context, selector artifactsSelector) error {
 		return err
 	}
 
-	tarFileName := path.Join(ctx.Build.BuildDir, "package", ctx.Build.Meta.Name)
+	tarFileName := path.Join(ctx.Path, "package", "occi.tar")
 	tarFileDir := path.Dir(tarFileName)
 
 	err = os.MkdirAll(tarFileDir, 0777)
diff --git a/pkg/controller/build/schedule_pod.go b/pkg/controller/build/schedule_pod.go
index 74ff938..30c2c38 100644
--- a/pkg/controller/build/schedule_pod.go
+++ b/pkg/controller/build/schedule_pod.go
@@ -141,7 +141,7 @@ func (action *schedulePodAction) newBuildPod(ctx context.Context, build *v1alpha
 			} else {
 				action.operatorImage = operatorImage
 			}
-			action.addCamelTaskToPod(build, task.Builder, pod)
+			action.addBuilderTaskToPod(build, task.Builder, pod)
 		} else if task.Kaniko != nil {
 			action.addKanikoTaskToPod(task.Kaniko, pod)
 		}
@@ -154,7 +154,7 @@ func (action *schedulePodAction) newBuildPod(ctx context.Context, build *v1alpha
 	return pod, nil
 }
 
-func (action *schedulePodAction) addCamelTaskToPod(build *v1alpha1.Build, task *v1alpha1.BuilderTask, pod *corev1.Pod) {
+func (action *schedulePodAction) addBuilderTaskToPod(build *v1alpha1.Build, task *v1alpha1.BuilderTask, pod *corev1.Pod) {
 	pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
 		Name:            task.Name,
 		Image:           action.operatorImage,
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 3001ca2..b65fd8c 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -60,8 +60,8 @@ func (t *builderTrait) Configure(e *Environment) (bool, error) {
 }
 
 func (t *builderTrait) Apply(e *Environment) error {
-	camelTask := t.camelTask(e)
-	e.BuildTasks = append(e.BuildTasks, v1alpha1.Task{Builder: camelTask})
+	builderTask := t.builderTask(e)
+	e.BuildTasks = append(e.BuildTasks, v1alpha1.Task{Builder: builderTask})
 
 	if platform.SupportsKanikoPublishStrategy(e.Platform) {
 		kanikoTask, err := t.kanikoTask(e)
@@ -69,7 +69,7 @@ func (t *builderTrait) Apply(e *Environment) error {
 			return err
 		}
 		mount := corev1.VolumeMount{Name: "camel-k-builder", MountPath: kaniko.BuildDir}
-		camelTask.VolumeMounts = append(camelTask.VolumeMounts, mount)
+		builderTask.VolumeMounts = append(builderTask.VolumeMounts, mount)
 		kanikoTask.VolumeMounts = append(kanikoTask.VolumeMounts, mount)
 
 		if e.Platform.Status.Build.IsKanikoCacheEnabled() {
@@ -114,7 +114,7 @@ func (t *builderTrait) Apply(e *Environment) error {
 				},
 			}
 			// Use the PVC used to warm the Kaniko cache to coordinate the Camel Maven build and the Kaniko image build
-			camelTask.Volumes = append(camelTask.Volumes, corev1.Volume{
+			builderTask.Volumes = append(builderTask.Volumes, corev1.Volume{
 				Name: "camel-k-builder",
 				VolumeSource: corev1.VolumeSource{
 					PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
@@ -124,11 +124,10 @@ func (t *builderTrait) Apply(e *Environment) error {
 			})
 		} else {
 			// Use an emptyDir volume to coordinate the Camel Maven build and the Kaniko image build
-			camelTask.Volumes = append(camelTask.Volumes, corev1.Volume{
+			builderTask.Volumes = append(builderTask.Volumes, corev1.Volume{
 				Name: "camel-k-builder",
 				VolumeSource: corev1.VolumeSource{
-					EmptyDir: &corev1.EmptyDirVolumeSource{
-					},
+					EmptyDir: &corev1.EmptyDirVolumeSource{},
 				},
 			})
 		}
@@ -149,10 +148,10 @@ func (t *builderTrait) InfluencesKit() bool {
 	return true
 }
 
-func (t *builderTrait) camelTask(e *Environment) *v1alpha1.BuilderTask {
+func (t *builderTrait) builderTask(e *Environment) *v1alpha1.BuilderTask {
 	task := &v1alpha1.BuilderTask{
 		BaseTask: v1alpha1.BaseTask{
-			Name: "camel",
+			Name: "builder",
 		},
 		Meta:            e.IntegrationKit.ObjectMeta,
 		BaseImage:       e.Platform.Status.Build.BaseImage,
@@ -173,7 +172,7 @@ func (t *builderTrait) camelTask(e *Environment) *v1alpha1.BuilderTask {
 		task.Steps = append(task.Steps, builder.StepIDsFor(s2i.S2iSteps...)...)
 	} else if platform.SupportsKanikoPublishStrategy(e.Platform) {
 		task.Steps = append(task.Steps, builder.StepIDsFor(kaniko.KanikoSteps...)...)
-		task.BuildDir = kaniko.BuildDir
+		task.BuildDir = path.Join(kaniko.BuildDir, e.IntegrationKit.Name)
 	}
 
 	quarkus := e.Catalog.GetTrait("quarkus").(*quarkusTrait)
@@ -198,7 +197,7 @@ func (t *builderTrait) kanikoTask(e *Environment) (*v1alpha1.KanikoTask, error)
 	env := make([]corev1.EnvVar, 0)
 	baseArgs := []string{
 		"--dockerfile=Dockerfile",
-		"--context=" + path.Join(kaniko.BuildDir, "package", "context"),
+		"--context=" + path.Join(kaniko.BuildDir, e.IntegrationKit.Name, "package", "context"),
 		"--destination=" + image,
 		"--cache=" + strconv.FormatBool(e.Platform.Status.Build.IsKanikoCacheEnabled()),
 		"--cache-dir=" + path.Join(kaniko.BuildDir, "cache"),