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 7d17bfc3928e5b01f9f1127e1db3c2f87886190d Author: Nicola Ferraro <[email protected]> AuthorDate: Thu Jun 25 11:29:37 2020 +0200 kamelets: adding support for kamelet dependencies --- pkg/trait/kamelets.go | 7 ++++ pkg/trait/kamelets_test.go | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/pkg/trait/kamelets.go b/pkg/trait/kamelets.go index 180985a..55b61a2 100644 --- a/pkg/trait/kamelets.go +++ b/pkg/trait/kamelets.go @@ -86,6 +86,7 @@ func (t *kameletsTrait) Apply(e *Environment) error { if err := t.addKamelets(e); err != nil { return err } + return nil } @@ -99,10 +100,16 @@ func (t *kameletsTrait) addKamelets(e *Environment) error { if err := t.Client.Get(t.Ctx, key, &kamelet); err != nil { return err } + if err := t.addKameletAsSource(e, kamelet); err != nil { return err } + + // Adding dependencies from Kamelets + util.StringSliceUniqueConcat(&e.Integration.Status.Dependencies, kamelet.Spec.Dependencies) } + // resort dependencies + sort.Strings(e.Integration.Status.Dependencies) return nil } diff --git a/pkg/trait/kamelets_test.go b/pkg/trait/kamelets_test.go index 30585c3..500baba 100644 --- a/pkg/trait/kamelets_test.go +++ b/pkg/trait/kamelets_test.go @@ -84,6 +84,10 @@ func TestKameletLookup(t *testing.T) { "uri": "timer:tick", }, }, + Dependencies: []string{ + "camel:timer", + "camel:log", + }, }, }) enabled, err := trait.Configure(environment) @@ -102,6 +106,8 @@ func TestKameletLookup(t *testing.T) { source := environment.Integration.Status.GeneratedSources[0] assert.Equal(t, "timer.yaml", source.Name) assert.Equal(t, "kamelet", string(source.Type)) + + assert.Equal(t, []string{"camel:log", "camel:timer"}, environment.Integration.Status.Dependencies) } func TestKameletSecondarySourcesLookup(t *testing.T) { @@ -237,6 +243,100 @@ func TestErrorMultipleKameletSources(t *testing.T) { assert.Error(t, err) } +func TestMultipleKamelets(t *testing.T) { + trait, environment := createKameletsTestEnvironment(` +- from: + uri: kamelet:timer + steps: + - to: kamelet:logger + - to: kamelet:logger +`, &v1alpha1.Kamelet{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test", + Name: "timer", + }, + Spec: v1alpha1.KameletSpec{ + Flow: &v1.Flow{ + "from": map[string]interface{}{ + "uri": "timer:tick", + }, + }, + Sources: []v1.SourceSpec{ + { + DataSpec: v1.DataSpec{ + Name: "support.groovy", + Content: "from('xxx:xxx').('to:log:info')", + }, + Language: v1.LanguageGroovy, + }, + }, + Dependencies: []string{ + "camel:timer", + "camel:xxx", + }, + }, + }, &v1alpha1.Kamelet{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test", + Name: "logger", + }, + Spec: v1alpha1.KameletSpec{ + Flow: &v1.Flow{ + "from": map[string]interface{}{ + "uri": "tbd:endpoint", + "steps": []interface{}{ + map[string]interface{}{ + "to": map[string]interface{}{ + "uri": "log:info", + }, + }, + }, + }, + }, + Dependencies: []string{ + "camel:log", + "camel:tbd", + }, + }, + }) + enabled, err := trait.Configure(environment) + assert.NoError(t, err) + assert.True(t, enabled) + assert.Equal(t, []string{"logger", "timer"}, trait.getKamelets()) + + err = trait.Apply(environment) + assert.NoError(t, err) + + cmFlow := environment.Resources.GetConfigMap(func(c *corev1.ConfigMap) bool { return c.Name == "it-kamelet-timer-flow" }) + assert.NotNil(t, cmFlow) + cmRes := environment.Resources.GetConfigMap(func(c *corev1.ConfigMap) bool { return c.Name == "it-kamelet-timer-000" }) + assert.NotNil(t, cmRes) + cmFlow2 := environment.Resources.GetConfigMap(func(c *corev1.ConfigMap) bool { return c.Name == "it-kamelet-logger-flow" }) + assert.NotNil(t, cmFlow2) + + assert.Len(t, environment.Integration.Status.GeneratedSources, 3) + + flowSource2 := environment.Integration.Status.GeneratedSources[0] + assert.Equal(t, "logger.yaml", flowSource2.Name) + assert.Equal(t, "kamelet", string(flowSource2.Type)) + assert.Equal(t, "it-kamelet-logger-flow", flowSource2.ContentRef) + assert.Equal(t, "content", flowSource2.ContentKey) + + flowSource := environment.Integration.Status.GeneratedSources[1] + assert.Equal(t, "timer.yaml", flowSource.Name) + assert.Equal(t, "kamelet", string(flowSource.Type)) + assert.Equal(t, "it-kamelet-timer-flow", flowSource.ContentRef) + assert.Equal(t, "content", flowSource.ContentKey) + + supportSource := environment.Integration.Status.GeneratedSources[2] + assert.Equal(t, "support.groovy", supportSource.Name) + assert.Equal(t, "", string(supportSource.Type)) + assert.Equal(t, "it-kamelet-timer-000", supportSource.ContentRef) + assert.Equal(t, "content", supportSource.ContentKey) + + assert.Equal(t, []string{"camel:log", "camel:tbd", "camel:timer", "camel:xxx"}, environment.Integration.Status.Dependencies) +} + func createKameletsTestEnvironment(flow string, objects ...runtime.Object) (*kameletsTrait, *Environment) { catalog, _ := camel.DefaultCatalog()
