You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2024/01/26 08:54:00 UTC

(camel-k) 03/03: Add test for new annotation on build pod

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

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

commit ec9406b410bcea0b2d74bfb0dc1ec57e50394d44
Author: Rodrigue Cloutier <rc...@petalmd.com>
AuthorDate: Thu Jan 25 13:58:56 2024 -0500

    Add test for new annotation on build pod
---
 pkg/apis/camel/v1/trait/builder.go     |  2 +
 pkg/controller/build/build_pod_test.go | 68 ++++++++++++++++++++++++++++++++++
 pkg/trait/builder.go                   |  1 +
 pkg/trait/builder_test.go              | 20 ++++++++++
 4 files changed, 91 insertions(+)

diff --git a/pkg/apis/camel/v1/trait/builder.go b/pkg/apis/camel/v1/trait/builder.go
index f8b495a5b..0e626438e 100644
--- a/pkg/apis/camel/v1/trait/builder.go
+++ b/pkg/apis/camel/v1/trait/builder.go
@@ -70,4 +70,6 @@ type BuilderTrait struct {
 	TasksLimitMemory []string `property:"tasks-limit-memory" json:"tasksLimitMemory,omitempty"`
 	// Defines a set of nodes the builder pod is eligible to be scheduled on, based on labels on the node.
 	NodeSelector map[string]string `property:"node-selector" json:"nodeSelector,omitempty"`
+	// When using `pod` strategy, annotation to use for the builder pod.
+	Annotations map[string]string `property:"annotations" json:"annotations,omitempty"`
 }
diff --git a/pkg/controller/build/build_pod_test.go b/pkg/controller/build/build_pod_test.go
new file mode 100644
index 000000000..3dc5c87da
--- /dev/null
+++ b/pkg/controller/build/build_pod_test.go
@@ -0,0 +1,68 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package build
+
+import (
+	"context"
+	"testing"
+
+	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/v2/pkg/util/test"
+	"github.com/stretchr/testify/assert"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+)
+
+func TestNewBuildPodConfiguration(t *testing.T) {
+
+	ctx := context.TODO()
+	c, err := test.NewFakeClient()
+	assert.Nil(t, err)
+
+	build := v1.Build{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "theBuildName",
+		},
+		Spec: v1.BuildSpec{
+			Tasks: []v1.Task{
+				{
+					Builder: &v1.BuilderTask{
+						BaseTask: v1.BaseTask{
+							Name: "builder",
+							Configuration: v1.BuildConfiguration{
+								BuilderPodNamespace: "theNamespace",
+								NodeSelector:        map[string]string{"node": "selector"},
+								Annotations:         map[string]string{"annotation": "value"},
+							},
+						},
+					},
+				},
+			},
+		},
+	}
+
+	pod := newBuildPod(ctx, c, &build)
+
+	assert.Equal(t, "Pod", pod.Kind)
+	assert.Equal(t, "theNamespace", pod.Namespace)
+	assert.Equal(t, map[string]string{
+		"camel.apache.org/build":     "theBuildName",
+		"camel.apache.org/component": "builder",
+	}, pod.Labels)
+	assert.Equal(t, map[string]string{"node": "selector"}, pod.Spec.NodeSelector)
+	assert.Equal(t, map[string]string{"annotation": "value"}, pod.Annotations)
+}
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 26202d269..6b9375126 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -185,6 +185,7 @@ func (t *builderTrait) Apply(e *Environment) error {
 		return nil
 	}
 	builderTask.Configuration.NodeSelector = t.NodeSelector
+	builderTask.Configuration.Annotations = t.Annotations
 	pipelineTasks = append(pipelineTasks, v1.Task{Builder: builderTask})
 
 	// Custom tasks
diff --git a/pkg/trait/builder_test.go b/pkg/trait/builder_test.go
index d7e4e87f1..de1af59d6 100644
--- a/pkg/trait/builder_test.go
+++ b/pkg/trait/builder_test.go
@@ -483,6 +483,26 @@ func TestBuilderWithNodeSelector(t *testing.T) {
 	assert.Equal(t, map[string]string{"size": "large"}, builderTrait.NodeSelector)
 }
 
+func TestBuilderWithAnnotations(t *testing.T) {
+	env := createBuilderTestEnv(v1.IntegrationPlatformClusterKubernetes, v1.IntegrationPlatformBuildPublishStrategyJib, v1.BuildStrategyRoutine)
+	builderTrait := createNominalBuilderTraitTest()
+	builderTrait.Annotations = map[string]string{
+		"annotation": "value",
+	}
+
+	active, condition, err := builderTrait.Configure(env)
+	assert.Nil(t, err)
+
+	err = builderTrait.Apply(env)
+	assert.Nil(t, err)
+
+	assert.True(t, active)
+	assert.Nil(t, condition)
+
+	assert.Equal(t, map[string]string{"annotation": "value"}, env.Pipeline[0].Builder.Configuration.Annotations)
+	assert.Equal(t, map[string]string{"annotation": "value"}, builderTrait.Annotations)
+}
+
 func TestBuilderNoTasksFilter(t *testing.T) {
 	env := createBuilderTestEnv(v1.IntegrationPlatformClusterKubernetes, v1.IntegrationPlatformBuildPublishStrategyJib, v1.BuildStrategyPod)
 	builderTrait := createNominalBuilderTraitTest()