You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2018/09/12 14:38:43 UTC

[camel-k] branch master updated: Store integration code in a configmap #54

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 c194b5a  Store integration code in a configmap #54
c194b5a is described below

commit c194b5aad45235efb4b9cb32012a6860f7c2f4c4
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Sep 12 16:18:03 2018 +0200

    Store integration code in a configmap #54
---
 deploy/operator.yaml                  |   7 --
 deploy/resources.go                   |   7 --
 pkg/build/local/local_builder.go      |  18 +----
 pkg/stub/action/integration/deploy.go | 139 ++++++++++++++++++++++++++++++----
 4 files changed, 128 insertions(+), 43 deletions(-)

diff --git a/deploy/operator.yaml b/deploy/operator.yaml
index 426387a..a2c4abc 100644
--- a/deploy/operator.yaml
+++ b/deploy/operator.yaml
@@ -2,8 +2,6 @@ apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: camel-k-operator
-  labels:
-    app: "camel-k"
 spec:
   replicas: 1
   selector:
@@ -17,9 +15,6 @@ spec:
       containers:
         - name: camel-k-operator
           image: docker.io/apache/camel-k:0.0.1-SNAPSHOT
-          ports:
-          - containerPort: 60000
-            name: metrics
           command:
           - camel-k-operator
           imagePullPolicy: Always
@@ -28,5 +23,3 @@ spec:
               valueFrom:
                 fieldRef:
                   fieldPath: metadata.namespace
-            - name: OPERATOR_NAME
-              value: "camel-k-operator"
diff --git a/deploy/resources.go b/deploy/resources.go
index 02b58ba..96995ce 100644
--- a/deploy/resources.go
+++ b/deploy/resources.go
@@ -359,8 +359,6 @@ apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: camel-k-operator
-  labels:
-    app: "camel-k"
 spec:
   replicas: 1
   selector:
@@ -374,9 +372,6 @@ spec:
       containers:
         - name: camel-k-operator
           image: docker.io/apache/camel-k:0.0.1-SNAPSHOT
-          ports:
-          - containerPort: 60000
-            name: metrics
           command:
           - camel-k-operator
           imagePullPolicy: Always
@@ -385,8 +380,6 @@ spec:
               valueFrom:
                 fieldRef:
                   fieldPath: metadata.namespace
-            - name: OPERATOR_NAME
-              value: "camel-k-operator"
 
 `
 	Resources["user-cluster-role.yaml"] =
diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index df96879..cd5b7b0 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -280,10 +280,8 @@ func generateProjectDefinition(source build.BuildSource) (maven.ProjectDefinitio
 				Dependencies: make([]maven.Dependency, 0),
 			},
 		},
-		Resources: map[string]string{
-			source.Code.Name: source.Code.Content,
-		},
-		Env: make(map[string]string),
+		Resources: make(map[string]string),
+		Env:       make(map[string]string),
 	}
 
 	//
@@ -312,17 +310,5 @@ func generateProjectDefinition(source build.BuildSource) (maven.ProjectDefinitio
 		}
 	}
 
-	//
-	// set-up env
-	//
-
-	project.Env["JAVA_MAIN_CLASS"] = "org.apache.camel.k.jvm.Application"
-	project.Env["CAMEL_K_ROUTES_URI"] = "classpath:" + source.Code.Name
-
-	// Don't set the language if not set
-	if source.Code.Language != "" {
-		project.Env["CAMEL_K_ROUTES_LANGUAGE"] = source.Code.Language
-	}
-
 	return project, nil
 }
diff --git a/pkg/stub/action/integration/deploy.go b/pkg/stub/action/integration/deploy.go
index 43e50b8..3403928 100644
--- a/pkg/stub/action/integration/deploy.go
+++ b/pkg/stub/action/integration/deploy.go
@@ -18,10 +18,12 @@ limitations under the License.
 package action
 
 import (
+	"strings"
+
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/operator-framework/operator-sdk/pkg/sdk"
 	"github.com/pkg/errors"
-	"k8s.io/api/apps/v1"
+	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -43,32 +45,88 @@ func (a *DeployAction) CanHandle(integration *v1alpha1.Integration) bool {
 }
 
 func (a *DeployAction) Handle(integration *v1alpha1.Integration) error {
+	if err := createOrUpdateConfigMap(integration); err != nil {
+		return err
+	}
+	if err := createOrUpdateDeployment(integration); err != nil {
+		return err
+	}
 
-	deployment := a.getDeploymentFor(integration)
-	err := sdk.Create(deployment)
-	if err != nil && k8serrors.IsAlreadyExists(err) {
-		err = sdk.Update(deployment)
+	return nil
+}
+
+// **********************************
+//
+// ConfigMap
+//
+// **********************************
+
+func getConfigMapFor(integration *v1alpha1.Integration) *corev1.ConfigMap {
+	controller := true
+	blockOwnerDeletion := true
+
+	return &corev1.ConfigMap{
+		TypeMeta: metav1.TypeMeta{
+			Kind:       "ConfigMap",
+			APIVersion: "v1",
+		},
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      integration.Name,
+			Namespace: integration.Namespace,
+			Labels:    integration.Labels,
+			Annotations: map[string]string{
+				"camel.apache.org/source.language": integration.Spec.Source.Language,
+				"camel.apache.org/source.name":     integration.Spec.Source.Name,
+			},
+			OwnerReferences: []metav1.OwnerReference{
+				{
+					APIVersion:         integration.APIVersion,
+					Kind:               integration.Kind,
+					Name:               integration.Name,
+					UID:                integration.UID,
+					Controller:         &controller,
+					BlockOwnerDeletion: &blockOwnerDeletion,
+				},
+			},
+		},
+		Data: map[string]string{
+			"integration": integration.Spec.Source.Content,
+		},
 	}
+}
+
+func createOrUpdateConfigMap(integration *v1alpha1.Integration) error {
+	cm := getConfigMapFor(integration)
 
+	err := sdk.Create(cm)
+	if err != nil && k8serrors.IsAlreadyExists(err) {
+		err = sdk.Update(cm)
+	}
 	if err != nil {
-		return errors.Wrap(err, "could not create or replace deployment for integration "+integration.Name)
+		return errors.Wrap(err, "could not create or replace configmap for integration "+integration.Name)
 	}
 
-	target := integration.DeepCopy()
-	target.Status.Phase = v1alpha1.IntegrationPhaseRunning
-	return sdk.Update(target)
+	return err
 }
 
-func (*DeployAction) getDeploymentFor(integration *v1alpha1.Integration) *v1.Deployment {
+// **********************************
+//
+// Deployment
+//
+// **********************************
+
+func getDeploymentFor(integration *v1alpha1.Integration) *appsv1.Deployment {
 	controller := true
 	blockOwnerDeletion := true
+	integrationName := strings.TrimPrefix(integration.Spec.Source.Name, "/")
+
 	labels := map[string]string{
 		"camel.apache.org/integration": integration.Name,
 	}
-	deployment := v1.Deployment{
+	deployment := appsv1.Deployment{
 		TypeMeta: metav1.TypeMeta{
 			Kind:       "Deployment",
-			APIVersion: v1.SchemeGroupVersion.String(),
+			APIVersion: appsv1.SchemeGroupVersion.String(),
 		},
 		ObjectMeta: metav1.ObjectMeta{
 			Name:        integration.Name,
@@ -86,7 +144,7 @@ func (*DeployAction) getDeploymentFor(integration *v1alpha1.Integration) *v1.Dep
 				},
 			},
 		},
-		Spec: v1.DeploymentSpec{
+		Spec: appsv1.DeploymentSpec{
 			Replicas: integration.Spec.Replicas,
 			Selector: &metav1.LabelSelector{
 				MatchLabels: labels,
@@ -100,6 +158,44 @@ func (*DeployAction) getDeploymentFor(integration *v1alpha1.Integration) *v1.Dep
 						{
 							Name:  integration.Name,
 							Image: integration.Status.Image,
+							VolumeMounts: []corev1.VolumeMount{
+								{
+									Name:      "integration",
+									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,
+								},
+							},
+						},
+					},
+					Volumes: []corev1.Volume{
+						{
+							Name: "integration",
+							VolumeSource: corev1.VolumeSource{
+								ConfigMap: &corev1.ConfigMapVolumeSource{
+									LocalObjectReference: corev1.LocalObjectReference{
+										Name: integration.Name,
+									},
+									Items: []corev1.KeyToPath{
+										{
+											Key:  "integration",
+											Path: integrationName,
+										},
+									},
+								},
+							},
 						},
 					},
 				},
@@ -109,3 +205,20 @@ func (*DeployAction) getDeploymentFor(integration *v1alpha1.Integration) *v1.Dep
 
 	return &deployment
 }
+
+func createOrUpdateDeployment(integration *v1alpha1.Integration) error {
+	deployment := getDeploymentFor(integration)
+
+	err := sdk.Create(deployment)
+	if err != nil && k8serrors.IsAlreadyExists(err) {
+		err = sdk.Update(deployment)
+	}
+	if err != nil {
+		return errors.Wrap(err, "could not create or replace deployment for integration "+integration.Name)
+	}
+
+	target := integration.DeepCopy()
+	target.Status.Phase = v1alpha1.IntegrationPhaseRunning
+
+	return sdk.Update(target)
+}