nicolaferraro closed pull request #39: add support for non java integrations
URL: https://github.com/apache/camel-k/pull/39
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/README.md b/README.md
index 43c0db8..6c9dafc 100644
--- a/README.md
+++ b/README.md
@@ -55,10 +55,16 @@ For Minishift, this means executing `oc login -u
system:admin` then `kamel insta
After the initial setup, you can run a Camel integration on the cluster
executing:
```
-kamel run Sample.java
+kamel run runtime/examples/Sample.java
```
-A "Sample.java" file is included in the root of this repository. You can
change the content of the file and execute the command again to see the changes.
+A "Sample.java" file is included in the folder runtime/examples of this
repository. You can change the content of the file and execute the command
again to see the changes.
+
+A JavaScript integration has also been provided as example, to run it:
+
+```
+kamel run runtime/examples/routes.js
+```
### Monitoring the Status
diff --git a/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go
index efdd022..324d056 100644
--- a/pkg/apis/camel/v1alpha1/types.go
+++ b/pkg/apis/camel/v1alpha1/types.go
@@ -45,7 +45,9 @@ type IntegrationSpec struct {
}
type SourceSpec struct {
- Code *string `json:"code,omitempty"`
+ Name *string `json:"name,omitempty"`
+ Content *string `json:"content,omitempty"`
+ Language *string `json:"language,omitempty"`
}
type IntegrationStatus struct {
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index d29a234..8b16b1e 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -267,8 +267,13 @@ func (in *PropertySpec) DeepCopy() *PropertySpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver,
writing into out. in must be non-nil.
func (in *SourceSpec) DeepCopyInto(out *SourceSpec) {
*out = *in
- if in.Code != nil {
- in, out := &in.Code, &out.Code
+ if in.Name != nil {
+ in, out := &in.Name, &out.Name
+ *out = new(string)
+ **out = **in
+ }
+ if in.Content != nil {
+ in, out := &in.Content, &out.Content
*out = new(string)
**out = **in
}
diff --git a/pkg/build/api/types.go b/pkg/build/api/types.go
index 892e526..b94a9e7 100644
--- a/pkg/build/api/types.go
+++ b/pkg/build/api/types.go
@@ -20,7 +20,7 @@ package api
// a request to build a specific code
type BuildSource struct {
Identifier BuildIdentifier
- Code string
+ Code Code
}
type BuildIdentifier struct {
@@ -28,6 +28,11 @@ type BuildIdentifier struct {
Digest string
}
+type Code struct {
+ Name string
+ Content string
+}
+
// represents the result of a build
type BuildResult struct {
Source *BuildSource
diff --git a/pkg/build/build_manager_integration_test.go
b/pkg/build/build_manager_integration_test.go
index 932b315..11deca7 100644
--- a/pkg/build/build_manager_integration_test.go
+++ b/pkg/build/build_manager_integration_test.go
@@ -21,12 +21,13 @@ package build
import (
"context"
+ "testing"
+ "time"
+
build "github.com/apache/camel-k/pkg/build/api"
"github.com/apache/camel-k/pkg/util/digest"
"github.com/apache/camel-k/pkg/util/test"
"github.com/stretchr/testify/assert"
- "testing"
- "time"
)
func TestBuild(t *testing.T) {
@@ -38,7 +39,9 @@ func TestBuild(t *testing.T) {
}
buildManager.Start(build.BuildSource{
Identifier: identifier,
- Code: code(),
+ Code: build.Code{
+ Content: code(),
+ },
})
deadline := time.Now().Add(5 * time.Minute)
@@ -66,7 +69,9 @@ func TestFailedBuild(t *testing.T) {
}
buildManager.Start(build.BuildSource{
Identifier: identifier,
- Code: code() + "XX",
+ Code: build.Code{
+ Content: code() + "XX",
+ },
})
deadline := time.Now().Add(5 * time.Minute)
diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index daa5483..38a9812 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -127,12 +127,12 @@ func (b *localBuilder) execute(source build.BuildSource)
(string, error) {
},
},
},
- JavaSources: map[string]string{
- "kamel/Routes.java": source.Code,
+ Resources: map[string]string{
+ source.Code.Name: source.Code.Content,
},
Env: map[string]string{
"JAVA_MAIN_CLASS":
"org.apache.camel.k.jvm.Application",
- "CAMEL_K_ROUTES_URI": "classpath:kamel.Routes",
+ "CAMEL_K_ROUTES_URI": "classpath:" + source.Code.Name,
},
}
diff --git a/pkg/build/local/local_builder_integration_test.go
b/pkg/build/local/local_builder_integration_test.go
index 8ab1460..8c4f4a4 100644
--- a/pkg/build/local/local_builder_integration_test.go
+++ b/pkg/build/local/local_builder_integration_test.go
@@ -21,11 +21,12 @@ package local
import (
"context"
+ "testing"
+
build "github.com/apache/camel-k/pkg/build/api"
"github.com/apache/camel-k/pkg/util/digest"
"github.com/apache/camel-k/pkg/util/test"
"github.com/stretchr/testify/assert"
- "testing"
)
func TestBuild(t *testing.T) {
@@ -38,7 +39,9 @@ func TestBuild(t *testing.T) {
Name: "test0",
Digest: digest.Random(),
},
- Code: code(),
+ Code: build.Code{
+ Content: code(),
+ },
})
res := <-execution
@@ -56,7 +59,9 @@ func TestDoubleBuild(t *testing.T) {
Name: "test1",
Digest: digest.Random(),
},
- Code: code(),
+ Code: build.Code{
+ Content: code(),
+ },
})
execution2 := builder.Build(build.BuildSource{
@@ -64,7 +69,9 @@ func TestDoubleBuild(t *testing.T) {
Name: "test2",
Digest: digest.Random(),
},
- Code: code(),
+ Code: build.Code{
+ Content: code(),
+ },
})
res1 := <-execution1
@@ -84,7 +91,9 @@ func TestFailedBuild(t *testing.T) {
Name: "test3",
Digest: digest.Random(),
},
- Code: code() + "-",
+ Code: build.Code{
+ Content: code() + "-",
+ },
})
res := <-execution
diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index 889b326..5ec40d7 100644
--- a/pkg/client/cmd/run.go
+++ b/pkg/client/cmd/run.go
@@ -23,6 +23,7 @@ import (
"io/ioutil"
"os"
"strconv"
+ "strings"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/kubernetes"
@@ -81,6 +82,11 @@ func (o *RunCmdOptions) run(cmd *cobra.Command, args
[]string) error {
name = "integration"
}
+ codeName := args[0]
+ if idx := strings.LastIndexByte(args[0], os.PathSeparator); idx > -1 {
+ codeName = codeName[idx:]
+ }
+
integration := v1alpha1.Integration{
TypeMeta: v1.TypeMeta{
Kind: "Integration",
@@ -92,7 +98,9 @@ func (o *RunCmdOptions) run(cmd *cobra.Command, args
[]string) error {
},
Spec: v1alpha1.IntegrationSpec{
Source: v1alpha1.SourceSpec{
- Code: &code,
+ Name: &codeName,
+ Content: &code,
+ Language: &o.Language,
},
},
}
diff --git a/pkg/stub/action/integration/build.go
b/pkg/stub/action/integration/build.go
index 98b71d4..25747cc 100644
--- a/pkg/stub/action/integration/build.go
+++ b/pkg/stub/action/integration/build.go
@@ -54,7 +54,10 @@ func (b *BuildAction) Handle(integration
*v1alpha1.Integration) error {
if buildResult.Status == api.BuildStatusNotRequested {
b.buildManager.Start(api.BuildSource{
Identifier: buildIdentifier,
- Code: *integration.Spec.Source.Code, // FIXME
possible panic
+ Code: api.Code{
+ Name: *integration.Spec.Source.Name,
+ Content: *integration.Spec.Source.Content,
+ }, // FIXME possible panic
})
logrus.Info("Build started")
} else if buildResult.Status == api.BuildStatusError {
diff --git a/pkg/util/digest/digest.go b/pkg/util/digest/digest.go
index ad10e6c..09bec1c 100644
--- a/pkg/util/digest/digest.go
+++ b/pkg/util/digest/digest.go
@@ -20,10 +20,11 @@ package digest
import (
"crypto/sha256"
"encoding/base64"
- "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
- "github.com/apache/camel-k/version"
"math/rand"
"strconv"
+
+ "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+ "github.com/apache/camel-k/version"
)
// Compute a digest of the fields that are relevant for the deployment
@@ -33,8 +34,8 @@ func Compute(integration *v1alpha1.Integration) string {
// Operator version is relevant
hash.Write([]byte(version.Version))
// Integration relevant fields
- if integration.Spec.Source.Code != nil {
- hash.Write([]byte(*integration.Spec.Source.Code))
+ if integration.Spec.Source.Content != nil {
+ hash.Write([]byte(*integration.Spec.Source.Content))
}
// Add a letter at the beginning and use URL safe encoding
return "v" + base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index 262f275..f474f58 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -26,6 +26,8 @@ import (
"os"
"os/exec"
"path"
+ "regexp"
+ "strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -246,3 +248,28 @@ func pomFileContent(project Project) (string, error) {
return w.String(), nil
}
+
+func ParseGAV(gav string) (Dependency, error) {
+ //
<groupId>:<artifactId>[:<packagingType>[:<classifier>]]:(<version>|'?')
+ dep := Dependency{}
+ rex := regexp.MustCompile("([^: ]+):([^: ]+)(:([^: ]*)(:([^:
]+))?)?(:([^: ]+))?")
+ res := rex.FindStringSubmatch(gav)
+
+ dep.GroupId = res[1]
+ dep.ArtifactId = res[2]
+ dep.Type = "jar"
+
+ cnt := strings.Count(gav, ":")
+ if cnt == 2 {
+ dep.Version = res[4]
+ } else if cnt == 3 {
+ dep.Type = res[4]
+ dep.Version = res[6]
+ } else {
+ dep.Type = res[4]
+ dep.Classifier = res[6]
+ dep.Version = res[8]
+ }
+
+ return dep, nil
+}
diff --git a/pkg/util/maven/mavent_test.go b/pkg/util/maven/mavent_test.go
index 8a84910..3092e4a 100644
--- a/pkg/util/maven/mavent_test.go
+++ b/pkg/util/maven/mavent_test.go
@@ -67,3 +67,36 @@ func TestPomGeneration(t *testing.T) {
assert.Equal(t, pom, expectedPom)
}
+
+func TestParseSimpleGAV(t *testing.T) {
+ dep, err := ParseGAV("org.apache.camel:camel-core:2.21.1")
+
+ assert.Nil(t, err)
+ assert.Equal(t, dep.GroupId, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.Version, "2.21.1")
+ assert.Equal(t, dep.Type, "jar")
+ assert.Equal(t, dep.Classifier, "")
+}
+
+func TestParseGAVWithType(t *testing.T) {
+ dep, err := ParseGAV("org.apache.camel:camel-core:war:2.21.1")
+
+ assert.Nil(t, err)
+ assert.Equal(t, dep.GroupId, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.Version, "2.21.1")
+ assert.Equal(t, dep.Type, "war")
+ assert.Equal(t, dep.Classifier, "")
+}
+
+func TestParseGAVWithClassifierAndType(t *testing.T) {
+ dep, err := ParseGAV("org.apache.camel:camel-core:war:test:2.21.1")
+
+ assert.Nil(t, err)
+ assert.Equal(t, dep.GroupId, "org.apache.camel")
+ assert.Equal(t, dep.ArtifactId, "camel-core")
+ assert.Equal(t, dep.Version, "2.21.1")
+ assert.Equal(t, dep.Type, "war")
+ assert.Equal(t, dep.Classifier, "test")
+}
diff --git a/pkg/util/maven/types.go b/pkg/util/maven/types.go
index 68796aa..1ebebe0 100644
--- a/pkg/util/maven/types.go
+++ b/pkg/util/maven/types.go
@@ -17,7 +17,9 @@ limitations under the License.
package maven
-import "encoding/xml"
+import (
+ "encoding/xml"
+)
type ProjectDefinition struct {
Project Project
@@ -45,5 +47,17 @@ type Dependencies struct {
type Dependency struct {
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
- Version string `xml:"version"`
+ Version string `xml:"version,omitempty"`
+ Type string `xml:"type,omitempty"`
+ Classifier string `xml:"classifier,omitempty"`
+}
+
+func NewDependency(groupId string, artifactId string, version string)
Dependency {
+ return Dependency{
+ GroupId: groupId,
+ ArtifactId: artifactId,
+ Version: version,
+ Type: "jar",
+ Classifier: "",
+ }
}
diff --git a/Sample.java b/runtime/examples/Sample.java
similarity index 83%
rename from Sample.java
rename to runtime/examples/Sample.java
index 4ba8c88..cf1be6a 100644
--- a/Sample.java
+++ b/runtime/examples/Sample.java
@@ -1,14 +1,10 @@
-package kamel;
-
import org.apache.camel.builder.RouteBuilder;
-public class Routes extends RouteBuilder {
-
+public class Sample extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:tick")
.setBody(constant("-\n r\n o\n
c\nHello! Camel K\n s\n !\n"))
.to("log:info?skipBodyLineSeparator=false");
}
-
}
\ No newline at end of file
diff --git a/runtime/examples/routes.js b/runtime/examples/routes.js
new file mode 100644
index 0000000..a020511
--- /dev/null
+++ b/runtime/examples/routes.js
@@ -0,0 +1,34 @@
+
+// ****************
+//
+// Setup
+//
+// ****************
+
+l = components.get('log')
+l.exchangeFormatter = function(e) {
+ return "log - body=" + e.in.body + ", headers=" + e.in.headers
+}
+
+// ****************
+//
+// Functions
+//
+// ****************
+
+function proc(e) {
+ e.getIn().setHeader('RandomValue', Math.floor((Math.random() * 100) + 1))
+}
+
+// ****************
+//
+// Route
+//
+// ****************
+
+from('timer:js?period=1s')
+ .routeId('js')
+ .setBody()
+ .constant('Hello Camel K')
+ .process(proc)
+ .to('log:info')
\ No newline at end of file
diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
index 77cd3bc..48a3fb6 100644
--- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
+++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
@@ -95,7 +95,7 @@ public void configure() throws Exception {
// Exposed to the underlying script, but maybe better to
have
// a nice dsl
bindings.put("context", context);
- bindings.put("components", context);
+ bindings.put("components", new Components(context));
bindings.put("from", (Function<String, RouteDefinition>)
uri -> from(uri));
try (InputStream is = is(resource)) {
@@ -166,9 +166,12 @@ public static RoutesLoader loaderFor(String resource) {
private static InputStream is(String resource) throws IOException {
if (resource.startsWith(Application.SCHEME_CLASSPATH)) {
- return Application.class.getResourceAsStream(
- resource.substring(Application.SCHEME_CLASSPATH.length())
- );
+ String location = StringUtils.removeStart(resource,
Application.SCHEME_CLASSPATH);
+ if (!location.startsWith("/")) {
+ location = "/" + location;
+ }
+
+ return Application.class.getResourceAsStream(location);
} else {
return Files.newInputStream(
Paths.get(resource.substring(Application.SCHEME_FILE.length()))
diff --git
a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
index 4dcea3b..8701558 100644
--- a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
+++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
@@ -46,7 +46,7 @@ public void testLoadClass() throws Exception {
@Test
public void testLoadJava() throws Exception {
- String resource = "classpath:/MyRoutes.java";
+ String resource = "classpath:MyRoutes.java";
RoutesLoader loader = RouteLoaders.loaderFor(resource);
RouteBuilder builder = loader.load(resource);
@@ -63,7 +63,7 @@ public void testLoadJava() throws Exception {
@Test
public void testLoadJavaScript() throws Exception {
- String resource = "classpath:/routes.js";
+ String resource = "classpath:routes.js";
RoutesLoader loader = RouteLoaders.loaderFor(resource);
RouteBuilder builder = loader.load(resource);
@@ -80,7 +80,7 @@ public void testLoadJavaScript() throws Exception {
@Test
public void testLoadGroovy() throws Exception {
- String resource = "classpath:/routes.groovy";
+ String resource = "classpath:routes.groovy";
RoutesLoader loader = RouteLoaders.loaderFor(resource);
RouteBuilder builder = loader.load(resource);
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services