git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] lburgazzoli closed pull request #251: runtime(springboot): set the exact list of dependencies used by spring boot instead of use everithing in the dependencies folder


lburgazzoli closed pull request #251: runtime(springboot): set the exact list of dependencies used by spring boot instead of use everithing in the dependencies folder
URL: https://github.com/apache/camel-k/pull/251
 
 
   

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/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go
index 901c0015..5626fef7 100644
--- a/pkg/apis/camel/v1alpha1/types.go
+++ b/pkg/apis/camel/v1alpha1/types.go
@@ -171,7 +171,7 @@ type IntegrationContextStatus struct {
 	Phase     IntegrationContextPhase `json:"phase,omitempty"`
 	Image     string                  `json:"image,omitempty"`
 	Digest    string                  `json:"digest,omitempty"`
-	Classpath []string                `json:"classpath,omitempty"`
+	Artifacts []Artifact              `json:"artifacts,omitempty"`
 }
 
 // IntegrationContextPhase --
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index 9ae3d88f..3086ad83 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -187,9 +187,9 @@ func (in *IntegrationContextSpec) DeepCopy() *IntegrationContextSpec {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *IntegrationContextStatus) DeepCopyInto(out *IntegrationContextStatus) {
 	*out = *in
-	if in.Classpath != nil {
-		in, out := &in.Classpath, &out.Classpath
-		*out = make([]string, len(*in))
+	if in.Artifacts != nil {
+		in, out := &in.Artifacts, &out.Artifacts
+		*out = make([]Artifact, len(*in))
 		copy(*out, *in)
 	}
 	return
diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go
index a04f3d81..163df7f8 100644
--- a/pkg/builder/builder.go
+++ b/pkg/builder/builder.go
@@ -27,6 +27,8 @@ import (
 	"sync/atomic"
 	"time"
 
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+
 	"github.com/sirupsen/logrus"
 )
 
@@ -192,9 +194,9 @@ func (b *defaultBuilder) submit(request Request) {
 		r.Status = StatusError
 	}
 
-	r.Classpath = make([]string, 0, len(c.Artifacts))
-	for _, l := range c.Artifacts {
-		r.Classpath = append(r.Classpath, l.ID)
+	r.Artifacts = make([]v1alpha1.Artifact, 0, len(c.Artifacts))
+	for _, artifact := range c.Artifacts {
+		r.Artifacts = append(r.Artifacts, artifact)
 	}
 
 	// update the cache
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index e0c34e71..74eb3787 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -151,10 +151,17 @@ func ComputeDependencies(ctx *Context) error {
 	}
 
 	for _, e := range cp["dependencies"] {
+		_, fileName := path.Split(e.Location)
+
+		gav, err := maven.ParseGAV(e.ID)
+		if err != nil {
+			return nil
+		}
+
 		ctx.Artifacts = append(ctx.Artifacts, v1alpha1.Artifact{
 			ID:       e.ID,
 			Location: e.Location,
-			Target:   "dependencies",
+			Target:   path.Join("dependencies", gav.GroupID+"."+fileName),
 		})
 	}
 
@@ -221,14 +228,10 @@ func packager(ctx *Context, selector ArtifactsSelector) error {
 	defer tarAppender.Close()
 
 	for _, entry := range selectedArtifacts {
-		gav, err := maven.ParseGAV(entry.ID)
-		if err != nil {
-			return err
-		}
+		_, tarFileName := path.Split(entry.Target)
+		tarFilePath := path.Dir(entry.Target)
 
-		_, fileName := path.Split(entry.Location)
-
-		_, err = tarAppender.AddFileWithName(gav.GroupID+"."+fileName, entry.Location, entry.Target)
+		_, err = tarAppender.AddFileWithName(tarFileName, entry.Location, tarFilePath)
 		if err != nil {
 			return err
 		}
@@ -237,12 +240,7 @@ func packager(ctx *Context, selector ArtifactsSelector) error {
 	if ctx.ComputeClasspath {
 		cp := ""
 		for _, entry := range ctx.Artifacts {
-			gav, err := maven.ParseGAV(entry.ID)
-			if err != nil {
-				return nil
-			}
-			_, fileName := path.Split(entry.Location)
-			cp += path.Join(entry.Target, gav.GroupID+"."+fileName) + "\n"
+			cp += path.Join(entry.Target) + "\n"
 		}
 
 		err = tarAppender.AppendData([]byte(cp), "classpath")
@@ -276,7 +274,7 @@ func ListPublishedImages(namespace string) ([]PublishedImage, error) {
 
 		images = append(images, PublishedImage{
 			Image:     ctx.Status.Image,
-			Classpath: ctx.Status.Classpath,
+			Artifacts: ctx.Status.Artifacts,
 		})
 	}
 	return images, nil
@@ -297,15 +295,15 @@ func FindBestImage(images []PublishedImage, entries []v1alpha1.Artifact) (*Publi
 	bestImageSurplusLibs := 0
 	for _, image := range images {
 		common := make(map[string]bool)
-		for _, id := range image.Classpath {
-			if _, ok := requiredLibs[id]; ok {
-				common[id] = true
+		for _, artifact := range image.Artifacts {
+			if _, ok := requiredLibs[artifact.ID]; ok {
+				common[artifact.ID] = true
 			}
 		}
 		numCommonLibs := len(common)
-		surplus := len(image.Classpath) - numCommonLibs
+		surplus := len(image.Artifacts) - numCommonLibs
 
-		if numCommonLibs != len(image.Classpath) && surplus >= numCommonLibs/3 {
+		if numCommonLibs != len(image.Artifacts) && surplus >= numCommonLibs/3 {
 			// Heuristic approach: if there are too many unrelated libraries, just use the base image
 			continue
 		}
diff --git a/pkg/builder/builder_types.go b/pkg/builder/builder_types.go
index a81f8791..1f970f0d 100644
--- a/pkg/builder/builder_types.go
+++ b/pkg/builder/builder_types.go
@@ -122,7 +122,7 @@ type Result struct {
 	Image     string
 	Error     error
 	Status    Status
-	Classpath []string
+	Artifacts []v1alpha1.Artifact
 	Task      Task
 }
 
@@ -144,7 +144,7 @@ type Context struct {
 // PublishedImage --
 type PublishedImage struct {
 	Image     string
-	Classpath []string
+	Artifacts []v1alpha1.Artifact
 }
 
 // Status --
diff --git a/pkg/builder/springboot/dependencies.go b/pkg/builder/springboot/dependencies.go
index 6c3aec13..e7f7e9e0 100644
--- a/pkg/builder/springboot/dependencies.go
+++ b/pkg/builder/springboot/dependencies.go
@@ -18,8 +18,11 @@ limitations under the License.
 package springboot
 
 import (
+	"path"
 	"strings"
 
+	"github.com/apache/camel-k/pkg/util/maven"
+
 	"github.com/apache/camel-k/pkg/builder"
 )
 
@@ -27,9 +30,16 @@ import (
 func ComputeDependencies(ctx *builder.Context) error {
 	for i := 0; i < len(ctx.Artifacts); i++ {
 		if strings.HasPrefix(ctx.Artifacts[i].ID, "org.apache.camel.k:camel-k-runtime-spring-boot:") {
+
+			_, fileName := path.Split(ctx.Artifacts[i].Location)
+			gav, err := maven.ParseGAV(ctx.Artifacts[i].ID)
+			if err != nil {
+				return nil
+			}
+
 			// Don't set a target so the jar will be copied to the
 			// deployment root
-			ctx.Artifacts[i].Target = ""
+			ctx.Artifacts[i].Target = gav.GroupID + "." + fileName
 		}
 	}
 
diff --git a/pkg/stub/action/context/build.go b/pkg/stub/action/context/build.go
index 4734bbc4..8f7a9bff 100644
--- a/pkg/stub/action/context/build.go
+++ b/pkg/stub/action/context/build.go
@@ -88,7 +88,16 @@ func (action *buildAction) Handle(context *v1alpha1.IntegrationContext) error {
 		target := context.DeepCopy()
 		target.Status.Image = res.Image
 		target.Status.Phase = v1alpha1.IntegrationContextPhaseReady
-		target.Status.Classpath = res.Classpath
+		target.Status.Artifacts = make([]v1alpha1.Artifact, 0, len(res.Artifacts))
+
+		for _, a := range res.Artifacts {
+			// do not include artifact location
+			target.Status.Artifacts = append(target.Status.Artifacts, v1alpha1.Artifact{
+				ID:       a.ID,
+				Location: "",
+				Target:   a.Target,
+			})
+		}
 
 		logrus.Info("Context ", target.Name, " transitioning to state ", v1alpha1.IntegrationContextPhaseReady)
 
diff --git a/pkg/trait/springboot.go b/pkg/trait/springboot.go
index 7069d67e..1314bfbb 100644
--- a/pkg/trait/springboot.go
+++ b/pkg/trait/springboot.go
@@ -19,6 +19,10 @@ package trait
 
 import (
 	"sort"
+	"strings"
+
+	"github.com/operator-framework/operator-sdk/pkg/sdk"
+	"github.com/pkg/errors"
 
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/builder"
@@ -80,6 +84,28 @@ func (trait *springBootTrait) apply(e *Environment) error {
 		// Override env vars
 		e.EnvVars["JAVA_MAIN_CLASS"] = "org.springframework.boot.loader.PropertiesLauncher"
 		e.EnvVars["LOADER_PATH"] = "/deployments/dependencies/"
+
+		if e.Integration.Spec.Context != "" {
+			name := e.Integration.Spec.Context
+			ctx := v1alpha1.NewIntegrationContext(e.Integration.Namespace, name)
+
+			if err := sdk.Get(&ctx); err != nil {
+				return errors.Wrapf(err, "unable to find integration context %s, %s", ctx.Name, err)
+			}
+
+			deps := make([]string, 0, len(ctx.Status.Artifacts))
+			for _, artifact := range ctx.Status.Artifacts {
+				if strings.HasPrefix(artifact.ID, "org.apache.camel.k:camel-k-runtime-spring-boot:") {
+					// do not include runner jar
+					continue
+				}
+
+				deps = append(deps, artifact.Target)
+			}
+
+			e.EnvVars["LOADER_HOME"] = "/deployments"
+			e.EnvVars["LOADER_PATH"] = strings.Join(deps, ",")
+		}
 	}
 
 	//
diff --git a/pkg/util/tar/appender.go b/pkg/util/tar/appender.go
index 7ffbfadd..f4fbecb1 100644
--- a/pkg/util/tar/appender.go
+++ b/pkg/util/tar/appender.go
@@ -92,7 +92,7 @@ func (t *Appender) AddFile(filePath string, tarDir string) (string, error) {
 	return fileName, nil
 }
 
-// AddFileWithName adds a file content to the tarDir, using the fiven file name.
+// AddFileWithName adds a file content to the tarDir, using the given file name.
 // It returns the full path of the file inside the tar.
 func (t *Appender) AddFileWithName(fileName string, filePath string, tarDir string) (string, error) {
 	info, err := os.Stat(filePath)


 

----------------------------------------------------------------
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:
users@xxxxxxxxxxxxxxxx


With regards,
Apache Git Services