Skip to content

Commit

Permalink
Fix #312: move lookup logic to the images trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and lburgazzoli committed Jan 13, 2019
1 parent a114beb commit 3387b5e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 63 deletions.
15 changes: 7 additions & 8 deletions pkg/apis/camel/v1alpha1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -70,7 +69,6 @@ type installCmdOptions struct {
clusterSetupOnly bool
skipClusterSetup bool
exampleSetup bool
predefinedImages bool
registry string
outputFormat string
organization string
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions pkg/controller/integration/build_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions pkg/controller/integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions pkg/controller/integrationcontext/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Catalog struct {
tRoute Trait
tIngress Trait
tOwner Trait
tImages Trait
tBuilder Trait
tSpringBoot Trait
tIstio Trait
Expand All @@ -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(),
Expand Down Expand Up @@ -84,6 +86,7 @@ func (c *Catalog) allTraits() []Trait {
c.tRoute,
c.tIngress,
c.tOwner,
c.tImages,
c.tBuilder,
c.tSpringBoot,
c.tIstio,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions pkg/trait/images.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3387b5e

Please sign in to comment.