Skip to content

Commit

Permalink
chore(trait): Use container args to configure JVM debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 22, 2020
1 parent 8cd7ff2 commit 2836a5e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
12 changes: 2 additions & 10 deletions pkg/trait/classpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ func (t *classpathTrait) Apply(e *Environment) error {
classpath.Add("./resources")

quarkus := e.Catalog.GetTrait("quarkus").(*quarkusTrait)
if quarkus.isEnabled() {
quarkus.addClasspath(e)
} else {
if !quarkus.isEnabled() {
for _, artifact := range kit.Status.Artifacts {
classpath.Add(artifact.Target)
}
Expand All @@ -100,13 +98,7 @@ func (t *classpathTrait) Apply(e *Environment) error {
classpath.Add("/deployments/dependencies/*")
}

containerName := defaultContainerName
dt := e.Catalog.GetTrait(containerTraitID)
if dt != nil {
containerName = dt.(*containerTrait).Name
}

container := e.Resources.GetContainerByName(containerName)
container := e.getIntegrationContainer()
if container != nil {
// Add mounted resources to the class path
for _, m := range container.VolumeMounts {
Expand Down
14 changes: 9 additions & 5 deletions pkg/trait/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ limitations under the License.
package trait

import (
"fmt"

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

// The Debug trait can be used to enable debugging on the integration container,
// so that a remote debugger can be attached.
//
// Enabling the trait will inject the `JAVA_DEBUG` environment variable into the integration container.
//
// +camel-k:trait=debug
type debugTrait struct {
BaseTrait `property:",squash"`
Expand All @@ -47,8 +46,13 @@ func (t *debugTrait) Configure(e *Environment) (bool, error) {
}

func (t *debugTrait) Apply(e *Environment) error {
// this is all that's needed as long as the base image is `fabric8/s2i-java` look into builder/builder.go
envvar.SetVal(&e.EnvVars, "JAVA_DEBUG", True)
container := e.getIntegrationContainer()
if container == nil {
return fmt.Errorf("unable to find integration container")
}

// TODO: Add options to configure debugging agent
container.Args = append(container.Args, "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005")

return nil
}
29 changes: 24 additions & 5 deletions pkg/trait/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ limitations under the License.
package trait

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/apache/camel-k/pkg/util/kubernetes"
)

func TestDebugTraitApplicability(t *testing.T) {
Expand All @@ -49,7 +51,6 @@ func TestDebugTraitApplicability(t *testing.T) {
},
},
},
EnvVars: make([]corev1.EnvVar, 0),
}

trait := newDebugTrait()
Expand All @@ -67,6 +68,7 @@ func TestDebugTraitApplicability(t *testing.T) {

func TestApplyDebugTrait(t *testing.T) {
environment := Environment{
Catalog: NewCatalog(context.TODO(), nil),
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
Expand All @@ -81,12 +83,29 @@ func TestApplyDebugTrait(t *testing.T) {
},
},
},
EnvVars: make([]corev1.EnvVar, 0),
Resources: kubernetes.NewCollection(),
}

d := appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: defaultContainerName,
},
},
},
},
},
}

environment.Resources.Add(&d)

trait := newDebugTrait()

assert.Nil(t, trait.Apply(&environment))
assert.NotNil(t, envvar.Get(environment.EnvVars, "JAVA_DEBUG"))
assert.Equal(t, True, envvar.Get(environment.EnvVars, "JAVA_DEBUG").Value)
assert.Equal(t, d.Spec.Template.Spec.Containers[0].Args, []string{
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005",
})
}
8 changes: 1 addition & 7 deletions pkg/trait/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
return nil
}

containerName := defaultContainerName
dt := e.Catalog.GetTrait(containerTraitID)
if dt != nil {
containerName = dt.(*containerTrait).Name
}

container := e.Resources.GetContainerByName(containerName)
container := e.getIntegrationContainer()
if container == nil {
e.Integration.Status.SetCondition(
v1.IntegrationConditionJolokiaAvailable,
Expand Down
8 changes: 1 addition & 7 deletions pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ func (t *prometheusTrait) Configure(e *Environment) (bool, error) {
}

func (t *prometheusTrait) Apply(e *Environment) (err error) {
containerName := defaultContainerName
dt := e.Catalog.GetTrait(containerTraitID)
if dt != nil {
containerName = dt.(*containerTrait).Name
}

container := e.Resources.GetContainerByName(containerName)
container := e.getIntegrationContainer()
if container == nil {
e.Integration.Status.SetCondition(
v1.IntegrationConditionPrometheusAvailable,
Expand Down
4 changes: 0 additions & 4 deletions pkg/trait/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ func (t *quarkusTrait) addBuildSteps(task *v1.BuilderTask) {
task.Steps = append(task.Steps, builder.StepIDsFor(runtime.QuarkusSteps...)...)
}

func (t *quarkusTrait) addClasspath(_ *Environment) {
// No-op as we rely on the Quarkus runner
}

func (t *quarkusTrait) addRuntimeDependencies(e *Environment) error {
dependencies := &e.Integration.Status.Dependencies

Expand Down
6 changes: 3 additions & 3 deletions pkg/trait/trait_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
return []Trait{
c.tPlatform,
c.tCamel,
c.tDebug,
c.tRestDsl,
c.tDependencies,
c.tBuilder,
Expand All @@ -167,6 +166,7 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
c.tPullSecret,
c.tJolokia,
c.tPrometheus,
c.tDebug,
c.tClasspath,
c.tProbes,
c.tRoute,
Expand All @@ -177,7 +177,6 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
return []Trait{
c.tPlatform,
c.tCamel,
c.tDebug,
c.tRestDsl,
c.tDependencies,
c.tBuilder,
Expand All @@ -193,6 +192,7 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
c.tPullSecret,
c.tJolokia,
c.tPrometheus,
c.tDebug,
c.tClasspath,
c.tProbes,
c.tIngress,
Expand All @@ -203,7 +203,6 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
return []Trait{
c.tPlatform,
c.tCamel,
c.tDebug,
c.tRestDsl,
c.tKnative,
c.tDependencies,
Expand All @@ -220,6 +219,7 @@ func (c *Catalog) TraitsForProfile(profile v1.TraitProfile) []Trait {
c.tPullSecret,
c.tJolokia,
c.tPrometheus,
c.tDebug,
c.tClasspath,
c.tProbes,
c.tIstio,
Expand Down
10 changes: 10 additions & 0 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,13 @@ func (e *Environment) CollectConfigurationValues(configurationType string) []str
func (e *Environment) CollectConfigurationPairs(configurationType string) map[string]string {
return CollectConfigurationPairs(configurationType, e.Platform, e.IntegrationKit, e.Integration)
}

func (e *Environment) getIntegrationContainer() *corev1.Container {
containerName := defaultContainerName
dt := e.Catalog.GetTrait(containerTraitID)
if dt != nil {
containerName = dt.(*containerTrait).Name
}

return e.Resources.GetContainerByName(containerName)
}

0 comments on commit 2836a5e

Please sign in to comment.