You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2019/01/04 09:12:02 UTC
[camel-k] 01/03: Avoid changing the spec field if not necessary
This is an automated email from the ASF dual-hosted git repository.
nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 920d3ae2cb0d6c93270fb0bd3eb9908d3824bb12
Author: nferraro <ni...@gmail.com>
AuthorDate: Mon Dec 24 15:05:37 2018 +0100
Avoid changing the spec field if not necessary
---
pkg/apis/camel/v1alpha1/types.go | 11 ++++++
pkg/apis/camel/v1alpha1/types_support.go | 13 +++++++
.../camel/v1alpha1/types_support_test.go} | 19 ++++-----
pkg/metadata/http.go | 2 +-
pkg/metadata/languages.go | 45 ----------------------
pkg/metadata/metadata.go | 9 +----
pkg/metadata/types.go | 4 --
pkg/stub/action/integration/initialize.go | 14 +------
pkg/trait/dependencies.go | 2 +-
pkg/trait/deployment.go | 6 +--
pkg/trait/knative.go | 4 +-
11 files changed, 41 insertions(+), 88 deletions(-)
diff --git a/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go
index 9951e3f..6df4c30 100644
--- a/pkg/apis/camel/v1alpha1/types.go
+++ b/pkg/apis/camel/v1alpha1/types.go
@@ -137,6 +137,17 @@ const (
LanguageYamlFlow Language = "flow"
)
+// Languages is the list of all supported languages
+var Languages = []Language{
+ LanguageJavaSource,
+ LanguageJavaClass,
+ LanguageJavaScript,
+ LanguageGroovy,
+ LanguageJavaScript,
+ LanguageKotlin,
+ LanguageYamlFlow,
+}
+
// A IntegrationTraitSpec contains the configuration of a trait
type IntegrationTraitSpec struct {
Configuration map[string]string `json:"configuration,omitempty"`
diff --git a/pkg/apis/camel/v1alpha1/types_support.go b/pkg/apis/camel/v1alpha1/types_support.go
index 95a842f..861223b 100644
--- a/pkg/apis/camel/v1alpha1/types_support.go
+++ b/pkg/apis/camel/v1alpha1/types_support.go
@@ -138,3 +138,16 @@ func (flows Flows) Serialize() (string, error) {
}
return string(res), nil
}
+
+// InferLanguage returns the language of the source or discovers it from file extension if not set
+func (s SourceSpec) InferLanguage() Language {
+ if s.Language != "" {
+ return s.Language
+ }
+ for _, l := range Languages {
+ if strings.HasSuffix(s.Name, "."+string(l)) {
+ return l
+ }
+ }
+ return ""
+}
diff --git a/pkg/metadata/metadata_languages_test.go b/pkg/apis/camel/v1alpha1/types_support_test.go
similarity index 71%
rename from pkg/metadata/metadata_languages_test.go
rename to pkg/apis/camel/v1alpha1/types_support_test.go
index 8a11a56..ebfbc7e 100644
--- a/pkg/metadata/metadata_languages_test.go
+++ b/pkg/apis/camel/v1alpha1/types_support_test.go
@@ -15,32 +15,29 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package metadata
+package v1alpha1
import (
"testing"
- "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/stretchr/testify/assert"
)
func TestLanguageJavaSource(t *testing.T) {
- code := v1alpha1.SourceSpec{
- DataSpec: v1alpha1.DataSpec{
+ code := SourceSpec{
+ DataSpec: DataSpec{
Name: "Request.java",
},
}
- meta := Extract(code)
- assert.Equal(t, v1alpha1.LanguageJavaSource, meta.Language)
+ assert.Equal(t, LanguageJavaSource, code.InferLanguage())
}
func TestLanguageAlreadySet(t *testing.T) {
- code := v1alpha1.SourceSpec{
- DataSpec: v1alpha1.DataSpec{
+ code := SourceSpec{
+ DataSpec: DataSpec{
Name: "Request.java",
},
- Language: v1alpha1.LanguageJavaScript,
+ Language: LanguageJavaScript,
}
- meta := Extract(code)
- assert.Equal(t, v1alpha1.LanguageJavaScript, meta.Language)
+ assert.Equal(t, LanguageJavaScript, code.InferLanguage())
}
diff --git a/pkg/metadata/http.go b/pkg/metadata/http.go
index 2feeecb..9ca6a30 100644
--- a/pkg/metadata/http.go
+++ b/pkg/metadata/http.go
@@ -87,7 +87,7 @@ func getURIPrefix(uri string) string {
}
func hasRestIndicator(source v1alpha1.SourceSpec) bool {
- pat := getRestIndicatorRegexpsForLanguage(source.Language)
+ pat := getRestIndicatorRegexpsForLanguage(source.InferLanguage())
return pat.MatchString(source.Content)
}
diff --git a/pkg/metadata/languages.go b/pkg/metadata/languages.go
deleted file mode 100644
index 55389dc..0000000
--- a/pkg/metadata/languages.go
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements. See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package metadata
-
-import (
- "strings"
-
- "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-)
-
-// discoverLanguage discovers the code language from file extension if not set
-func discoverLanguage(source v1alpha1.SourceSpec) v1alpha1.Language {
- if source.Language != "" {
- return source.Language
- }
- for _, l := range []v1alpha1.Language{
- v1alpha1.LanguageJavaSource,
- v1alpha1.LanguageJavaClass,
- v1alpha1.LanguageJavaScript,
- v1alpha1.LanguageGroovy,
- v1alpha1.LanguageJavaScript,
- v1alpha1.LanguageKotlin,
- v1alpha1.LanguageYamlFlow} {
-
- if strings.HasSuffix(source.Name, "."+string(l)) {
- return l
- }
- }
- return ""
-}
diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go
index 4f7d980..bbdacfd 100644
--- a/pkg/metadata/metadata.go
+++ b/pkg/metadata/metadata.go
@@ -28,7 +28,6 @@ import (
func ExtractAll(sources []v1alpha1.SourceSpec) IntegrationMetadata {
// neutral metadata
meta := IntegrationMetadata{
- Language: "",
Dependencies: []string{},
FromURIs: []string{},
ToURIs: []string{},
@@ -42,10 +41,6 @@ func ExtractAll(sources []v1alpha1.SourceSpec) IntegrationMetadata {
}
func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
- language := m2.Language
- if m1.Language != "" && m1.Language != language {
- language = ""
- }
deps := make(map[string]bool)
for _, d := range m1.Dependencies {
deps[d] = true
@@ -59,7 +54,6 @@ func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
}
sort.Strings(allDependencies)
return IntegrationMetadata{
- Language: language,
FromURIs: append(m1.FromURIs, m2.FromURIs...),
ToURIs: append(m1.ToURIs, m2.ToURIs...),
Dependencies: allDependencies,
@@ -70,7 +64,7 @@ func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
// Extract returns metadata information from the source code
func Extract(source v1alpha1.SourceSpec) IntegrationMetadata {
- language := discoverLanguage(source)
+ language := source.InferLanguage()
// TODO: handle error
fromURIs, _ := src.InspectorForLanguage(language).FromURIs(source)
// TODO:: handle error
@@ -79,7 +73,6 @@ func Extract(source v1alpha1.SourceSpec) IntegrationMetadata {
requiresHTTPService := requiresHTTPService(source, fromURIs)
passiveEndpoints := hasOnlyPassiveEndpoints(source, fromURIs)
return IntegrationMetadata{
- Language: language,
FromURIs: fromURIs,
ToURIs: toURIs,
Dependencies: dependencies,
diff --git a/pkg/metadata/types.go b/pkg/metadata/types.go
index 2874eaf..af81ef1 100644
--- a/pkg/metadata/types.go
+++ b/pkg/metadata/types.go
@@ -17,8 +17,6 @@ limitations under the License.
package metadata
-import "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-
// IntegrationMetadata contains aggregate metadata about all Camel routes in a integrations
type IntegrationMetadata struct {
// All starting URIs of defined routes
@@ -27,8 +25,6 @@ type IntegrationMetadata struct {
ToURIs []string
// All inferred dependencies required to run the integration
Dependencies []string
- // The language in which the integration is written
- Language v1alpha1.Language
// RequiresHTTPService indicates if the integration needs to be invoked through HTTP
RequiresHTTPService bool
// PassiveEndpoints indicates that the integration contains only passive endpoints that are activated from
diff --git a/pkg/stub/action/integration/initialize.go b/pkg/stub/action/integration/initialize.go
index eea1957..b9ca930 100644
--- a/pkg/stub/action/integration/initialize.go
+++ b/pkg/stub/action/integration/initialize.go
@@ -19,7 +19,6 @@ package integration
import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
- "github.com/apache/camel-k/pkg/metadata"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/digest"
@@ -54,18 +53,7 @@ func (action *initializeAction) Handle(integration *v1alpha1.Integration) error
}
target := integration.DeepCopy()
- // set default values
- if target.Spec.Replicas == nil {
- var defaultReplicas int32 = 1
- target.Spec.Replicas = &defaultReplicas
- }
- for i := range target.Spec.Sources {
- // extract metadata
- s := &target.Spec.Sources[i]
-
- meta := metadata.Extract(*s)
- s.Language = meta.Language
- }
+ // better not changing the spec section of the target because it may be used for comparison by a higher level controller (e.g. Knative source controller)
// execute custom initialization
if _, err := trait.Apply(target, nil); err != nil {
diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go
index eddee8b..79862a6 100644
--- a/pkg/trait/dependencies.go
+++ b/pkg/trait/dependencies.go
@@ -49,7 +49,7 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
for _, s := range e.Integration.Spec.Sources {
meta := metadata.Extract(s)
- switch meta.Language {
+ switch s.InferLanguage() {
case v1alpha1.LanguageGroovy:
util.StringSliceUniqueAdd(&e.Integration.Spec.Dependencies, "runtime:groovy")
case v1alpha1.LanguageKotlin:
diff --git a/pkg/trait/deployment.go b/pkg/trait/deployment.go
index ec53ac3..63790c6 100644
--- a/pkg/trait/deployment.go
+++ b/pkg/trait/deployment.go
@@ -142,7 +142,7 @@ func (t *deploymentTrait) getConfigMapsFor(e *Environment) []runtime.Object {
"camel.apache.org/integration": e.Integration.Name,
},
Annotations: map[string]string{
- "camel.apache.org/source.language": string(s.Language),
+ "camel.apache.org/source.language": string(s.InferLanguage()),
"camel.apache.org/source.name": s.Name,
"camel.apache.org/source.compression": strconv.FormatBool(s.Compression),
},
@@ -207,8 +207,8 @@ func (t *deploymentTrait) getSources(e *Environment) []string {
src = "file:" + src
params := make([]string, 0)
- if s.Language != "" {
- params = append(params, "language="+string(s.Language))
+ if s.InferLanguage() != "" {
+ params = append(params, "language="+string(s.InferLanguage()))
}
if s.Compression {
params = append(params, "compression=true")
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 977d6ad..4157185 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -142,8 +142,8 @@ func (t *knativeTrait) getServiceFor(e *Environment) *serving.Service {
envvar.SetVal(&environment, envName, s.Content)
params := make([]string, 0)
- if s.Language != "" {
- params = append(params, "language="+string(s.Language))
+ if s.InferLanguage() != "" {
+ params = append(params, "language="+string(s.InferLanguage()))
}
if s.Compression {
params = append(params, "compression=true")