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 2019/01/08 16:57:04 UTC

[camel-k] branch master updated: Allow to customize contexts create at installation #318

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 2cb5754  Allow to customize contexts create at installation #318
2cb5754 is described below

commit 2cb57540897aab4754c62df810a1c6960f841fcd
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Jan 8 14:18:52 2019 +0100

    Allow to customize contexts create at installation #318
---
 Gopkg.lock                                         |  9 ++--
 .../camel/v1alpha1/integrationplatform_types.go    | 12 +++--
 pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go   | 22 +++++++++
 pkg/cmd/completion_bash.go                         |  8 ++++
 pkg/cmd/install.go                                 | 37 ++++++++++-----
 pkg/controller/integrationplatform/create.go       | 53 ++++++++++++++--------
 pkg/platform/resources.go                          | 53 ++++++++++++++++++++++
 7 files changed, 157 insertions(+), 37 deletions(-)

diff --git a/Gopkg.lock b/Gopkg.lock
index f9ea74a..4ce6fa5 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -1006,7 +1006,6 @@
     "pkg/client/apiutil",
     "pkg/client/config",
     "pkg/controller",
-    "pkg/controller/controllerutil",
     "pkg/event",
     "pkg/handler",
     "pkg/internal/controller",
@@ -1067,14 +1066,19 @@
     "k8s.io/apimachinery/pkg/api/errors",
     "k8s.io/apimachinery/pkg/apis/meta/v1",
     "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
+    "k8s.io/apimachinery/pkg/labels",
     "k8s.io/apimachinery/pkg/runtime",
     "k8s.io/apimachinery/pkg/runtime/schema",
+    "k8s.io/apimachinery/pkg/runtime/serializer",
     "k8s.io/apimachinery/pkg/runtime/serializer/json",
     "k8s.io/apimachinery/pkg/runtime/serializer/versioning",
-    "k8s.io/apimachinery/pkg/types",
     "k8s.io/apimachinery/pkg/util/intstr",
+    "k8s.io/apimachinery/pkg/util/json",
     "k8s.io/apimachinery/pkg/util/yaml",
     "k8s.io/apimachinery/pkg/watch",
+    "k8s.io/client-go/dynamic",
+    "k8s.io/client-go/kubernetes",
+    "k8s.io/client-go/kubernetes/scheme",
     "k8s.io/client-go/plugin/pkg/client/auth/gcp",
     "k8s.io/client-go/rest",
     "k8s.io/client-go/tools/clientcmd",
@@ -1091,7 +1095,6 @@
     "sigs.k8s.io/controller-runtime/pkg/client",
     "sigs.k8s.io/controller-runtime/pkg/client/config",
     "sigs.k8s.io/controller-runtime/pkg/controller",
-    "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil",
     "sigs.k8s.io/controller-runtime/pkg/handler",
     "sigs.k8s.io/controller-runtime/pkg/manager",
     "sigs.k8s.io/controller-runtime/pkg/reconcile",
diff --git a/pkg/apis/camel/v1alpha1/integrationplatform_types.go b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
index d13ea63..7db0ae0 100644
--- a/pkg/apis/camel/v1alpha1/integrationplatform_types.go
+++ b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
@@ -8,9 +8,15 @@ import (
 
 // IntegrationPlatformSpec defines the desired state of IntegrationPlatform
 type IntegrationPlatformSpec struct {
-	Cluster IntegrationPlatformCluster   `json:"cluster,omitempty"`
-	Profile TraitProfile                 `json:"profile,omitempty"`
-	Build   IntegrationPlatformBuildSpec `json:"build,omitempty"`
+	Cluster   IntegrationPlatformCluster       `json:"cluster,omitempty"`
+	Profile   TraitProfile                     `json:"profile,omitempty"`
+	Build     IntegrationPlatformBuildSpec     `json:"build,omitempty"`
+	Resources IntegrationPlatformResourcesSpec `json:"resources,omitempty"`
+}
+
+// IntegrationPlatformResourcesSpec contains platform related resources
+type IntegrationPlatformResourcesSpec struct {
+	Contexts []string `json:"contexts,omitempty"`
 }
 
 // IntegrationPlatformStatus defines the observed state of IntegrationPlatform
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index f26f108..38ebafe 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -386,9 +386,31 @@ func (in *IntegrationPlatformList) DeepCopyObject() runtime.Object {
 }
 
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *IntegrationPlatformResourcesSpec) DeepCopyInto(out *IntegrationPlatformResourcesSpec) {
+	*out = *in
+	if in.Contexts != nil {
+		in, out := &in.Contexts, &out.Contexts
+		*out = make([]string, len(*in))
+		copy(*out, *in)
+	}
+	return
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IntegrationPlatformResourcesSpec.
+func (in *IntegrationPlatformResourcesSpec) DeepCopy() *IntegrationPlatformResourcesSpec {
+	if in == nil {
+		return nil
+	}
+	out := new(IntegrationPlatformResourcesSpec)
+	in.DeepCopyInto(out)
+	return out
+}
+
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *IntegrationPlatformSpec) DeepCopyInto(out *IntegrationPlatformSpec) {
 	*out = *in
 	in.Build.DeepCopyInto(&out.Build)
+	in.Resources.DeepCopyInto(&out.Resources)
 	return
 }
 
diff --git a/pkg/cmd/completion_bash.go b/pkg/cmd/completion_bash.go
index 836b391..115f18e 100644
--- a/pkg/cmd/completion_bash.go
+++ b/pkg/cmd/completion_bash.go
@@ -23,6 +23,8 @@ import (
 	"os"
 	"strings"
 
+	"github.com/apache/camel-k/pkg/platform"
+
 	"github.com/apache/camel-k/pkg/trait"
 
 	"github.com/apache/camel-k/pkg/util/camel"
@@ -141,6 +143,12 @@ __kamel_kubectl_get_user_integrationcontexts() {
     fi
 }
 
+__kamel_kubectl_get_known_integrationcontexts() {
+    local type_list="` + strings.Join(platform.GetContextsNames(), " ") + `"
+    COMPREPLY=( $( compgen -W "${type_list}" -- "$cur") )
+    compopt -o nospace
+}
+
 __custom_func() {
     case ${last_command} in
         kamel_delete)
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 342c4a9..f9bbff2 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -30,26 +30,36 @@ import (
 )
 
 func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
-	options := installCmdOptions{
+	impl := installCmdOptions{
 		RootCmdOptions: rootCmdOptions,
 	}
 	cmd := cobra.Command{
 		Use:   "install",
 		Short: "Install Camel K on a Kubernetes cluster",
 		Long:  `Installs Camel K on a Kubernetes or OpenShift cluster.`,
-		RunE:  options.install,
+		RunE:  impl.install,
 	}
 
-	cmd.Flags().BoolVar(&options.clusterSetupOnly, "cluster-setup", false, "Execute cluster-wide operations only (may require admin rights)")
-	cmd.Flags().BoolVar(&options.skipClusterSetup, "skip-cluster-setup", false, "Skip the cluster-setup phase")
-	cmd.Flags().BoolVar(&options.exampleSetup, "example", false, "Install example integration")
-	cmd.Flags().StringVar(&options.registry, "registry", "", "A Docker registry that can be used to publish images")
-	cmd.Flags().StringVarP(&options.outputFormat, "output", "o", "", "Output format. One of: json|yaml")
-	cmd.Flags().StringVar(&options.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
-	cmd.Flags().StringVar(&options.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
-	cmd.Flags().StringSliceVar(&options.repositories, "repository", nil, "Add a maven repository")
-	cmd.Flags().StringSliceVarP(&options.properties, "property", "p", nil, "Add a camel property")
-	cmd.Flags().StringVar(&options.camelVersion, "camel-version", "", "Set the camel version")
+	cmd.Flags().BoolVar(&impl.clusterSetupOnly, "cluster-setup", false, "Execute cluster-wide operations only (may require admin rights)")
+	cmd.Flags().BoolVar(&impl.skipClusterSetup, "skip-cluster-setup", false, "Skip the cluster-setup phase")
+	cmd.Flags().BoolVar(&impl.exampleSetup, "example", false, "Install example integration")
+	cmd.Flags().StringVar(&impl.registry, "registry", "", "A Docker registry that can be used to publish images")
+	cmd.Flags().StringVarP(&impl.outputFormat, "output", "o", "", "Output format. One of: json|yaml")
+	cmd.Flags().StringVar(&impl.organization, "organization", "", "A organization on the Docker registry that can be used to publish images")
+	cmd.Flags().StringVar(&impl.pushSecret, "push-secret", "", "A secret used to push images to the Docker registry")
+	cmd.Flags().StringSliceVar(&impl.repositories, "repository", nil, "Add a maven repository")
+	cmd.Flags().StringSliceVarP(&impl.properties, "property", "p", nil, "Add a camel property")
+	cmd.Flags().StringVar(&impl.camelVersion, "camel-version", "", "Set the camel version")
+	cmd.Flags().StringSliceVar(&impl.contexts, "context", nil, "Add a camel context to build at startup, by default all known contexts are built")
+
+	// completion support
+	configureBashAnnotationForFlag(
+		&cmd,
+		"context",
+		map[string][]string{
+			cobra.BashCompCustom: {"__kamel_kubectl_get_known_integrationcontexts"},
+		},
+	)
 
 	return &cmd
 }
@@ -66,6 +76,7 @@ type installCmdOptions struct {
 	camelVersion     string
 	repositories     []string
 	properties       []string
+	contexts         []string
 }
 
 func (o *installCmdOptions) install(cmd *cobra.Command, args []string) error {
@@ -129,6 +140,8 @@ func (o *installCmdOptions) install(cmd *cobra.Command, args []string) error {
 			platform.Spec.Build.CamelVersion = o.camelVersion
 		}
 
+		platform.Spec.Resources.Contexts = o.contexts
+
 		err = install.RuntimeObjectOrCollect(o.Context, c, namespace, collection, platform)
 		if err != nil {
 			return err
diff --git a/pkg/controller/integrationplatform/create.go b/pkg/controller/integrationplatform/create.go
index 82cfcd3..fa203c2 100644
--- a/pkg/controller/integrationplatform/create.go
+++ b/pkg/controller/integrationplatform/create.go
@@ -19,23 +19,16 @@ package integrationplatform
 
 import (
 	"context"
+	"fmt"
+	"strings"
 
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/install"
+	p "github.com/apache/camel-k/pkg/platform"
+
 	"github.com/sirupsen/logrus"
 )
 
-var resources = []string{
-	"platform-integration-context-jvm.yaml",
-	"platform-integration-context-groovy.yaml",
-	"platform-integration-context-kotlin.yaml",
-	"platform-integration-context-spring-boot.yaml",
-}
-
-var knativeResources = []string{
-	"platform-integration-context-knative.yaml",
-}
-
 // NewCreateAction returns a action that creates resources needed by the platform
 func NewCreateAction() Action {
 	return &createAction{}
@@ -54,18 +47,40 @@ func (action *createAction) CanHandle(platform *v1alpha1.IntegrationPlatform) bo
 }
 
 func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.IntegrationPlatform) error {
-	logrus.Info("Installing platform resources")
-	err := install.Resources(ctx, action.client, platform.Namespace, resources...)
-	if err != nil {
-		return err
-	}
+	if l := len(platform.Spec.Resources.Contexts); l > 0 {
+		res := make([]string, 0, l)
+
+		for _, c := range platform.Spec.Resources.Contexts {
+			//
+			// Assuming that if the resource ends with a yaml extension, the full
+			// resource name is provided
+			//
+			if !strings.HasSuffix(c, ".yaml") && !strings.HasSuffix(c, ".yml") {
+				c = fmt.Sprintf("platform-integration-context-%s.yaml", c)
+			}
 
-	if platform.Spec.Profile == v1alpha1.TraitProfileKnative {
-		logrus.Info("Installing knative resources")
-		err := install.Resources(ctx, action.client, platform.Namespace, knativeResources...)
+			res = append(res, c)
+		}
+
+		logrus.Info("Installing custom platform resources")
+		err := install.Resources(ctx, action.client, platform.Namespace, res...)
 		if err != nil {
 			return err
 		}
+	} else {
+		logrus.Info("Installing default platform resources")
+		err := install.Resources(ctx, action.client, platform.Namespace, p.DefaultContexts...)
+		if err != nil {
+			return err
+		}
+
+		if platform.Spec.Profile == v1alpha1.TraitProfileKnative {
+			logrus.Info("Installing knative resources")
+			err := install.Resources(ctx, action.client, platform.Namespace, p.KnativeContexts...)
+			if err != nil {
+				return err
+			}
+		}
 	}
 
 	target := platform.DeepCopy()
diff --git a/pkg/platform/resources.go b/pkg/platform/resources.go
new file mode 100644
index 0000000..292776f
--- /dev/null
+++ b/pkg/platform/resources.go
@@ -0,0 +1,53 @@
+/*
+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 platform
+
+import "strings"
+
+// DefaultContexts --
+var DefaultContexts = []string{
+	"platform-integration-context-jvm.yaml",
+	"platform-integration-context-groovy.yaml",
+	"platform-integration-context-kotlin.yaml",
+	"platform-integration-context-spring-boot.yaml",
+}
+
+// KnativeContexts --
+var KnativeContexts = []string{
+	"platform-integration-context-knative.yaml",
+}
+
+// GetContexts --
+func GetContexts() []string {
+	return append(DefaultContexts, KnativeContexts...)
+}
+
+// GetContextsNames --
+func GetContextsNames() []string {
+	ctxs := GetContexts()
+	names := make([]string, 0, len(ctxs))
+
+	for _, r := range ctxs {
+		r = strings.TrimPrefix(r, "platform-integration-context-")
+		r = strings.TrimSuffix(r, ".yaml")
+
+		names = append(names, r)
+	}
+
+	return names
+}