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 2018/11/22 10:48:55 UTC
[camel-k] 02/04: traits: conditional apply
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
commit 5c5f527f46cd42113ab3108f7fe20290b8896a41
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Nov 20 23:40:46 2018 +0100
traits: conditional apply
---
pkg/builder/builder_types.go | 5 +++++
pkg/stub/action/context/initialize.go | 2 +-
pkg/trait/builder.go | 8 ++++----
pkg/trait/catalog.go | 4 ++++
pkg/trait/dependencies.go | 28 ++++++++--------------------
pkg/trait/deployment.go | 16 ++++++++--------
pkg/trait/ingress.go | 8 ++++----
pkg/trait/knative.go | 8 ++++----
pkg/trait/owner.go | 8 ++++----
pkg/trait/route.go | 4 ++++
pkg/trait/service.go | 8 ++++----
pkg/trait/trait.go | 2 +-
pkg/trait/types.go | 16 ++++++++++++++--
13 files changed, 65 insertions(+), 52 deletions(-)
diff --git a/pkg/builder/builder_types.go b/pkg/builder/builder_types.go
index e2c8a24..0cf38ed 100644
--- a/pkg/builder/builder_types.go
+++ b/pkg/builder/builder_types.go
@@ -19,6 +19,7 @@ package builder
import (
"context"
+ "fmt"
"time"
"github.com/apache/camel-k/pkg/util/maven"
@@ -56,6 +57,10 @@ type stepWrapper struct {
task func(*Context) error
}
+func (s *stepWrapper) String() string {
+ return fmt.Sprintf("%s@%d", s.id, s.phase)
+}
+
func (s *stepWrapper) ID() string {
return s.id
}
diff --git a/pkg/stub/action/context/initialize.go b/pkg/stub/action/context/initialize.go
index 3161dd9..f9e8d04 100644
--- a/pkg/stub/action/context/initialize.go
+++ b/pkg/stub/action/context/initialize.go
@@ -51,7 +51,7 @@ func (action *initializeAction) Handle(context *v1alpha1.IntegrationContext) err
target := context.DeepCopy()
// execute custom initialization
- //if err := trait.Apply(nil, context); err != nil {
+ //if err := trait.apply(nil, context); err != nil {
// return err
//}
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 0f9b71a..c8c4a50 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -35,11 +35,11 @@ func newBuilderTrait() *builderTrait {
}
}
-func (*builderTrait) apply(e *Environment) error {
- if e.Context == nil || e.Context.Status.Phase != v1alpha1.IntegrationContextPhaseBuilding {
- return nil
- }
+func (*builderTrait) appliesTo(e *Environment) bool {
+ return e.Context != nil && e.Context.Status.Phase == v1alpha1.IntegrationContextPhaseBuilding
+}
+func (*builderTrait) apply(e *Environment) error {
if platform.SupportsS2iPublishStrategy(e.Platform) {
e.Steps = s2i.DefaultSteps
} else if platform.SupportsKanikoPublishStrategy(e.Platform) {
diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go
index 1f95c82..9db06ab 100644
--- a/pkg/trait/catalog.go
+++ b/pkg/trait/catalog.go
@@ -109,6 +109,10 @@ func (c *Catalog) apply(environment *Environment) error {
c.configure(environment)
traits := c.traitsFor(environment)
for _, trait := range traits {
+ if !trait.appliesTo(environment) {
+ continue
+ }
+
if trait.IsAuto() {
if err := trait.autoconfigure(environment); err != nil {
return err
diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go
index 9dceefa..f284678 100644
--- a/pkg/trait/dependencies.go
+++ b/pkg/trait/dependencies.go
@@ -35,11 +35,11 @@ func newDependenciesTrait() *dependenciesTrait {
}
}
-func (d *dependenciesTrait) apply(e *Environment) error {
- if e.Integration == nil || e.Integration.Status.Phase != "" {
- return nil
- }
+func (*dependenciesTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == ""
+}
+func (d *dependenciesTrait) apply(e *Environment) error {
meta := metadata.Extract(e.Integration.Spec.Source)
if meta.Language == v1alpha1.LanguageGroovy {
@@ -52,23 +52,11 @@ func (d *dependenciesTrait) apply(e *Environment) error {
util.StringSliceUniqueAdd(&e.Integration.Spec.Dependencies, "runtime:jvm")
util.StringSliceUniqueAdd(&e.Integration.Spec.Dependencies, "camel:core")
- e.Integration.Spec.Dependencies = d.mergeDependencies(e.Integration.Spec.Dependencies, meta.Dependencies)
+ for _, d := range meta.Dependencies {
+ util.StringSliceUniqueAdd(&e.Integration.Spec.Dependencies, d)
+ }
+
// sort the dependencies to get always the same list if they don't change
sort.Strings(e.Integration.Spec.Dependencies)
return nil
}
-
-func (d *dependenciesTrait) mergeDependencies(list1 []string, list2 []string) []string {
- set := make(map[string]bool, 0)
- for _, d := range list1 {
- set[d] = true
- }
- for _, d := range list2 {
- set[d] = true
- }
- ret := make([]string, 0, len(set))
- for d := range set {
- ret = append(ret, d)
- }
- return ret
-}
diff --git a/pkg/trait/deployment.go b/pkg/trait/deployment.go
index 9669492..8023050 100644
--- a/pkg/trait/deployment.go
+++ b/pkg/trait/deployment.go
@@ -37,13 +37,13 @@ func newDeploymentTrait() *deploymentTrait {
}
}
-func (d *deploymentTrait) apply(e *Environment) error {
- if e.Integration == nil || e.Integration.Status.Phase != v1alpha1.IntegrationPhaseDeploying {
- return nil
- }
+func (d *deploymentTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
- e.Resources.Add(d.getConfigMapFor(e))
- e.Resources.Add(d.getDeploymentFor(e))
+func (d *deploymentTrait) apply(e *Environment) error {
+ e.Resources.Add(getConfigMapFor(e))
+ e.Resources.Add(getDeploymentFor(e))
return nil
}
@@ -53,7 +53,7 @@ func (d *deploymentTrait) apply(e *Environment) error {
//
// **********************************
-func (*deploymentTrait) getConfigMapFor(e *Environment) *corev1.ConfigMap {
+func getConfigMapFor(e *Environment) *corev1.ConfigMap {
// combine properties of integration with context, integration
// properties have the priority
properties := CombineConfigurationAsMap("property", e.Context, e.Integration)
@@ -89,7 +89,7 @@ func (*deploymentTrait) getConfigMapFor(e *Environment) *corev1.ConfigMap {
//
// **********************************
-func (*deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment {
+func getDeploymentFor(e *Environment) *appsv1.Deployment {
sourceName := strings.TrimPrefix(e.Integration.Spec.Source.Name, "/")
// combine Environment of integration with context, integration
diff --git a/pkg/trait/ingress.go b/pkg/trait/ingress.go
index b468d12..b40d74f 100644
--- a/pkg/trait/ingress.go
+++ b/pkg/trait/ingress.go
@@ -39,6 +39,10 @@ func newIngressTrait() *ingressTrait {
}
}
+func (*ingressTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
+
func (i *ingressTrait) autoconfigure(e *Environment) error {
if i.Enabled == nil {
hasService := i.getTargetService(e) != nil
@@ -50,10 +54,6 @@ func (i *ingressTrait) autoconfigure(e *Environment) error {
}
func (i *ingressTrait) apply(e *Environment) error {
- if e.Integration == nil || e.Integration.Status.Phase != v1alpha1.IntegrationPhaseDeploying {
- return nil
- }
-
if i.Host == "" {
return errors.New("cannot apply ingress trait: no host defined")
}
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 5bd3c95..b122f8d 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -43,6 +43,10 @@ func newKnativeTrait() *knativeTrait {
}
}
+func (t *knativeTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
+
func (t *knativeTrait) autoconfigure(e *Environment) error {
if t.Sources == "" {
channels := t.getSourceChannels(e)
@@ -52,10 +56,6 @@ func (t *knativeTrait) autoconfigure(e *Environment) error {
}
func (t *knativeTrait) apply(e *Environment) error {
- if e.Integration == nil || e.Integration.Status.Phase != v1alpha1.IntegrationPhaseDeploying {
- return nil
- }
-
for _, sub := range t.getSubscriptionsFor(e) {
e.Resources.Add(sub)
}
diff --git a/pkg/trait/owner.go b/pkg/trait/owner.go
index accc341..51e491a 100644
--- a/pkg/trait/owner.go
+++ b/pkg/trait/owner.go
@@ -33,11 +33,11 @@ func newOwnerTrait() *ownerTrait {
}
}
-func (*ownerTrait) apply(e *Environment) error {
- if e.Integration == nil || e.Integration.Status.Phase != v1alpha1.IntegrationPhaseDeploying {
- return nil
- }
+func (t *ownerTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
+func (*ownerTrait) apply(e *Environment) error {
controller := true
blockOwnerDeletion := true
e.Resources.VisitMetaObject(func(res metav1.Object) {
diff --git a/pkg/trait/route.go b/pkg/trait/route.go
index 97fec93..b2ef625 100644
--- a/pkg/trait/route.go
+++ b/pkg/trait/route.go
@@ -39,6 +39,10 @@ func newRouteTrait() *routeTrait {
}
}
+func (r *routeTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
+
func (r *routeTrait) autoconfigure(e *Environment) error {
if r.Enabled == nil {
hasService := r.getTargetService(e) != nil
diff --git a/pkg/trait/service.go b/pkg/trait/service.go
index b92a2fc..1c9b19b 100644
--- a/pkg/trait/service.go
+++ b/pkg/trait/service.go
@@ -50,6 +50,10 @@ func newServiceTrait() *serviceTrait {
}
}
+func (s *serviceTrait) appliesTo(e *Environment) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == v1alpha1.IntegrationPhaseDeploying
+}
+
func (s *serviceTrait) autoconfigure(e *Environment) error {
if s.Enabled == nil {
required := s.requiresService(e)
@@ -59,10 +63,6 @@ func (s *serviceTrait) autoconfigure(e *Environment) error {
}
func (s *serviceTrait) apply(e *Environment) (err error) {
- if e.Integration == nil || e.Integration.Status.Phase != v1alpha1.IntegrationPhaseDeploying {
- return nil
- }
-
var svc *corev1.Service
if svc, err = s.getServiceFor(e); err != nil {
return err
diff --git a/pkg/trait/trait.go b/pkg/trait/trait.go
index 1ab5f9e..766e56b 100644
--- a/pkg/trait/trait.go
+++ b/pkg/trait/trait.go
@@ -24,7 +24,7 @@ import (
"github.com/pkg/errors"
)
-// Apply --
+// apply --
func Apply(integration *v1alpha1.Integration, ctx *v1alpha1.IntegrationContext) (*Environment, error) {
environment, err := newEnvironment(integration, ctx)
if err != nil {
diff --git a/pkg/trait/types.go b/pkg/trait/types.go
index e9d3cc5..8c1d09a 100644
--- a/pkg/trait/types.go
+++ b/pkg/trait/types.go
@@ -34,10 +34,12 @@ type ID string
// Trait is the interface of all traits
type Trait interface {
Identifiable
- // enabled tells if the trait is enabled
+ // IsEnabled tells if the trait is enabled
IsEnabled() bool
- // auto determine if the trait should be configured automatically
+ // IsAuto determine if the trait should be configured automatically
IsAuto() bool
+ // appliesTo tells if the trait supports the given environment
+ appliesTo(environment *Environment) bool
// autoconfigure is called before any customization to ensure the trait is fully configured
autoconfigure(environment *Environment) error
// apply executes a customization of the Environment
@@ -99,3 +101,13 @@ type Environment struct {
Steps []builder.Step
ExecutedTraits []ID
}
+
+// IntegrationInPhase --
+func (e *Environment) IntegrationInPhase(phase v1alpha1.IntegrationPhase) bool {
+ return e.Integration != nil && e.Integration.Status.Phase == phase
+}
+
+// IntegrationContextInPhase --
+func (e *Environment) IntegrationContextInPhase(phase v1alpha1.IntegrationContextPhase) bool {
+ return e.Context != nil && e.Context.Status.Phase == phase
+}