Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release 2.3.x] fix(traits): don't skip for synthetic kits #5547

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pkg/trait/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,32 @@ func (t *camelTrait) Configure(e *Environment) (bool, *TraitCondition, error) {

func (t *camelTrait) Apply(e *Environment) error {
if e.IntegrationKit != nil && e.IntegrationKit.IsSynthetic() {
// Synthetic Integration Kit

// This is required as during init phase, the trait set by default these values
// which are widely used in the platform for different purposese.
if e.Integration != nil {
e.Integration.Status.RuntimeVersion = ""
e.Integration.Status.RuntimeProvider = ""
}
return nil
}
if e.CamelCatalog == nil {
if err := t.loadOrCreateCatalog(e, t.RuntimeVersion); err != nil {
return err
} else {
// Managed Integration
if e.CamelCatalog == nil {
if err := t.loadOrCreateCatalog(e, t.RuntimeVersion); err != nil {
return err
}
}
e.RuntimeVersion = e.CamelCatalog.Runtime.Version
if e.Integration != nil {
e.Integration.Status.RuntimeVersion = e.CamelCatalog.Runtime.Version
e.Integration.Status.RuntimeProvider = e.CamelCatalog.Runtime.Provider
}
if e.IntegrationKit != nil {
e.IntegrationKit.Status.RuntimeVersion = e.CamelCatalog.Runtime.Version
e.IntegrationKit.Status.RuntimeProvider = e.CamelCatalog.Runtime.Provider
}
}
e.RuntimeVersion = e.CamelCatalog.Runtime.Version
if e.Integration != nil {
e.Integration.Status.RuntimeVersion = e.CamelCatalog.Runtime.Version
e.Integration.Status.RuntimeProvider = e.CamelCatalog.Runtime.Provider
}
if e.IntegrationKit != nil {
e.IntegrationKit.Status.RuntimeVersion = e.CamelCatalog.Runtime.Version
e.IntegrationKit.Status.RuntimeProvider = e.CamelCatalog.Runtime.Provider
}

if e.IntegrationKitInPhase(v1.IntegrationKitPhaseReady) && e.IntegrationInRunningPhases() {
// Get all resources
maps := t.computeConfigMaps(e)
Expand Down
22 changes: 22 additions & 0 deletions pkg/trait/camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,28 @@ func TestApplyCamelTraitWithProperties(t *testing.T) {
}, userPropertiesCm.Data)
}

func TestApplyCamelTraitSyntheticKitWithProperties(t *testing.T) {
trait, environment := createNominalCamelTest(false)
trait.Properties = []string{"a=b", "c=d"}
environment.IntegrationKit.Labels[v1.IntegrationKitTypeLabel] = v1.IntegrationKitTypeSynthetic

configured, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.Nil(t, condition)
assert.True(t, configured)

err = trait.Apply(environment)
require.NoError(t, err)

userPropertiesCm := environment.Resources.GetConfigMap(func(cm *corev1.ConfigMap) bool {
return cm.Labels["camel.apache.org/properties.type"] == "user"
})
assert.NotNil(t, userPropertiesCm)
assert.Equal(t, map[string]string{
"application.properties": "a=b\nc=d\n",
}, userPropertiesCm.Data)
}

func TestApplyCamelTraitWithSources(t *testing.T) {
trait, environment := createNominalCamelTest(true)

Expand Down
16 changes: 8 additions & 8 deletions pkg/trait/kamelets.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ func (t *kameletsTrait) Configure(e *Environment) (bool, *TraitCondition, error)
if !pointer.BoolDeref(t.Enabled, true) {
return false, NewIntegrationConditionUserDisabled("Kamelets"), nil
}
if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}

if t.MountPoint == "" {
t.MountPoint = filepath.Join(camel.BasePath, "kamelets")
}
if pointer.BoolDeref(t.Auto, true) {
if e.CamelCatalog == nil {
// Cannot execute this trait for synthetic IntegrationKit. In order to use it, the
// user has to specify forcefully auto=false option and pass a list of kamelets explicitly
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
kamelets, err := kamelets.ExtractKameletFromSources(e.Ctx, e.Client, e.CamelCatalog, e.Resources, e.Integration)
if err != nil {
return false, nil, err
Expand All @@ -96,10 +100,6 @@ func (t *kameletsTrait) Configure(e *Environment) (bool, *TraitCondition, error)
sort.Strings(kamelets)
t.List = strings.Join(kamelets, ",")
}

if t.MountPoint == "" {
t.MountPoint = filepath.Join(camel.BasePath, "kamelets")
}
}

return len(t.getKameletKeys()) > 0, nil, nil
Expand Down
84 changes: 84 additions & 0 deletions pkg/trait/kamelets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

func TestConfigurationNoKameletsUsed(t *testing.T) {
Expand Down Expand Up @@ -529,6 +530,12 @@ func TestKameletConditionTrue(t *testing.T) {
assert.Equal(t, corev1.ConditionTrue, cond.Status)
assert.Equal(t, v1.IntegrationConditionKameletsAvailableReason, cond.Reason)
assert.Contains(t, cond.Message, "[none,timer] found")

kameletsBundle := environment.Resources.GetConfigMap(func(cm *corev1.ConfigMap) bool {
return cm.Labels[kubernetes.ConfigMapTypeLabel] == KameletBundleType
})
assert.NotNil(t, kameletsBundle)
assert.Contains(t, kameletsBundle.Data, "timer.kamelet.yaml", "uri: timer:tick")
}

func createKameletsTestEnvironment(flow string, objects ...runtime.Object) (*kameletsTrait, *Environment) {
Expand Down Expand Up @@ -576,3 +583,80 @@ func templateOrFail(template map[string]interface{}) *v1.Template {
t := v1.Template{RawMessage: data}
return &t
}

func TestKameletSyntheticKitConditionTrue(t *testing.T) {
trait, environment := createKameletsTestEnvironment(
"",
&v1.Kamelet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "timer-source",
},
Spec: v1.KameletSpec{
Template: templateOrFail(map[string]interface{}{
"from": map[string]interface{}{
"uri": "timer:tick",
},
}),
},
})
environment.CamelCatalog = nil
environment.Integration.Spec.Sources = nil
trait.Auto = pointer.Bool(false)
trait.List = "timer-source"

enabled, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.True(t, enabled)
assert.Nil(t, condition)

err = trait.Apply(environment)
require.NoError(t, err)
assert.Len(t, environment.Integration.Status.Conditions, 1)

cond := environment.Integration.Status.GetCondition(v1.IntegrationConditionKameletsAvailable)
assert.NotNil(t, cond)
assert.Equal(t, corev1.ConditionTrue, cond.Status)
assert.Equal(t, v1.IntegrationConditionKameletsAvailableReason, cond.Reason)
assert.Contains(t, cond.Message, "[timer-source] found")

kameletsBundle := environment.Resources.GetConfigMap(func(cm *corev1.ConfigMap) bool {
return cm.Labels[kubernetes.ConfigMapTypeLabel] == KameletBundleType
})
assert.NotNil(t, kameletsBundle)
assert.Contains(t, kameletsBundle.Data, "timer-source.kamelet.yaml", "uri: timer:tick")
}

func TestKameletSyntheticKitAutoConditionFalse(t *testing.T) {
trait, environment := createKameletsTestEnvironment(
"",
&v1.Kamelet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "timer-source",
},
Spec: v1.KameletSpec{
Template: templateOrFail(map[string]interface{}{
"from": map[string]interface{}{
"uri": "timer:tick",
},
}),
},
})
environment.CamelCatalog = nil
environment.Integration.Spec.Sources = nil
trait.List = "timer-source"

// Auto=true by default, so, we will need to skip as
// we cannot parse sources

enabled, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.False(t, enabled)
assert.NotNil(t, condition)

kameletsBundle := environment.Resources.GetConfigMap(func(cm *corev1.ConfigMap) bool {
return cm.Labels[kubernetes.ConfigMapTypeLabel] == KameletBundleType
})
assert.Nil(t, kameletsBundle)
}
12 changes: 3 additions & 9 deletions pkg/trait/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ func (t *mountTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
}
}

// mount trait needs to be executed only when it has sources attached or any trait configuration
return len(e.Integration.AllSources()) > 0 ||
len(t.Configs) > 0 ||
len(t.Resources) > 0 ||
len(t.Volumes) > 0, condition, nil
return true, condition, nil
}

func (t *mountTrait) Apply(e *Environment) error {
Expand Down Expand Up @@ -113,10 +109,8 @@ func (t *mountTrait) Apply(e *Environment) error {
}

if visited {
// Volumes declared in the Integration resources (we skip for synthetic kits)
if e.IntegrationKit == nil || !e.IntegrationKit.IsSynthetic() {
e.configureVolumesAndMounts(volumes, &container.VolumeMounts)
}
// Volumes declared in the Integration resources
e.configureVolumesAndMounts(volumes, &container.VolumeMounts)
// Volumes declared in the trait config/resource options
err := t.configureVolumesAndMounts(volumes, &container.VolumeMounts)
if err != nil {
Expand Down