nicolaferraro closed pull request #276: trait: add a trait to inject pod 
metadata as env var
URL: https://github.com/apache/camel-k/pull/276
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go
index ef0b9b39..6ab0ae23 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 00000000..f97ee134
--- /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 00000000..12fdd316
--- /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)
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to