Skip to content

Commit

Permalink
feat(trait): enable any runtime version
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Mar 6, 2023
1 parent 7bc102b commit e366941
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
41 changes: 39 additions & 2 deletions e2e/namespace/install/catalog/catalog_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func TestCamelCatalogBuilder(t *testing.T) {
)

// Run an integration with a catalog not compatible
// The operator should create the catalog, but fail on reconciliation and the integration should fail as well
// The operator should create the catalog, but fail on reconciliation as it is not compatible
// and the integration should fail as well
t.Run("Run catalog not compatible", func(t *testing.T) {
name := "java"
nonCompatibleCatalogName := "camel-catalog-1.15.0-quarkus"
nonCompatibleCatalogName := "camel-catalog-1.15.0"
Expect(
KamelRunWithID(operatorID, ns, "../files/Java.java", "--name", name,
"-t", "camel.runtime-version=1.15.0",
Expand All @@ -78,8 +79,44 @@ func TestCamelCatalogBuilder(t *testing.T) {
// Clean up
Eventually(DeleteIntegrations(ns), TestTimeoutLong).Should(Equal(0))
})

// Run an integration with a compatible catalog
// The operator should create the catalog, reconcile it properly and run the Integration accordingly
t.Run("Run catalog compatible", func(t *testing.T) {
name := "java"
// TODO replace with fixed version, when it is officially released
compatibleVersion := "1.17.0-SNAPSHOT"
compatibleCatalogName := "camel-catalog-" + strings.ToLower(compatibleVersion)

// First of all we delete the catalog, if by any chance it was created previously
Expect(DeleteCamelCatalog(ns, compatibleCatalogName)()).Should(BeTrue())
Eventually(CamelCatalog(ns, compatibleCatalogName)).Should(BeNil())

Expect(
KamelRunWithID(operatorID, ns, "../files/Java.java", "--name", name,
"-t", "camel.runtime-version="+compatibleVersion,
).Execute()).To(Succeed())

Eventually(CamelCatalog(ns, compatibleCatalogName)).ShouldNot(BeNil())
Eventually(CamelCatalogPhase(ns, compatibleCatalogName)).Should(Equal(v1.CamelCatalogPhaseReady))
Eventually(CamelCatalogCondition(ns, compatibleCatalogName, v1.CamelCatalogConditionReady)().Message).Should(
Or(Equal("Container image successfully built"), Equal("Container image exists on registry")),
)

Eventually(IntegrationKit(ns, name)).ShouldNot(Equal(""))
kitName := IntegrationKit(ns, name)()
Eventually(KitPhase(ns, kitName)).Should(Equal(v1.IntegrationKitPhaseReady))
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
Eventually(DeleteIntegrations(ns), TestTimeoutLong).Should(Equal(0))
})
})

// Create a new operator to check if it will reuse the container image catalog created in the previous test
WithNewTestNamespace(t, func(ns string) {
operatorID := fmt.Sprintf("camel-k-%s", ns)
Expect(KamelInstallWithID(operatorID, ns, "--operator-env-vars", "KAMEL_INSTALL_DEFAULT_KAMELETS=false").Execute()).To(Succeed())
Expand Down
13 changes: 13 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,19 @@ func CreateCamelCatalog(catalog *v1.CamelCatalog) func() error {
}
}

func DeleteCamelCatalog(ns, name string) func() bool {
return func() bool {
cat := CamelCatalog(ns, name)()
if cat == nil {
return true
}
if err := TestClient().Delete(TestContext, cat); err != nil {
log.Error(err, "Got error while deleting the catalog")
}
return true
}
}

func CamelCatalogPhase(ns, name string) func() v1.CamelCatalogPhase {
return func() v1.CamelCatalogPhase {
c := CamelCatalog(ns, name)()
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/catalog/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func imageExists(options spectrum.Options) bool {
Log.Errorf(err, "Cannot calculate digest")
return false
}
Log.Infof("found Camel K builder container with digest %s", hash.String())
Log.Infof("Found Camel K builder container with digest %s", hash.String())
return true
}

Log.Errorf(err, "Couldn't pull image")
Log.Infof("Couldn't pull image due to %s", err.Error())
return false
}

Expand All @@ -169,6 +169,7 @@ func buildRuntimeBuilderImage(options spectrum.Options) error {
options.Jobs = jobs
}

// TODO support also S2I
_, err := spectrum.Build(options,
"/usr/local/bin/kamel:/usr/local/bin/",
"/usr/share/maven/mvnw/:/usr/share/maven/mvnw/",
Expand Down
27 changes: 17 additions & 10 deletions pkg/controller/integrationkit/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/errors"
)

// NewInitializeAction creates a new initialization handling action for the kit.
Expand All @@ -43,7 +43,8 @@ func (action *initializeAction) Name() string {
}

func (action *initializeAction) CanHandle(kit *v1.IntegrationKit) bool {
return kit.Status.Phase == v1.IntegrationKitPhaseInitialization
return kit.Status.Phase == v1.IntegrationKitPhaseInitialization ||
kit.Status.Phase == v1.IntegrationKitPhaseWaitingForCatalog
}

func (action *initializeAction) Handle(ctx context.Context, kit *v1.IntegrationKit) (*v1.IntegrationKit, error) {
Expand All @@ -52,20 +53,24 @@ func (action *initializeAction) Handle(ctx context.Context, kit *v1.IntegrationK
return nil, err
}

if kit.Spec.Image == "" {
// by default the kit should be built
kit.Status.Phase = v1.IntegrationKitPhaseBuildSubmitted
} else {
kit.Status.Version = defaults.Version

if kit.Spec.Image == "" {
// Wait for CamelCatalog to be ready
catalog, err := kubernetes.GetCamelCatalog(
ctx,
action.client,
fmt.Sprintf("camel-catalog-%s", strings.ToLower(env.RuntimeVersion)),
kit.Namespace,
)

if err != nil {
Log.Infof("Camel Catalog - could not retrieve catalog %s", err.Error())
if errors.IsNotFound(err) {
// If the catalog is not available, likely it was required to be created
// by Integration trait, so we'll need to wait for it the be available
kit.Status.Phase = v1.IntegrationKitPhaseWaitingForCatalog
return kit, nil
}
return nil, err
}

Expand All @@ -74,23 +79,25 @@ func (action *initializeAction) Handle(ctx context.Context, kit *v1.IntegrationK
kit.Status.SetErrorCondition(
v1.IntegrationKitConditionCatalogAvailable,
fmt.Sprintf("Camel Catalog %s error", catalog.Spec.Runtime.Version),
errors.Errorf("%s", catalog.Status.GetCondition(v1.CamelCatalogConditionReady).Reason),
fmt.Errorf("%s", catalog.Status.GetCondition(v1.CamelCatalogConditionReady).Reason),
)
return kit, nil
}

if catalog.Status.Phase != v1.CamelCatalogPhaseReady {
kit.Status.Phase = v1.IntegrationKitPhaseWaitingForCatalog
return kit, nil
}

// now the kit can be built
kit.Status.Phase = v1.IntegrationKitPhaseBuildSubmitted
} else {
// but in case it has been created from an image, mark the
// kit as ready
kit.Status.Phase = v1.IntegrationKitPhaseReady

// and set the image to be used
kit.Status.Image = kit.Spec.Image
}
kit.Status.Version = defaults.Version

return kit, nil
}
2 changes: 1 addition & 1 deletion pkg/trait/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (t *camelTrait) loadOrCreateCatalog(e *Environment, runtimeVersion string)
}

// sanitize catalog name
catalogName := "camel-catalog-" + strings.ToLower(runtimeVersion) + "-" + string(runtime.Provider)
catalogName := "camel-catalog-" + strings.ToLower(runtimeVersion)

cx := v1.NewCamelCatalogWithSpecs(ns, catalogName, catalog.CamelCatalogSpec)
cx.Labels = make(map[string]string)
Expand Down
5 changes: 5 additions & 0 deletions pkg/trait/camel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TestApplyCamelTraitSucceeds(t *testing.T) {
assert.Equal(t, "0.0.1", environment.RuntimeVersion)
assert.Equal(t, "0.0.1", environment.Integration.Status.RuntimeVersion)
assert.Equal(t, "0.0.1", environment.IntegrationKit.Status.RuntimeVersion)

// Test regex as well
assert.True(t, exactVersionRegexp.MatchString("1.2.3"))
assert.True(t, exactVersionRegexp.MatchString("1.0.0-SNAPSHOT"))
assert.False(t, exactVersionRegexp.MatchString("wroong"))
}

func TestApplyCamelTraitWithoutEnvironmentCatalogAndUnmatchableVersionFails(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (u Options) Get(id string) (map[string]interface{}, bool) {
return nil, false
}

var exactVersionRegexp = regexp.MustCompile(`^(\d+)\.(\d+)\.([\w-.]+)$`)
var exactVersionRegexp = regexp.MustCompile(`^(\d+)\.(\d+)\.(\d+)([\w-.]*)$`)

// getIntegrationKit retrieves the kit set on the integration.
func getIntegrationKit(ctx context.Context, c client.Client, integration *v1.Integration) (*v1.IntegrationKit, error) {
Expand Down

0 comments on commit e366941

Please sign in to comment.