Skip to content

Commit

Permalink
chore(trait): disable traits requiring catalog when missing
Browse files Browse the repository at this point in the history
We must explicitly define which are the traits that can be executed and which can't because missing catalog.
The catalog has sense only for the "managed" Integrations.
  • Loading branch information
squakez committed Apr 24, 2024
1 parent 3e70068 commit 877c436
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 74 deletions.
4 changes: 3 additions & 1 deletion addons/keda/keda.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ func (t *kedaTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil, nil
}
if e.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if !e.IntegrationInPhase(camelv1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}

if t.Auto == nil || *t.Auto {
if err := t.populateTriggersFromKamelets(e); err != nil {
return false, nil, err
Expand Down
3 changes: 3 additions & 0 deletions addons/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitConditi
if !pointer.BoolDeref(t.Enabled, true) {
return false, trait.NewIntegrationConditionUserDisabled(masterComponent), nil
}
if e.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization, v1.IntegrationPhaseBuildingKit) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}
Expand Down
3 changes: 3 additions & 0 deletions addons/resume/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (r *resumeTrait) Configure(environment *trait.Environment) (bool, *trait.Tr
if !pointer.BoolDeref(r.Enabled, false) {
return false, nil, nil
}
if environment.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if !environment.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !environment.IntegrationInRunningPhases() {
return false, nil, nil
}
Expand Down
4 changes: 3 additions & 1 deletion addons/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (t *telemetryTrait) Configure(e *trait.Environment) (bool, *trait.TraitCond
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil, nil
}

if e.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
var condition *trait.TraitCondition
if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
Expand Down
42 changes: 19 additions & 23 deletions addons/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (t *tracingTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondit
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil, nil
}
if e.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}

if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
Expand Down Expand Up @@ -125,30 +128,23 @@ func (t *tracingTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondit
func (t *tracingTrait) Apply(e *trait.Environment) error {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityTracing)

if e.CamelCatalog != nil {
provider := e.CamelCatalog.CamelCatalogSpec.Runtime.Provider
properties := tracingProperties[provider]

if appPropEnabled := properties[propEnabled]; appPropEnabled != "" {
e.ApplicationProperties[appPropEnabled] = "true"
}

if appPropEndpoint := properties[propEndpoint]; appPropEndpoint != "" && t.Endpoint != "" {
e.ApplicationProperties[appPropEndpoint] = t.Endpoint
}

if appPropServiceName := properties[propServiceName]; appPropServiceName != "" && t.ServiceName != "" {
e.ApplicationProperties[appPropServiceName] = t.ServiceName
}

if appPropSamplerType := properties[propSamplerType]; appPropSamplerType != "" && t.SamplerType != nil {
e.ApplicationProperties[appPropSamplerType] = *t.SamplerType
}

if appPropSamplerParam := properties[propSamplerParam]; appPropSamplerParam != "" && t.SamplerParam != nil {
e.ApplicationProperties[appPropSamplerParam] = *t.SamplerParam
}
provider := e.CamelCatalog.CamelCatalogSpec.Runtime.Provider
properties := tracingProperties[provider]

if appPropEnabled := properties[propEnabled]; appPropEnabled != "" {
e.ApplicationProperties[appPropEnabled] = "true"
}
if appPropEndpoint := properties[propEndpoint]; appPropEndpoint != "" && t.Endpoint != "" {
e.ApplicationProperties[appPropEndpoint] = t.Endpoint
}
if appPropServiceName := properties[propServiceName]; appPropServiceName != "" && t.ServiceName != "" {
e.ApplicationProperties[appPropServiceName] = t.ServiceName
}
if appPropSamplerType := properties[propSamplerType]; appPropSamplerType != "" && t.SamplerType != nil {
e.ApplicationProperties[appPropSamplerType] = *t.SamplerType
}
if appPropSamplerParam := properties[propSamplerParam]; appPropSamplerParam != "" && t.SamplerParam != nil {
e.ApplicationProperties[appPropSamplerParam] = *t.SamplerParam
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/trait/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (t *builderTrait) Configure(e *Environment) (bool, *TraitCondition, error)
if e.IntegrationKit == nil {
return false, nil, nil
}

if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
condition := t.adaptDeprecatedFields()

t.setPlatform(e)
Expand Down
26 changes: 15 additions & 11 deletions pkg/trait/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ func (t *camelTrait) Matches(trait Trait) bool {
}

func (t *camelTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
if e.IntegrationKit != nil && e.IntegrationKit.IsExternal() {
return false, newIntegrationConditionPlatformDisabledWithMessage("Camel", "integration kit was not created via Camel K operator"), nil
}
if e.Integration != nil && e.Integration.IsSynthetic() {
return false, newIntegrationConditionPlatformDisabledWithMessage("Camel", "syntetic integration"), nil
return false, NewIntegrationConditionPlatformDisabledWithMessage("Camel", "synthetic integration"), nil
}

if t.RuntimeVersion == "" {
Expand All @@ -82,13 +79,26 @@ func (t *camelTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
}

func (t *camelTrait) Apply(e *Environment) error {
if e.IntegrationKitInPhase(v1.IntegrationKitPhaseReady) && e.IntegrationInRunningPhases() {
// Get all resources
maps := t.computeConfigMaps(e)
e.Resources.AddAll(maps)
}
if e.IntegrationKit != nil && e.IntegrationKit.IsSynthetic() {
// 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
}
}
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
Expand All @@ -98,12 +108,6 @@ func (t *camelTrait) Apply(e *Environment) error {
e.IntegrationKit.Status.RuntimeProvider = e.CamelCatalog.Runtime.Provider
}

if e.IntegrationKitInPhase(v1.IntegrationKitPhaseReady) && e.IntegrationInRunningPhases() {
// Get all resources
maps := t.computeConfigMaps(e)
e.Resources.AddAll(maps)
}

return nil
}

Expand Down
34 changes: 19 additions & 15 deletions pkg/trait/camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestConfigureEnabledCamelTraitSucceeds(t *testing.T) {

func TestApplyCamelTraitSucceeds(t *testing.T) {
trait, environment := createNominalCamelTest(false)
environment.Integration.Status.Phase = v1.IntegrationPhaseBuildingKit

configured, condition, err := trait.Configure(environment)
require.NoError(t, err)
Expand All @@ -63,6 +64,22 @@ func TestApplyCamelTraitSucceeds(t *testing.T) {
assert.False(t, exactVersionRegexp.MatchString("wroong"))
}

func TestApplyCamelTraitExternalKit(t *testing.T) {
trait, environment := createNominalCamelTest(false)
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)
assert.Equal(t, "", environment.Integration.Status.RuntimeVersion)
assert.Equal(t, v1.RuntimeProvider(""), environment.Integration.Status.RuntimeProvider)
assert.Equal(t, "", environment.IntegrationKit.Status.RuntimeVersion)
assert.Equal(t, v1.RuntimeProvider(""), environment.Integration.Status.RuntimeProvider)
}

func TestApplyCamelTraitWithoutEnvironmentCatalogAndUnmatchableVersionFails(t *testing.T) {
trait, environment := createNominalCamelTest(false)
environment.CamelCatalog = nil
Expand Down Expand Up @@ -233,6 +250,7 @@ func TestCamelMatches(t *testing.T) {

func TestCamelCatalogSemver(t *testing.T) {
trait, environment := createNominalCamelTest(true)
environment.Integration.Status.Phase = v1.IntegrationPhaseBuildingKit
trait.RuntimeVersion = "2.x"
environment.CamelCatalog.CamelCatalogSpec.Runtime.Version = "2.16.0"

Expand All @@ -247,20 +265,6 @@ func TestCamelCatalogSemver(t *testing.T) {
assert.Equal(t, environment.CamelCatalog.CamelCatalogSpec.Runtime.Version, environment.RuntimeVersion)
}

func TestCamelTraitExternalKit(t *testing.T) {
trait, environment := createNominalCamelTest(true)
environment.Integration.Status = v1.IntegrationStatus{}
environment.IntegrationKit.Labels[v1.IntegrationKitTypeLabel] = v1.IntegrationKitTypeExternal

configured, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.Equal(t, "explicitly disabled by the platform: integration kit was not created via Camel K operator", condition.message)
assert.False(t, configured)

assert.Equal(t, v1.RuntimeProvider(""), environment.Integration.Status.RuntimeProvider)
assert.Equal(t, "", environment.Integration.Status.RuntimeVersion)
}

func TestCamelTraitSyntheticIntegration(t *testing.T) {
trait, environment := createNominalCamelTest(true)
environment.Integration.Status = v1.IntegrationStatus{}
Expand All @@ -269,7 +273,7 @@ func TestCamelTraitSyntheticIntegration(t *testing.T) {

configured, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.Equal(t, "explicitly disabled by the platform: syntetic integration", condition.message)
assert.Equal(t, "explicitly disabled by the platform: synthetic integration", condition.message)
assert.False(t, configured)

assert.Equal(t, v1.RuntimeProvider(""), environment.Integration.Status.RuntimeProvider)
Expand Down
1 change: 0 additions & 1 deletion pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (t *containerTrait) configureImageIntegrationKit(e *Environment) error {
kit.SetOperatorID(operatorID)
}

t.L.Infof("image %s", kit.Spec.Image)
e.Resources.Add(kit)
e.Integration.SetIntegrationKit(kit)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/trait/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ func (t *cronTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
return false, nil, nil
}
if e.CamelCatalog == nil {
return false, newIntegrationConditionPlatformDisabledWithMessage("Cron", "no camel catalog available for this Integration"), nil
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}

if _, ok := e.CamelCatalog.Runtime.Capabilities[v1.CapabilityCron]; !ok {
return false, NewIntegrationCondition(
"Cron",
Expand All @@ -96,7 +95,6 @@ func (t *cronTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
"the runtime provider %s does not declare 'cron' capability",
), nil
}

if pointer.BoolDeref(t.Auto, true) {
globalCron, err := t.getGlobalCron(e)
if err != nil {
Expand Down Expand Up @@ -309,6 +307,9 @@ func (c *cronInfo) withSchedule(schedule string) *cronInfo {
}

func (t *cronTrait) getGlobalCron(e *Environment) (*cronInfo, error) {
if e.CamelCatalog == nil {
return nil, nil
}
fromURIs, err := t.getSourcesFromURIs(e)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/trait/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func (t *dependenciesTrait) Configure(e *Environment) (bool, *TraitCondition, er
if e.Integration == nil {
return false, nil, nil
}

if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
return e.IntegrationInPhase(v1.IntegrationPhaseInitialization), nil, nil
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/trait/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import (
)

func TestDependenciesTraitApplicability(t *testing.T) {
catalog, err := camel.DefaultCatalog()
require.NoError(t, err)

e := &Environment{
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
CamelCatalog: catalog,
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
}

trait := newDependenciesTrait()
Expand Down
4 changes: 3 additions & 1 deletion pkg/trait/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ func (t *errorHandlerTrait) Configure(e *Environment) (bool, *TraitCondition, er
if e.Integration == nil {
return false, nil, nil
}
if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}

if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}

if t.ErrorHandlerRef == "" {
t.ErrorHandlerRef = e.Integration.Spec.GetConfigurationProperty(v1.ErrorHandlerRefName)
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/trait/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ import (
)

func TestErrorHandlerConfigureFromIntegrationProperty(t *testing.T) {
catalog, err := camel.DefaultCatalog()
require.NoError(t, err)
e := &Environment{
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
CamelCatalog: catalog,
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
}
e.Integration.Spec.AddConfigurationProperty(fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))

Expand All @@ -56,9 +59,12 @@ func TestErrorHandlerConfigureFromIntegrationProperty(t *testing.T) {
}

func TestErrorHandlerApplySource(t *testing.T) {
catalog, err := camel.DefaultCatalog()
require.NoError(t, err)
e := &Environment{
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
CamelCatalog: catalog,
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
}
e.Integration.Spec.AddConfiguration("property", fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))
e.Integration.Status.Phase = v1.IntegrationPhaseInitialization
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func newHealthTrait() Trait {
}

func (t *healthTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if e.Integration == nil ||
!e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/trait/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func (t *jvmTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
// The JVM trait must be disabled in case the current IntegrationKit corresponds to a native build
if qt := e.Catalog.GetTrait(quarkusTraitID); qt != nil {
if quarkus, ok := qt.(*quarkusTrait); ok && quarkus.isNativeIntegration(e) {
return false, newIntegrationConditionPlatformDisabledWithMessage("JVM", "quarkus native build"), nil
return false, NewIntegrationConditionPlatformDisabledWithMessage("JVM", "quarkus native build"), nil
}
}

if e.IntegrationKit != nil && e.IntegrationKit.IsSynthetic() {
return false, newIntegrationConditionPlatformDisabledWithMessage("JVM", "integration kit was not created via Camel K operator"), nil
return false, NewIntegrationConditionPlatformDisabledWithMessage("JVM", "integration kit was not created via Camel K operator"), nil
}

if e.CamelCatalog == nil {
return false, newIntegrationConditionPlatformDisabledWithMessage("JVM", "no camel catalog available for this Integration"), nil
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
return true, nil, nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/kamelets.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ 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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (t *knativeTrait) Configure(e *Environment) (bool, *TraitCondition, error)
if !pointer.BoolDeref(t.Enabled, true) {
return false, NewIntegrationConditionUserDisabled("Knative"), nil
}
if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}
Expand Down
Loading

0 comments on commit 877c436

Please sign in to comment.