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 2021/03/02 13:46:52 UTC

[camel-k] 01/03: refactor(trait): deployment strategy

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 5aa0b5bb79a60d009eb6077ed69ce216ff9293b8
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Mon Mar 1 16:46:11 2021 +0100

    refactor(trait): deployment strategy
    
    * Moved the logic of retrieving the deployment strategy outside toleration trait
    
    Ref #2047
---
 pkg/trait/toleration.go       |  45 +++----------------
 pkg/trait/toleration_test.go  |  95 ++++-----------------------------------
 pkg/trait/trait_types.go      |  32 +++++++++++++
 pkg/trait/trait_types_test.go | 101 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 126 deletions(-)

diff --git a/pkg/trait/toleration.go b/pkg/trait/toleration.go
index 700b1ea..481cebf 100644
--- a/pkg/trait/toleration.go
+++ b/pkg/trait/toleration.go
@@ -22,10 +22,7 @@ import (
 	"regexp"
 	"strconv"
 
-	appsv1 "k8s.io/api/apps/v1"
-	"k8s.io/api/batch/v1beta1"
 	corev1 "k8s.io/api/core/v1"
-	serving "knative.dev/serving/pkg/apis/serving/v1"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/util"
@@ -74,46 +71,14 @@ func (t *tolerationTrait) Apply(e *Environment) (err error) {
 	if err != nil {
 		return err
 	}
-	var specTolerations *[]corev1.Toleration
-	found := false
-
-	// Deployment
-	deployment := e.Resources.GetDeployment(func(d *appsv1.Deployment) bool {
-		return d.Name == e.Integration.Name
-	})
-	if deployment != nil {
-		specTolerations = &deployment.Spec.Template.Spec.Tolerations
-		found = true
-	}
-
-	// Knative service
-	if !found {
-		knativeService := e.Resources.GetKnativeService(func(s *serving.Service) bool {
-			return s.Name == e.Integration.Name
-		})
-		if knativeService != nil {
-			specTolerations = &knativeService.Spec.Template.Spec.Tolerations
-			found = true
-		}
-	}
-
-	// Cronjob
-	if !found {
-		cronJob := e.Resources.GetCronJob(func(c *v1beta1.CronJob) bool {
-			return c.Name == e.Integration.Name
-		})
-		if cronJob != nil {
-			specTolerations = &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations
-			found = true
-		}
-	}
+	podSpec := e.GetIntegrationPodSpec()
 
 	// Add the toleration
-	if found {
-		if *specTolerations == nil {
-			*specTolerations = make([]corev1.Toleration, 0)
+	if podSpec != nil {
+		if podSpec.Tolerations == nil {
+			podSpec.Tolerations = make([]corev1.Toleration, 0)
 		}
-		*specTolerations = append(*specTolerations, tolerations...)
+		podSpec.Tolerations = append(podSpec.Tolerations, tolerations...)
 	}
 
 	return nil
diff --git a/pkg/trait/toleration_test.go b/pkg/trait/toleration_test.go
index b5b4741..8ce65cb 100644
--- a/pkg/trait/toleration_test.go
+++ b/pkg/trait/toleration_test.go
@@ -21,20 +21,14 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
-	serving "knative.dev/serving/pkg/apis/serving/v1"
 
-	appsv1 "k8s.io/api/apps/v1"
-	"k8s.io/api/batch/v1beta1"
 	corev1 "k8s.io/api/core/v1"
-	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
-	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/util"
-	"github.com/apache/camel-k/pkg/util/kubernetes"
 )
 
 func TestConfigureTolerationTraitMissingTaint(t *testing.T) {
-	environment, _ := createNominalDeploymentTolerationTest()
+	environment, _ := createNominalDeploymentTraitTest()
 	tolerationTrait := createNominalTolerationTrait()
 
 	success, err := tolerationTrait.Configure(environment)
@@ -44,7 +38,7 @@ func TestConfigureTolerationTraitMissingTaint(t *testing.T) {
 }
 
 func TestApplyTolerationTraitMalformedTaint(t *testing.T) {
-	environment, _ := createNominalDeploymentTolerationTest()
+	environment, _ := createNominalDeploymentTraitTest()
 	tolerationTrait := createNominalTolerationTrait()
 	tolerationTrait.Taints = append(tolerationTrait.Taints, "my-toleration-failure")
 
@@ -57,13 +51,13 @@ func TestApplyPodTolerationLabelsDefault(t *testing.T) {
 	tolerationTrait := createNominalTolerationTrait()
 	tolerationTrait.Taints = append(tolerationTrait.Taints, "my-toleration=my-value:NoExecute")
 
-	environment, deployment := createNominalDeploymentTolerationTest()
+	environment, deployment := createNominalDeploymentTraitTest()
 	testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &deployment.Spec.Template.Spec.Tolerations)
 
-	environment, knativeService := createNominalKnativeServiceTolerationTest()
+	environment, knativeService := createNominalKnativeServiceTraitTest()
 	testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &knativeService.Spec.Template.Spec.Tolerations)
 
-	environment, cronJob := createNominalCronJobTolerationTest()
+	environment, cronJob := createNominalCronJobTraitTest()
 	testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations)
 }
 
@@ -83,13 +77,13 @@ func TestApplyPodTolerationLabelsTolerationSeconds(t *testing.T) {
 	tolerationTrait := createNominalTolerationTrait()
 	tolerationTrait.Taints = append(tolerationTrait.Taints, "my-toleration:NoExecute:300")
 
-	environment, deployment := createNominalDeploymentTolerationTest()
+	environment, deployment := createNominalDeploymentTraitTest()
 	testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &deployment.Spec.Template.Spec.Tolerations)
 
-	environment, knativeService := createNominalKnativeServiceTolerationTest()
+	environment, knativeService := createNominalKnativeServiceTraitTest()
 	testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &knativeService.Spec.Template.Spec.Tolerations)
 
-	environment, cronJob := createNominalCronJobTolerationTest()
+	environment, cronJob := createNominalCronJobTraitTest()
 	testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations)
 }
 
@@ -106,7 +100,7 @@ func testApplyPodTolerationLabelsTolerationSeconds(t *testing.T, trait *tolerati
 }
 
 func TestTolerationValidTaints(t *testing.T) {
-	environment, _ := createNominalDeploymentTolerationTest()
+	environment, _ := createNominalDeploymentTraitTest()
 	tolerationTrait := createNominalTolerationTrait()
 	tolerationTrait.Taints = append(tolerationTrait.Taints, "my-toleration:NoExecute")
 	tolerationTrait.Taints = append(tolerationTrait.Taints, "my-toleration:NoSchedule")
@@ -122,77 +116,6 @@ func TestTolerationValidTaints(t *testing.T) {
 	assert.Nil(t, err)
 }
 
-func createNominalDeploymentTolerationTest() (*Environment, *appsv1.Deployment) {
-	deployment := &appsv1.Deployment{
-		ObjectMeta: metav1.ObjectMeta{
-			Name: "integration-name",
-		},
-		Spec: appsv1.DeploymentSpec{
-			Template: corev1.PodTemplateSpec{},
-		},
-	}
-
-	environment := &Environment{
-		Integration: &v1.Integration{
-			ObjectMeta: metav1.ObjectMeta{
-				Name: "integration-name",
-			},
-			Status: v1.IntegrationStatus{
-				Phase: v1.IntegrationPhaseDeploying,
-			},
-		},
-		Resources: kubernetes.NewCollection(deployment),
-	}
-
-	return environment, deployment
-}
-
-func createNominalKnativeServiceTolerationTest() (*Environment, *serving.Service) {
-	knativeService := &serving.Service{
-		ObjectMeta: metav1.ObjectMeta{
-			Name: "integration-name",
-		},
-		Spec: serving.ServiceSpec{},
-	}
-
-	environment := &Environment{
-		Integration: &v1.Integration{
-			ObjectMeta: metav1.ObjectMeta{
-				Name: "integration-name",
-			},
-			Status: v1.IntegrationStatus{
-				Phase: v1.IntegrationPhaseDeploying,
-			},
-		},
-		Resources: kubernetes.NewCollection(knativeService),
-	}
-
-	return environment, knativeService
-}
-
-func createNominalCronJobTolerationTest() (*Environment, *v1beta1.CronJob) {
-	cronJob := &v1beta1.CronJob{
-		ObjectMeta: metav1.ObjectMeta{
-			Name: "integration-name",
-		},
-		Spec: v1beta1.CronJobSpec{},
-	}
-
-	environment := &Environment{
-		Integration: &v1.Integration{
-			ObjectMeta: metav1.ObjectMeta{
-				Name: "integration-name",
-			},
-			Status: v1.IntegrationStatus{
-				Phase: v1.IntegrationPhaseDeploying,
-			},
-		},
-		Resources: kubernetes.NewCollection(cronJob),
-	}
-
-	return environment, cronJob
-}
-
 func createNominalTolerationTrait() *tolerationTrait {
 	tolerationTrait := newTolerationTrait().(*tolerationTrait)
 	tolerationTrait.Enabled = util.BoolP(true)
diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go
index 2fff975..5b360d0 100644
--- a/pkg/trait/trait_types.go
+++ b/pkg/trait/trait_types.go
@@ -25,9 +25,12 @@ import (
 	"strconv"
 	"strings"
 
+	appsv1 "k8s.io/api/apps/v1"
+	"k8s.io/api/batch/v1beta1"
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/client"
@@ -309,6 +312,35 @@ func (e *Environment) getControllerStrategyChoosers() (res []ControllerStrategyS
 	return res
 }
 
+// GetIntegrationPodSpec return the Integration Template Pod Specification, regardless of the deployment strategy
+func (e *Environment) GetIntegrationPodSpec() *corev1.PodSpec {
+	// Deployment
+	deployment := e.Resources.GetDeployment(func(d *appsv1.Deployment) bool {
+		return d.Name == e.Integration.Name
+	})
+	if deployment != nil {
+		return &deployment.Spec.Template.Spec
+	}
+
+	// Knative service
+	knativeService := e.Resources.GetKnativeService(func(s *serving.Service) bool {
+		return s.Name == e.Integration.Name
+	})
+	if knativeService != nil {
+		return &knativeService.Spec.Template.Spec.PodSpec
+	}
+
+	// Cronjob
+	cronJob := e.Resources.GetCronJob(func(c *v1beta1.CronJob) bool {
+		return c.Name == e.Integration.Name
+	})
+	if cronJob != nil {
+		return &cronJob.Spec.JobTemplate.Spec.Template.Spec
+	}
+
+	return nil
+}
+
 // DetermineNamespace --
 func (e *Environment) DetermineNamespace() string {
 	if e.Integration != nil && e.Integration.Namespace != "" {
diff --git a/pkg/trait/trait_types_test.go b/pkg/trait/trait_types_test.go
new file mode 100644
index 0000000..68e0238
--- /dev/null
+++ b/pkg/trait/trait_types_test.go
@@ -0,0 +1,101 @@
+/*
+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 trait
+
+import (
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
+)
+
+func createNominalDeploymentTraitTest() (*Environment, *appsv1.Deployment) {
+	deployment := &appsv1.Deployment{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "integration-name",
+		},
+		Spec: appsv1.DeploymentSpec{
+			Template: corev1.PodTemplateSpec{},
+		},
+	}
+
+	environment := &Environment{
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name: "integration-name",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseDeploying,
+			},
+		},
+		Resources: kubernetes.NewCollection(deployment),
+	}
+
+	return environment, deployment
+}
+
+func createNominalKnativeServiceTraitTest() (*Environment, *serving.Service) {
+	knativeService := &serving.Service{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "integration-name",
+		},
+		Spec: serving.ServiceSpec{},
+	}
+
+	environment := &Environment{
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name: "integration-name",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseDeploying,
+			},
+		},
+		Resources: kubernetes.NewCollection(knativeService),
+	}
+
+	return environment, knativeService
+}
+
+func createNominalCronJobTraitTest() (*Environment, *v1beta1.CronJob) {
+	cronJob := &v1beta1.CronJob{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "integration-name",
+		},
+		Spec: v1beta1.CronJobSpec{},
+	}
+
+	environment := &Environment{
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name: "integration-name",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseDeploying,
+			},
+		},
+		Resources: kubernetes.NewCollection(cronJob),
+	}
+
+	return environment, cronJob
+}