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

commit 3387b5eef526a80c79451c94387dd558034adbe5
Author: nferraro <ni.ferr...@gmail.com>
AuthorDate: Fri Jan 11 13:25:18 2019 +0100

    Fix #312: move lookup logic to the images trait
---
 .../camel/v1alpha1/integrationplatform_types.go    | 15 +++--
 pkg/cmd/install.go                                 |  3 -
 pkg/controller/integration/build_context.go        | 15 -----
 pkg/controller/integration/util.go                 | 32 -----------
 pkg/controller/integrationcontext/initialize.go    | 12 +++-
 pkg/platform/images/images.go                      |  4 +-
 pkg/trait/catalog.go                               |  6 ++
 pkg/trait/images.go                                | 65 ++++++++++++++++++++++
 8 files changed, 89 insertions(+), 63 deletions(-)

diff --git a/pkg/apis/camel/v1alpha1/integrationplatform_types.go 
b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
index f967b2a..7db0ae0 100644
--- a/pkg/apis/camel/v1alpha1/integrationplatform_types.go
+++ b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
@@ -71,14 +71,13 @@ var allTraitProfiles = 
[]TraitProfile{TraitProfileOpenShift, TraitProfileKuberne
 
 // IntegrationPlatformBuildSpec contains platform related build information
 type IntegrationPlatformBuildSpec struct {
-       PublishStrategy  IntegrationPlatformBuildPublishStrategy 
`json:"publishStrategy,omitempty"`
-       Registry         string                                  
`json:"registry,omitempty"`
-       Organization     string                                  
`json:"organization,omitempty"`
-       PushSecret       string                                  
`json:"pushSecret,omitempty"`
-       CamelVersion     string                                  
`json:"camelVersion,omitempty"`
-       PredefinedImages bool                                    
`json:"predefinedImages,omitempty"`
-       Properties       map[string]string                       
`json:"properties,omitempty"`
-       Repositories     []string                                
`json:"repositories,omitempty"`
+       PublishStrategy IntegrationPlatformBuildPublishStrategy 
`json:"publishStrategy,omitempty"`
+       Registry        string                                  
`json:"registry,omitempty"`
+       Organization    string                                  
`json:"organization,omitempty"`
+       PushSecret      string                                  
`json:"pushSecret,omitempty"`
+       CamelVersion    string                                  
`json:"camelVersion,omitempty"`
+       Properties      map[string]string                       
`json:"properties,omitempty"`
+       Repositories    []string                                
`json:"repositories,omitempty"`
 }
 
 // IntegrationPlatformBuildPublishStrategy enumerates all implemented build 
strategies
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 6f9cd02..f9bbff2 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -43,7 +43,6 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) 
*cobra.Command {
        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().BoolVar(&impl.predefinedImages, "predefined-images", false, 
"Enable usage of predefined images")
        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")
@@ -70,7 +69,6 @@ type installCmdOptions struct {
        clusterSetupOnly bool
        skipClusterSetup bool
        exampleSetup     bool
-       predefinedImages bool
        registry         string
        outputFormat     string
        organization     string
@@ -142,7 +140,6 @@ func (o *installCmdOptions) install(cmd *cobra.Command, 
args []string) error {
                        platform.Spec.Build.CamelVersion = o.camelVersion
                }
 
-               platform.Spec.Build.PredefinedImages = o.predefinedImages
                platform.Spec.Resources.Contexts = o.contexts
 
                err = install.RuntimeObjectOrCollect(o.Context, c, namespace, 
collection, platform)
diff --git a/pkg/controller/integration/build_context.go 
b/pkg/controller/integration/build_context.go
index eb1ee6a..abfb2fd 100644
--- a/pkg/controller/integration/build_context.go
+++ b/pkg/controller/integration/build_context.go
@@ -22,7 +22,6 @@ import (
        "fmt"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-       "github.com/apache/camel-k/pkg/platform"
        "github.com/apache/camel-k/pkg/trait"
        "github.com/apache/camel-k/pkg/util"
        "github.com/apache/camel-k/pkg/util/digest"
@@ -57,20 +56,6 @@ func (action *buildContextAction) Handle(ctx 
context.Context, integration *v1alp
                return err
        }
 
-       if ictx == nil {
-               // Try to create an external context if possible
-               pl, err := platform.GetCurrentPlatform(ctx, action.client, 
integration.Namespace)
-               if err != nil {
-                       return nil
-               }
-               if pl.Spec.Build.PredefinedImages {
-                       ictx, err = ImportPredefinedContextIfPresent(ctx, 
action.client, integration)
-                       if err != nil {
-                               return err
-                       }
-               }
-       }
-
        if ictx != nil {
                if ictx.Labels["camel.apache.org/context.type"] == 
v1alpha1.IntegrationContextTypePlatform {
                        // This is a platform context and as it is auto 
generated it may get
diff --git a/pkg/controller/integration/util.go 
b/pkg/controller/integration/util.go
index 9eecf11..e9fe974 100644
--- a/pkg/controller/integration/util.go
+++ b/pkg/controller/integration/util.go
@@ -19,14 +19,10 @@ package integration
 
 import (
        "context"
-       "fmt"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-       "github.com/apache/camel-k/pkg/client"
-       "github.com/apache/camel-k/pkg/platform/images"
        "github.com/apache/camel-k/pkg/util"
        "github.com/pkg/errors"
-       "github.com/rs/xid"
        k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
@@ -74,31 +70,3 @@ func LookupContextForIntegration(ctx context.Context, c 
k8sclient.Reader, integr
 
        return nil, nil
 }
-
-// ImportPredefinedContextIfPresent tries to create an external context from a 
predefined image
-func ImportPredefinedContextIfPresent(ctx context.Context, c client.Client, 
integration *v1alpha1.Integration) (*v1alpha1.IntegrationContext, error) {
-       image := images.LookupPredefinedImage(integration.Status.Dependencies)
-       if image == "" {
-               return nil, nil
-       }
-
-       externalCtxName := fmt.Sprintf("ctx-base-%s", xid.New())
-       externalCtx := v1alpha1.NewIntegrationContext(integration.Namespace, 
externalCtxName)
-
-       externalCtx.Labels = map[string]string{
-               "camel.apache.org/context.type":               
v1alpha1.IntegrationContextTypeExternal,
-               "camel.apache.org/context.created.by.kind":    
v1alpha1.IntegrationKind,
-               "camel.apache.org/context.created.by.name":    integration.Name,
-               "camel.apache.org/context.created.by.version": 
integration.ResourceVersion,
-       }
-
-       externalCtx.Spec = v1alpha1.IntegrationContextSpec{
-               Dependencies: integration.Status.Dependencies,
-               Image:        image,
-       }
-
-       if err := c.Create(ctx, &externalCtx); err != nil {
-               return nil, err
-       }
-       return &externalCtx, nil
-}
diff --git a/pkg/controller/integrationcontext/initialize.go 
b/pkg/controller/integrationcontext/initialize.go
index 80b9452..f84ee44 100644
--- a/pkg/controller/integrationcontext/initialize.go
+++ b/pkg/controller/integrationcontext/initialize.go
@@ -22,6 +22,7 @@ import (
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/platform"
+       "github.com/apache/camel-k/pkg/trait"
        "github.com/apache/camel-k/pkg/util/digest"
        "github.com/sirupsen/logrus"
 )
@@ -52,10 +53,15 @@ func (action *initializeAction) Handle(ctx context.Context, 
ictx *v1alpha1.Integ
 
        target := ictx.DeepCopy()
 
-       // by default the context should be build
-       target.Status.Phase = v1alpha1.IntegrationContextPhaseBuilding
+       _, err := trait.Apply(ctx, action.client, nil, target)
+       if err != nil {
+               return err
+       }
 
-       if target.Spec.Image != "" {
+       if target.Spec.Image == "" {
+               // by default the context should be build
+               target.Status.Phase = v1alpha1.IntegrationContextPhaseBuilding
+       } else {
                // but in case it has been created from an image, mark the
                // context as ready
                target.Status.Phase = v1alpha1.IntegrationContextPhaseReady
diff --git a/pkg/platform/images/images.go b/pkg/platform/images/images.go
index da65856..03278ab 100644
--- a/pkg/platform/images/images.go
+++ b/pkg/platform/images/images.go
@@ -19,9 +19,9 @@ package images
 
 import (
        "fmt"
-       "github.com/apache/camel-k/pkg/util/camel"
        "strings"
 
+       "github.com/apache/camel-k/pkg/util/camel"
        "github.com/apache/camel-k/version"
 )
 
@@ -85,7 +85,7 @@ func PredefinedImageNameFor(comp string) string {
 }
 
 func isInCamelCatalog(comp string) bool {
-       if _, ok := camel.Runtime.Artifacts["camel-" + comp]; ok {
+       if _, ok := camel.Runtime.Artifacts["camel-"+comp]; ok {
                return true
        }
        return false
diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go
index 47decb0..0984975 100644
--- a/pkg/trait/catalog.go
+++ b/pkg/trait/catalog.go
@@ -38,6 +38,7 @@ type Catalog struct {
        tRoute        Trait
        tIngress      Trait
        tOwner        Trait
+       tImages       Trait
        tBuilder      Trait
        tSpringBoot   Trait
        tIstio        Trait
@@ -56,6 +57,7 @@ func NewCatalog(ctx context.Context, c client.Client) 
*Catalog {
                tRoute:        newRouteTrait(),
                tIngress:      newIngressTrait(),
                tOwner:        newOwnerTrait(),
+               tImages:       newImagesTrait(),
                tBuilder:      newBuilderTrait(),
                tSpringBoot:   newSpringBootTrait(),
                tIstio:        newIstioTrait(),
@@ -84,6 +86,7 @@ func (c *Catalog) allTraits() []Trait {
                c.tRoute,
                c.tIngress,
                c.tOwner,
+               c.tImages,
                c.tBuilder,
                c.tSpringBoot,
                c.tIstio,
@@ -98,6 +101,7 @@ func (c *Catalog) traitsFor(environment *Environment) 
[]Trait {
                return []Trait{
                        c.tDebug,
                        c.tDependencies,
+                       c.tImages,
                        c.tBuilder,
                        c.tEnvironment,
                        c.tClasspath,
@@ -111,6 +115,7 @@ func (c *Catalog) traitsFor(environment *Environment) 
[]Trait {
                return []Trait{
                        c.tDebug,
                        c.tDependencies,
+                       c.tImages,
                        c.tBuilder,
                        c.tEnvironment,
                        c.tClasspath,
@@ -124,6 +129,7 @@ func (c *Catalog) traitsFor(environment *Environment) 
[]Trait {
                return []Trait{
                        c.tDebug,
                        c.tDependencies,
+                       c.tImages,
                        c.tBuilder,
                        c.tEnvironment,
                        c.tClasspath,
diff --git a/pkg/trait/images.go b/pkg/trait/images.go
new file mode 100644
index 0000000..3ed4c0b
--- /dev/null
+++ b/pkg/trait/images.go
@@ -0,0 +1,65 @@
+/*
+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 trait
+
+import (
+       "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+       "github.com/apache/camel-k/pkg/platform/images"
+)
+
+type imagesTrait struct {
+       BaseTrait `property:",squash"`
+}
+
+func newImagesTrait() *imagesTrait {
+       return &imagesTrait{
+               BaseTrait: BaseTrait{
+                       id: ID("images"),
+               },
+       }
+}
+
+func (t *imagesTrait) Configure(e *Environment) (bool, error) {
+       if t.Enabled == nil || !*t.Enabled {
+               // Disabled by default
+               return false, nil
+       }
+
+       if e.IntegrationContextInPhase("") {
+               return true, nil
+       }
+
+       return false, nil
+}
+
+func (t *imagesTrait) Apply(e *Environment) error {
+       // Try to lookup a image from predefined images
+       image := images.LookupPredefinedImage(e.Context.Spec.Dependencies)
+       if image == "" {
+               return nil
+       }
+
+       // Change the context type to external
+       if e.Context.Labels == nil {
+               e.Context.Labels = make(map[string]string)
+       }
+       e.Context.Labels["camel.apache.org/context.type"] = 
v1alpha1.IntegrationContextTypeExternal
+
+       e.Context.Spec.Image = image
+       return nil
+}

Reply via email to