Repository: apache/camel-k
Pull request #852 (2019-07-23 07:06:26)

fix(classpath): lookup integration container by name instead of iterating amoung all the containers


Patch:
diff --git a/.travis.yml b/.travis.yml
index c2fa9b9b1..b3c42b932 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -45,5 +45,5 @@ before_script:
   - ./golangci-lint --version
 
 script:
-  - ./golangci-lint run --verbose --config .golangci.yml || travis_terminate 1;
+  - GOGC=10 ./golangci-lint run --verbose --deadline 5m --config .golangci.yml || travis_terminate 1;
   - ./script/travis_build.sh
diff --git a/pkg/trait/classpath.go b/pkg/trait/classpath.go
index d2191da5d..fba666caf 100644
--- a/pkg/trait/classpath.go
+++ b/pkg/trait/classpath.go
@@ -19,20 +19,15 @@ package trait
 
 import (
 	"fmt"
-
 	"sort"
 	"strings"
 
-	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/util/envvar"
 
-	"github.com/scylladb/go-set/strset"
-
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/pkg/errors"
+	"github.com/scylladb/go-set/strset"
 
-	serving "github.com/knative/serving/pkg/apis/serving/v1alpha1"
-	appsv1 "k8s.io/api/apps/v1"
-	corev1 "k8s.io/api/core/v1"
 	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
@@ -96,38 +91,25 @@ func (t *classpathTrait) Apply(e *Environment) error {
 		e.Classpath.Add("/deployments/dependencies/*")
 	}
 
-	if e.Resources != nil {
-		e.Resources.VisitDeployment(func(deployment *appsv1.Deployment) {
-			for i := 0; i < len(deployment.Spec.Template.Spec.Containers); i++ {
-				cp := e.Classpath.Copy()
-
-				for _, m := range deployment.Spec.Template.Spec.Containers[i].VolumeMounts {
-					cp.Add(m.MountPath)
-				}
-
-				t.setJavaClasspath(cp, &deployment.Spec.Template.Spec.Containers[i].Env)
-			}
-		})
-		e.Resources.VisitKnativeService(func(service *serving.Service) {
-			for ci := range service.Spec.ConfigurationSpec.GetTemplate().Spec.Containers {
-				c := &service.Spec.ConfigurationSpec.GetTemplate().Spec.Containers[ci]
-				for mi := range c.VolumeMounts {
-					m := &c.VolumeMounts[mi]
-					e.Classpath.Add(m.MountPath)
-				}
-				t.setJavaClasspath(e.Classpath, &c.Env)
-			}
-		})
+	containerName := defaultContainerName
+	dt := e.Catalog.GetTrait(containerTraitID)
+	if dt != nil {
+		containerName = dt.(*containerTrait).Name
 	}
 
-	return nil
-}
+	container := e.Resources.GetContainerByName(containerName)
+	if container != nil {
+		for _, m := range container.VolumeMounts {
+			e.Classpath.Add(m.MountPath)
+		}
 
-func (t *classpathTrait) setJavaClasspath(cp *strset.Set, env *[]corev1.EnvVar) {
-	items := cp.List()
+		items := e.Classpath.List()
 
-	// keep classpath sorted
-	sort.Strings(items)
+		// keep classpath sorted
+		sort.Strings(items)
 
-	envvar.SetVal(env, "JAVA_CLASSPATH", strings.Join(items, ":"))
+		envvar.SetVal(&container.Env, "JAVA_CLASSPATH", strings.Join(items, ":"))
+	}
+
+	return nil
 }


Test patch:
diff --git a/pkg/trait/classpath_test.go b/pkg/trait/classpath_test.go
index a9412df07..2863fee85 100644
--- a/pkg/trait/classpath_test.go
+++ b/pkg/trait/classpath_test.go
@@ -19,6 +19,8 @@ package trait
 
 import (
 	"context"
+	"sort"
+	"strings"
 	"testing"
 
 	serving "github.com/knative/serving/pkg/apis/serving/v1alpha1"
@@ -107,42 +109,48 @@ func TestConfigureClasspathTraitWithIntegrationKitStatusArtifact(t *testing.T) {
 func TestConfigureClasspathTraitWithDeploymentResource(t *testing.T) {
 	trait, environment := createNominalTest()
 
-	container := corev1.Container{
-		VolumeMounts: []corev1.VolumeMount{{MountPath: "/mount/path"}},
-	}
-
-	deployment := appsv1.Deployment{
+	d := appsv1.Deployment{
 		Spec: appsv1.DeploymentSpec{
 			Template: corev1.PodTemplateSpec{
 				Spec: corev1.PodSpec{
-					Containers: []corev1.Container{container},
+					Containers: []corev1.Container{
+						{
+							Name: defaultContainerName,
+							VolumeMounts: []corev1.VolumeMount{
+								{
+									MountPath: "/mount/path",
+								},
+							},
+						},
+					},
 				},
 			},
 		},
 	}
 
-	environment.Resources = kubernetes.NewCollection(&deployment)
+	environment.Resources.Add(&d)
 
 	err := trait.Apply(environment)
 
+	cp := environment.Classpath.List()
+	sort.Strings(cp)
+
 	assert.Nil(t, err)
 	assert.NotNil(t, environment.Classpath)
-	assert.Equal(t, strset.New("/etc/camel/resources", "./resources"), environment.Classpath)
-
-	assert.Len(t, container.Env, 0)
+	assert.Equal(t, strset.New("/etc/camel/resources", "./resources", "/mount/path"), environment.Classpath)
+	assert.Len(t, d.Spec.Template.Spec.Containers[0].Env, 1)
+	assert.Equal(t, "JAVA_CLASSPATH", d.Spec.Template.Spec.Containers[0].Env[0].Name)
+	assert.Equal(t, strings.Join(cp, ":"), d.Spec.Template.Spec.Containers[0].Env[0].Value)
 }
 
 func TestConfigureClasspathTraitWithKNativeResource(t *testing.T) {
 	trait, environment := createNominalTest()
 
-	container := corev1.Container{
-		VolumeMounts: []corev1.VolumeMount{{MountPath: "/mount/path"}},
-	}
-
 	s := serving.Service{}
 	s.Spec.ConfigurationSpec.Template = &serving.RevisionTemplateSpec{}
 	s.Spec.ConfigurationSpec.Template.Spec.Containers = []corev1.Container{
 		{
+			Name: defaultContainerName,
 			VolumeMounts: []corev1.VolumeMount{
 				{
 					MountPath: "/mount/path",
@@ -151,15 +159,19 @@ func TestConfigureClasspathTraitWithKNativeResource(t *testing.T) {
 		},
 	}
 
-	environment.Resources = kubernetes.NewCollection(&s)
+	environment.Resources.Add(&s)
 
 	err := trait.Apply(environment)
 
+	cp := environment.Classpath.List()
+	sort.Strings(cp)
+
 	assert.Nil(t, err)
 	assert.NotNil(t, environment.Classpath)
-	assert.Equal(t, strset.New("/etc/camel/resources", "./resources", "/mount/path"), environment.Classpath)
-
-	assert.Len(t, container.Env, 0)
+	assert.ElementsMatch(t, []string{"/etc/camel/resources", "./resources", "/mount/path"}, cp)
+	assert.Len(t, s.Spec.ConfigurationSpec.Template.Spec.Containers[0].Env, 1)
+	assert.Equal(t, "JAVA_CLASSPATH", s.Spec.ConfigurationSpec.Template.Spec.Containers[0].Env[0].Name)
+	assert.Equal(t, strings.Join(cp, ":"), s.Spec.ConfigurationSpec.Template.Spec.Containers[0].Env[0].Value)
 }
 
 func TestConfigureClasspathTraitWithNominalIntegrationKit(t *testing.T) {
@@ -201,6 +213,7 @@ func createTestWithKitType(kitType string) (*classpathTrait, *Environment) {
 	trait.client = client
 
 	environment := &Environment{
+		Catalog: NewCatalog(context.TODO(), nil),
 		Integration: &v1alpha1.Integration{
 			Status: v1alpha1.IntegrationStatus{
 				Phase: v1alpha1.IntegrationPhaseDeploying,
@@ -211,6 +224,7 @@ func createTestWithKitType(kitType string) (*classpathTrait, *Environment) {
 				Phase: v1alpha1.IntegrationKitPhaseReady,
 			},
 		},
+		Resources: kubernetes.NewCollection(),
 	}
 
 	return trait, environment