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

feat(trait): operate via default catalog #5798

Merged
merged 4 commits into from
Aug 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
14 changes: 4 additions & 10 deletions addons/keda/keda.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/apache/camel-k/v2/pkg/platform"
"github.com/apache/camel-k/v2/pkg/trait"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/property"
"github.com/apache/camel-k/v2/pkg/util/source"
"github.com/apache/camel-k/v2/pkg/util/uri"
Expand Down Expand Up @@ -120,9 +119,6 @@ func (t *kedaTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition
if e.Integration == nil || !ptr.Deref(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
}
Expand Down Expand Up @@ -315,12 +311,8 @@ func (t *kedaTrait) getTopControllerReference(e *trait.Environment) *v1.ObjectRe
}

func (t *kedaTrait) populateTriggersFromKamelets(e *trait.Environment) error {
sources, err := kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, e.Resources)
if err != nil {
return err
}
kameletURIs := make(map[string][]string)
if err := metadata.Each(e.CamelCatalog, sources, func(_ int, meta metadata.IntegrationMetadata) bool {
_, err := e.ConsumeMeta(func(meta metadata.IntegrationMetadata) bool {
for _, kameletURI := range meta.FromURIs {
if kameletStr := source.ExtractKamelet(kameletURI); kameletStr != "" && camelv1.ValidKameletName(kameletStr) {
kamelet := kameletStr
Expand All @@ -333,8 +325,10 @@ func (t *kedaTrait) populateTriggersFromKamelets(e *trait.Environment) error {
kameletURIs[kamelet] = uriList
}
}

return true
}); err != nil {
})
if err != nil {
return err
}

Expand Down
70 changes: 32 additions & 38 deletions addons/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,63 +88,57 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitConditi
if !ptr.Deref(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
}

if !ptr.Deref(t.Auto, true) {
return ptr.Deref(t.Enabled, false), nil, nil
}

// Check if the master component has been used
sources, err := kubernetes.ResolveIntegrationSources(e.Ctx, t.Client, e.Integration, e.Resources)
if err != nil {
return false, nil, err
}

meta, err := metadata.ExtractAll(e.CamelCatalog, sources)
if err != nil {
return false, nil, err
}

if t.Enabled == nil {
enabled, err := e.ConsumeMeta(func(meta metadata.IntegrationMetadata) bool {
found := false
loop:
for _, endpoint := range meta.FromURIs {
if uri.GetComponent(endpoint) == masterComponent {
enabled := true
t.Enabled = &enabled
found = true
break loop
}
}
// No master component, can skip the trait execution
if !ptr.Deref(t.Enabled, false) {
return false, nil, nil
if found {
if t.IncludeDelegateDependencies == nil || *t.IncludeDelegateDependencies {
t.delegateDependencies = findAdditionalDependencies(e, meta)
}
}
}
if t.IncludeDelegateDependencies == nil || *t.IncludeDelegateDependencies {
t.delegateDependencies = findAdditionalDependencies(e, meta)
}

if t.ResourceName == nil {
val := e.Integration.Name + "-lock"
t.ResourceName = &val
}
return found
})

if t.ResourceType == nil {
t.ResourceType = ptr.To(leaseResourceType)
if err != nil {
return false, nil, err
}
if enabled {
if t.ResourceName == nil {
val := e.Integration.Name + "-lock"
t.ResourceName = &val
}

if t.LabelKey == nil {
val := v1.IntegrationLabel
t.LabelKey = &val
}
if t.ResourceType == nil {
t.ResourceType = ptr.To(leaseResourceType)
}

if t.LabelKey == nil {
val := v1.IntegrationLabel
t.LabelKey = &val
}

if t.LabelValue == nil {
t.LabelValue = &e.Integration.Name
}

if t.LabelValue == nil {
t.LabelValue = &e.Integration.Name
return true, nil, nil
}

return ptr.Deref(t.Enabled, false), nil, nil
return false, nil, nil
}

func (t *masterTrait) Apply(e *trait.Environment) error {
Expand Down
20 changes: 6 additions & 14 deletions addons/resume/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/apache/camel-k/v2/pkg/metadata"
"github.com/apache/camel-k/v2/pkg/trait"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/log"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -79,29 +78,22 @@ func (r *resumeTrait) Configure(environment *trait.Environment) (bool, *trait.Tr
if !ptr.Deref(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
}

if ptr.Deref(r.Auto, true) {
// Check which components have been used
sources, err := kubernetes.ResolveIntegrationSources(environment.Ctx, r.Client, environment.Integration, environment.Resources)
if err != nil {
return false, nil, err
}
_, err := environment.ConsumeMeta(func(meta metadata.IntegrationMetadata) bool {
for _, endpoint := range meta.FromURIs {
log.Infof("Processing component %s", endpoint)
}

meta, err := metadata.ExtractAll(environment.CamelCatalog, sources)
return true
})
if err != nil {
return false, nil, err
}

for _, endpoint := range meta.FromURIs {
log.Infof("Processing component %s", endpoint)
}

if r.ResumeStrategy == "" {
r.ResumeStrategy = KafkaSingle
}
Expand Down
11 changes: 2 additions & 9 deletions addons/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,10 @@ func NewTelemetryTrait() trait.Trait {
}
}

func (t *telemetryTrait) isForcefullyEnabled() bool {
return ptr.Deref(t.Enabled, false) && !ptr.Deref(t.Auto, true)
}

func (t *telemetryTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !ptr.Deref(t.Enabled, false) {
return false, nil, nil
}
if e.CamelCatalog == nil && !t.isForcefullyEnabled() {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}

if !ptr.Deref(t.Auto, true) {
return true, nil, nil
Expand Down Expand Up @@ -143,7 +136,7 @@ func (t *telemetryTrait) Configure(e *trait.Environment) (bool, *trait.TraitCond
func (t *telemetryTrait) Apply(e *trait.Environment) error {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityTelemetry)

if t.isForcefullyEnabled() || e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties != nil {
if e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties != nil {
t.setCatalogConfiguration(e)
} else {
t.setRuntimeProviderProperties(e)
Expand Down Expand Up @@ -175,7 +168,7 @@ func (t *telemetryTrait) setCatalogConfiguration(e *trait.Environment) {
e.ApplicationProperties["camel.k.telemetry.samplerParentBased"] = boolean.FalseString
}

if e.CamelCatalog != nil && e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties != nil {
if e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties != nil {
for _, cp := range e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties {
e.ApplicationProperties[trait.CapabilityPropertyKey(cp.Key, e.ApplicationProperties)] = cp.Value
}
Expand Down
3 changes: 0 additions & 3 deletions addons/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ func TestTelemetryTraitWithValues(t *testing.T) {

func TestTelemetryForSourceless(t *testing.T) {
e := createEnvironment(t, camel.QuarkusCatalog)
// CamelCatalog not necessarily available during a sourceless deployment
e.CamelCatalog = nil
telemetry := NewTelemetryTrait()
tt, _ := telemetry.(*telemetryTrait)
tt.Enabled = ptr.To(true)
Expand All @@ -104,7 +102,6 @@ func TestTelemetryForSourceless(t *testing.T) {
tt.Sampler = "ratio"
tt.SamplerRatio = "0.001"
tt.SamplerParentBased = ptr.To(false)
assert.True(t, tt.isForcefullyEnabled())

ok, condition, err := telemetry.Configure(e)
require.NoError(t, err)
Expand Down
Loading