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

commit 1b89b017365f76515561ecd788e91c4b2cbd320e
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Thu Sep 13 18:25:55 2018 +0200

    Support for environment variables #65
---
 pkg/apis/camel/v1alpha1/types.go                 | 11 +++--
 pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go |  5 ++
 pkg/client/cmd/run.go                            |  9 ++++
 pkg/stub/action/integration/deploy.go            | 58 ++++++++++--------------
 pkg/stub/action/integration/util.go              | 32 +++++++++++++
 pkg/util/maven/types.go                          |  2 +-
 runtime/examples/env.js                          |  9 ++++
 7 files changed, 87 insertions(+), 39 deletions(-)

diff --git a/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go
index e59a3e6..22fefe9 100644
--- a/pkg/apis/camel/v1alpha1/types.go
+++ b/pkg/apis/camel/v1alpha1/types.go
@@ -42,11 +42,12 @@ type Integration struct {
 
 // IntegrationSpec --
 type IntegrationSpec struct {
-       Replicas     *int32         `json:"replicas,omitempty"`
-       Source       SourceSpec     `json:"source,omitempty"`
-       Context      string         `json:"context,omitempty"`
-       Dependencies []string       `json:"dependencies,omitempty"`
-       Properties   []PropertySpec `json:"properties,omitempty"`
+       Replicas     *int32            `json:"replicas,omitempty"`
+       Source       SourceSpec        `json:"source,omitempty"`
+       Context      string            `json:"context,omitempty"`
+       Dependencies []string          `json:"dependencies,omitempty"`
+       Properties   []PropertySpec    `json:"properties,omitempty"`
+       Environment  []EnvironmentSpec `json:"environment,omitempty"`
 }
 
 // SourceSpec --
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go 
b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index 1da7ad7..83af289 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -229,6 +229,11 @@ func (in *IntegrationSpec) DeepCopyInto(out 
*IntegrationSpec) {
                *out = make([]PropertySpec, len(*in))
                copy(*out, *in)
        }
+       if in.Environment != nil {
+               in, out := &in.Environment, &out.Environment
+               *out = make([]EnvironmentSpec, len(*in))
+               copy(*out, *in)
+       }
        return
 }
 
diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index ff66631..4af17df 100644
--- a/pkg/client/cmd/run.go
+++ b/pkg/client/cmd/run.go
@@ -54,6 +54,7 @@ func NewCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command 
{
        cmd.Flags().BoolVarP(&options.Wait, "wait", "w", false, "Waits for the 
integration to be running")
        cmd.Flags().StringVarP(&options.IntegrationContext, "context", "x", "", 
"The contex used to run the integration")
        cmd.Flags().StringSliceVarP(&options.Properties, "property", "p", nil, 
"Add a system property")
+       cmd.Flags().StringSliceVarP(&options.Environment, "env", "e", nil, "Add 
an environment variable")
 
        return &cmd
 }
@@ -65,6 +66,7 @@ type runCmdOptions struct {
        IntegrationName    string
        Dependencies       []string
        Properties         []string
+       Environment        []string
        Wait               bool
 }
 
@@ -185,6 +187,13 @@ func (o *runCmdOptions) createIntegration(cmd 
*cobra.Command, args []string) (*v
                        integration.Spec.Properties = 
append(integration.Spec.Properties, v1alpha1.PropertySpec{Name: pair[0], Value: 
pair[1]})
                }
        }
+       integration.Spec.Environment = make([]v1alpha1.EnvironmentSpec, 0)
+       for _, item := range o.Environment {
+               pair := strings.Split(item, "=")
+               if len(pair) == 2 {
+                       integration.Spec.Environment = 
append(integration.Spec.Environment, v1alpha1.EnvironmentSpec{Name: pair[0], 
Value: pair[1]})
+               }
+       }
 
        existed := false
        err = sdk.Create(&integration)
diff --git a/pkg/stub/action/integration/deploy.go 
b/pkg/stub/action/integration/deploy.go
index 9acd9d8..551a267 100644
--- a/pkg/stub/action/integration/deploy.go
+++ b/pkg/stub/action/integration/deploy.go
@@ -46,10 +46,14 @@ func (action *deployAction) CanHandle(integration 
*v1alpha1.Integration) bool {
 }
 
 func (action *deployAction) Handle(integration *v1alpha1.Integration) error {
-       if err := createOrUpdateConfigMap(integration); err != nil {
+       ctx, err := LookupContextForIntegration(integration)
+       if err != nil {
                return err
        }
-       if err := createOrUpdateDeployment(integration); err != nil {
+       if err = createOrUpdateConfigMap(ctx, integration); err != nil {
+               return err
+       }
+       if err = createOrUpdateDeployment(ctx, integration); err != nil {
                return err
        }
 
@@ -62,15 +66,10 @@ func (action *deployAction) Handle(integration 
*v1alpha1.Integration) error {
 //
 // **********************************
 
-func getConfigMapFor(integration *v1alpha1.Integration) (*corev1.ConfigMap, 
error) {
+func getConfigMapFor(ctx *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) (*corev1.ConfigMap, error) {
        controller := true
        blockOwnerDeletion := true
 
-       ctx, err := LookupContextForIntegration(integration)
-       if err != nil {
-               return nil, err
-       }
-
        // combine properties of integration with context, integration
        // properties have the priority
        properties := CombinePropertiesAsMap(ctx, integration)
@@ -108,8 +107,8 @@ func getConfigMapFor(integration *v1alpha1.Integration) 
(*corev1.ConfigMap, erro
        return &cm, nil
 }
 
-func createOrUpdateConfigMap(integration *v1alpha1.Integration) error {
-       cm, err := getConfigMapFor(integration)
+func createOrUpdateConfigMap(ctx *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) error {
+       cm, err := getConfigMapFor(ctx, integration)
        if err != nil {
                return err
        }
@@ -131,10 +130,20 @@ func createOrUpdateConfigMap(integration 
*v1alpha1.Integration) error {
 //
 // **********************************
 
-func getDeploymentFor(integration *v1alpha1.Integration) (*appsv1.Deployment, 
error) {
+func getDeploymentFor(ctx *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) (*appsv1.Deployment, error) {
        controller := true
        blockOwnerDeletion := true
-       integrationName := strings.TrimPrefix(integration.Spec.Source.Name, "/")
+       sourceName := strings.TrimPrefix(integration.Spec.Source.Name, "/")
+
+       // combine environment of integration with context, integration
+       // environment has the priority
+       environment := CombineEnvironmentAsMap(ctx, integration)
+
+       // set env vars needed by the runtime
+       environment["JAVA_MAIN_CLASS"] = "org.apache.camel.k.jvm.Application"
+       environment["CAMEL_K_ROUTES_URI"] = "file:/etc/camel/" + sourceName
+       environment["CAMEL_K_ROUTES_LANGUAGE"] = 
integration.Spec.Source.Language
+       environment["CAMEL_K_PROPERTIES"] = 
"file:/etc/camel/application.properties"
 
        labels := map[string]string{
                "camel.apache.org/integration": integration.Name,
@@ -174,30 +183,13 @@ func getDeploymentFor(integration *v1alpha1.Integration) 
(*appsv1.Deployment, er
                                                {
                                                        Name:  integration.Name,
                                                        Image: 
integration.Status.Image,
+                                                       Env:   
EnvironmentAsEnvVarSlice(environment),
                                                        VolumeMounts: 
[]corev1.VolumeMount{
                                                                {
                                                                        Name:   
   "integration-volume",
                                                                        
MountPath: "/etc/camel",
                                                                },
                                                        },
-                                                       Env: []corev1.EnvVar{
-                                                               {
-                                                                       Name:  
"JAVA_MAIN_CLASS",
-                                                                       Value: 
"org.apache.camel.k.jvm.Application",
-                                                               },
-                                                               {
-                                                                       Name:  
"CAMEL_K_ROUTES_URI",
-                                                                       Value: 
"file:/etc/camel/" + integrationName,
-                                                               },
-                                                               {
-                                                                       Name:  
"CAMEL_K_ROUTES_LANGUAGE",
-                                                                       Value: 
integration.Spec.Source.Language,
-                                                               },
-                                                               {
-                                                                       Name:  
"CAMEL_K_PROPERTIES",
-                                                                       Value: 
"file:/etc/camel/application.properties",
-                                                               },
-                                                       },
                                                },
                                        },
                                        Volumes: []corev1.Volume{
@@ -211,7 +203,7 @@ func getDeploymentFor(integration *v1alpha1.Integration) 
(*appsv1.Deployment, er
                                                                        Items: 
[]corev1.KeyToPath{
                                                                                
{
                                                                                
        Key:  "integration",
-                                                                               
        Path: integrationName,
+                                                                               
        Path: sourceName,
                                                                                
}, {
                                                                                
        Key:  "properties",
                                                                                
        Path: "application.properties",
@@ -229,8 +221,8 @@ func getDeploymentFor(integration *v1alpha1.Integration) 
(*appsv1.Deployment, er
        return &deployment, nil
 }
 
-func createOrUpdateDeployment(integration *v1alpha1.Integration) error {
-       deployment, err := getDeploymentFor(integration)
+func createOrUpdateDeployment(ctx *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) error {
+       deployment, err := getDeploymentFor(ctx, integration)
        if err != nil {
                return err
        }
diff --git a/pkg/stub/action/integration/util.go 
b/pkg/stub/action/integration/util.go
index 339a996..96e9d1b 100644
--- a/pkg/stub/action/integration/util.go
+++ b/pkg/stub/action/integration/util.go
@@ -5,6 +5,7 @@ import (
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/operator-framework/operator-sdk/pkg/sdk"
+       "k8s.io/api/core/v1"
 
        "github.com/pkg/errors"
 )
@@ -35,6 +36,17 @@ func PropertiesString(m map[string]string) string {
        return properties
 }
 
+// EnvironmentAsEnvVarSlice --
+func EnvironmentAsEnvVarSlice(m map[string]string) []v1.EnvVar {
+       env := make([]v1.EnvVar, 0, len(m))
+
+       for k, v := range m {
+               env = append(env, v1.EnvVar{Name: k, Value: v})
+       }
+
+       return env
+}
+
 // CombinePropertiesAsMap --
 func CombinePropertiesAsMap(context *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) map[string]string {
        properties := make(map[string]string)
@@ -54,3 +66,23 @@ func CombinePropertiesAsMap(context 
*v1alpha1.IntegrationContext, integration *v
 
        return properties
 }
+
+// CombineEnvironmentAsMap --
+func CombineEnvironmentAsMap(context *v1alpha1.IntegrationContext, integration 
*v1alpha1.Integration) map[string]string {
+       environment := make(map[string]string)
+       if context != nil {
+               // Add context environment first so integrations can
+               // override it
+               for _, p := range context.Spec.Environment {
+                       environment[p.Name] = p.Value
+               }
+       }
+
+       if integration != nil {
+               for _, p := range integration.Spec.Environment {
+                       environment[p.Name] = p.Value
+               }
+       }
+
+       return environment
+}
diff --git a/pkg/util/maven/types.go b/pkg/util/maven/types.go
index 30a80de..5b1158e 100644
--- a/pkg/util/maven/types.go
+++ b/pkg/util/maven/types.go
@@ -25,7 +25,7 @@ type ProjectDefinition struct {
        Project     Project
        JavaSources map[string]string
        Resources   map[string]string
-       Env         map[string]string
+       Env         map[string]string // TODO: should we deprecate it ? env are 
set on deployment
 }
 
 type Project struct {
diff --git a/runtime/examples/env.js b/runtime/examples/env.js
new file mode 100644
index 0000000..8e26f8e
--- /dev/null
+++ b/runtime/examples/env.js
@@ -0,0 +1,9 @@
+//
+// To run this integrations use:
+//
+//     kamel run -e MY_MESSAGE=test-env runtime/examples/env.js
+//
+
+from('timer:env?period=1s')
+    .routeId('env')
+    .log('{{env:MY_MESSAGE}}')
\ No newline at end of file

Reply via email to