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 resources to an integration #300

Merged
merged 3 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions examples/resources-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the file body
11 changes: 11 additions & 0 deletions examples/resources-route.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// To run this integrations use:
//
// kamel run --resource examples/resources-data.txt examples/resources-route.groovy
//

from('timer:resources')
.routeId('resources')
.setBody()
.simple("resource:platform:resources-data.txt")
.log('file content is: ${body}')
26 changes: 21 additions & 5 deletions pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Integration struct {
type IntegrationSpec struct {
Replicas *int32 `json:"replicas,omitempty"`
Sources []SourceSpec `json:"sources,omitempty"`
Resources []ResourceSpec `json:"resources,omitempty"`
Context string `json:"context,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Profile TraitProfile `json:"profile,omitempty"`
Expand All @@ -65,14 +66,19 @@ type IntegrationSpec struct {

// AddSource --
func (is *IntegrationSpec) AddSource(name string, content string, language Language) {
is.Sources = append(is.Sources, SourceSpec{Name: name, Content: content, Language: language})
is.Sources = append(is.Sources, NewSourceSpec(name, content, language))
}

// AddSources --
func (is *IntegrationSpec) AddSources(sources ...SourceSpec) {
is.Sources = append(is.Sources, sources...)
}

// AddResources --
func (is *IntegrationSpec) AddResources(resources ...ResourceSpec) {
is.Resources = append(is.Resources, resources...)
}

// AddConfiguration --
func (is *IntegrationSpec) AddConfiguration(confType string, confValue string) {
is.Configuration = append(is.Configuration, ConfigurationSpec{
Expand All @@ -93,12 +99,22 @@ func (is *IntegrationSpec) AddDependency(dependency string) {
}
}

// DataSpec --
type DataSpec struct {
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Compression bool `json:"compression,omitempty"`
}

// ResourceSpec --
type ResourceSpec struct {
DataSpec
}

// SourceSpec --
type SourceSpec struct {
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Language Language `json:"language,omitempty"`
Compression bool `json:"compression,omitempty"`
DataSpec
Language Language `json:"language,omitempty"`
}

// Language --
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/camel/v1alpha1/types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ func (spec ConfigurationSpec) String() string {
//
// **********************************

// NewSourceSpec --
func NewSourceSpec(name string, content string, language Language) SourceSpec {
return SourceSpec{
DataSpec: DataSpec{
Name: name,
Content: content,
},
Language: language,
}
}

// NewResourceSpec --
func NewResourceSpec(name string, content string, destination string) ResourceSpec {
return ResourceSpec{
DataSpec: DataSpec{
Name: name,
Content: content,
},
}
}

// NewIntegrationPlatformList --
func NewIntegrationPlatformList() IntegrationPlatformList {
return IntegrationPlatformList{
Expand Down
39 changes: 39 additions & 0 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.

11 changes: 5 additions & 6 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@ func (b *defaultBuilder) submit(request Request) {
b.request.Store(request.Meta.Name, r)

c := Context{
C: b.ctx,
Path: builderPath,
Namespace: b.namespace,
Request: request,
ComputeClasspath: true,
Image: "fabric8/s2i-java:2.3", // TODO: externalize
C: b.ctx,
Path: builderPath,
Namespace: b.namespace,
Request: request,
Image: "fabric8/s2i-java:2.3", // TODO: externalize,
}

if request.Image != "" {
Expand Down
20 changes: 6 additions & 14 deletions pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func IncrementalPackager(ctx *Context) error {
return StandardPackager(ctx)
}

images, err := ListPublishedImages(ctx.Namespace)
images, err := ListPublishedImages(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -228,27 +228,16 @@ func packager(ctx *Context, selector ArtifactsSelector) error {
}
}

if ctx.ComputeClasspath && len(ctx.Artifacts) > 0 {
cp := ""
for _, entry := range ctx.Artifacts {
cp += entry.Target + "\n"
}

if err := tarAppender.AddData([]byte(cp), "classpath"); err != nil {
return err
}
}

ctx.Archive = tarFileName

return nil
}

// ListPublishedImages --
func ListPublishedImages(namespace string) ([]PublishedImage, error) {
func ListPublishedImages(context *Context) ([]PublishedImage, error) {
list := v1alpha1.NewIntegrationContextList()

err := sdk.List(namespace, &list, sdk.WithListOptions(&metav1.ListOptions{}))
err := sdk.List(context.Namespace, &list, sdk.WithListOptions(&metav1.ListOptions{}))
if err != nil {
return nil, err
}
Expand All @@ -257,6 +246,9 @@ func ListPublishedImages(namespace string) ([]PublishedImage, error) {
if ctx.Status.Phase != v1alpha1.IntegrationContextPhaseReady || ctx.Labels == nil {
continue
}
if context.ContextFilter != nil && !context.ContextFilter(&ctx) {
continue
}
if ctxType, present := ctx.Labels["camel.apache.org/context.type"]; !present || ctxType != v1alpha1.IntegrationContextTypePlatform {
continue
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/builder/builder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ type Context struct {
Artifacts []v1alpha1.Artifact
SelectedArtifacts []v1alpha1.Artifact
Archive string
ComputeClasspath bool
MainClass string
ContextFilter func(integrationContext *v1alpha1.IntegrationContext) bool
}

// HasRequiredImage --
Expand Down
16 changes: 13 additions & 3 deletions pkg/builder/springboot/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ limitations under the License.
package springboot

import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/builder"
)

// Initialize --
func Initialize(ctx *builder.Context) error {
// no need to compute classpath as we do use spring boot own
// loader: PropertiesLauncher
ctx.ComputeClasspath = false
// do not take into account any image that does not have spring-boot
// as required dependency to avoid picking up a base image with wrong
// classpath or layout
ctx.ContextFilter = func(context *v1alpha1.IntegrationContext) bool {
for _, i := range context.Spec.Dependencies {
if i == "runtime:spring" {
return true
}
}

return false
}

return nil
}
73 changes: 49 additions & 24 deletions pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
"E.g. \"--logging-level org.apache.camel=DEBUG\"")
cmd.Flags().StringVarP(&options.OutputFormat, "output", "o", "", "Output format. One of: json|yaml")
cmd.Flags().BoolVar(&options.Compression, "compression", false, "Enable store source as a compressed binary blob")
cmd.Flags().StringSliceVar(&options.Resources, "resource", nil, "Add a resource")

// completion support
configureKnownCompletions(&cmd)
Expand All @@ -104,6 +105,7 @@ type runCmdOptions struct {
IntegrationName string
Profile string
OutputFormat string
Resources []string
Dependencies []string
Properties []string
ConfigMaps []string
Expand Down Expand Up @@ -247,8 +249,8 @@ func (o *runCmdOptions) syncIntegration(sources []string) error {
return nil
}

func (o *runCmdOptions) createIntegration(args []string) (*v1alpha1.Integration, error) {
return o.updateIntegrationCode(args)
func (o *runCmdOptions) createIntegration(sources []string) (*v1alpha1.Integration, error) {
return o.updateIntegrationCode(sources)
}

func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integration, error) {
Expand Down Expand Up @@ -285,25 +287,32 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ
}

for _, source := range sources {
code, err := o.loadCode(source)
data, err := o.loadData(source, o.Compression)
if err != nil {
return nil, err
}

if o.Compression {
var b bytes.Buffer

if err := gzip.Compress(&b, []byte(code)); err != nil {
return nil, err
}
integration.Spec.AddSources(v1alpha1.SourceSpec{
DataSpec: v1alpha1.DataSpec{
Name: path.Base(source),
Content: data,
Compression: o.Compression,
},
})
}

code = base64.StdEncoding.EncodeToString(b.Bytes())
for _, resource := range o.Resources {
data, err := o.loadData(resource, o.Compression)
if err != nil {
return nil, err
}

integration.Spec.AddSources(v1alpha1.SourceSpec{
Name: path.Base(source),
Content: code,
Compression: o.Compression,
integration.Spec.AddResources(v1alpha1.ResourceSpec{
DataSpec: v1alpha1.DataSpec{
Name: path.Base(resource),
Content: data,
Compression: o.Compression,
},
})
}

Expand Down Expand Up @@ -381,23 +390,39 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ
return &integration, nil
}

func (*runCmdOptions) loadCode(fileName string) (string, error) {
func (*runCmdOptions) loadData(fileName string, compress bool) (string, error) {
var content []byte
var err error

if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") {
content, err := ioutil.ReadFile(fileName)
content, err = ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
} else {
resp, err := http.Get(fileName)
if err != nil {
return "", err
}
defer resp.Body.Close()

content, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(content), nil
}

resp, err := http.Get(fileName)
if err != nil {
return "", err
if compress {
var b bytes.Buffer

if err := gzip.Compress(&b, content); err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
return bodyString, err

return string(content), nil
}

func (*runCmdOptions) configureTrait(integration *v1alpha1.Integration, config string) error {
Expand Down
Loading