This is an automated email from the ASF dual-hosted git repository.

lburgazzoli 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 ebda259  feature(kamel): Validate --context when given
ebda259 is described below

commit ebda25900cc7e11bd679a81a46b289a9a464981f
Author: Roland Huß <rol...@ro14nd.de>
AuthorDate: Tue Feb 12 19:29:39 2019 +0100

    feature(kamel): Validate --context when given
---
 deploy/resources.go | 46 +++++++++++++++++++++----------------------
 pkg/cmd/install.go  | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/deploy/resources.go b/deploy/resources.go
index aea1c50..dd46d97 100644
--- a/deploy/resources.go
+++ b/deploy/resources.go
@@ -4178,6 +4178,29 @@ spec:
   version: 2.23.1
 
 `
+       Resources["cr-example.yaml"] =
+               `
+apiVersion: camel.apache.org/v1alpha1
+kind: Integration
+metadata:
+  name: example
+spec:
+  source:
+    content: |-
+      // This is Camel K Groovy example route
+
+      rnd = new Random()
+
+      from('timer:groovy?period=1s')
+          .routeId('groovy')
+          .setBody()
+              .constant('Hello Camel K!')
+          .process {
+              it.in.headers['RandomValue'] = rnd.nextInt()
+          }
+          .to('log:info?showHeaders=true')
+    name: routes.groovy
+`
        Resources["crd-camel-catalog.yaml"] =
                `
 apiVersion: apiextensions.k8s.io/v1beta1
@@ -4297,29 +4320,6 @@ spec:
       JSONPath: .status.context
 
 `
-       Resources["cr-example.yaml"] =
-               `
-apiVersion: camel.apache.org/v1alpha1
-kind: Integration
-metadata:
-  name: example
-spec:
-  source:
-    content: |-
-      // This is Camel K Groovy example route
-
-      rnd = new Random()
-
-      from('timer:groovy?period=1s')
-          .routeId('groovy')
-          .setBody()
-              .constant('Hello Camel K!')
-          .process {
-              it.in.headers['RandomValue'] = rnd.nextInt()
-          }
-          .to('log:info?showHeaders=true')
-    name: routes.groovy
-`
        Resources["operator-deployment-kubernetes.yaml"] =
                `
 apiVersion: apps/v1
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index fa7d428..6ea6bad 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -19,6 +19,11 @@ package cmd
 
 import (
        "fmt"
+       "github.com/apache/camel-k/deploy"
+       "github.com/apache/camel-k/pkg/apis"
+       "github.com/apache/camel-k/pkg/platform"
+       "go.uber.org/multierr"
+       "k8s.io/apimachinery/pkg/runtime"
        "strings"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -40,6 +45,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) 
*cobra.Command {
                Use:   "install",
                Short: "Install Camel K on a Kubernetes cluster",
                Long:  `Installs Camel K on a Kubernetes or OpenShift cluster.`,
+               PreRunE: impl.validate,
                RunE:  impl.install,
        }
 
@@ -230,3 +236,53 @@ func (o *installCmdOptions) waitForPlatformReady(platform 
*v1alpha1.IntegrationP
 
        return watch.HandlePlatformStateChanges(o.Context, platform, handler)
 }
+
+func (o *installCmdOptions) validate(cmd *cobra.Command, args []string) error {
+       var result error
+
+       // Let's register only our own APIs
+       schema := runtime.NewScheme()
+       if err := apis.AddToScheme(schema); err != nil {
+               return err
+       }
+
+       if o.contexts == nil {
+               return nil
+       }
+       for _, context := range o.contexts {
+               err := errorIfContextIsNotAvailable(schema, context, 
len(o.contexts))
+               result = multierr.Append(result, err)
+       }
+       return result
+}
+
+func errorIfContextIsNotAvailable(schema *runtime.Scheme, context string, 
nrContexts int) error {
+
+       if context == platform.NoContext {
+               if nrContexts > 1 {
+                       return errors.New("You can only use one --context 
argument when selecting 'none'")
+               }
+
+               // Indicates that nothing should be installed
+               return nil
+       }
+
+       for _, resource := range deploy.Resources {
+               resource, err := kubernetes.LoadResourceFromYaml(schema, 
resource)
+               if err != nil {
+                       // Not one of our registered schemas
+                       continue
+               }
+               kind := resource.GetObjectKind().GroupVersionKind()
+               if kind.Kind != "IntegrationContext" {
+                       continue
+               }
+               integrationContext := resource.(*v1alpha1.IntegrationContext)
+               if integrationContext.Name == context {
+                       return nil
+               }
+       }
+       return errors.Errorf("Unknown context '%s'", context)
+}
+
+

Reply via email to