Skip to content

Commit

Permalink
Adapt cron trait to latest camel-k-runtime apache#1329
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Mar 9, 2020
1 parent 650c293 commit 5cc5b5a
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 29 deletions.
9 changes: 9 additions & 0 deletions e2e/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,14 @@ func TestRunCronExample(t *testing.T) {
Eventually(integrationLogs(ns, "cron-fallback"), testTimeoutShort).Should(ContainSubstring("Magicstring!"))
Expect(kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})

t.Run("cron-fallback-quarkus", func(t *testing.T) {
RegisterTestingT(t)

Expect(kamel("run", "-n", ns, "files/cron.groovy").Execute()).Should(BeNil())
Eventually(integrationPodPhase(ns, "cron"), testTimeoutMedium).Should(Equal(v1.PodRunning))
Eventually(integrationLogs(ns, "cron"), testTimeoutShort).Should(ContainSubstring("Magicstring!"))
Expect(kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})
})
}
79 changes: 54 additions & 25 deletions pkg/trait/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package trait
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

Expand All @@ -31,7 +32,6 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/metadata"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/uri"
)
Expand Down Expand Up @@ -125,7 +125,18 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

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

if _, ok := e.CamelCatalog.Runtime.Capabilities["cron"]; !ok {
e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionFalse,
v1.IntegrationConditionCronJobNotAvailableReason,
"the runtime provider %s does not declare 'cron' capability",
)

return false, nil
}

Expand Down Expand Up @@ -156,7 +167,14 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
t.ConcurrencyPolicy = string(v1beta1.ForbidConcurrent)
}

if t.Schedule == "" && t.Components == "" && t.Fallback == nil {
hasQuarkus := false

qt := e.GetTrait("quarkus")
if qt != nil {
hasQuarkus = qt.(*quarkusTrait).Enabled != nil && *(qt.(*quarkusTrait).Enabled)
}

if (hasQuarkus || (t.Schedule == "" && t.Components == "")) && t.Fallback == nil {
// If there's at least a `cron` endpoint, add a fallback implementation
fromURIs, err := t.getSourcesFromURIs(e)
if err != nil {
Expand All @@ -169,18 +187,16 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
break
}
}

}
}

dt := e.Catalog.GetTrait("deployer")
if dt != nil {
t.deployer = *dt.(*deployerTrait)
}

// Fallback strategy can be implemented in any other controller
if t.Fallback != nil && *t.Fallback {
if e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) {
if e.IntegrationInPhase(v1.IntegrationPhaseDeploying) {
e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionFalse,
Expand All @@ -202,7 +218,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
return false, err
}
if strategy != ControllerStrategyCronJob {
if e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) {
if e.IntegrationInPhase(v1.IntegrationPhaseDeploying) {
e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionFalse,
Expand All @@ -217,30 +233,43 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
}

func (t *cronTrait) Apply(e *Environment) error {
if t.Fallback != nil && *t.Fallback {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
if capability, ok := e.CamelCatalog.Runtime.Capabilities["cron"]; ok {
for _, dependency := range capability.Dependencies {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID))
}

// sort the dependencies to get always the same list if they don't change
sort.Strings(e.Integration.Status.Dependencies)
}

if t.Fallback != nil && *t.Fallback {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, genericCronComponentFallback)
}
} else {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-cron")
} else if e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) {
cronJob := t.getCronJobFor(e)
maps := e.ComputeConfigMaps()
}

e.Resources.AddAll(maps)
e.Resources.Add(cronJob)
if e.IntegrationInPhase(v1.IntegrationPhaseDeploying) {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}

e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionCronJobAvailableReason,
fmt.Sprintf("CronJob name is %s", cronJob.Name),
)
e.ApplicationProperties["camel.main.duration-max-messages"] = "1"
e.ApplicationProperties["loader.interceptor.cron.overridable-components"] = t.Components
e.Interceptors = append(e.Interceptors, "cron")

envvar.SetVal(&e.EnvVars, "CAMEL_K_CRON_OVERRIDE", t.Components)
}
cronJob := t.getCronJobFor(e)
maps := e.ComputeConfigMaps()

e.Resources.AddAll(maps)
e.Resources.Add(cronJob)

e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionCronJobAvailableReason,
fmt.Sprintf("CronJob name is %s", cronJob.Name))
}

return nil
}

Expand Down
Loading

0 comments on commit 5cc5b5a

Please sign in to comment.