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

[Backport 1.12.x] fix(builder): native from source should rebuild #4161

Merged
merged 4 commits into from
Mar 28, 2023
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
28 changes: 28 additions & 0 deletions e2e/namespace/native/files/Java2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.apache.camel.builder.RouteBuilder;

public class Java2 extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:tick")
.setHeader("m").constant("string!")
.setBody().simple("Magic2${header.m}")
.log("Java ${body}");
}
}
28 changes: 28 additions & 0 deletions e2e/namespace/native/files/yaml2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# ---------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ---------------------------------------------------------------------------

- from:
uri: "timer:yaml"
parameters:
period: "5000"
steps:
- set-header:
name: "m"
constant: "string!"
- set-body:
simple: "Magic${header.m}2"
- to: "log:info"
39 changes: 19 additions & 20 deletions e2e/namespace/native/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,10 @@ func TestNativeIntegrations(t *testing.T) {
Expect(Kamel("delete", name, "-n", ns).Execute()).To(Succeed())
})

t.Run("warm up before native build testing", func(t *testing.T) {
// The following native build test is under tight time constraints, so here it runs
// a warm up testing to make sure necessary jars are already downloaded.
name := "warm-up-yaml"
Expect(KamelRunWithID(operatorID, ns, "files/yaml.yaml", "--name", name).Execute()).To(Succeed())

Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))

// Clean up
Expect(Kamel("delete", name, "-n", ns).Execute()).To(Succeed())
Expect(DeleteKits(ns)).To(Succeed())
})

t.Run("automatic rollout deployment from fast-jar to native kit", func(t *testing.T) {
name := "jvm-to-native"
// Let's make sure we start from a clean state
Expect(DeleteKits(ns)).To(Succeed())
name := "yaml-native"
Expect(KamelRunWithID(operatorID, ns, "files/yaml.yaml", "--name", name,
"-t", "quarkus.package-type=fast-jar",
"-t", "quarkus.package-type=native",
Expand Down Expand Up @@ -143,11 +129,24 @@ func TestNativeIntegrations(t *testing.T) {

Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))

t.Run("yaml native should not rebuild", func(t *testing.T) {
name := "yaml-native-2"
Expect(KamelRunWithID(operatorID, ns, "files/yaml2.yaml", "--name", name,
"-t", "quarkus.package-type=native",
).Execute()).To(Succeed())

// This one should run quickly as it suppose to reuse an IntegrationKit
Eventually(IntegrationPodPhase(ns, name), TestTimeoutShort).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPod(ns, name), TestTimeoutShort).
Should(WithTransform(getContainerCommand(), MatchRegexp(".*camel-k-integration-\\d+\\.\\d+\\.\\d+[-A-Za-z]*-runner.*")))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!2"))
Eventually(IntegrationKit(ns, "yaml-native-2")).Should(Equal(IntegrationKit(ns, "yaml-native")()))
})

// Clean up
Expect(Kamel("delete", name, "-n", ns).Execute()).To(Succeed())
})

// Clean up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
}
32 changes: 31 additions & 1 deletion e2e/namespace/native/native_with_sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,39 @@ func TestNativeHighMemoryIntegrations(t *testing.T) {
Should(WithTransform(getContainerCommand(), MatchRegexp(".*camel-k-integration-\\d+\\.\\d+\\.\\d+[-A-Za-z]*-runner.*")))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))

Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Java Magicstring!"))

t.Run("java native same should not rebuild", func(t *testing.T) {
name := "java-native-clone"
Expect(KamelRunWithID(operatorID, ns, "files/Java.java", "--name", name,
"-t", "quarkus.package-type=native",
).Execute()).To(Succeed())

// This one should run quickly as it suppose to reuse an IntegrationKit
Eventually(IntegrationPodPhase(ns, name), TestTimeoutShort).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPod(ns, name), TestTimeoutShort).
Should(WithTransform(getContainerCommand(), MatchRegexp(".*camel-k-integration-\\d+\\.\\d+\\.\\d+[-A-Za-z]*-runner.*")))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Java Magicstring!"))
Eventually(IntegrationKit(ns, "java-native-clone")).Should(Equal(IntegrationKit(ns, "java-native")()))
})

t.Run("java native should rebuild", func(t *testing.T) {
name := "java-native-2"
Expect(KamelRunWithID(operatorID, ns, "files/Java2.java", "--name", name,
"-t", "quarkus.package-type=native",
).Execute()).To(Succeed())

Eventually(IntegrationPodPhase(ns, name), TestTimeoutVeryLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPod(ns, name), TestTimeoutShort).
Should(WithTransform(getContainerCommand(), MatchRegexp(".*camel-k-integration-\\d+\\.\\d+\\.\\d+[-A-Za-z]*-runner.*")))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Java Magic2string!"))
Eventually(IntegrationKit(ns, "java-native-2")).ShouldNot(Equal(IntegrationKit(ns, "java-native")()))
})

// Clean up
Expect(Kamel("delete", name, "-n", ns).Execute()).To(Succeed())
})
Expand Down
25 changes: 25 additions & 0 deletions pkg/controller/integration/kits.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ func integrationMatches(integration *v1.Integration, kit *v1.IntegrationKit) (bo
ilog.Debug("Integration and integration-kit dependencies do not match", "integration", integration.Name, "integration-kit", kit.Name, "namespace", integration.Namespace)
return false, nil
}
// If IntegrationKit has any source, we must verify that it corresponds with the one in the Integration.
// This is important in case of Native builds as we need to rebuild when language requires a source during build.
if (kit.Spec.Sources != nil && len(kit.Spec.Sources) > 0) && !hasMatchingSources(integration, kit) {
ilog.Debug("Integration and integration-kit sources do not match", "integration", integration.Name, "integration-kit", kit.Name, "namespace", integration.Namespace)
return false, nil
}

ilog.Debug("Matched Integration and integration-kit", "integration", integration.Name, "integration-kit", kit.Name, "namespace", integration.Namespace)
return true, nil
Expand Down Expand Up @@ -250,3 +256,22 @@ func matchesTrait(it map[string]interface{}, kt map[string]interface{}) bool {
// perform exact match on the two trait maps
return reflect.DeepEqual(it, kt)
}

func hasMatchingSources(it *v1.Integration, kit *v1.IntegrationKit) bool {
if len(it.Sources()) != len(kit.Spec.Sources) {
return false
}
for _, itSource := range it.Sources() {
found := false
for _, ikSource := range kit.Spec.Sources {
if itSource.Content == ikSource.Content {
found = true
break
}
}
if !found {
return false
}
}
return true
}
97 changes: 97 additions & 0 deletions pkg/controller/integration/kits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,100 @@ func TestHasMatchingTraits_KitSameTraitShouldBePicked(t *testing.T) {
assert.Nil(t, err)
assert.True(t, ok)
}

func TestHasMatchingSources(t *testing.T) {
integration := &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

kit := &v1.IntegrationKit{
Spec: v1.IntegrationKitSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

hms := hasMatchingSources(integration, kit)
assert.True(t, hms)

kit2 := &v1.IntegrationKit{
Spec: v1.IntegrationKitSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content 2", v1.LanguageJavaShell),
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

hms2 := hasMatchingSources(integration, kit2)
assert.False(t, hms2)
}

func TestHasMatchingMultipleSources(t *testing.T) {
integration := &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
v1.NewSourceSpec("test", "some content 2", v1.LanguageJavaShell),
},
},
}

kit := &v1.IntegrationKit{
Spec: v1.IntegrationKitSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content 2", v1.LanguageJavaShell),
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

hms := hasMatchingSources(integration, kit)
assert.True(t, hms)

integration2 := &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

hms2 := hasMatchingSources(integration2, kit)
assert.False(t, hms2)
}

func TestHasNotMatchingSources(t *testing.T) {
integration := &v1.Integration{
Spec: v1.IntegrationSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content", v1.LanguageJavaShell),
},
},
}

kit := &v1.IntegrationKit{
Spec: v1.IntegrationKitSpec{
Sources: []v1.SourceSpec{
v1.NewSourceSpec("test", "some content 2", v1.LanguageJavaShell),
},
},
}

hsm := hasMatchingSources(integration, kit)
assert.False(t, hsm)

kit2 := &v1.IntegrationKit{
Spec: v1.IntegrationKitSpec{
Sources: []v1.SourceSpec{},
},
}

hsm2 := hasMatchingSources(integration, kit2)
assert.False(t, hsm2)
}
17 changes: 17 additions & 0 deletions pkg/resources/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.