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

Add support for capabilities #1318

Merged
merged 2 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2,949 changes: 2,949 additions & 0 deletions deploy/camel-catalog-1.1.1-SNAPSHOT-main.yaml

Large diffs are not rendered by default.

775 changes: 775 additions & 0 deletions deploy/camel-catalog-1.1.1-SNAPSHOT-quarkus.yaml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions deploy/resources.go

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions pkg/apis/camel/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ type MavenArtifact struct {

// RuntimeSpec --
type RuntimeSpec struct {
Version string `json:"version" yaml:"version"`
Provider RuntimeProvider `json:"provider" yaml:"provider"`
ApplicationClass string `json:"applicationClass" yaml:"applicationClass"`
Dependencies []MavenArtifact `json:"dependencies" yaml:"dependencies"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Version string `json:"version" yaml:"version"`
Provider RuntimeProvider `json:"provider" yaml:"provider"`
ApplicationClass string `json:"applicationClass" yaml:"applicationClass"`
Dependencies []MavenArtifact `json:"dependencies" yaml:"dependencies"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Capabilities map[string]Capability `json:"capabilities,omitempty" yaml:"capabilities,omitempty"`
}

// Capability --
type Capability struct {
Dependencies []MavenArtifact `json:"dependencies" yaml:"dependencies"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}

const (
Expand Down
28 changes: 28 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.

8 changes: 5 additions & 3 deletions pkg/trait/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ func (t *dependenciesTrait) Configure(e *Environment) (bool, error) {
}

func (t *dependenciesTrait) Apply(e *Environment) error {
if e.Integration.Spec.Dependencies != nil {
e.Integration.Status.Dependencies = strset.New(e.Integration.Spec.Dependencies...).List()
} else {
if e.Integration.Status.Dependencies == nil {
e.Integration.Status.Dependencies = make([]string, 0)
}

Expand Down Expand Up @@ -92,6 +90,10 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
}
}

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

// add dependencies back to integration
dependencies.Each(func(item string) bool {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, item)
Expand Down
39 changes: 26 additions & 13 deletions pkg/trait/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package trait

import (
"fmt"
"sort"
"strconv"

Expand Down Expand Up @@ -73,6 +74,11 @@ func newProbesTrait() Trait {

func (t *probesTrait) Configure(e *Environment) (bool, error) {
if t.Enabled != nil && *t.Enabled {
// check if the runtime provides 'health' capabilities
if _, ok := e.CamelCatalog.Runtime.Capabilities["health"]; !ok {
t.L.Infof("the runtime provider %s does not declare 'health' capability", e.CamelCatalog.Runtime.Provider)
}

return e.IntegrationInPhase(
v1.IntegrationPhaseInitialization,
v1.IntegrationPhaseDeploying,
Expand All @@ -84,21 +90,13 @@ func (t *probesTrait) Configure(e *Environment) (bool, error) {
}

func (t *probesTrait) Apply(e *Environment) error {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-health")

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

if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
//
// TODO: At the moment the servlet engine is used only for health but we need to
// have a dedicated servlet trait and maybe an option to create a dedicated
// server for management stuffs like health
//
v1.ConfigurationSpec{Type: "property", Value: "customizer.servlet.enabled=true"},
v1.ConfigurationSpec{Type: "property", Value: "customizer.servlet.bindHost=" + t.BindHost},
v1.ConfigurationSpec{Type: "property", Value: "customizer.servlet.bindPort=" + strconv.Itoa(t.BindPort)},
v1.ConfigurationSpec{Type: "property", Value: "customizer.inspector.enabled=true"},
v1.ConfigurationSpec{Type: "property", Value: "customizer.inspector.bind-host=" + t.BindHost},
v1.ConfigurationSpec{Type: "property", Value: "customizer.inspector.bind-port=" + strconv.Itoa(t.BindPort)},
v1.ConfigurationSpec{Type: "property", Value: "customizer.health.enabled=true"},
v1.ConfigurationSpec{Type: "property", Value: "customizer.health.path=" + t.Path},
)
Expand Down Expand Up @@ -155,3 +153,18 @@ func (t *probesTrait) newReadinessProbe() *corev1.Probe {

return &p
}

func (t *probesTrait) computeDependencies(e *Environment) {
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
return
}

if capability, ok := e.CamelCatalog.Runtime.Capabilities["health"]; 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)
}
}
46 changes: 44 additions & 2 deletions pkg/trait/probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package trait
import (
"testing"

"github.com/apache/camel-k/pkg/util/camel"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util/kubernetes"

Expand All @@ -31,7 +33,11 @@ import (
)

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

e := Environment{
CamelCatalog: catalog,
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
Expand All @@ -54,7 +60,38 @@ func TestProbesDeps(t *testing.T) {
assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-health")
}

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

e := Environment{
CamelCatalog: catalog,
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
}

enabled := true

tr := newProbesTrait().(*probesTrait)
tr.Enabled = &enabled
tr.BindPort = 9191

ok, err := tr.Configure(&e)
assert.Nil(t, err)
assert.True(t, ok)

err = tr.Apply(&e)
assert.Nil(t, err)
assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel.quarkus/camel-quarkus-microprofile-health")
}

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

target := appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Expand All @@ -68,7 +105,8 @@ func TestProbesOnDeployment(t *testing.T) {
}

e := Environment{
Resources: kubernetes.NewCollection(&target),
CamelCatalog: catalog,
Resources: kubernetes.NewCollection(&target),
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
Expand Down Expand Up @@ -99,6 +137,9 @@ func TestProbesOnDeployment(t *testing.T) {
}

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

target := serving.Service{
Spec: serving.ServiceSpec{
ConfigurationSpec: serving.ConfigurationSpec{
Expand All @@ -118,7 +159,8 @@ func TestProbesOnKnativeService(t *testing.T) {
}

e := Environment{
Resources: kubernetes.NewCollection(&target),
CamelCatalog: catalog,
Resources: kubernetes.NewCollection(&target),
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
Expand Down
26 changes: 26 additions & 0 deletions pkg/trait/rest-dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/apache/camel-k/pkg/util"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -59,6 +62,11 @@ func (t *restDslTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

// check if the runtime provides 'rest' capabilities
if _, ok := e.CamelCatalog.Runtime.Capabilities["rest"]; !ok {
t.L.Infof("the runtime provider %s does not declare 'rest' capability", e.CamelCatalog.Runtime.Provider)
}

for _, resource := range e.Integration.Spec.Resources {
if resource.Type == v1.ResourceTypeOpenAPI {
return e.IntegrationInPhase(v1.IntegrationPhaseInitialization), nil
Expand All @@ -73,6 +81,8 @@ func (t *restDslTrait) Apply(e *Environment) error {
return nil
}

t.computeDependencies(e)

root := os.TempDir()
tmpDir, err := ioutil.TempDir(root, "rest-dsl")
if err != nil {
Expand Down Expand Up @@ -240,3 +250,19 @@ func (t *restDslTrait) generateMavenProject(e *Environment) (maven.Project, erro

return p, nil
}

func (t *restDslTrait) computeDependencies(e *Environment) {
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
return
}

// check if the runtime provides 'rest' capabilities
if capability, ok := e.CamelCatalog.Runtime.Capabilities["rest"]; 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)
}
}
59 changes: 58 additions & 1 deletion pkg/trait/rest-dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ package trait
import (
"testing"

"github.com/apache/camel-k/pkg/util/camel"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"

"github.com/stretchr/testify/assert"
)

func TestRestDslTraitApplicability(t *testing.T) {
e := &Environment{}
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

e := &Environment{
CamelCatalog: catalog,
}

trait := newRestDslTrait()
enabled, err := trait.Configure(e)
Expand Down Expand Up @@ -55,3 +62,53 @@ func TestRestDslTraitApplicability(t *testing.T) {
assert.Nil(t, err)
assert.True(t, enabled)
}

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

e := &Environment{
CamelCatalog: catalog,
Integration: &v1.Integration{
Spec: v1.IntegrationSpec{
Resources: []v1.ResourceSpec{
{Type: v1.ResourceTypeOpenAPI},
},
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
}

trait := newRestDslTrait().(*restDslTrait)
trait.computeDependencies(e)

assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel/camel-rest")
assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel/camel-undertow")
}

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

e := &Environment{
CamelCatalog: catalog,
Integration: &v1.Integration{
Spec: v1.IntegrationSpec{
Resources: []v1.ResourceSpec{
{Type: v1.ResourceTypeOpenAPI},
},
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
}

trait := newRestDslTrait().(*restDslTrait)
trait.computeDependencies(e)

assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel.quarkus/camel-quarkus-rest")
assert.Contains(t, e.Integration.Status.Dependencies, "mvn:org.apache.camel.quarkus/camel-quarkus-platform-http")
}
2 changes: 1 addition & 1 deletion pkg/util/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
Version = "1.0.0-SNAPSHOT"

// DefaultRuntimeVersion --
DefaultRuntimeVersion = "1.1.0"
DefaultRuntimeVersion = "1.1.1-SNAPSHOT"

// BuildahVersion --
BuildahVersion = "1.14.0"
Expand Down
3 changes: 1 addition & 2 deletions script/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
VERSIONFILE := pkg/util/defaults/defaults.go
VERSION := 1.0.0-SNAPSHOT
LAST_RELEASED_VERSION := 1.0.0-RC2
RUNTIME_VERSION := 1.1.0
QUARKUS_VERSION := 1.2.0.Final
RUNTIME_VERSION := 1.1.1-SNAPSHOT
BUILDAH_VERSION := 1.14.0
KANIKO_VERSION := 0.17.1
BASE_IMAGE := adoptopenjdk/openjdk8:slim
Expand Down