Skip to content

Commit

Permalink
fix(ctrl): use maven settings to install Kamelets
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jan 18, 2025
1 parent e54b18f commit d9d6541
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pkg/controller/integrationplatform/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"strings"
"testing"
"time"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/platform"
Expand All @@ -35,6 +36,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8stesting "k8s.io/client-go/testing"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -124,6 +126,9 @@ func TestCreateNewCatalog(t *testing.T) {
Build: v1.IntegrationPlatformBuildSpec{
RuntimeProvider: v1.RuntimeProviderQuarkus,
RuntimeVersion: defaults.DefaultRuntimeVersion,
Timeout: &metav1.Duration{
Duration: 1 * time.Minute,
},
},
},
}
Expand Down Expand Up @@ -187,6 +192,9 @@ func TestCreateNewCatalog(t *testing.T) {
Build: v1.IntegrationPlatformBuildSpec{
RuntimeProvider: v1.RuntimeProviderQuarkus,
RuntimeVersion: defaults.DefaultRuntimeVersion,
Timeout: &metav1.Duration{
Duration: 1 * time.Minute,
},
},
},
}
Expand Down
47 changes: 42 additions & 5 deletions pkg/controller/integrationplatform/kamelets.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/jvm"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/log"
"github.com/apache/camel-k/v2/pkg/util/maven"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -61,7 +63,7 @@ func installKameletCatalog(ctx context.Context, c client.Client, platform *v1.In
return -1, -1, err
}
// Download Kamelet dependency
if err := downloadKameletDependency(ctx, version, kameletDir); err != nil {
if err := downloadKameletDependency(ctx, c, platform, version, kameletDir); err != nil {
return -1, -1, err
}
// Extract Kamelets files
Expand Down Expand Up @@ -100,9 +102,7 @@ func prepareKameletDirectory() (string, error) {
return kameletDir, nil
}

func downloadKameletDependency(ctx context.Context, version, kameletsDir string) error {
// TODO: we may want to add the maven settings coming from the platform
// in order to cover any user security setting in place
func downloadKameletDependency(ctx context.Context, c client.Client, platform *v1.IntegrationPlatform, version, kameletsDir string) error {
p := maven.NewProjectWithGAV("org.apache.camel.k.kamelets", "kamelets-catalog", defaults.Version)
mc := maven.NewContext(kameletsDir)
mc.AddArgument("-q")
Expand All @@ -111,7 +111,44 @@ func downloadKameletDependency(ctx context.Context, version, kameletsDir string)
mc.AddArgument("-Dmdep.useBaseVersion=true")
mc.AddArgument(fmt.Sprintf("-DoutputDirectory=%s", kameletsDir))

return p.Command(mc).Do(ctx)
if settings, err := kubernetes.ResolveValueSource(ctx, c, platform.Namespace, &platform.Status.Build.Maven.Settings); err != nil {
return err
} else if settings != "" {
mc.UserSettings = []byte(settings)
}

settings, err := maven.NewSettings(maven.DefaultRepositories, maven.ProxyFromEnvironment)
if err != nil {
return err
}
data, err := settings.MarshalBytes()
if err != nil {
return err
}
mc.GlobalSettings = data
secrets := platform.Status.Build.Maven.CASecrets

if secrets != nil {
certsData, err := kubernetes.GetSecretsRefData(ctx, c, platform.Namespace, secrets)
if err != nil {
return err
}
trustStoreName := "trust.jks"
trustStorePass := jvm.NewKeystorePassword()
err = jvm.GenerateKeystore(ctx, kameletsDir, trustStoreName, trustStorePass, certsData)
if err != nil {
return err
}
mc.ExtraMavenOpts = append(mc.ExtraMavenOpts,
"-Djavax.net.ssl.trustStore="+trustStoreName,
"-Djavax.net.ssl.trustStorePassword="+trustStorePass,
)
}

timeoutCtx, cancel := context.WithTimeout(ctx, platform.Status.Build.GetTimeout().Duration)
defer cancel()

return p.Command(mc).Do(timeoutCtx)
}

func extractKameletsFromDependency(ctx context.Context, version, kameletsDir string) error {
Expand Down
19 changes: 18 additions & 1 deletion pkg/controller/integrationplatform/kamelets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
"path/filepath"
"strings"
"testing"
"time"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/boolean"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestLoadKamelet(t *testing.T) {
Expand Down Expand Up @@ -112,6 +115,20 @@ func TestPrepareKameletsDirectory(t *testing.T) {
}

func TestDownloadKameletDependencyAndExtract(t *testing.T) {
cli, err := test.NewFakeClient()
assert.NoError(t, err)
itp := v1.NewIntegrationPlatform("itp-ns", "my-itp")
itp.Status = v1.IntegrationPlatformStatus{
IntegrationPlatformSpec: v1.IntegrationPlatformSpec{
Build: v1.IntegrationPlatformBuildSpec{
RuntimeProvider: v1.RuntimeProviderQuarkus,
RuntimeVersion: defaults.DefaultRuntimeVersion,
Timeout: &metav1.Duration{
Duration: 1 * time.Minute,
},
},
},
}
// use local Maven executable in tests
t.Setenv("MAVEN_WRAPPER", boolean.FalseString)
_, ok := os.LookupEnv("MAVEN_CMD")
Expand All @@ -126,7 +143,7 @@ func TestDownloadKameletDependencyAndExtract(t *testing.T) {
assert.NoError(t, err)
camelVersion := c.Runtime.Metadata["camel.version"]
assert.NotEqual(t, "", camelVersion)
err = downloadKameletDependency(context.TODO(), camelVersion, tmpDir)
err = downloadKameletDependency(context.TODO(), cli, &itp, camelVersion, tmpDir)
assert.NoError(t, err)
downloadedDependency, err := os.Stat(path.Join(tmpDir, fmt.Sprintf("camel-kamelets-%s.jar", camelVersion)))
assert.NoError(t, err)
Expand Down

0 comments on commit d9d6541

Please sign in to comment.