You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2022/01/12 13:53:56 UTC

[camel-k] 10/31: chore(maven): Separate user and platform Maven settings

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

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

commit ebcc70c2c453b367b1c4a0b5787eba9211b54ecc
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Fri Jan 7 17:49:54 2022 +0100

    chore(maven): Separate user and platform Maven settings
---
 pkg/cmd/install.go                                 | 78 ++++++++++++++++++++--
 .../integrationplatform/initialize_test.go         | 26 --------
 pkg/platform/defaults.go                           | 74 --------------------
 pkg/trait/builder.go                               |  5 +-
 4 files changed, 77 insertions(+), 106 deletions(-)

diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 1921a2c..0f1225c 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -18,27 +18,33 @@ limitations under the License.
 package cmd
 
 import (
+	"context"
 	"fmt"
 	"os"
 	"regexp"
 	"strings"
 	"time"
 
-	"go.uber.org/multierr"
-
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 
+	"go.uber.org/multierr"
+
 	corev1 "k8s.io/api/core/v1"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/types"
+
+	ctrl "sigs.k8s.io/controller-runtime/pkg/client"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/client"
 	"github.com/apache/camel-k/pkg/install"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
+	"github.com/apache/camel-k/pkg/util/maven"
 	"github.com/apache/camel-k/pkg/util/olm"
+	"github.com/apache/camel-k/pkg/util/patch"
 	"github.com/apache/camel-k/pkg/util/registry"
 	"github.com/apache/camel-k/pkg/util/watch"
 )
@@ -383,8 +389,37 @@ func (o *installCmdOptions) install(cobraCmd *cobra.Command, _ []string) error {
 		}
 
 		if len(o.MavenRepositories) > 0 {
-			for _, r := range o.MavenRepositories {
-				platform.AddConfiguration("repository", r)
+			var repositories []v1.Repository
+			var mirrors []maven.Mirror
+
+			for i, r := range o.MavenRepositories {
+				if strings.Contains(r, "@mirrorOf=") {
+					mirror := maven.NewMirror(r)
+					if mirror.ID == "" {
+						mirror.ID = fmt.Sprintf("mirror-%03d", i)
+					}
+					mirrors = append(mirrors, mirror)
+				} else {
+					repository := maven.NewRepository(r)
+					if repository.ID == "" {
+						repository.ID = fmt.Sprintf("repository-%03d", i)
+					}
+					repositories = append(repositories, repository)
+				}
+			}
+
+			settings := maven.NewDefaultSettings(repositories, mirrors)
+
+			err := createDefaultMavenSettingsConfigMap(o.Context, c, namespace, platform.Name, settings)
+			if err != nil {
+				return err
+			}
+
+			platform.Spec.Build.Maven.Settings.ConfigMapKeyRef = &corev1.ConfigMapKeySelector{
+				LocalObjectReference: corev1.LocalObjectReference{
+					Name: platform.Name + "-maven-settings",
+				},
+				Key: "settings.xml",
 			}
 		}
 
@@ -675,3 +710,38 @@ func decodeSecretKeySelector(secretKey string) (*corev1.SecretKeySelector, error
 		Key: match[2],
 	}, nil
 }
+
+func createDefaultMavenSettingsConfigMap(ctx context.Context, client client.Client, namespace, name string, settings maven.Settings) error {
+	cm, err := maven.SettingsConfigMap(namespace, name, settings)
+	if err != nil {
+		return err
+	}
+
+	err = client.Create(ctx, cm)
+	if err != nil && !k8serrors.IsAlreadyExists(err) {
+		return err
+	} else if k8serrors.IsAlreadyExists(err) {
+		existing := &corev1.ConfigMap{
+			ObjectMeta: metav1.ObjectMeta{
+				Namespace: cm.Namespace,
+				Name:      cm.Name,
+			},
+		}
+		err = client.Get(ctx, ctrl.ObjectKeyFromObject(existing), existing)
+		if err != nil {
+			return err
+		}
+
+		p, err := patch.PositiveMergePatch(existing, cm)
+		if err != nil {
+			return err
+		} else if len(p) != 0 {
+			err = client.Patch(ctx, cm, ctrl.RawPatch(types.MergePatchType, p))
+			if err != nil {
+				return errors.Wrap(err, "error during patch resource")
+			}
+		}
+	}
+
+	return nil
+}
diff --git a/pkg/controller/integrationplatform/initialize_test.go b/pkg/controller/integrationplatform/initialize_test.go
index 4c7aa12..a718b12 100644
--- a/pkg/controller/integrationplatform/initialize_test.go
+++ b/pkg/controller/integrationplatform/initialize_test.go
@@ -115,29 +115,3 @@ func TestTimeouts_Truncated(t *testing.T) {
 
 	assert.Equal(t, 5*time.Minute, answer.Status.Build.GetTimeout().Duration)
 }
-
-func TestDefaultMavenSettingsApplied(t *testing.T) {
-	ip := v1.IntegrationPlatform{}
-	ip.Namespace = "ns"
-	ip.Name = "test-platform"
-	ip.Spec.Cluster = v1.IntegrationPlatformClusterOpenShift
-	ip.Spec.Profile = v1.TraitProfileOpenShift
-
-	c, err := test.NewFakeClient(&ip)
-	assert.Nil(t, err)
-
-	assert.Nil(t, platform.ConfigureDefaults(context.TODO(), c, &ip, false))
-
-	h := NewInitializeAction()
-	h.InjectLogger(log.Log)
-	h.InjectClient(c)
-
-	answer, err := h.Handle(context.TODO(), &ip)
-	assert.Nil(t, err)
-	assert.NotNil(t, answer)
-
-	assert.NotNil(t, answer.Status.Build.Maven.Settings.ConfigMapKeyRef)
-	assert.Nil(t, answer.Spec.Build.Maven.Settings.ConfigMapKeyRef)
-	assert.Equal(t, "test-platform-maven-settings", answer.Status.Build.Maven.Settings.ConfigMapKeyRef.Name)
-	assert.Equal(t, "settings.xml", answer.Status.Build.Maven.Settings.ConfigMapKeyRef.Key)
-}
diff --git a/pkg/platform/defaults.go b/pkg/platform/defaults.go
index 45bc09a..8355b32 100644
--- a/pkg/platform/defaults.go
+++ b/pkg/platform/defaults.go
@@ -19,7 +19,6 @@ package platform
 
 import (
 	"context"
-	"fmt"
 	"runtime"
 	"strings"
 	"time"
@@ -40,9 +39,7 @@ import (
 	"github.com/apache/camel-k/pkg/kamelet/repository"
 	"github.com/apache/camel-k/pkg/util/defaults"
 	"github.com/apache/camel-k/pkg/util/log"
-	"github.com/apache/camel-k/pkg/util/maven"
 	"github.com/apache/camel-k/pkg/util/openshift"
-	"github.com/apache/camel-k/pkg/util/patch"
 )
 
 // BuilderServiceAccount --.
@@ -186,42 +183,6 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration
 		}
 	}
 
-	if p.Status.Build.Maven.Settings.ConfigMapKeyRef == nil && p.Status.Build.Maven.Settings.SecretKeyRef == nil {
-		var repositories []v1.Repository
-		var mirrors []maven.Mirror
-		for i, c := range p.Status.Configuration {
-			if c.Type == "repository" {
-				if strings.Contains(c.Value, "@mirrorOf=") {
-					mirror := maven.NewMirror(c.Value)
-					if mirror.ID == "" {
-						mirror.ID = fmt.Sprintf("mirror-%03d", i)
-					}
-					mirrors = append(mirrors, mirror)
-				} else {
-					repo := maven.NewRepository(c.Value)
-					if repo.ID == "" {
-						repo.ID = fmt.Sprintf("repository-%03d", i)
-					}
-					repositories = append(repositories, repo)
-				}
-			}
-		}
-
-		settings := maven.NewDefaultSettings(repositories, mirrors)
-
-		err := createDefaultMavenSettingsConfigMap(ctx, c, p, settings)
-		if err != nil {
-			return err
-		}
-
-		p.Status.Build.Maven.Settings.ConfigMapKeyRef = &corev1.ConfigMapKeySelector{
-			LocalObjectReference: corev1.LocalObjectReference{
-				Name: p.Name + "-maven-settings",
-			},
-			Key: "settings.xml",
-		}
-	}
-
 	if p.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyKaniko && p.Status.Build.KanikoBuildCache == nil {
 		// Default to disabling Kaniko cache warmer
 		// Using the cache warmer pod seems unreliable with the current Kaniko version
@@ -262,41 +223,6 @@ func setStatusAdditionalInfo(platform *v1.IntegrationPlatform) {
 	platform.Status.Info["gitCommit"] = defaults.GitCommit
 }
 
-func createDefaultMavenSettingsConfigMap(ctx context.Context, client client.Client, p *v1.IntegrationPlatform, settings maven.Settings) error {
-	cm, err := maven.SettingsConfigMap(p.Namespace, p.Name, settings)
-	if err != nil {
-		return err
-	}
-
-	err = client.Create(ctx, cm)
-	if err != nil && !k8serrors.IsAlreadyExists(err) {
-		return err
-	} else if k8serrors.IsAlreadyExists(err) {
-		existing := &corev1.ConfigMap{
-			ObjectMeta: metav1.ObjectMeta{
-				Namespace: cm.Namespace,
-				Name:      cm.Name,
-			},
-		}
-		err = client.Get(ctx, ctrl.ObjectKeyFromObject(existing), existing)
-		if err != nil {
-			return err
-		}
-
-		p, err := patch.PositiveMergePatch(existing, cm)
-		if err != nil {
-			return err
-		} else if len(p) != 0 {
-			err = client.Patch(ctx, cm, ctrl.RawPatch(types.MergePatchType, p))
-			if err != nil {
-				return errors.Wrap(err, "error during patch resource")
-			}
-		}
-	}
-
-	return nil
-}
-
 func createServiceCaBundleConfigMap(ctx context.Context, client client.Client, p *v1.IntegrationPlatform) (*corev1.ConfigMap, error) {
 	cm := &corev1.ConfigMap{
 		ObjectMeta: metav1.ObjectMeta{
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index e14f282..22f970d 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -135,8 +135,9 @@ func (t *builderTrait) Apply(e *Environment) error {
 }
 
 func (t *builderTrait) builderTask(e *Environment) (*v1.BuilderTask, error) {
-	maven := e.Platform.Status.Build.Maven
-
+	maven := v1.MavenBuildSpec{
+		MavenSpec: e.Platform.Status.Build.Maven,
+	}
 	// Add Maven repositories defined in the IntegrationKit
 	for _, repo := range e.IntegrationKit.Spec.Repositories {
 		maven.Repositories = append(maven.Repositories, mvn.NewRepository(repo))