You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/04/03 15:56:10 UTC

[camel-k] 01/02: Include checksum when checking for compatible kits #743

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 8207098c23ad6c11279d6e533131101e1fe660d6
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Fri Apr 3 15:08:20 2020 +0200

    Include checksum when checking for compatible kits #743
---
 examples/Sample.java                   |  2 +-
 pkg/apis/camel/v1/common_types.go      |  1 +
 pkg/builder/builder_steps.go           | 20 +++++++++++++-------
 pkg/builder/runtime/main.go            | 15 +++++++++++++++
 pkg/builder/runtime/quarkus.go         | 25 +++++++++++++++++++++++++
 pkg/controller/integrationkit/build.go |  1 +
 pkg/util/digest/digest.go              | 22 ++++++++++++++++++++++
 7 files changed, 78 insertions(+), 8 deletions(-)

diff --git a/examples/Sample.java b/examples/Sample.java
index 0904fa4..a8223ac 100644
--- a/examples/Sample.java
+++ b/examples/Sample.java
@@ -21,6 +21,6 @@ public class Sample extends RouteBuilder {
   @Override
   public void configure() throws Exception {
 	  from("timer:tick")
-      .log("Hello Camel K!");
+        .log("Hello Camel K!");
   }
 }
\ No newline at end of file
diff --git a/pkg/apis/camel/v1/common_types.go b/pkg/apis/camel/v1/common_types.go
index f731aa8..7d45eea 100644
--- a/pkg/apis/camel/v1/common_types.go
+++ b/pkg/apis/camel/v1/common_types.go
@@ -33,6 +33,7 @@ type Artifact struct {
 	ID       string `json:"id" yaml:"id"`
 	Location string `json:"location,omitempty" yaml:"location,omitempty"`
 	Target   string `json:"target,omitempty" yaml:"target,omitempty"`
+	Checksum string `json:"checksum,omitempty" yaml:"checksum,omitempty"`
 }
 
 // Failure --
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index 0ecd89a..cef2dd6 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -371,9 +371,6 @@ func listPublishedImages(context *Context) ([]publishedImage, error) {
 		if kit.Status.Phase != v1.IntegrationKitPhaseReady {
 			continue
 		}
-		if kit.Status.Phase != v1.IntegrationKitPhaseReady {
-			continue
-		}
 
 		images = append(images, publishedImage{
 			Image:        kit.Status.Image,
@@ -391,9 +388,9 @@ func findBestImage(images []publishedImage, artifacts []v1.Artifact) (publishedI
 		return bestImage, nil
 	}
 
-	requiredLibs := make(map[string]bool, len(artifacts))
+	requiredLibs := make(map[string]string, len(artifacts))
 	for _, entry := range artifacts {
-		requiredLibs[entry.ID] = true
+		requiredLibs[entry.ID] = entry.Checksum
 	}
 
 	bestImageCommonLibs := make(map[string]bool)
@@ -402,7 +399,15 @@ func findBestImage(images []publishedImage, artifacts []v1.Artifact) (publishedI
 	for _, image := range images {
 		common := make(map[string]bool)
 		for _, artifact := range image.Artifacts {
-			if _, ok := requiredLibs[artifact.ID]; ok {
+			//
+			// If the Artifact's checksum is not defined we can't reliably determine if for some
+			// reason the artifact has been changed but not the ID (as example for snapshots or
+			// other generated jar) thus we do not take this artifact into account.
+			//
+			if artifact.Checksum == "" {
+				continue
+			}
+			if requiredLibs[artifact.ID] == artifact.Checksum {
 				common[artifact.ID] = true
 			}
 		}
@@ -411,7 +416,8 @@ func findBestImage(images []publishedImage, artifacts []v1.Artifact) (publishedI
 		surplus := len(image.Artifacts) - numCommonLibs
 
 		if numCommonLibs != len(image.Artifacts) && surplus >= numCommonLibs/3 {
-			// Heuristic approach: if there are too many unrelated libraries, just use the base image
+			// Heuristic approach: if there are too many unrelated libraries then this image is
+			// not suitable to be used as base image
 			continue
 		}
 
diff --git a/pkg/builder/runtime/main.go b/pkg/builder/runtime/main.go
index 343cfbb..991f548 100644
--- a/pkg/builder/runtime/main.go
+++ b/pkg/builder/runtime/main.go
@@ -22,6 +22,8 @@ import (
 	"io/ioutil"
 	"path"
 
+	"github.com/apache/camel-k/pkg/util/digest"
+
 	"github.com/pkg/errors"
 
 	yaml2 "gopkg.in/yaml.v2"
@@ -117,10 +119,23 @@ func computeDependencies(ctx *builder.Context) error {
 			return nil
 		}
 
+		//
+		// Compute the checksum if it has not been computed by the camel-k-maven-plugin
+		//
+		if e.Checksum == "" {
+			chksum, err := digest.ComputeSHA1(e.Location)
+			if err != nil {
+				return err
+			}
+
+			e.Checksum = "sha1:" + chksum
+		}
+
 		ctx.Artifacts = append(ctx.Artifacts, v1.Artifact{
 			ID:       e.ID,
 			Location: e.Location,
 			Target:   path.Join("dependencies", gav.GroupID+"."+fileName),
+			Checksum: e.Checksum,
 		})
 	}
 
diff --git a/pkg/builder/runtime/quarkus.go b/pkg/builder/runtime/quarkus.go
index aefc706..06655dc 100644
--- a/pkg/builder/runtime/quarkus.go
+++ b/pkg/builder/runtime/quarkus.go
@@ -23,6 +23,8 @@ import (
 	"os"
 	"path"
 
+	"github.com/apache/camel-k/pkg/util/digest"
+
 	yaml2 "gopkg.in/yaml.v2"
 
 	"github.com/pkg/errors"
@@ -178,18 +180,41 @@ func computeQuarkusDependencies(ctx *builder.Context) error {
 			return err
 		}
 
+		//
+		// Compute the checksum if it has not been computed by the camel-k-maven-plugin
+		//
+		if e.Checksum == "" {
+			chksum, err := digest.ComputeSHA1(e.Location)
+			if err != nil {
+				return err
+			}
+
+			e.Checksum = "sha1:" + chksum
+		}
+
 		ctx.Artifacts = append(ctx.Artifacts, v1.Artifact{
 			ID:       e.ID,
 			Location: e.Location,
 			Target:   path.Join("dependencies", gav.GroupID+"."+fileName),
+			Checksum: e.Checksum,
 		})
 	}
 
 	runner := "camel-k-integration-" + defaults.Version + "-runner.jar"
+
+	//
+	// Quarkus' runner checksum need to be recomputed each time
+	//
+	runnerChecksum, err := digest.ComputeSHA1(mc.Path, "target", runner)
+	if err != nil {
+		return err
+	}
+
 	ctx.Artifacts = append(ctx.Artifacts, v1.Artifact{
 		ID:       runner,
 		Location: path.Join(mc.Path, "target", runner),
 		Target:   path.Join("dependencies", runner),
+		Checksum: "sha1:" + runnerChecksum,
 	})
 
 	return nil
diff --git a/pkg/controller/integrationkit/build.go b/pkg/controller/integrationkit/build.go
index bed746f..c182e30 100644
--- a/pkg/controller/integrationkit/build.go
+++ b/pkg/controller/integrationkit/build.go
@@ -166,6 +166,7 @@ func (action *buildAction) handleBuildRunning(ctx context.Context, kit *v1.Integ
 				ID:       a.ID,
 				Location: "",
 				Target:   a.Target,
+				Checksum: a.Checksum,
 			})
 		}
 
diff --git a/pkg/util/digest/digest.go b/pkg/util/digest/digest.go
index 53469f1..4c0b7cf 100644
--- a/pkg/util/digest/digest.go
+++ b/pkg/util/digest/digest.go
@@ -18,8 +18,12 @@ limitations under the License.
 package digest
 
 import (
+	"crypto/sha1"
 	"crypto/sha256"
 	"encoding/base64"
+	"io"
+	"os"
+	"path"
 	"sort"
 	"strconv"
 
@@ -167,3 +171,21 @@ func sortedTraitSpecMapKeys(m map[string]v1.TraitSpec) []string {
 	sort.Strings(res)
 	return res
 }
+
+// ComputeSHA1 ---
+func ComputeSHA1(elem ...string) (string, error) {
+	file := path.Join(elem...)
+
+	f, err := os.Open(file)
+	if err != nil {
+		return "", err
+	}
+	defer f.Close()
+
+	h := sha1.New()
+	if _, err := io.Copy(h, f); err != nil {
+		return "", err
+	}
+
+	return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
+}