Skip to content

Commit

Permalink
trait: add a trait to inject pod metadata as env var apache#275
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Dec 11, 2018
1 parent d51bb0d commit 8047557
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Catalog struct {
tBuilder Trait
tSpringBoot Trait
tIstio Trait
tEnvironment Trait
}

// NewCatalog creates a new trait Catalog
Expand All @@ -56,6 +57,7 @@ func NewCatalog() *Catalog {
tBuilder: newBuilderTrait(),
tSpringBoot: newSpringBootTrait(),
tIstio: newIstioTrait(),
tEnvironment: newEnvironmentTrait(),
}
}

Expand All @@ -72,6 +74,7 @@ func (c *Catalog) allTraits() []Trait {
c.tBuilder,
c.tSpringBoot,
c.tIstio,
c.tEnvironment,
}
}

Expand All @@ -84,6 +87,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
c.tBuilder,
c.tSpringBoot,
c.tDeployment,
c.tEnvironment,
c.tService,
c.tRoute,
c.tOwner,
Expand All @@ -95,6 +99,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
c.tBuilder,
c.tSpringBoot,
c.tDeployment,
c.tEnvironment,
c.tService,
c.tIngress,
c.tOwner,
Expand All @@ -107,6 +112,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
c.tSpringBoot,
c.tKnative,
c.tDeployment,
c.tEnvironment,
c.tIstio,
c.tOwner,
}
Expand Down
79 changes: 79 additions & 0 deletions pkg/trait/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
)

type environmentTrait struct {
BaseTrait `property:",squash"`
ContainerMeta bool `property:"container-meta"`
}

const (
envVarNamespace = "NAMESPACE"
envVarPodName = "POD_NAME"
)

func newEnvironmentTrait() *environmentTrait {
return &environmentTrait{
BaseTrait: BaseTrait{
id: ID("environment"),
},
ContainerMeta: true,
}
}

func (t *environmentTrait) Configure(e *Environment) (bool, error) {
if t.Enabled == nil || *t.Enabled {
return e.IntegrationInPhase(v1alpha1.IntegrationPhaseDeploying), nil
}

return false, nil
}

func (t *environmentTrait) Apply(e *Environment) error {
if t.ContainerMeta {
e.Resources.VisitDeployment(func(deployment *appsv1.Deployment) {
for i := 0; i < len(deployment.Spec.Template.Spec.Containers); i++ {
c := &deployment.Spec.Template.Spec.Containers[i]
c.Env = append(c.Env, v1.EnvVar{
Name: envVarNamespace,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
})
c.Env = append(c.Env, v1.EnvVar{
Name: envVarPodName,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
})
}
})
}

return nil
}
172 changes: 172 additions & 0 deletions pkg/trait/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
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 (
"testing"

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

"k8s.io/api/apps/v1"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/stretchr/testify/assert"
)

func TestDefaultEnvironment(t *testing.T) {
env := Environment{
Integration: &v1alpha1.Integration{
Status: v1alpha1.IntegrationStatus{
Phase: v1alpha1.IntegrationPhaseDeploying,
},
Spec: v1alpha1.IntegrationSpec{
Profile: v1alpha1.TraitProfileOpenShift,
},
},
Context: &v1alpha1.IntegrationContext{},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
},
},
EnvVars: make(map[string]string),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
}

err := NewCatalog().apply(&env)

assert.Nil(t, err)

ns := false
name := false

env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
for _, e := range deployment.Spec.Template.Spec.Containers[0].Env {
if e.Name == envVarNamespace {
ns = true
}
if e.Name == envVarPodName {
name = true
}
}
})

assert.True(t, ns)
assert.True(t, name)
}

func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
env := Environment{
Integration: &v1alpha1.Integration{
Status: v1alpha1.IntegrationStatus{
Phase: v1alpha1.IntegrationPhaseDeploying,
},
Spec: v1alpha1.IntegrationSpec{
Profile: v1alpha1.TraitProfileOpenShift,
Traits: map[string]v1alpha1.IntegrationTraitSpec{
"environment": {
Configuration: map[string]string{
"container-meta": "true",
},
},
},
},
},
Context: &v1alpha1.IntegrationContext{},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
},
},
EnvVars: make(map[string]string),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
}

err := NewCatalog().apply(&env)

assert.Nil(t, err)

ns := false
name := false

env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
for _, e := range deployment.Spec.Template.Spec.Containers[0].Env {
if e.Name == envVarNamespace {
ns = true
}
if e.Name == envVarPodName {
name = true
}
}
})

assert.True(t, ns)
assert.True(t, name)
}

func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
env := Environment{
Integration: &v1alpha1.Integration{
Status: v1alpha1.IntegrationStatus{
Phase: v1alpha1.IntegrationPhaseDeploying,
},
Spec: v1alpha1.IntegrationSpec{
Profile: v1alpha1.TraitProfileOpenShift,
Traits: map[string]v1alpha1.IntegrationTraitSpec{
"environment": {
Configuration: map[string]string{
"container-meta": "false",
},
},
},
},
},
Context: &v1alpha1.IntegrationContext{},
Platform: &v1alpha1.IntegrationPlatform{
Spec: v1alpha1.IntegrationPlatformSpec{
Cluster: v1alpha1.IntegrationPlatformClusterOpenShift,
},
},
EnvVars: make(map[string]string),
ExecutedTraits: make([]Trait, 0),
Resources: kubernetes.NewCollection(),
}

err := NewCatalog().apply(&env)

assert.Nil(t, err)

ns := false
name := false

env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
for _, e := range deployment.Spec.Template.Spec.Containers[0].Env {
if e.Name == envVarNamespace {
ns = true
}
if e.Name == envVarPodName {
name = true
}
}
})

assert.False(t, ns)
assert.False(t, name)
}

0 comments on commit 8047557

Please sign in to comment.