Repository: apache/camel-k
Pull request #1896 (2021-01-11 16:33:10)

Backport #1707 to 1.3.x
<!-- 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
```



Patch:
diff --git a/pkg/platform/defaults.go b/pkg/platform/defaults.go
index db3dfabe2..9da4e883e 100644
--- a/pkg/platform/defaults.go
+++ b/pkg/platform/defaults.go
@@ -166,7 +166,7 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration
 		p.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
 	}
 	if p.Status.Build.BaseImage == "" {
-		p.Status.Build.BaseImage = defaults.BaseImage
+		p.Status.Build.BaseImage = defaults.BaseImage()
 	}
 	if p.Status.Build.Maven.LocalRepository == "" {
 		p.Status.Build.Maven.LocalRepository = defaults.LocalRepository
diff --git a/pkg/util/defaults/defaults.go b/pkg/util/defaults/defaults.go
index 872f923bd..73e3f96a4 100644
--- a/pkg/util/defaults/defaults.go
+++ b/pkg/util/defaults/defaults.go
@@ -34,8 +34,8 @@ const (
 	// KanikoVersion --
 	KanikoVersion = "0.17.1"
 
-	// BaseImage --
-	BaseImage = "adoptopenjdk/openjdk11:slim"
+	// baseImage --
+	baseImage = "adoptopenjdk/openjdk11:slim"
 
 	// LocalRepository --
 	LocalRepository = "/tmp/artifacts/m2"
diff --git a/pkg/util/defaults/defaults_support.go b/pkg/util/defaults/defaults_support.go
new file mode 100644
index 000000000..ec2a3978b
--- /dev/null
+++ b/pkg/util/defaults/defaults_support.go
@@ -0,0 +1,35 @@
+/*
+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.
+*/
+
+package defaults
+
+import "os"
+
+var baseImageOverrideEnvs = []string{"BASE_IMAGE", "RELATED_IMAGE_BASE"}
+
+func BaseImage() string {
+	return envOrDefault(baseImage, baseImageOverrideEnvs...)
+}
+
+func envOrDefault(def string, envs ...string) string {
+	for i := range envs {
+		if val := os.Getenv(envs[i]); val != "" {
+			return val
+		}
+	}
+	return def
+}
diff --git a/pkg/util/docker/docker.go b/pkg/util/docker/docker.go
index 90ce1f36b..7a48b6be4 100644
--- a/pkg/util/docker/docker.go
+++ b/pkg/util/docker/docker.go
@@ -32,7 +32,7 @@ func CreateBaseImageDockerFile() error {
 	dockerFile := []string{}
 
 	// Base image is a java-only image since the integration command is just a java command.
-	dockerFile = append(dockerFile, FROM(defaults.BaseImage))
+	dockerFile = append(dockerFile, FROM(defaults.BaseImage()))
 
 	// Ensure Maven is already installed.
 	dockerFile = append(dockerFile, RUNMavenInstall())
diff --git a/script/Makefile b/script/Makefile
index c6ae0c404..aaa0d52d2 100644
--- a/script/Makefile
+++ b/script/Makefile
@@ -107,8 +107,8 @@ codegen:
 	@echo "  // KanikoVersion -- " >> $(VERSIONFILE)
 	@echo "  KanikoVersion = \"$(KANIKO_VERSION)\"" >> $(VERSIONFILE)
 	@echo "" >> $(VERSIONFILE)
-	@echo "  // BaseImage -- " >> $(VERSIONFILE)
-	@echo "  BaseImage = \"$(BASE_IMAGE)\"" >> $(VERSIONFILE)
+	@echo "  // baseImage -- " >> $(VERSIONFILE)
+	@echo "  baseImage = \"$(BASE_IMAGE)\"" >> $(VERSIONFILE)
 	@echo "" >> $(VERSIONFILE)
 	@echo "  // LocalRepository -- " >> $(VERSIONFILE)
 	@echo "  LocalRepository = \"$(LOCAL_REPOSITORY)\"" >> $(VERSIONFILE)


Test patch:
diff --git a/pkg/util/defaults/defaults_test.go b/pkg/util/defaults/defaults_test.go
new file mode 100644
index 000000000..90dba55cf
--- /dev/null
+++ b/pkg/util/defaults/defaults_test.go
@@ -0,0 +1,38 @@
+/*
+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.
+*/
+
+package defaults
+
+import (
+	"os"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestDefaultBaseImage(t *testing.T) {
+	assert.NotEmpty(t, BaseImage())
+}
+
+func TestOverriddenBaseImage(t *testing.T) {
+	env := "RELATED_IMAGE_BASE"
+	oldEnvVal := os.Getenv(env)
+	overriddenImage := "xxx"
+	assert.NoError(t, os.Setenv(env, overriddenImage))
+	assert.Equal(t, overriddenImage, BaseImage())
+	assert.NoError(t, os.Setenv(env, oldEnvVal))
+}