Skip to content

Commit

Permalink
Fix apache#324: use standard deployment when knative-services are not…
Browse files Browse the repository at this point in the history
… needed
  • Loading branch information
nicolaferraro committed Jan 17, 2019
1 parent 6ea21ff commit 715a1b9
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 248 deletions.
1 change: 1 addition & 0 deletions pkg/controller/integrationplatform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package integrationplatform

import (
"context"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
platformutils "github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/openshift"
Expand Down
24 changes: 24 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"sort"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/gzip"
src "github.com/apache/camel-k/pkg/util/source"
"github.com/sirupsen/logrus"
)

// ExtractAll returns metadata information from all listed source codes
Expand Down Expand Up @@ -68,6 +70,12 @@ func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {

// Extract returns metadata information from the source code
func Extract(source v1alpha1.SourceSpec) IntegrationMetadata {
var err error
source, err = uncompress(source)
if err != nil {
logrus.Errorf("unable to uncompress source %s: %v", source.Name, err)
}

language := source.InferLanguage()

m := IntegrationMetadata{}
Expand All @@ -91,3 +99,19 @@ func Each(sources []v1alpha1.SourceSpec, consumer func(int, IntegrationMetadata)
}
}
}

func uncompress(spec v1alpha1.SourceSpec) (v1alpha1.SourceSpec, error) {
if spec.Compression {
data := []byte(spec.Content)
var uncompressed []byte
var err error
if uncompressed, err = gzip.UncompressBase64(data); err != nil {
return spec, err
}
newSpec := spec
newSpec.Compression = false
newSpec.Content = string(uncompressed)
return newSpec, nil
}
return spec, nil
}
68 changes: 36 additions & 32 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,43 @@ import (

// Catalog collects all information about traits in one place
type Catalog struct {
tDebug Trait
tDependencies Trait
tDeployment Trait
tKnative Trait
tService Trait
tRoute Trait
tIngress Trait
tOwner Trait
tImages Trait
tBuilder Trait
tSpringBoot Trait
tIstio Trait
tEnvironment Trait
tClasspath Trait
tRest Trait
tDebug Trait
tDependencies Trait
tDeployment Trait
tKnativeService Trait
tKnative Trait
tService Trait
tRoute Trait
tIngress Trait
tOwner Trait
tImages Trait
tBuilder Trait
tSpringBoot Trait
tIstio Trait
tEnvironment Trait
tClasspath Trait
tRest Trait
}

// NewCatalog creates a new trait Catalog
func NewCatalog(ctx context.Context, c client.Client) *Catalog {
catalog := Catalog{
tDebug: newDebugTrait(),
tRest: newRestTrait(),
tDependencies: newDependenciesTrait(),
tDeployment: newDeploymentTrait(),
tKnative: newKnativeTrait(),
tService: newServiceTrait(),
tRoute: newRouteTrait(),
tIngress: newIngressTrait(),
tOwner: newOwnerTrait(),
tImages: newImagesTrait(),
tBuilder: newBuilderTrait(),
tSpringBoot: newSpringBootTrait(),
tIstio: newIstioTrait(),
tEnvironment: newEnvironmentTrait(),
tClasspath: newClasspathTrait(),
tDebug: newDebugTrait(),
tRest: newRestTrait(),
tKnative: newKnativeTrait(),
tDependencies: newDependenciesTrait(),
tDeployment: newDeploymentTrait(),
tKnativeService: newKnativeServiceTrait(),
tService: newServiceTrait(),
tRoute: newRouteTrait(),
tIngress: newIngressTrait(),
tOwner: newOwnerTrait(),
tImages: newImagesTrait(),
tBuilder: newBuilderTrait(),
tSpringBoot: newSpringBootTrait(),
tIstio: newIstioTrait(),
tEnvironment: newEnvironmentTrait(),
tClasspath: newClasspathTrait(),
}

for _, t := range catalog.allTraits() {
Expand All @@ -82,9 +84,10 @@ func (c *Catalog) allTraits() []Trait {
return []Trait{
c.tDebug,
c.tRest,
c.tKnative,
c.tDependencies,
c.tDeployment,
c.tKnative,
c.tKnativeService,
c.tService,
c.tRoute,
c.tIngress,
Expand Down Expand Up @@ -134,14 +137,15 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
return []Trait{
c.tDebug,
c.tRest,
c.tKnative,
c.tDependencies,
c.tImages,
c.tBuilder,
c.tEnvironment,
c.tClasspath,
c.tSpringBoot,
c.tKnative,
c.tDeployment,
c.tKnativeService,
c.tIstio,
c.tOwner,
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/trait/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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/client"
"github.com/apache/camel-k/pkg/metadata"
"golang.org/x/net/context"
)

// ControllerStrategy determines the kind of controller that needs to be created for the integration
type ControllerStrategy string

// List of controller strategies
const (
ControllerStrategyDeployment = "deployment"
ControllerStrategyKnativeService = "knative-service"
)

// DetermineControllerStrategy determines the type of controller that should be used for the integration
func DetermineControllerStrategy(ctx context.Context, c client.Client, e *Environment) (ControllerStrategy, error) {
if e.DetermineProfile() != v1alpha1.TraitProfileKnative {
return ControllerStrategyDeployment, nil
}

sources := e.Integration.Sources()
var err error
if sources, err = GetEnrichedSources(ctx, c, e, sources); err != nil {
return "", err
}

// In Knative profile: use knative service only if needed
meta := metadata.ExtractAll(sources)
if !meta.RequiresHTTPService {
return ControllerStrategyDeployment, nil
}

return ControllerStrategyKnativeService, nil
}
9 changes: 7 additions & 2 deletions pkg/trait/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func (t *deploymentTrait) Configure(e *Environment) (bool, error) {

if e.IntegrationInPhase(v1alpha1.IntegrationPhaseDeploying) {
//
// Don't deploy on knative
// Don't deploy when a different strategy is needed (e.g. Knative)
//
return e.DetermineProfile() != v1alpha1.TraitProfileKnative, nil
var strategy ControllerStrategy
var err error
if strategy, err = DetermineControllerStrategy(t.ctx, t.client, e); err != nil {
return false, err
}
return strategy == ControllerStrategyDeployment, nil
}

if t.ContainerImage && e.InPhase(v1alpha1.IntegrationContextPhaseReady, v1alpha1.IntegrationPhaseBuildingContext) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/trait/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type istioTrait struct {
}

const (
istioIncludeAnnotation = "traffic.sidecar.istio.io/includeOutboundIPRanges"
istioSidecarInjectAnnotation = "sidecar.istio.io/inject"
istioOutboundIPRangesAnnotation = "traffic.sidecar.istio.io/includeOutboundIPRanges"
)

func newIstioTrait() *istioTrait {
Expand All @@ -52,19 +53,22 @@ func (t *istioTrait) Configure(e *Environment) (bool, error) {
func (t *istioTrait) Apply(e *Environment) error {
if t.Allow != "" {
e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
d.Spec.Template.Annotations = t.injectIstioAnnotation(d.Spec.Template.Annotations)
d.Spec.Template.Annotations = t.injectIstioAnnotation(d.Spec.Template.Annotations, true)
})
e.Resources.VisitKnativeConfigurationSpec(func(cs *serving.ConfigurationSpec) {
cs.RevisionTemplate.Annotations = t.injectIstioAnnotation(cs.RevisionTemplate.Annotations)
cs.RevisionTemplate.Annotations = t.injectIstioAnnotation(cs.RevisionTemplate.Annotations, false)
})
}
return nil
}

func (t *istioTrait) injectIstioAnnotation(annotations map[string]string) map[string]string {
func (t *istioTrait) injectIstioAnnotation(annotations map[string]string, includeInject bool) map[string]string {
if annotations == nil {
annotations = make(map[string]string)
}
annotations[istioIncludeAnnotation] = t.Allow
annotations[istioOutboundIPRangesAnnotation] = t.Allow
if includeInject {
annotations[istioSidecarInjectAnnotation] = "true"
}
return annotations
}
Loading

0 comments on commit 715a1b9

Please sign in to comment.