Skip to content

Commit

Permalink
feat(trait): extending toleration to cronJob and KnativeService
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Feb 18, 2021
1 parent 6513fd6 commit 5dac0b8
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 36 deletions.
62 changes: 46 additions & 16 deletions pkg/trait/toleration.go
Expand Up @@ -22,7 +22,9 @@ import (
"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"
Expand Down Expand Up @@ -80,22 +82,56 @@ func (t *tolerationTrait) Configure(e *Environment) (bool, error) {
}

func (t *tolerationTrait) Apply(e *Environment) (err error) {
var deployment *appsv1.Deployment
e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
if d.Name == e.Integration.Name {
deployment = d
}
toleration, err := t.getToleration()
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 {
if err := t.addToleration(e, deployment); err != nil {
return err
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
}
}

// Add the toleration
if found {
if *specTolerations == nil {
*specTolerations = make([]corev1.Toleration, 0)
}
*specTolerations = append(*specTolerations, toleration)
}

return nil
}

func (t *tolerationTrait) addToleration(_ *Environment, deployment *appsv1.Deployment) error {
func (t *tolerationTrait) getToleration() (corev1.Toleration, error) {
toleration := corev1.Toleration{
Key: t.Key,
Operator: corev1.TolerationOperator(t.Operator),
Expand All @@ -106,16 +142,10 @@ func (t *tolerationTrait) addToleration(_ *Environment, deployment *appsv1.Deplo
if t.TolerationSeconds != "" {
tolerationSeconds, err := strconv.ParseInt(t.TolerationSeconds, 10, 64)
if err != nil {
return err
return corev1.Toleration{}, err
}
toleration.TolerationSeconds = &tolerationSeconds
}

if deployment.Spec.Template.Spec.Tolerations == nil {
deployment.Spec.Template.Spec.Tolerations = make([]corev1.Toleration, 0)
}

deployment.Spec.Template.Spec.Tolerations = append(deployment.Spec.Template.Spec.Tolerations, toleration)

return nil
return toleration, nil
}
121 changes: 101 additions & 20 deletions pkg/trait/toleration_test.go
Expand Up @@ -21,8 +21,10 @@ 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"

Expand All @@ -32,7 +34,8 @@ import (
)

func TestConfigureTolerationTraitMissingKey(t *testing.T) {
tolerationTrait, environment, _ := createNominalTolerationTest()
environment, _ := createNominalDeploymentTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)

success, err := tolerationTrait.Configure(environment)
Expand All @@ -42,7 +45,8 @@ func TestConfigureTolerationTraitMissingKey(t *testing.T) {
}

func TestConfigureTolerationTraitMissingOperator(t *testing.T) {
tolerationTrait, environment, _ := createNominalTolerationTest()
environment, _ := createNominalDeploymentTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"

Expand All @@ -53,7 +57,8 @@ func TestConfigureTolerationTraitMissingOperator(t *testing.T) {
}

func TestConfigureTolerationTraitMissingValue(t *testing.T) {
tolerationTrait, environment, _ := createNominalTolerationTest()
environment, _ := createNominalDeploymentTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"
tolerationTrait.Operator = "Equal"
Expand All @@ -65,7 +70,8 @@ func TestConfigureTolerationTraitMissingValue(t *testing.T) {
}

func TestConfigureTolerationTraitMissingEffect(t *testing.T) {
tolerationTrait, environment, _ := createNominalTolerationTest()
environment, _ := createNominalDeploymentTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"
tolerationTrait.Operator = "Exists"
Expand All @@ -77,61 +83,90 @@ func TestConfigureTolerationTraitMissingEffect(t *testing.T) {
}

func TestApplyPodTolerationLabelsDefault(t *testing.T) {
tolerationTrait, environment, deployment := createNominalTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"
tolerationTrait.Operator = "Equal"
tolerationTrait.Value = "my-value"
tolerationTrait.Effect = "NoExecute"

err := tolerationTrait.Apply(environment)
environment, deployment := createNominalDeploymentTolerationTest()
testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &deployment.Spec.Template.Spec.Tolerations)

environment, knativeService := createNominalKnativeServiceTolerationTest()
testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &knativeService.Spec.Template.Spec.Tolerations)

environment, cronJob := createNominalCronJobTolerationTest()
testApplyPodTolerationLabelsDefault(t, tolerationTrait, environment, &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations)
}

func testApplyPodTolerationLabelsDefault(t *testing.T, trait *tolerationTrait, environment *Environment, tolerations *[]corev1.Toleration) {
err := trait.Apply(environment)

assert.Nil(t, err)
assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Tolerations))
toleration := deployment.Spec.Template.Spec.Tolerations[0]
assert.Equal(t, 1, len(*tolerations))
toleration := (*tolerations)[0]
assert.Equal(t, "my-toleration", toleration.Key)
assert.Equal(t, corev1.TolerationOpEqual, toleration.Operator)
assert.Equal(t, "my-value", toleration.Value)
assert.Equal(t, corev1.TaintEffectNoExecute, toleration.Effect)
}

func TestApplyPodTolerationLabelsTolerationSeconds(t *testing.T) {
tolerationTrait, environment, deployment := createNominalTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"
tolerationTrait.Operator = "Exists"
tolerationTrait.Effect = "NoExecute"
tolerationTrait.TolerationSeconds = "300"

err := tolerationTrait.Apply(environment)
environment, deployment := createNominalDeploymentTolerationTest()
testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &deployment.Spec.Template.Spec.Tolerations)

environment, knativeService := createNominalKnativeServiceTolerationTest()
testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &knativeService.Spec.Template.Spec.Tolerations)

environment, cronJob := createNominalCronJobTolerationTest()
testApplyPodTolerationLabelsTolerationSeconds(t, tolerationTrait, environment, &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations)
}

func testApplyPodTolerationLabelsTolerationSeconds(t *testing.T, trait *tolerationTrait, environment *Environment, tolerations *[]corev1.Toleration) {
err := trait.Apply(environment)

assert.Nil(t, err)
assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Tolerations))
toleration := deployment.Spec.Template.Spec.Tolerations[0]
assert.Equal(t, 1, len(*tolerations))
toleration := (*tolerations)[0]
assert.Equal(t, "my-toleration", toleration.Key)
assert.Equal(t, corev1.TolerationOpExists, toleration.Operator)
assert.Equal(t, corev1.TaintEffectNoExecute, toleration.Effect)
assert.Equal(t, int64(300), *toleration.TolerationSeconds)
}

func TestApplyPodTolerationLabelsTolerationSecondsFail(t *testing.T) {
tolerationTrait, environment, _ := createNominalTolerationTest()
tolerationTrait := newTolerationTrait().(*tolerationTrait)
tolerationTrait.Enabled = util.BoolP(true)
tolerationTrait.Key = "my-toleration"
tolerationTrait.Operator = "Exists"
tolerationTrait.Effect = "NoExecute"
tolerationTrait.TolerationSeconds = "abc"

err := tolerationTrait.Apply(environment)
environment, deployment := createNominalDeploymentTolerationTest()
testApplyPodTolerationLabelsTolerationSecondsFail(t, tolerationTrait, environment, &deployment.Spec.Template.Spec.Tolerations)

assert.NotNil(t, err)
environment, knativeService := createNominalKnativeServiceTolerationTest()
testApplyPodTolerationLabelsTolerationSecondsFail(t, tolerationTrait, environment, &knativeService.Spec.Template.Spec.Tolerations)

environment, cronJob := createNominalCronJobTolerationTest()
testApplyPodTolerationLabelsTolerationSecondsFail(t, tolerationTrait, environment, &cronJob.Spec.JobTemplate.Spec.Template.Spec.Tolerations)
}

func createNominalTolerationTest() (*tolerationTrait, *Environment, *appsv1.Deployment) {
trait := newTolerationTrait().(*tolerationTrait)
enabled := true
trait.Enabled = &enabled
func testApplyPodTolerationLabelsTolerationSecondsFail(t *testing.T, trait *tolerationTrait, environment *Environment, tolerations *[]corev1.Toleration) {
err := trait.Apply(environment)

assert.NotNil(t, err)
}

func createNominalDeploymentTolerationTest() (*Environment, *appsv1.Deployment) {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
Expand All @@ -153,5 +188,51 @@ func createNominalTolerationTest() (*tolerationTrait, *Environment, *appsv1.Depl
Resources: kubernetes.NewCollection(deployment),
}

return trait, environment, 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
}

0 comments on commit 5dac0b8

Please sign in to comment.