Skip to content

Commit

Permalink
add support for non java integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 11, 2018
1 parent bff423d commit a96e01e
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 38 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 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.

7 changes: 6 additions & 1 deletion pkg/build/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ package api
// a request to build a specific code
type BuildSource struct {
Identifier BuildIdentifier
Code string
Code Code
}

type BuildIdentifier struct {
Name string
Digest string
}

type Code struct {
Name string
Content string
}

// represents the result of a build
type BuildResult struct {
Source *BuildSource
Expand Down
13 changes: 9 additions & 4 deletions pkg/build/build_manager_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/build/local/local_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
19 changes: 14 additions & 5 deletions pkg/build/local/local_builder_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -38,7 +39,9 @@ func TestBuild(t *testing.T) {
Name: "test0",
Digest: digest.Random(),
},
Code: code(),
Code: build.Code{
Content: code(),
},
})

res := <-execution
Expand All @@ -56,15 +59,19 @@ func TestDoubleBuild(t *testing.T) {
Name: "test1",
Digest: digest.Random(),
},
Code: code(),
Code: build.Code{
Content: code(),
},
})

execution2 := builder.Build(build.BuildSource{
Identifier: build.BuildIdentifier{
Name: "test2",
Digest: digest.Random(),
},
Code: code(),
Code: build.Code{
Content: code(),
},
})

res1 := <-execution1
Expand All @@ -84,7 +91,9 @@ func TestFailedBuild(t *testing.T) {
Name: "test3",
Digest: digest.Random(),
},
Code: code() + "-",
Code: build.Code{
Content: code() + "-",
},
})

res := <-execution
Expand Down
10 changes: 9 additions & 1 deletion pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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,
},
},
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/stub/action/integration/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions pkg/util/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"os"
"os/exec"
"path"
"regexp"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions pkg/util/maven/mavent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
18 changes: 16 additions & 2 deletions pkg/util/maven/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.

package maven

import "encoding/xml"
import (
"encoding/xml"
)

type ProjectDefinition struct {
Project Project
Expand Down Expand Up @@ -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: "",
}
}
6 changes: 1 addition & 5 deletions Sample.java → runtime/examples/Sample.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
Loading

0 comments on commit a96e01e

Please sign in to comment.