This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 3c82efe  trait: add a trait to inject pod metadata as env var #275
3c82efe is described below

commit 3c82efe175349ab0369740a9b6f6941a8df10c1b
Author: lburgazzoli <[email protected]>
AuthorDate: Mon Dec 10 17:32:59 2018 +0100

    trait: add a trait to inject pod metadata as env var #275
---
 pkg/trait/catalog.go          |   6 ++
 pkg/trait/environment.go      |  79 +++++++++++++++++++
 pkg/trait/environment_test.go | 172 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 257 insertions(+)

diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go
index ef0b9b3..6ab0ae2 100644
--- a/pkg/trait/catalog.go
+++ b/pkg/trait/catalog.go
@@ -40,6 +40,7 @@ type Catalog struct {
        tBuilder      Trait
        tSpringBoot   Trait
        tIstio        Trait
+       tEnvironment  Trait
 }
 
 // NewCatalog creates a new trait Catalog
@@ -56,6 +57,7 @@ func NewCatalog() *Catalog {
                tBuilder:      newBuilderTrait(),
                tSpringBoot:   newSpringBootTrait(),
                tIstio:        newIstioTrait(),
+               tEnvironment:  newEnvironmentTrait(),
        }
 }
 
@@ -72,6 +74,7 @@ func (c *Catalog) allTraits() []Trait {
                c.tBuilder,
                c.tSpringBoot,
                c.tIstio,
+               c.tEnvironment,
        }
 }
 
@@ -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,
@@ -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,
@@ -107,6 +112,7 @@ func (c *Catalog) traitsFor(environment *Environment) 
[]Trait {
                        c.tSpringBoot,
                        c.tKnative,
                        c.tDeployment,
+                       c.tEnvironment,
                        c.tIstio,
                        c.tOwner,
                }
diff --git a/pkg/trait/environment.go b/pkg/trait/environment.go
new file mode 100644
index 0000000..f97ee13
--- /dev/null
+++ b/pkg/trait/environment.go
@@ -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
+}
diff --git a/pkg/trait/environment_test.go b/pkg/trait/environment_test.go
new file mode 100644
index 0000000..12fdd31
--- /dev/null
+++ b/pkg/trait/environment_test.go
@@ -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)
+}

Reply via email to