Repository: apache/camel-k
Pull request #2089 (2021-03-03 13:57:23)

Fix #200: Add support for mirrorOf attribute
<!-- Description -->




<!--
Enter your extended release note in the below block. If the PR requires
additional action from users switching to the new release, include the string
"action required". If no release note is required, write "NONE". 

You can (optionally) mark this PR with labels "kind/bug" or "kind/feature" to make sure
the text is added to the right section of the release notes. 
-->

**Release Note**
```release-note
NONE
```


bouskaJ: I have seen similar errors in the other pipelines so I would say it's not my fault. Am I right @astefanutti ?

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcce1371b..dfe1fb8b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@
 - Corrupted binaries attached as resource [\#1881](https://github.com/apache/camel-k/issues/1881)
 - Release 1.3.0 [\#1867](https://github.com/apache/camel-k/issues/1867)
 - Refactor artifacts of deploy directory and continue alignment towards operator-sdk & kustomize [\#1820](https://github.com/apache/camel-k/issues/1820)
+- Implementing a global build strategy [\#1799](https://github.com/apache/camel-k/issues/1799)
 - Add a cli subcommand to inspect dependencies required by an Integration [\#1738](https://github.com/apache/camel-k/issues/1738)
 - Camel K installed globally by default [\#1693](https://github.com/apache/camel-k/issues/1693)
 - Example Integration in the ClusterServiceVersion stuck in Deploying phase [\#1677](https://github.com/apache/camel-k/issues/1677)
@@ -49,6 +50,7 @@
 - chore\(e2e\): Add test case to Integration scaling e2e tests [\#2061](https://github.com/apache/camel-k/pull/2061) ([astefanutti](https://github.com/astefanutti))
 - fix\(api\): Remove deprecated deepcopy-gen markers [\#2060](https://github.com/apache/camel-k/pull/2060) ([astefanutti](https://github.com/astefanutti))
 - Move trait test to separate directory [\#2059](https://github.com/apache/camel-k/pull/2059) ([bouskaJ](https://github.com/bouskaJ))
+- Implement a lightning-fast global strategy [\#2058](https://github.com/apache/camel-k/pull/2058) ([nicolaferraro](https://github.com/nicolaferraro))
 - test: Add PodDisruptionBudget trait e2e tests [\#2057](https://github.com/apache/camel-k/pull/2057) ([astefanutti](https://github.com/astefanutti))
 - chore\(cmd\): skip operator compatibility warning [\#2056](https://github.com/apache/camel-k/pull/2056) ([squakez](https://github.com/squakez))
 - feat: add command completion for connect flag [\#2055](https://github.com/apache/camel-k/pull/2055) ([johnpoth](https://github.com/johnpoth))
@@ -141,14 +143,6 @@
 - chore: Bump resources to version 1.4.0-SNAPSHOT [\#1908](https://github.com/apache/camel-k/pull/1908) ([astefanutti](https://github.com/astefanutti))
 - fix\(cli\): help command offline [\#1907](https://github.com/apache/camel-k/pull/1907) ([squakez](https://github.com/squakez))
 - fix: Unique syntax for Maven artifact GAV string representation [\#1903](https://github.com/apache/camel-k/pull/1903) ([astefanutti](https://github.com/astefanutti))
-- fix\(ci\): Use AdoptOpenJDK/install-jdk@v1 action [\#1901](https://github.com/apache/camel-k/pull/1901) ([astefanutti](https://github.com/astefanutti))
-- fix: Do not filter Camel catalogs by runtime provider label [\#1900](https://github.com/apache/camel-k/pull/1900) ([astefanutti](https://github.com/astefanutti))
-- refactor\(test\): included all run command flag test [\#1895](https://github.com/apache/camel-k/pull/1895) ([squakez](https://github.com/squakez))
-- Fix \#1707: add a way to override the base image in the operator [\#1894](https://github.com/apache/camel-k/pull/1894) ([nicolaferraro](https://github.com/nicolaferraro))
-- feat\(test\): operator command unit test [\#1893](https://github.com/apache/camel-k/pull/1893) ([squakez](https://github.com/squakez))
-- feat\(test\): builder command unit test [\#1891](https://github.com/apache/camel-k/pull/1891) ([squakez](https://github.com/squakez))
-- feat\(test\): kit create command unit test [\#1889](https://github.com/apache/camel-k/pull/1889) ([squakez](https://github.com/squakez))
-- feat\(test\): delete command unit test [\#1888](https://github.com/apache/camel-k/pull/1888) ([squakez](https://github.com/squakez))
 - proposal: add Service Binding proposal \(\#1445\) [\#1864](https://github.com/apache/camel-k/pull/1864) ([johnpoth](https://github.com/johnpoth))
 
 ## [v1.3.1](https://github.com/apache/camel-k/tree/v1.3.1) (2021-02-01)
@@ -205,7 +199,6 @@
 - fix\(ci\): Use AdoptOpenJDK/install-jdk@v1 action [\#1940](https://github.com/apache/camel-k/pull/1940) ([astefanutti](https://github.com/astefanutti))
 - Backport fix \#1936: avoid patching if the target resource contains all expecte… [\#1938](https://github.com/apache/camel-k/pull/1938) ([nicolaferraro](https://github.com/nicolaferraro))
 - \[backport 1.3.x\] fix\(cli\): check run sources exists [\#1932](https://github.com/apache/camel-k/pull/1932) ([squakez](https://github.com/squakez))
-- Backport \#1900 to 1.3.x [\#1905](https://github.com/apache/camel-k/pull/1905) ([astefanutti](https://github.com/astefanutti))
 - Backport \#1707 to 1.3.x [\#1896](https://github.com/apache/camel-k/pull/1896) ([nicolaferraro](https://github.com/nicolaferraro))
 - Backport \#1873 to 1.3.x [\#1887](https://github.com/apache/camel-k/pull/1887) ([nicolaferraro](https://github.com/nicolaferraro))
 - Backport  \#1880 to 1.3.x [\#1886](https://github.com/apache/camel-k/pull/1886) ([nicolaferraro](https://github.com/nicolaferraro))
@@ -445,6 +438,7 @@
 - Related to \#1638 kamel init - should not create source file with ASF … [\#1639](https://github.com/apache/camel-k/pull/1639) ([oscerd](https://github.com/oscerd))
 - Add support for sinkbinding [\#1637](https://github.com/apache/camel-k/pull/1637) ([nicolaferraro](https://github.com/nicolaferraro))
 - Fix \#1536: take modeline changes into account in dev mode [\#1634](https://github.com/apache/camel-k/pull/1634) ([johnpoth](https://github.com/johnpoth))
+- fix\(doc\): Fix Affinity trait examples formatting [\#1631](https://github.com/apache/camel-k/pull/1631) ([astefanutti](https://github.com/astefanutti))
 - Use Quarkus as default runtime [\#1609](https://github.com/apache/camel-k/pull/1609) ([nicolaferraro](https://github.com/nicolaferraro))
 - Implement Kamelets [\#1602](https://github.com/apache/camel-k/pull/1602) ([nicolaferraro](https://github.com/nicolaferraro))
 
@@ -530,7 +524,6 @@
 
 **Merged pull requests:**
 
-- fix\(doc\): Fix Affinity trait examples formatting [\#1631](https://github.com/apache/camel-k/pull/1631) ([astefanutti](https://github.com/astefanutti))
 - Initial architecture documentation [\#1629](https://github.com/apache/camel-k/pull/1629) ([lburgazzoli](https://github.com/lburgazzoli))
 - fix\(doc\): PodMonitoring -\> PodMonitor [\#1628](https://github.com/apache/camel-k/pull/1628) ([astefanutti](https://github.com/astefanutti))
 - doc: Init troubleshooting guide [\#1627](https://github.com/apache/camel-k/pull/1627) ([astefanutti](https://github.com/astefanutti))
@@ -1308,7 +1301,6 @@
 - Fix embedded resources snapshot version [\#882](https://github.com/apache/camel-k/pull/882) ([dmvolod](https://github.com/dmvolod))
 - Switch to images-dev build on install-mini\<plaform\> [\#881](https://github.com/apache/camel-k/pull/881) ([dmvolod](https://github.com/dmvolod))
 - Added unit tests for ingress\_trait \#255 [\#880](https://github.com/apache/camel-k/pull/880) ([aldettinger](https://github.com/aldettinger))
-- Remove Dep files [\#872](https://github.com/apache/camel-k/pull/872) ([astefanutti](https://github.com/astefanutti))
 - feat: Enable integration scale sub-resource [\#962](https://github.com/apache/camel-k/pull/962) ([astefanutti](https://github.com/astefanutti))
 - fix: Add option to disable Kaniko cache warming [\#922](https://github.com/apache/camel-k/pull/922) ([asifdxtreme](https://github.com/asifdxtreme))
 
@@ -1369,6 +1361,7 @@
 - Fix make clean for tests and remove binary file from repo [\#879](https://github.com/apache/camel-k/pull/879) ([dmvolod](https://github.com/dmvolod))
 - chore\(js\): fix examples, use arrow function for lambda style processor [\#877](https://github.com/apache/camel-k/pull/877) ([lburgazzoli](https://github.com/lburgazzoli))
 - fix\(GC\): Skip 503 errors when scanning for resources to be GCed [\#876](https://github.com/apache/camel-k/pull/876) ([astefanutti](https://github.com/astefanutti))
+- Remove Dep files [\#872](https://github.com/apache/camel-k/pull/872) ([astefanutti](https://github.com/astefanutti))
 - chore\(build\): remove go mod vendor from travis build [\#871](https://github.com/apache/camel-k/pull/871) ([lburgazzoli](https://github.com/lburgazzoli))
 - Fix \#789: add correct example for operator hub [\#868](https://github.com/apache/camel-k/pull/868) ([nicolaferraro](https://github.com/nicolaferraro))
 - Fix \#730: put example that demonstrate backward compatibility works [\#867](https://github.com/apache/camel-k/pull/867) ([nicolaferraro](https://github.com/nicolaferraro))
diff --git a/pkg/cmd/util_dependencies.go b/pkg/cmd/util_dependencies.go
index 197879945..f89a6a41f 100644
--- a/pkg/cmd/util_dependencies.go
+++ b/pkg/cmd/util_dependencies.go
@@ -126,15 +126,24 @@ func getTransitiveDependencies(
 
 	if len(repositories) > 0 {
 		var repoList []maven.Repository
+		var mirrors []maven.Mirror
 		for i, repo := range repositories {
-			repository := maven.NewRepository(repo)
-			if repository.ID == "" {
-				repository.ID = fmt.Sprintf("repository-%03d", i)
+			if strings.Contains(repo, "@mirrorOf=") {
+				mirror := maven.NewMirror(repo)
+				if mirror.ID == "" {
+					mirror.ID = fmt.Sprintf("mirror-%03d", i)
+				}
+				mirrors = append(mirrors, mirror)
+			} else {
+				repository := maven.NewRepository(repo)
+				if repository.ID == "" {
+					repository.ID = fmt.Sprintf("repository-%03d", i)
+				}
+				repoList = append(repoList, repository)
 			}
-			repoList = append(repoList, repository)
 		}
 
-		settings := maven.NewDefaultSettings(repoList)
+		settings := maven.NewDefaultSettings(repoList, mirrors)
 		settingsData, err := util.EncodeXML(settings)
 		if err != nil {
 			return nil, err
diff --git a/pkg/platform/defaults.go b/pkg/platform/defaults.go
index 8b86accda..c4f138233 100644
--- a/pkg/platform/defaults.go
+++ b/pkg/platform/defaults.go
@@ -221,17 +221,26 @@ 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 []maven.Repository
+		var mirrors []maven.Mirror
 		for i, c := range p.Status.Configuration {
 			if c.Type == "repository" {
-				repository := maven.NewRepository(c.Value)
-				if repository.ID == "" {
-					repository.ID = fmt.Sprintf("repository-%03d", i)
+				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 {
+					repository := maven.NewRepository(c.Value)
+					if repository.ID == "" {
+						repository.ID = fmt.Sprintf("repository-%03d", i)
+					}
+					repositories = append(repositories, repository)
 				}
-				repositories = append(repositories, repository)
 			}
 		}
 
-		settings := maven.NewDefaultSettings(repositories)
+		settings := maven.NewDefaultSettings(repositories, mirrors)
 
 		err := createDefaultMavenSettingsConfigMap(ctx, c, p, settings)
 		if err != nil {
diff --git a/pkg/util/maven/maven_project.go b/pkg/util/maven/maven_project.go
index 4de522585..068065b19 100644
--- a/pkg/util/maven/maven_project.go
+++ b/pkg/util/maven/maven_project.go
@@ -197,3 +197,22 @@ func NewRepository(repo string) Repository {
 
 	return r
 }
+
+func NewMirror(repo string) Mirror{
+	m := Mirror{}
+	if idx := strings.Index(repo, "@"); idx != -1 {
+		m.URL = repo[:idx]
+
+		for _, attribute := range strings.Split(repo[idx+1:], "@") {
+			switch {
+			case strings.HasPrefix(attribute, "mirrorOf="):
+				m.MirrorOf = attribute[9:]
+			case strings.HasPrefix(attribute, "id="):
+				m.ID = attribute[3:]
+			case strings.HasPrefix(attribute, "name="):
+				m.Name = attribute[5:]
+			}
+		}
+	}
+	return m
+}
diff --git a/pkg/util/maven/maven_settings.go b/pkg/util/maven/maven_settings.go
index 4bdf28232..206b3ed22 100644
--- a/pkg/util/maven/maven_settings.go
+++ b/pkg/util/maven/maven_settings.go
@@ -42,7 +42,7 @@ func NewSettings() Settings {
 }
 
 // NewDefaultSettings --
-func NewDefaultSettings(repositories []Repository) Settings {
+func NewDefaultSettings(repositories []Repository, mirrors []Mirror) Settings {
 	settings := NewSettings()
 
 	var additionalRepos []Repository
@@ -66,6 +66,8 @@ func NewDefaultSettings(repositories []Repository) Settings {
 		},
 	}
 
+	settings.Mirrors = mirrors
+
 	return settings
 }
 
diff --git a/pkg/util/maven/maven_types.go b/pkg/util/maven/maven_types.go
index 44cbf71c7..3b7dba5f1 100644
--- a/pkg/util/maven/maven_types.go
+++ b/pkg/util/maven/maven_types.go
@@ -41,6 +41,14 @@ type RepositoryPolicy struct {
 	ChecksumPolicy string `xml:"checksumPolicy,omitempty"`
 }
 
+// Mirror --
+type Mirror struct {
+	ID        string           `xml:"id"`
+	Name      string           `xml:"name,omitempty"`
+	URL       string           `xml:"url"`
+	MirrorOf  string           `xml:"mirrorOf"`
+}
+
 // Build --
 type Build struct {
 	DefaultGoal string   `xml:"defaultGoal,omitempty"`
@@ -157,6 +165,7 @@ type Settings struct {
 	XsiSchemaLocation string    `xml:"xsi:schemaLocation,attr"`
 	LocalRepository   string    `xml:"localRepository"`
 	Profiles          []Profile `xml:"profiles>profile,omitempty"`
+	Mirrors           []Mirror  `xml:"mirrors>mirror,omitempty"`
 }
 
 // MarshalBytes --
@@ -235,3 +244,5 @@ type PropertyActivation struct {
 	Name  string `xml:"name"`
 	Value string `xml:"value"`
 }
+
+


Test patch:
diff --git a/pkg/util/maven/maven_settings_test.go b/pkg/util/maven/maven_settings_test.go
index a4a1776a8..cebada994 100644
--- a/pkg/util/maven/maven_settings_test.go
+++ b/pkg/util/maven/maven_settings_test.go
@@ -52,6 +52,7 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?>
       <pluginRepositories></pluginRepositories>
     </profile>
   </profiles>
+  <mirrors></mirrors>
 </settings>`
 
 const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
@@ -94,6 +95,7 @@ const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
       </pluginRepositories>
     </profile>
   </profiles>
+  <mirrors></mirrors>
 </settings>`
 
 const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-8"?>
@@ -160,6 +162,13 @@ const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-
       </pluginRepositories>
     </profile>
   </profiles>
+  <mirrors>
+    <mirror>
+      <id>foo</id>
+      <url>https://foo.bar.org/repo</url>
+      <mirrorOf>*</mirrorOf>
+    </mirror>
+  </mirrors>
 </settings>`
 
 func TestSettingsGeneration(t *testing.T) {
@@ -198,7 +207,7 @@ func TestSettingsGeneration(t *testing.T) {
 }
 
 func TestDefaultSettingsGeneration(t *testing.T) {
-	settings := NewDefaultSettings([]Repository{})
+	settings := NewDefaultSettings([]Repository{}, []Mirror{})
 
 	content, err := util.EncodeXML(settings)
 
@@ -213,7 +222,10 @@ func TestDefaultSettingsGenerationWithAdditionalRepo(t *testing.T) {
 		NewRepository("https://repo1.maven.org/maven2@id=central"),
 		NewRepository("https://foo.bar.org/repo@id=foo"),
 	}
-	settings := NewDefaultSettings(repositories)
+	mirrors := []Mirror{
+		NewMirror("https://foo.bar.org/repo@id=foo@mirrorOf=*"),
+	}
+	settings := NewDefaultSettings(repositories, mirrors)
 
 	content, err := util.EncodeXML(settings)
 
@@ -224,7 +236,7 @@ func TestDefaultSettingsGenerationWithAdditionalRepo(t *testing.T) {
 }
 
 func TestCreateSettingsConfigMap(t *testing.T) {
-	settings := NewDefaultSettings([]Repository{})
+	settings := NewDefaultSettings([]Repository{}, []Mirror{})
 
 	configMap, err := CreateSettingsConfigMap("foo", "bar", settings)
 	assert.Nil(t, err)