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

Include checksum when checking for compatible kits #1389

Merged
merged 2 commits into from
Apr 3, 2020
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
2 changes: 1 addition & 1 deletion examples/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
5 changes: 3 additions & 2 deletions pkg/apis/camel/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -112,8 +113,8 @@ const (
CapabilityHealth = "health"
// CapabilityCron --
CapabilityCron = "cron"
// CapabilityPlatformHttp --
CapabilityPlatformHttp = "platform-http"
// CapabilityPlatformHTTP --
CapabilityPlatformHTTP = "platform-http"
)

// ResourceCondition is a common type for all conditions
Expand Down
20 changes: 13 additions & 7 deletions pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ func listPublishedImages(context *Context) ([]publishedImage, error) {
for _, item := range list.Items {
kit := item

if kit.Status.Phase != v1.IntegrationKitPhaseReady {
continue
}
if kit.Status.Phase != v1.IntegrationKitPhaseReady {
continue
}
Expand All @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/builder/runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/builder/runtime/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os"
"path"

"github.com/apache/camel-k/pkg/util/digest"

yaml2 "gopkg.in/yaml.v2"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/integrationkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (action *buildAction) handleBuildRunning(ctx context.Context, kit *v1.Integ
ID: a.ID,
Location: "",
Target: a.Target,
Checksum: a.Checksum,
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (t *containerTrait) configureCapabilities(e *Environment) error {
requiresHTTP = true
}

if util.StringSliceExists(e.Integration.Status.Capabilities, v1.CapabilityPlatformHttp) {
if util.StringSliceExists(e.Integration.Status.Capabilities, v1.CapabilityPlatformHTTP) {
requiresHTTP = true
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ func (t *knativeTrait) Configure(e *Environment) (bool, error) {

func (t *knativeTrait) Apply(e *Environment) error {
if len(t.ChannelSources) > 0 || len(t.EndpointSources) > 0 || len(t.EventSources) > 0 {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityPlatformHttp)
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityPlatformHTTP)
}
if len(t.ChannelSinks) > 0 || len(t.EndpointSinks) > 0 || len(t.EventSinks) > 0 {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityPlatformHttp)
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityPlatformHTTP)
}

if e.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/trait/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func TestKnativePlatformHttpConfig(t *testing.T) {
},
}

for _, source := range sources {
for _, ref := range sources {
source := ref
t.Run(source.Name, func(t *testing.T) {
environment := NewFakeEnvironment(t, source)

Expand All @@ -279,7 +280,7 @@ func TestKnativePlatformHttpConfig(t *testing.T) {
err = tc.apply(&environment)
assert.Nil(t, err)

assert.Contains(t, environment.Integration.Status.Capabilities, v1.CapabilityPlatformHttp)
assert.Contains(t, environment.Integration.Status.Capabilities, v1.CapabilityPlatformHTTP)
assert.Equal(t, "true", environment.ApplicationProperties["customizer.platform-http.enabled"])
assert.Equal(t, "8080", environment.ApplicationProperties["customizer.platform-http.bind-port"])
})
Expand Down Expand Up @@ -311,7 +312,8 @@ func TestKnativePlatformHttpDepdencies(t *testing.T) {
},
}

for _, source := range sources {
for _, ref := range sources {
source := ref
t.Run(source.Name, func(t *testing.T) {
environment := NewFakeEnvironment(t, source)
environment.Integration.Status.Phase = v1.IntegrationPhaseInitialization
Expand All @@ -327,7 +329,7 @@ func TestKnativePlatformHttpDepdencies(t *testing.T) {
err = tc.apply(&environment)
assert.Nil(t, err)

assert.Contains(t, environment.Integration.Status.Capabilities, v1.CapabilityPlatformHttp)
assert.Contains(t, environment.Integration.Status.Capabilities, v1.CapabilityPlatformHTTP)
assert.Contains(t, environment.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-http")
})
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ limitations under the License.
package digest

import (
// nolint: gosec
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"io"
"os"
"path"
"sort"
"strconv"

Expand Down Expand Up @@ -167,3 +172,22 @@ 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()

// nolint: gosec
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}