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): expose conditions #4852

Merged
merged 2 commits into from
Oct 24, 2023
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
11 changes: 5 additions & 6 deletions addons/keda/keda.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,21 @@ func NewKedaTrait() trait.Trait {
}
}

func (t *kedaTrait) Configure(e *trait.Environment) (bool, error) {
func (t *kedaTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil
return false, nil, nil
}

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

if t.Auto == nil || *t.Auto {
if err := t.populateTriggersFromKamelets(e); err != nil {
return false, err
return false, nil, err
}
}

return len(t.Triggers) > 0, nil
return len(t.Triggers) > 0, nil, nil
}

func (t *kedaTrait) Apply(e *trait.Environment) error {
Expand Down
21 changes: 14 additions & 7 deletions addons/keda/keda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func TestManualConfig(t *testing.T) {
})
env := createBasicTestEnvironment()

res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
so := getScaledObject(env)
assert.NotNil(t, so)
Expand Down Expand Up @@ -90,9 +91,10 @@ func TestConfigFromSecret(t *testing.T) {
},
})

res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
so := getScaledObject(env)
assert.NotNil(t, so)
Expand Down Expand Up @@ -177,9 +179,10 @@ func TestKameletAutoDetection(t *testing.T) {
},
})

res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
so := getScaledObject(env)
assert.NotNil(t, so)
Expand Down Expand Up @@ -284,15 +287,17 @@ func TestPipeAutoDetection(t *testing.T) {

it.Status.Phase = camelv1.IntegrationPhaseInitialization
init := trait.NewInitTrait()
ok, err := init.Configure(env)
ok, condition, err := init.Configure(env)
assert.NoError(t, err)
assert.True(t, ok)
assert.Nil(t, condition)
assert.NoError(t, init.Apply(env))

it.Status.Phase = camelv1.IntegrationPhaseDeploying
res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
so := getScaledObject(env)
assert.NotNil(t, so)
Expand Down Expand Up @@ -339,9 +344,10 @@ func TestHackReplicas(t *testing.T) {
},
)

res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
scalesClient, err := env.Client.ScalesClient()
assert.NoError(t, err)
Expand Down Expand Up @@ -386,9 +392,10 @@ func TestHackKLBReplicas(t *testing.T) {
},
)

res, err := keda.Configure(env)
res, condition, err := keda.Configure(env)
assert.NoError(t, err)
assert.True(t, res)
assert.Nil(t, condition)
assert.NoError(t, keda.Apply(env))
scalesClient, err := env.Client.ScalesClient()
assert.NoError(t, err)
Expand Down
20 changes: 8 additions & 12 deletions addons/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,23 @@ var (
configMapResourceType = "ConfigMap"
)

func (t *masterTrait) Configure(e *trait.Environment) (bool, error) {
func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, true) {
return false, nil
return false, nil, nil
}

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

if pointer.BoolDeref(t.Auto, true) {
// 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, err
return false, nil, err
}

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

if t.Enabled == nil {
Expand All @@ -113,11 +111,9 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, error) {
}
}
}

if !pointer.BoolDeref(t.Enabled, false) {
return false, nil
return false, trait.NewIntegrationConditionUserDisabled(), nil
}

if t.IncludeDelegateDependencies == nil || *t.IncludeDelegateDependencies {
t.delegateDependencies = findAdditionalDependencies(e, meta)
}
Expand All @@ -130,7 +126,7 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, error) {
if t.ResourceType == nil {
canUseLeases, err := t.canUseLeases(e)
if err != nil {
return false, err
return false, nil, err
}
if canUseLeases {
t.ResourceType = &leaseResourceType
Expand All @@ -149,7 +145,7 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, error) {
}
}

return pointer.BoolDeref(t.Enabled, true), nil
return pointer.BoolDeref(t.Enabled, true), nil, nil
}

func (t *masterTrait) Apply(e *trait.Environment) error {
Expand Down
13 changes: 6 additions & 7 deletions addons/resume/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,24 @@ func NewResumeTrait() trait.Trait {
}
}

func (r *resumeTrait) Configure(environment *trait.Environment) (bool, error) {
func (r *resumeTrait) Configure(environment *trait.Environment) (bool, *trait.TraitCondition, error) {
if !pointer.BoolDeref(r.Enabled, false) {
return false, nil
return false, nil, nil
}

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

if pointer.BoolDeref(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, err
return false, nil, err
}

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

for _, endpoint := range meta.FromURIs {
Expand All @@ -109,7 +108,7 @@ func (r *resumeTrait) Configure(environment *trait.Environment) (bool, error) {
}
}

return r.Enabled != nil && *r.Enabled, nil
return r.Enabled != nil && *r.Enabled, nil, nil
}

func (r *resumeTrait) Apply(environment *trait.Environment) error {
Expand Down
17 changes: 13 additions & 4 deletions addons/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package telemetry
import (
"k8s.io/utils/pointer"

corev1 "k8s.io/api/core/v1"

"github.com/apache/camel-k/v2/addons/telemetry/discovery"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
Expand Down Expand Up @@ -85,20 +87,27 @@ func NewTelemetryTrait() trait.Trait {
}
}

func (t *telemetryTrait) Configure(e *trait.Environment) (bool, error) {
func (t *telemetryTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil
return false, nil, nil
}

var condition *trait.TraitCondition
if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
for _, locator := range discovery.TelemetryLocators {
endpoint, err := locator.FindEndpoint(e.Ctx, t.Client, t.L, e)
if err != nil {
return false, err
return false, nil, err
}
if endpoint != "" {
t.L.Infof("Using tracing endpoint: %s", endpoint)
condition = trait.NewIntegrationCondition(
v1.IntegrationConditionTraitInfo,
corev1.ConditionTrue,
"Tracing endpoint",
endpoint,
)
t.Endpoint = endpoint
break
}
Expand All @@ -114,7 +123,7 @@ func (t *telemetryTrait) Configure(e *trait.Environment) (bool, error) {
}
}

return true, nil
return true, condition, nil
}

func (t *telemetryTrait) Apply(e *trait.Environment) error {
Expand Down
6 changes: 4 additions & 2 deletions addons/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func TestTelemetryTraitOnDefaultQuarkus(t *testing.T) {
tt, _ := telemetry.(*telemetryTrait)
tt.Enabled = pointer.Bool(true)
tt.Endpoint = "http://endpoint3"
ok, err := telemetry.Configure(e)
ok, condition, err := telemetry.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)
assert.Nil(t, condition)

err = telemetry.Apply(e)
assert.Nil(t, err)
Expand All @@ -61,9 +62,10 @@ func TestTelemetryTraitWithValues(t *testing.T) {
tt.Sampler = "ratio"
tt.SamplerRatio = "0.001"
tt.SamplerParentBased = pointer.Bool(false)
ok, err := telemetry.Configure(e)
ok, condition, err := telemetry.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)
assert.Nil(t, condition)

err = telemetry.Apply(e)
assert.Nil(t, err)
Expand Down
10 changes: 4 additions & 6 deletions addons/threescale/3scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ func NewThreeScaleTrait() trait.Trait {
}
}

func (t *threeScaleTrait) Configure(e *trait.Environment) (bool, error) {
func (t *threeScaleTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
// disabled by default
return false, nil
return false, nil, nil
}

if !e.IntegrationInRunningPhases() {
return false, nil
return false, nil, nil
}

if pointer.BoolDeref(t.Auto, true) {
Expand All @@ -113,7 +111,7 @@ func (t *threeScaleTrait) Configure(e *trait.Environment) (bool, error) {
}
}

return true, nil
return true, nil, nil
}

func (t *threeScaleTrait) Apply(e *trait.Environment) error {
Expand Down
9 changes: 6 additions & 3 deletions addons/threescale/3scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ func TestThreeScaleDisabled(t *testing.T) {
}

threeScale := NewThreeScaleTrait()
enabled, err := threeScale.Configure(e)
enabled, condition, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.False(t, enabled)
assert.Nil(t, condition)
}

func TestThreeScaleInjection(t *testing.T) {
svc, e := createEnvironment(t)
threeScale := NewThreeScaleTrait()
tst, _ := threeScale.(*threeScaleTrait)
tst.Enabled = pointer.Bool(true)
ok, err := threeScale.Configure(e)
ok, condition, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)
assert.Nil(t, condition)

err = threeScale.Apply(e)
assert.Nil(t, err)
Expand All @@ -69,9 +71,10 @@ func TestThreeScaleInjectionNoAPIPath(t *testing.T) {
tst, _ := threeScale.(*threeScaleTrait)
tst.Enabled = pointer.Bool(true)
tst.DescriptionPath = pointer.String("")
ok, err := threeScale.Configure(e)
ok, condition, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)
assert.Nil(t, condition)

err = threeScale.Apply(e)
assert.Nil(t, err)
Expand Down
8 changes: 4 additions & 4 deletions addons/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ func NewTracingTrait() trait.Trait {
}
}

func (t *tracingTrait) Configure(e *trait.Environment) (bool, error) {
func (t *tracingTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil
return false, nil, nil
}

if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
for _, locator := range discovery.TracingLocators {
endpoint, err := locator.FindEndpoint(e.Ctx, t.Client, t.L, e)
if err != nil {
return false, err
return false, nil, err
}
if endpoint != "" {
t.L.Infof("Using tracing endpoint: %s", endpoint)
Expand All @@ -119,7 +119,7 @@ func (t *tracingTrait) Configure(e *trait.Environment) (bool, error) {
}
}

return true, nil
return true, nil, nil
}

func (t *tracingTrait) Apply(e *trait.Environment) error {
Expand Down
3 changes: 2 additions & 1 deletion addons/tracing/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func TestTracingTraitOnQuarkus(t *testing.T) {
tt, _ := tracing.(*tracingTrait)
tt.Enabled = pointer.Bool(true)
tt.Endpoint = "http://endpoint3"
ok, err := tracing.Configure(e)
ok, condition, err := tracing.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)
assert.Nil(t, condition)

err = tracing.Apply(e)
assert.Nil(t, err)
Expand Down
Loading
Loading