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

Set default for rest based services #1349

Merged
merged 2 commits into from
Mar 16, 2020
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
7 changes: 7 additions & 0 deletions pkg/apis/camel/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ type Capability struct {
const (
// ServiceTypeUser --
ServiceTypeUser = "user"

// CapabilityRest --
CapabilityRest = "rest"
// CapabilityHealth --
CapabilityHealth = "health"
// CapabilityCron --
CapabilityCron = "cron"
)

// ResourceCondition is a common type for all conditions
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/camel/v1/common_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func NewErrorFailure(err error) *Failure {
Time: metav1.Now(),
}
}

// CapabilityDependencies ---
func (in *RuntimeSpec) CapabilityDependencies(capability string) []MavenArtifact {
deps := make([]MavenArtifact, 0)

if capability, ok := in.Capabilities[capability]; ok {
deps = append(deps, capability.Dependencies...)
}

return deps
}
1 change: 1 addition & 0 deletions pkg/apis/camel/v1/integration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type IntegrationStatus struct {
Conditions []IntegrationCondition `json:"conditions,omitempty"`
Version string `json:"version,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/camel/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions pkg/metadata/metadata_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,11 @@ func TestXMLRestDependency(t *testing.T) {
t,
[]string{
"camel:direct",
"camel:rest",
"camel:mock",
},
meta.Dependencies.List())

assert.True(t, meta.RequiredCapabilities.Has("rest"))
}

func TestXMLLanguageDependencies(t *testing.T) {
Expand Down Expand Up @@ -683,7 +684,15 @@ func TestYAMLRestDependency(t *testing.T) {

meta := Extract(catalog, code)

assert.ElementsMatch(t, []string{"camel:direct", "camel:rest", "camel:log"}, meta.Dependencies.List())
assert.ElementsMatch(
t,
[]string{
"camel:direct",
"camel:log",
},
meta.Dependencies.List())

assert.True(t, meta.RequiredCapabilities.Has("rest"))
}

func TestYAMLHystrixDependency(t *testing.T) {
Expand Down
42 changes: 41 additions & 1 deletion pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ const (
defaultServicePort = 80
defaultProbePath = "/health"
containerTraitID = "container"

// CamelRestPortProperty ---
CamelRestPortProperty = "camel.context.rest-configuration.port"
// CamelRestDefaultPort ---
CamelRestDefaultPort = "8080"

// CamelRestComponentProperty ---
CamelRestComponentProperty = "camel.context.rest-configuration.component"
// CamelRestDefaultComponentMain ---
CamelRestDefaultComponentMain = "undertow"
// CamelRestDefaultComponentQuarkus ---
CamelRestDefaultComponentQuarkus = "platform-http"
)

// The Container trait can be used to configure properties of the container where the integration will run.
Expand Down Expand Up @@ -169,7 +181,7 @@ func (t *containerTrait) configureDependencies(e *Environment) {
}

if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
if capability, ok := e.CamelCatalog.Runtime.Capabilities["health"]; ok {
if capability, ok := e.CamelCatalog.Runtime.Capabilities[v1.CapabilityHealth]; ok {
for _, dependency := range capability.Dependencies {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID))
}
Expand Down Expand Up @@ -204,6 +216,10 @@ func (t *containerTrait) configureContainer(e *Environment) error {
t.configureService(e, &container)
}

if err := t.configureCapabilities(e); err != nil {
return err
}

//
// Deployment
//
Expand Down Expand Up @@ -414,6 +430,30 @@ func (t *containerTrait) configureResources(_ *Environment, container *corev1.Co
}
}
}

func (t *containerTrait) configureCapabilities(e *Environment) error {
if !util.StringSliceExists(e.Integration.Status.Capabilities, v1.CapabilityRest) {
return nil
}

if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}

switch e.CamelCatalog.Runtime.Provider {
case v1.RuntimeProviderMain:
e.ApplicationProperties[CamelRestPortProperty] = CamelRestDefaultPort
e.ApplicationProperties[CamelRestComponentProperty] = CamelRestDefaultComponentMain
case v1.RuntimeProviderQuarkus:
// On quarkus, the rest endpoint is bound to the platform http service
e.ApplicationProperties[CamelRestComponentProperty] = CamelRestDefaultComponentQuarkus
default:
return fmt.Errorf("unsupported runtime: %s", e.CamelCatalog.Runtime.Provider)
}

return nil
}

func (t *containerTrait) configureProbes(e *Environment, container *corev1.Container, port int, path string) error {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
Expand Down
12 changes: 2 additions & 10 deletions pkg/trait/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package trait
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -129,7 +128,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

if _, ok := e.CamelCatalog.Runtime.Capabilities["cron"]; !ok {
if _, ok := e.CamelCatalog.Runtime.Capabilities[v1.CapabilityCron]; !ok {
e.Integration.Status.SetCondition(
v1.IntegrationConditionCronJobAvailable,
corev1.ConditionFalse,
Expand Down Expand Up @@ -234,14 +233,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) {

func (t *cronTrait) Apply(e *Environment) error {
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)
}
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityCron)

if t.Fallback != nil && *t.Fallback {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, genericCronComponentFallback)
Expand Down
18 changes: 2 additions & 16 deletions pkg/trait/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package trait

import (
"context"
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -286,13 +285,7 @@ func TestCronDeps(t *testing.T) {
ct := environment.GetTrait("cron").(*cronTrait)
assert.NotNil(t, ct)
assert.Nil(t, ct.Fallback)

capability, ok := environment.CamelCatalog.Runtime.Capabilities["cron"]
assert.True(t, ok)

for _, dependency := range capability.Dependencies {
assert.True(t, util.StringSliceExists(environment.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID)))
}
assert.True(t, util.StringSliceExists(environment.Integration.Status.Capabilities, v1.CapabilityCron))
}

func TestCronDepsFallback(t *testing.T) {
Expand Down Expand Up @@ -367,14 +360,7 @@ func TestCronDepsFallback(t *testing.T) {
ct := environment.GetTrait("cron").(*cronTrait)
assert.NotNil(t, ct)
assert.NotNil(t, ct.Fallback)

capability, ok := environment.CamelCatalog.Runtime.Capabilities["cron"]
assert.True(t, ok)

for _, dependency := range capability.Dependencies {
assert.True(t, util.StringSliceExists(environment.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID)))
}

assert.True(t, util.StringSliceExists(environment.Integration.Status.Capabilities, v1.CapabilityCron))
assert.True(t, util.StringSliceExists(environment.Integration.Status.Dependencies, genericCronComponentFallback))
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/trait/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,24 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
}
}
}

meta.RequiredCapabilities.Each(func(item string) bool {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, item)
return true
})
}

for _, dependency := range e.Integration.Spec.Dependencies {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, dependency)
}

// add runtime specific dependencies
for _, capability := range e.Integration.Status.Capabilities {
for _, dependency := range e.CamelCatalog.Runtime.CapabilityDependencies(capability) {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID))
}
}

// add dependencies back to integration
dependencies.Each(func(item string) bool {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, item)
Expand Down
87 changes: 86 additions & 1 deletion pkg/trait/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func TestIntegrationAutoGeneratedDeps(t *testing.T) {
[]string{
"camel:direct",
"camel:log",
"camel:rest",
"mvn:org.apache.camel/camel-rest",
"mvn:org.apache.camel/camel-undertow",
"mvn:org.apache.camel.k/camel-k-loader-java",
"mvn:org.apache.camel.k/camel-k-loader-xml",
"mvn:org.apache.camel.k/camel-k-runtime-main"},
Expand Down Expand Up @@ -238,3 +239,87 @@ func TestIntegrationCustomLoader(t *testing.T) {
e.Integration.Status.Dependencies,
)
}

func TestRestDeps(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

e := &Environment{
Catalog: NewEnvironmentTestCatalog(),
CamelCatalog: catalog,
Integration: &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "flow.java",
Content: `rest().to("log:bar");`,
},
Language: v1.LanguageJavaSource,
},
},
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
}

trait := newDependenciesTrait()
enabled, err := trait.Configure(e)
assert.Nil(t, err)
assert.True(t, enabled)

err = trait.Apply(e)
assert.Nil(t, err)
assert.Subset(
t,
e.Integration.Status.Dependencies,
[]string{
"mvn:org.apache.camel/camel-rest",
"mvn:org.apache.camel/camel-undertow",
},
)
}

func TestRestDepsQuarkus(t *testing.T) {
catalog, err := camel.QuarkusCatalog()
assert.Nil(t, err)

e := &Environment{
Catalog: NewEnvironmentTestCatalog(),
CamelCatalog: catalog,
Integration: &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "flow.java",
Content: `rest().route().to("log:bar");`,
},
Language: v1.LanguageJavaSource,
},
},
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
}

trait := newDependenciesTrait()
enabled, err := trait.Configure(e)
assert.Nil(t, err)
assert.True(t, enabled)

err = trait.Apply(e)
assert.Nil(t, err)
assert.Subset(
t,
e.Integration.Status.Dependencies,
[]string{
"mvn:org.apache.camel.quarkus/camel-quarkus-rest",
"mvn:org.apache.camel.quarkus/camel-quarkus-platform-http",
},
)
}
Loading