Skip to content

Commit

Permalink
fix: include mount paths in java's classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and nicolaferraro committed Mar 12, 2019
1 parent c713352 commit 83d8e03
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 26 deletions.
3 changes: 3 additions & 0 deletions pkg/trait/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"testing"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/util/defaults"

"github.com/apache/camel-k/pkg/util/test"
Expand Down Expand Up @@ -154,6 +156,7 @@ func createBuilderTestEnv(cluster v1alpha1.IntegrationPlatformCluster, strategy
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}
}

Expand Down
27 changes: 13 additions & 14 deletions pkg/trait/classpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package trait

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"

"github.com/pkg/errors"

k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -42,11 +43,8 @@ func (t *classpathTrait) Configure(e *Environment) (bool, error) {
if t.Enabled != nil && !*t.Enabled {
return false, nil
}
if e.InPhase(v1alpha1.IntegrationContextPhaseReady, v1alpha1.IntegrationPhaseDeploying) {
return true, nil
}

return false, nil
return e.InPhase(v1alpha1.IntegrationContextPhaseReady, v1alpha1.IntegrationPhaseDeploying), nil
}

func (t *classpathTrait) Apply(e *Environment) error {
Expand All @@ -71,12 +69,15 @@ func (t *classpathTrait) Apply(e *Environment) error {
return fmt.Errorf("unable to find integration context %s", e.Integration.Status.Context)
}

deps := make([]string, 0, 2+len(ctx.Status.Artifacts))
deps = append(deps, "/etc/camel/resources")
deps = append(deps, "./resources")
if e.Classpath == nil {
e.Classpath = strset.New()
}

e.Classpath.Add("/etc/camel/resources")
e.Classpath.Add("./resources")

for _, artifact := range ctx.Status.Artifacts {
deps = append(deps, artifact.Target)
e.Classpath.Add(artifact.Target)
}

if e.IntegrationContext.Labels["camel.apache.org/context.type"] == v1alpha1.IntegrationContextTypeExternal {
Expand All @@ -85,10 +86,8 @@ func (t *classpathTrait) Apply(e *Environment) error {
// the classpath so we assume the all jars in /deployments/dependencies/ have
// to be taken into account
//
deps = append(deps, "/deployments/dependencies/*")
e.Classpath.Add("/deployments/dependencies/*")
}

envvar.SetVal(&e.EnvVars, "JAVA_CLASSPATH", strings.Join(deps, ":"))

return nil
}
22 changes: 20 additions & 2 deletions pkg/trait/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ limitations under the License.
package trait

import (
"sort"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -82,9 +82,11 @@ func (t *deploymentTrait) Apply(e *Environment) error {
// trigger integration deploy
e.Integration.Status.Phase = v1alpha1.IntegrationPhaseDeploying
}

return nil
}

if e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying {
if e.InPhase(v1alpha1.IntegrationContextPhaseReady, v1alpha1.IntegrationPhaseDeploying) {
e.Resources.AddAll(e.ComputeConfigMaps(t.deployer.ContainerImage))
e.Resources.Add(t.getDeploymentFor(e))
}
Expand Down Expand Up @@ -178,5 +180,21 @@ func (t *deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment {
&deployment.Spec.Template.Spec.Containers[0].VolumeMounts,
)

//
// Add mounted volumes as resources
//
for _, c := range deployment.Spec.Template.Spec.Containers {
for _, m := range c.VolumeMounts {
e.Classpath.Add(m.MountPath)
}
}

cp := e.Classpath.List()

// keep classpath sorted
sort.Strings(cp)

envvar.SetVal(&deployment.Spec.Template.Spec.Containers[0].Env, "JAVA_CLASSPATH", strings.Join(cp, ":"))

return &deployment
}
23 changes: 20 additions & 3 deletions pkg/trait/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"testing"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/util/test"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand All @@ -46,7 +48,11 @@ func TestDefaultEnvironment(t *testing.T) {
Profile: v1alpha1.TraitProfileOpenShift,
},
},
IntegrationContext: &v1alpha1.IntegrationContext{},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -55,6 +61,7 @@ func TestDefaultEnvironment(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = NewEnvironmentTestCatalog().apply(&env)
Expand Down Expand Up @@ -106,7 +113,11 @@ func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -115,6 +126,7 @@ func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = NewEnvironmentTestCatalog().apply(&env)
Expand Down Expand Up @@ -166,7 +178,11 @@ func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -175,6 +191,7 @@ func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = NewEnvironmentTestCatalog().apply(&env)
Expand Down
23 changes: 18 additions & 5 deletions pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ limitations under the License.
package trait

import (
"sort"
"strconv"
"strings"

"github.com/apache/camel-k/pkg/util/kubernetes"

Expand Down Expand Up @@ -62,7 +64,7 @@ func (t *knativeServiceTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

if !e.IntegrationInPhase(v1alpha1.IntegrationPhaseDeploying) {
if !e.InPhase(v1alpha1.IntegrationContextPhaseReady, v1alpha1.IntegrationPhaseDeploying) {
return false, nil
}

Expand Down Expand Up @@ -203,16 +205,27 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e

if t.ConfigurationType == "volume" {
t.bindToVolumes(e, &svc)
} else {
if err := t.bindToEnvVar(e, &svc); err != nil {
return nil, err
}
} else if err := t.bindToEnvVar(e, &svc); err != nil {
return nil, err
}

// add env vars from traits
for _, envVar := range e.EnvVars {
envvar.SetVar(&svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env, envVar)
}

// Add mounted volumes as resources
for _, m := range svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.VolumeMounts {
e.Classpath.Add(m.MountPath)
}

cp := e.Classpath.List()

// keep classpath sorted
sort.Strings(cp)

// set the classpath
envvar.SetVal(environment, "JAVA_CLASSPATH", strings.Join(cp, ":"))

return &svc, nil
}
20 changes: 20 additions & 0 deletions pkg/trait/knative_service_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"testing"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/envvar"
Expand Down Expand Up @@ -85,6 +87,11 @@ func TestKnativeTraitWithCompressedSources(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -97,6 +104,7 @@ func TestKnativeTraitWithCompressedSources(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = traitCatalog.apply(&environment)
Expand Down Expand Up @@ -135,6 +143,9 @@ func TestKnativeTraitWithCompressedSources(t *testing.T) {
resource = util.LookupEnvVar(vars, "CAMEL_K_RESOURCE_001")
assert.NotNil(t, resource)
assert.Equal(t, content, resource.Value)

resource = util.LookupEnvVar(vars, "JAVA_CLASSPATH")
assert.NotNil(t, resource)
})

assert.True(t, services > 0)
Expand Down Expand Up @@ -174,6 +185,11 @@ func TestKnativeTraitWithConfigMapSources(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -198,6 +214,7 @@ func TestKnativeTraitWithConfigMapSources(t *testing.T) {
"content": content,
},
}),
Classpath: strset.New(),
}

err = traitCatalog.apply(&environment)
Expand All @@ -220,6 +237,9 @@ func TestKnativeTraitWithConfigMapSources(t *testing.T) {
route := util.LookupEnvVar(vars, "CAMEL_K_ROUTE_000")
assert.NotNil(t, route)
assert.Equal(t, content, route.Value)

resource := util.LookupEnvVar(vars, "JAVA_CLASSPATH")
assert.NotNil(t, resource)
})

assert.True(t, services > 0)
Expand Down
16 changes: 16 additions & 0 deletions pkg/trait/knative_service_vol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"testing"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/apache/camel-k/pkg/util/kubernetes"
Expand Down Expand Up @@ -86,6 +88,11 @@ func TestKnativeWithVolumeBinding(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -98,6 +105,7 @@ func TestKnativeWithVolumeBinding(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = traitCatalog.apply(&environment)
Expand Down Expand Up @@ -160,6 +168,7 @@ func TestKnativeWithVolumeBinding(t *testing.T) {
}
})

test.EnvVarExists(t, spec.Container.Env, "JAVA_CLASSPATH")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_ROUTES", "file:/etc/camel/sources/i-source-000/routes.js?language=js&compression=true")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_CONF", "/etc/camel/conf/application.properties")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_CONF_D", "/etc/camel/conf.d")
Expand Down Expand Up @@ -222,6 +231,11 @@ func TestKnativeWithVolumeBindingAndContainerImage(t *testing.T) {
},
},
},
IntegrationContext: &v1alpha1.IntegrationContext{
Status: v1alpha1.IntegrationContextStatus{
Phase: v1alpha1.IntegrationContextPhaseReady,
},
},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
Expand All @@ -234,6 +248,7 @@ func TestKnativeWithVolumeBindingAndContainerImage(t *testing.T) {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
Classpath: strset.New(),
}

err = traitCatalog.apply(&environment)
Expand Down Expand Up @@ -266,6 +281,7 @@ func TestKnativeWithVolumeBindingAndContainerImage(t *testing.T) {
test.HasVolume(t, spec.Volumes, "my-secret")
test.HasVolume(t, spec.Volumes, "integration-properties")

test.EnvVarExists(t, spec.Container.Env, "JAVA_CLASSPATH")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_ROUTES", "file:/deployments/sources/routes.js?language=js&compression=true")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_CONF", "/etc/camel/conf/application.properties")
test.EnvVarHasValue(t, spec.Container.Env, "CAMEL_K_CONF_D", "/etc/camel/conf.d")
Expand Down
Loading

0 comments on commit 83d8e03

Please sign in to comment.