nicolaferraro closed pull request #286: Inject camel-k version as environment 
variable
URL: https://github.com/apache/camel-k/pull/286
 
 
   

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/environment.go b/pkg/trait/environment.go
index f97ee134..9e05d451 100644
--- a/pkg/trait/environment.go
+++ b/pkg/trait/environment.go
@@ -19,6 +19,7 @@ package trait
 
 import (
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+       "github.com/apache/camel-k/version"
        appsv1 "k8s.io/api/apps/v1"
        "k8s.io/api/core/v1"
 )
@@ -29,8 +30,9 @@ type environmentTrait struct {
 }
 
 const (
-       envVarNamespace = "NAMESPACE"
-       envVarPodName   = "POD_NAME"
+       envVarNamespace     = "NAMESPACE"
+       envVarPodName       = "POD_NAME"
+       envVarCamelKVersion = "CAMEL_K_VERSION"
 )
 
 func newEnvironmentTrait() *environmentTrait {
@@ -51,10 +53,16 @@ func (t *environmentTrait) Configure(e *Environment) (bool, 
error) {
 }
 
 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]
+       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:  envVarCamelKVersion,
+                               Value: version.Version,
+                       })
+
+                       if t.ContainerMeta {
                                c.Env = append(c.Env, v1.EnvVar{
                                        Name: envVarNamespace,
                                        ValueFrom: &v1.EnvVarSource{
@@ -72,8 +80,8 @@ func (t *environmentTrait) Apply(e *Environment) error {
                                        },
                                })
                        }
-               })
-       }
+               }
+       })
 
        return nil
 }
diff --git a/pkg/trait/environment_test.go b/pkg/trait/environment_test.go
index 12fdd316..4707a968 100644
--- a/pkg/trait/environment_test.go
+++ b/pkg/trait/environment_test.go
@@ -55,6 +55,7 @@ func TestDefaultEnvironment(t *testing.T) {
 
        ns := false
        name := false
+       ck := false
 
        env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
                for _, e := range 
deployment.Spec.Template.Spec.Containers[0].Env {
@@ -64,11 +65,15 @@ func TestDefaultEnvironment(t *testing.T) {
                        if e.Name == envVarPodName {
                                name = true
                        }
+                       if e.Name == envVarCamelKVersion {
+                               ck = true
+                       }
                }
        })
 
        assert.True(t, ns)
        assert.True(t, name)
+       assert.True(t, ck)
 }
 
 func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
@@ -105,6 +110,7 @@ func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
 
        ns := false
        name := false
+       ck := false
 
        env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
                for _, e := range 
deployment.Spec.Template.Spec.Containers[0].Env {
@@ -114,11 +120,15 @@ func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
                        if e.Name == envVarPodName {
                                name = true
                        }
+                       if e.Name == envVarCamelKVersion {
+                               ck = true
+                       }
                }
        })
 
        assert.True(t, ns)
        assert.True(t, name)
+       assert.True(t, ck)
 }
 
 func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
@@ -155,6 +165,7 @@ func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
 
        ns := false
        name := false
+       ck := false
 
        env.Resources.VisitDeployment(func(deployment *v1.Deployment) {
                for _, e := range 
deployment.Spec.Template.Spec.Containers[0].Env {
@@ -164,9 +175,13 @@ func TestDisabledContainerMetaDataEnvVars(t *testing.T) {
                        if e.Name == envVarPodName {
                                name = true
                        }
+                       if e.Name == envVarCamelKVersion {
+                               ck = true
+                       }
                }
        })
 
        assert.False(t, ns)
        assert.False(t, name)
+       assert.True(t, ck)
 }


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to