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

Added some traits and full refactoring #203

Merged
merged 6 commits into from
Nov 5, 2018
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
2 changes: 0 additions & 2 deletions deploy/cr-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ kind: Integration
metadata:
name: example
spec:
dependencies:
- camel:groovy
source:
content: |-
// This is Camel K Groovy example route
Expand Down
13 changes: 13 additions & 0 deletions deploy/operator-role-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ rules:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
8 changes: 8 additions & 0 deletions deploy/operator-role-openshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
- route.openshift.io
resources:
- routes/custom-host
verbs:
- create

23 changes: 21 additions & 2 deletions deploy/resources.go

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

36 changes: 35 additions & 1 deletion docs/traits.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ The flag `--trait` can be also abbreviated with `-t`.
The `enabled` property is available on all traits and can be used to enable/disable them. All traits have their own
internal logic to determine if they need to be enabled when the user does not activate them explicitly.

All traits share also a `auto` property that can be used to enable/disable auto-configuration of the trait based on the
environment. The auto-configuration mechanism is able to enable/disable the trait when the `enabled` property is not explicitly
set by the user and also change the trait configuration. The `auto` property is enabled by default.

NOTE: Some traits are applicable only to specific platforms (see "profiles" in the table).

A trait may have additional properties that can be configured by the end user.
Expand Down Expand Up @@ -51,6 +55,13 @@ The following is a list of common traits that can be configured by the end users
|=======================
| Trait | Profiles | Description

| dependencies
| Kubernetes, OpenShift
| Automatically adds dependencies required by the Camel routes by inspecting the user code.
+
+
It's enabled by default.

| service
| Kubernetes, OpenShift
| Exposes the integration with a Service resource so that it can be accessed by other applications (or integrations) in the same namespace.
Expand All @@ -73,6 +84,29 @@ The following is a list of common traits that can be configured by the end users
+
It's enabled by default whenever a Service is added to the integration (through the `service` trait).

[cols="m,"]
!===

! route.host
! To configure the host exposed by the route.

!===

| ingress
| Kubernetes
| Exposes the service associated with the integration to the outside world with a Kubernetes Ingress.
+
+
It's enabled by default whenever a Service is added to the integration (through the `service` trait).

[cols="m,"]
!===

! ingress.host
! **Required**. To configure the host exposed by the ingress.

!===

|=======================


Expand All @@ -83,6 +117,6 @@ There are also platform traits that **normally should not be configured** by the
[options="header",cols="m,,"]
|=======================
| Trait | Profiles | Description
| base | Kubernetes, OpenShift | Creates the basic Kubernetes resource needed for running the integration.
| deployment | Kubernetes, OpenShift | Creates the basic Kubernetes resource needed for running the integration.
| owner | Kubernetes, OpenShift | Makes sure that every resource created by the traits belongs to the integration custom resource (so they are deleted when the integration is deleted).
|=======================
13 changes: 6 additions & 7 deletions pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ type Integration struct {

// IntegrationSpec --
type IntegrationSpec struct {
Replicas *int32 `json:"replicas,omitempty"`
Source SourceSpec `json:"source,omitempty"`
Context string `json:"context,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Traits map[string]IntegrationTraitSpec `json:"traits,omitempty"`
DependenciesAutoDiscovery *bool `json:"dependenciesAutoDiscovery,omitempty"`
Configuration []ConfigurationSpec `json:"configuration,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
Source SourceSpec `json:"source,omitempty"`
Context string `json:"context,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Traits map[string]IntegrationTraitSpec `json:"traits,omitempty"`
Configuration []ConfigurationSpec `json:"configuration,omitempty"`
}

// SourceSpec --
Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion pkg/client/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ __kamel_dependency_type() {
}

__kamel_traits() {
local type_list="` + strings.Join(trait.ComputeTraitsProperties(), " ") + `"
local type_list="` + strings.Join(trait.NewCatalog().ComputeTraitsProperties(), " ") + `"
COMPREPLY=( $( compgen -W "${type_list}" -- "$cur") )
compopt -o nospace
}
Expand Down
56 changes: 20 additions & 36 deletions pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
}

cmd.Flags().StringVarP(&options.Language, "language", "l", "", "Programming Language used to write the file")
cmd.Flags().StringVarP(&options.Runtime, "runtime", "r", "jvm", "Runtime used by the integration")
cmd.Flags().StringVarP(&options.Runtime, "runtime", "r", "", "Runtime used by the integration")
cmd.Flags().StringVar(&options.IntegrationName, "name", "", "The integration name")
cmd.Flags().StringSliceVarP(&options.Dependencies, "dependency", "d", nil, "The integration dependency")
cmd.Flags().BoolVarP(&options.Wait, "wait", "w", false, "Waits for the integration to be running")
Expand All @@ -74,7 +74,6 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
cmd.Flags().BoolVar(&options.Logs, "logs", false, "Print integration logs")
cmd.Flags().BoolVar(&options.Sync, "sync", false, "Synchronize the local source file with the cluster, republishing at each change")
cmd.Flags().BoolVar(&options.Dev, "dev", false, "Enable Dev mode (equivalent to \"-w --logs --sync\")")
cmd.Flags().BoolVar(&options.DependenciesAutoDiscovery, "auto-discovery", true, "Automatically discover Camel modules by analyzing user code")
cmd.Flags().StringSliceVarP(&options.Traits, "trait", "t", nil, "Configure a trait. E.g. \"-t service.enabled=false\"")
cmd.Flags().StringSliceVar(&options.LoggingLevels, "logging-level", nil, "Configure the logging level. E.g. \"--logging-level org.apache.camel=DEBUG\"")

Expand All @@ -86,21 +85,20 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {

type runCmdOptions struct {
*RootCmdOptions
IntegrationContext string
Language string
Runtime string
IntegrationName string
Dependencies []string
Properties []string
ConfigMaps []string
Secrets []string
Wait bool
Logs bool
Sync bool
Dev bool
DependenciesAutoDiscovery bool
Traits []string
LoggingLevels []string
IntegrationContext string
Language string
Runtime string
IntegrationName string
Dependencies []string
Properties []string
ConfigMaps []string
Secrets []string
Wait bool
Logs bool
Sync bool
Dev bool
Traits []string
LoggingLevels []string
}

func (o *runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
Expand All @@ -127,7 +125,8 @@ func (o *runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
}

func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
tp := trait.ComputeTraitsProperties()
catalog := trait.NewCatalog()
tp := catalog.ComputeTraitsProperties()
for _, t := range o.Traits {
kv := strings.SplitN(t, "=", 2)

Expand Down Expand Up @@ -272,10 +271,9 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
Content: code,
Language: v1alpha1.Language(o.Language),
},
Dependencies: make([]string, 0, len(o.Dependencies)),
DependenciesAutoDiscovery: &o.DependenciesAutoDiscovery,
Context: o.IntegrationContext,
Configuration: make([]v1alpha1.ConfigurationSpec, 0),
Dependencies: make([]string, 0, len(o.Dependencies)),
Context: o.IntegrationContext,
Configuration: make([]v1alpha1.ConfigurationSpec, 0),
},
}

Expand All @@ -289,24 +287,10 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
}
}

if o.Language == "groovy" || strings.HasSuffix(filename, ".groovy") {
util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:groovy")
}
if o.Language == "kotlin" || strings.HasSuffix(filename, ".kts") {
util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:kotlin")
}

// jvm runtime required by default
util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:jvm")

if o.Runtime != "" {
util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:"+o.Runtime)
}

switch o.Runtime {

}

for _, item := range o.Properties {
integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{
Type: "property",
Expand Down
62 changes: 13 additions & 49 deletions pkg/discover/dependencies.go → pkg/metadata/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,24 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package discover
package metadata

import (
"regexp"
"sort"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/camel"
)

var (
singleQuotedURI *regexp.Regexp
doubleQuotedURI *regexp.Regexp
)

func init() {
singleQuotedURI = regexp.MustCompile("'([a-z0-9-]+):[^']+'")
doubleQuotedURI = regexp.MustCompile("\"([a-z0-9-]+):[^\"]+\"")
}

// Dependencies returns a list of dependencies required by the given source code
func Dependencies(source v1alpha1.SourceSpec) []string {
// discoverDependencies returns a list of dependencies required by the given source code
func discoverDependencies(source v1alpha1.SourceSpec, fromURIs []string, toURIs []string) []string {
candidateMap := make(map[string]bool)
regexps := getRegexpsForLanguage(source.Language)
subMatches := findAllStringSubmatch(source.Content, regexps...)
for _, uriPrefix := range subMatches {
candidateComp := decodeComponent(uriPrefix)
uris := make([]string, 0, len(fromURIs)+len(toURIs))
uris = append(uris, fromURIs...)
uris = append(uris, toURIs...)
for _, uri := range uris {
candidateComp := decodeComponent(uri)
if candidateComp != "" {
candidateMap[candidateComp] = true
}
Expand All @@ -56,38 +46,12 @@ func Dependencies(source v1alpha1.SourceSpec) []string {
return candidateComponents
}

func getRegexpsForLanguage(language v1alpha1.Language) []*regexp.Regexp {
switch language {
case v1alpha1.LanguageJavaSource:
return []*regexp.Regexp{doubleQuotedURI}
case v1alpha1.LanguageXML:
return []*regexp.Regexp{doubleQuotedURI}
case v1alpha1.LanguageGroovy:
return []*regexp.Regexp{singleQuotedURI, doubleQuotedURI}
case v1alpha1.LanguageJavaScript:
return []*regexp.Regexp{singleQuotedURI, doubleQuotedURI}
case v1alpha1.LanguageKotlin:
return []*regexp.Regexp{doubleQuotedURI}
}
return []*regexp.Regexp{}
}

func findAllStringSubmatch(data string, regexps ...*regexp.Regexp) []string {
candidates := make([]string, 0)
for _, reg := range regexps {
hits := reg.FindAllStringSubmatch(data, -1)
for _, hit := range hits {
if hit != nil && len(hit) > 1 {
for _, match := range hit[1:] {
candidates = append(candidates, match)
}
}
}
func decodeComponent(uri string) string {
uriSplit := strings.SplitN(uri, ":", 2)
if len(uriSplit) < 2 {
return ""
}
return candidates
}

func decodeComponent(uriStart string) string {
uriStart := uriSplit[0]
if component := camel.Runtime.GetArtifactByScheme(uriStart); component != nil {
artifactID := component.ArtifactID
if strings.HasPrefix(artifactID, "camel-") {
Expand Down
5 changes: 2 additions & 3 deletions pkg/discover/doc.go → pkg/metadata/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package discover contains functions for extracting
// information from user code before compilation
package discover
// Package metadata contains tools to discover metadata from Camel routes
package metadata
Loading