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

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


The following commit(s) were added to refs/heads/main by this push:
     new 18c1b86a3 feat(cli): Allow to specify custom Kaniko and Buildah images
18c1b86a3 is described below

commit 18c1b86a35f04295cec8f91f55073afd17c02608
Author: Nicolas Filotto <[email protected]>
AuthorDate: Tue Aug 30 10:25:05 2022 +0200

    feat(cli): Allow to specify custom Kaniko and Buildah images
---
 config/crd/bases/camel.apache.org_builds.yaml      |  6 +++++
 docs/modules/ROOT/partials/apis/camel-k-crds.adoc  | 14 +++++++++++
 helm/camel-k/crds/crd-build.yaml                   |  6 +++++
 pkg/apis/camel/v1/build_types.go                   |  4 ++++
 .../camel/v1/integrationplatform_types_support.go  | 10 ++++++++
 pkg/builder/buildah.go                             |  2 ++
 pkg/builder/kaniko.go                              |  4 ++++
 pkg/cmd/install.go                                 | 27 ++++++++++++++++++----
 pkg/cmd/install_test.go                            | 21 +++++++++++++++++
 pkg/controller/build/build_pod.go                  | 14 +++++++++--
 pkg/controller/integrationplatform/kaniko_cache.go |  9 +++++++-
 pkg/resources/resources.go                         |  4 ++--
 pkg/trait/builder.go                               | 18 ++++++++++++---
 13 files changed, 127 insertions(+), 12 deletions(-)

diff --git a/config/crd/bases/camel.apache.org_builds.yaml 
b/config/crd/bases/camel.apache.org_builds.yaml
index f7d585a7e..67c78769b 100644
--- a/config/crd/bases/camel.apache.org_builds.yaml
+++ b/config/crd/bases/camel.apache.org_builds.yaml
@@ -101,6 +101,9 @@ spec:
                         contextDir:
                           description: can be useful to share info with other 
tasks
                           type: string
+                        executorImage:
+                          description: docker image to use
+                          type: string
                         image:
                           description: final image name
                           type: string
@@ -634,6 +637,9 @@ spec:
                         contextDir:
                           description: can be useful to share info with other 
tasks
                           type: string
+                        executorImage:
+                          description: docker image to use
+                          type: string
                         image:
                           description: final image name
                           type: string
diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc 
b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
index 96eb012ad..4aef3677f 100644
--- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
+++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
@@ -592,6 +592,13 @@ bool
 
 log more information
 
+|`executorImage` +
+string
+|
+
+
+docker image to use
+
 
 |===
 
@@ -2294,6 +2301,13 @@ log more information
 
 use a cache
 
+|`executorImage` +
+string
+|
+
+
+docker image to use
+
 
 |===
 
diff --git a/helm/camel-k/crds/crd-build.yaml b/helm/camel-k/crds/crd-build.yaml
index f7d585a7e..67c78769b 100644
--- a/helm/camel-k/crds/crd-build.yaml
+++ b/helm/camel-k/crds/crd-build.yaml
@@ -101,6 +101,9 @@ spec:
                         contextDir:
                           description: can be useful to share info with other 
tasks
                           type: string
+                        executorImage:
+                          description: docker image to use
+                          type: string
                         image:
                           description: final image name
                           type: string
@@ -634,6 +637,9 @@ spec:
                         contextDir:
                           description: can be useful to share info with other 
tasks
                           type: string
+                        executorImage:
+                          description: docker image to use
+                          type: string
                         image:
                           description: final image name
                           type: string
diff --git a/pkg/apis/camel/v1/build_types.go b/pkg/apis/camel/v1/build_types.go
index 0de3fd47c..d79c4fa19 100644
--- a/pkg/apis/camel/v1/build_types.go
+++ b/pkg/apis/camel/v1/build_types.go
@@ -111,6 +111,8 @@ type BuildahTask struct {
        Platform string `json:"platform,omitempty"`
        // log more information
        Verbose *bool `json:"verbose,omitempty"`
+       // docker image to use
+       ExecutorImage string `json:"executorImage,omitempty"`
 }
 
 // KanikoTask is used to configure Kaniko
@@ -121,6 +123,8 @@ type KanikoTask struct {
        Verbose *bool `json:"verbose,omitempty"`
        // use a cache
        Cache KanikoTaskCache `json:"cache,omitempty"`
+       // docker image to use
+       ExecutorImage string `json:"executorImage,omitempty"`
 }
 
 // KanikoTaskCache is used to configure Kaniko cache
diff --git a/pkg/apis/camel/v1/integrationplatform_types_support.go 
b/pkg/apis/camel/v1/integrationplatform_types_support.go
index fdd53b664..32d3ccc34 100644
--- a/pkg/apis/camel/v1/integrationplatform_types_support.go
+++ b/pkg/apis/camel/v1/integrationplatform_types_support.go
@@ -186,6 +186,16 @@ func (b IntegrationPlatformBuildSpec) 
IsOptionEnabled(option string) bool {
        return false
 }
 
+// Add a publish strategy option
+func (b *IntegrationPlatformBuildSpec) AddOption(option string, value string) {
+       options := b.PublishStrategyOptions
+       if options == nil {
+               options = make(map[string]string)
+               b.PublishStrategyOptions = options
+       }
+       options[option] = value
+}
+
 // GetTimeout returns the specified duration or a default one
 func (b IntegrationPlatformBuildSpec) GetTimeout() metav1.Duration {
        if b.Timeout == nil {
diff --git a/pkg/builder/buildah.go b/pkg/builder/buildah.go
index d900ec1fe..ae3fe9e17 100644
--- a/pkg/builder/buildah.go
+++ b/pkg/builder/buildah.go
@@ -18,3 +18,5 @@ limitations under the License.
 package builder
 
 const BuildahPlatform = "BuildahPlatform"
+const BuildahImage = "BuildahImage"
+const BuildahDefaultImageName = "quay.io/buildah/stable"
diff --git a/pkg/builder/kaniko.go b/pkg/builder/kaniko.go
index 260dc72ec..16e95a1ca 100644
--- a/pkg/builder/kaniko.go
+++ b/pkg/builder/kaniko.go
@@ -21,3 +21,7 @@ package builder
 const KanikoCacheDir = "/kaniko/cache"
 const KanikoPVCName = "KanikoPersistentVolumeClaim"
 const KanikoBuildCacheEnabled = "KanikoBuildCacheEnabled"
+const KanikoExecutorImage = "KanikoExecutorImage"
+const KanikoWarmerImage = "KanikoWarmerImage"
+const KanikoDefaultExecutorImageName = "gcr.io/kaniko-project/executor"
+const KanikoDefaultWarmerImageName = "gcr.io/kaniko-project/warmer"
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 1185c50b6..3a86bd435 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -110,7 +110,14 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *installCmdO
        cmd.Flags().String("build-publish-strategy", "", "Set the build publish 
strategy")
        cmd.Flags().String("build-timeout", "", "Set how long the build process 
can last")
        cmd.Flags().String("trait-profile", "", "The profile to use for traits")
+
+       // Kaniko
        cmd.Flags().Bool("kaniko-build-cache", false, "To enable or disable the 
Kaniko cache")
+       cmd.Flags().String("kaniko-executor-image", "", "The docker image of 
the Kaniko executor")
+       cmd.Flags().String("kaniko-warmer-image", "", "The docker image of the 
Kaniko warmer")
+
+       // Buildah
+       cmd.Flags().String("buildah-image", "", "The docker image to use for 
Buildah")
 
        // OLM
        cmd.Flags().Bool("olm", true, "Try to install everything via OLM 
(Operator Lifecycle Manager) if available")
@@ -165,6 +172,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *installCmdO
 type installCmdOptions struct {
        *RootCmdOptions
        Wait                     bool     `mapstructure:"wait"`
+       BuildahImage             string   `mapstructure:"buildah-image"`
        ClusterSetupOnly         bool     `mapstructure:"cluster-setup"`
        SkipOperatorSetup        bool     `mapstructure:"skip-operator-setup"`
        SkipClusterSetup         bool     `mapstructure:"skip-cluster-setup"`
@@ -173,6 +181,8 @@ type installCmdOptions struct {
        ExampleSetup             bool     `mapstructure:"example"`
        Global                   bool     `mapstructure:"global"`
        KanikoBuildCache         bool     `mapstructure:"kaniko-build-cache"`
+       KanikoExecutorImage      string   `mapstructure:"kaniko-executor-image"`
+       KanikoWarmerImage        string   `mapstructure:"kaniko-warmer-image"`
        Save                     bool     `mapstructure:"save" kamel:"omitsave"`
        Force                    bool     `mapstructure:"force"`
        Olm                      bool     `mapstructure:"olm"`
@@ -480,11 +490,20 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
                        }
                }
 
-               kanikoBuildCacheFlag := 
cobraCmd.Flags().Lookup("kaniko-build-cache")
-               if kanikoBuildCacheFlag.Changed {
-                       
platform.Spec.Build.PublishStrategyOptions[builder.KanikoBuildCacheEnabled] = 
strconv.FormatBool(o.KanikoBuildCache)
+               if platform.Spec.Build.PublishStrategy == 
v1.IntegrationPlatformBuildPublishStrategyKaniko {
+                       kanikoBuildCacheFlag := 
cobraCmd.Flags().Lookup("kaniko-build-cache")
+                       if kanikoBuildCacheFlag.Changed {
+                               
platform.Spec.Build.AddOption(builder.KanikoBuildCacheEnabled, 
strconv.FormatBool(o.KanikoBuildCache))
+                       }
+                       if o.KanikoExecutorImage != "" {
+                               
platform.Spec.Build.AddOption(builder.KanikoExecutorImage, 
o.KanikoExecutorImage)
+                       }
+                       if o.KanikoWarmerImage != "" {
+                               
platform.Spec.Build.AddOption(builder.KanikoWarmerImage, o.KanikoWarmerImage)
+                       }
+               } else if platform.Spec.Build.PublishStrategy == 
v1.IntegrationPlatformBuildPublishStrategyBuildah && o.BuildahImage != "" {
+                       platform.Spec.Build.AddOption(builder.BuildahImage, 
o.BuildahImage)
                }
-
                // Always create a platform in the namespace where the operator 
is located
                err = install.ObjectOrCollect(o.Context, c, namespace, 
collection, o.Force, platform)
                if err != nil {
diff --git a/pkg/cmd/install_test.go b/pkg/cmd/install_test.go
index c1aaaee59..27007a60a 100644
--- a/pkg/cmd/install_test.go
+++ b/pkg/cmd/install_test.go
@@ -164,6 +164,27 @@ func TestInstallKanikoBuildCacheFlag(t *testing.T) {
        assert.Equal(t, true, installCmdOptions.KanikoBuildCache)
 }
 
+func TestInstallKanikoExecutorImage(t *testing.T) {
+       installCmdOptions, rootCmd, _ := initializeInstallCmdOptions(t)
+       _, err := test.ExecuteCommand(rootCmd, cmdInstall, 
"--kaniko-executor-image", "some-executor-image")
+       assert.Nil(t, err)
+       assert.Equal(t, "some-executor-image", 
installCmdOptions.KanikoExecutorImage)
+}
+
+func TestInstallKanikoWarmerImage(t *testing.T) {
+       installCmdOptions, rootCmd, _ := initializeInstallCmdOptions(t)
+       _, err := test.ExecuteCommand(rootCmd, cmdInstall, 
"--kaniko-warmer-image", "some-warmer-image")
+       assert.Nil(t, err)
+       assert.Equal(t, "some-warmer-image", 
installCmdOptions.KanikoWarmerImage)
+}
+
+func TestInstallBuildahImage(t *testing.T) {
+       installCmdOptions, rootCmd, _ := initializeInstallCmdOptions(t)
+       _, err := test.ExecuteCommand(rootCmd, cmdInstall, "--buildah-image", 
"some-buildah-image")
+       assert.Nil(t, err)
+       assert.Equal(t, "some-buildah-image", installCmdOptions.BuildahImage)
+}
+
 func TestInstallLocalRepositoryFlag(t *testing.T) {
        installCmdOptions, rootCmd, _ := initializeInstallCmdOptions(t)
        _, err := test.ExecuteCommand(rootCmd, cmdInstall, 
"--maven-local-repository", "someString")
diff --git a/pkg/controller/build/build_pod.go 
b/pkg/controller/build/build_pod.go
index 1ebb74c52..42385d95c 100644
--- a/pkg/controller/build/build_pod.go
+++ b/pkg/controller/build/build_pod.go
@@ -324,9 +324,14 @@ func addBuildahTaskToPod(ctx context.Context, c 
ctrl.Reader, build *v1.Build, ta
                args = append([]string{auth}, args...)
        }
 
+       image := task.ExecutorImage
+       if image == "" {
+               image = fmt.Sprintf("%s:v%s", builder.BuildahDefaultImageName, 
defaults.BuildahVersion)
+       }
+
        container := corev1.Container{
                Name:            task.Name,
-               Image:           fmt.Sprintf("quay.io/buildah/stable:v%s", 
defaults.BuildahVersion),
+               Image:           image,
                ImagePullPolicy: corev1.PullIfNotPresent,
                Command:         []string{"/bin/sh", "-c"},
                Args:            []string{strings.Join(args, " && ")},
@@ -436,9 +441,14 @@ func addKanikoTaskToPod(ctx context.Context, c 
ctrl.Reader, build *v1.Build, tas
                })
        }
 
+       image := task.ExecutorImage
+       if image == "" {
+               image = fmt.Sprintf("%s:v%s", 
builder.KanikoDefaultExecutorImageName, defaults.KanikoVersion)
+       }
+
        container := corev1.Container{
                Name:            task.Name,
-               Image:           
fmt.Sprintf("gcr.io/kaniko-project/executor:v%s", defaults.KanikoVersion),
+               Image:           image,
                ImagePullPolicy: corev1.PullIfNotPresent,
                Args:            args,
                Env:             env,
diff --git a/pkg/controller/integrationplatform/kaniko_cache.go 
b/pkg/controller/integrationplatform/kaniko_cache.go
index 6292eea9b..4ac5e773d 100644
--- a/pkg/controller/integrationplatform/kaniko_cache.go
+++ b/pkg/controller/integrationplatform/kaniko_cache.go
@@ -46,6 +46,13 @@ func createKanikoCacheWarmerPod(ctx context.Context, client 
client.Client, platf
                pvcName = persistentVolumeClaim
        }
 
+       var warmerImage string
+       if image, found := 
platform.Status.Build.PublishStrategyOptions[builder.KanikoWarmerImage]; found {
+               warmerImage = image
+       } else {
+               warmerImage = fmt.Sprintf("%s:v%s", 
builder.KanikoDefaultWarmerImageName, defaults.KanikoVersion)
+       }
+
        pod := corev1.Pod{
                TypeMeta: metav1.TypeMeta{
                        APIVersion: corev1.SchemeGroupVersion.String(),
@@ -62,7 +69,7 @@ func createKanikoCacheWarmerPod(ctx context.Context, client 
client.Client, platf
                        Containers: []corev1.Container{
                                {
                                        Name:  "warm-kaniko-cache",
-                                       Image: 
fmt.Sprintf("gcr.io/kaniko-project/warmer:v%s", defaults.KanikoVersion),
+                                       Image: warmerImage,
                                        Args: []string{
                                                "--cache-dir=" + 
builder.KanikoCacheDir,
                                                "--image=" + 
platform.Status.Build.BaseImage,
diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go
index ed390e1eb..01631e620 100644
--- a/pkg/resources/resources.go
+++ b/pkg/resources/resources.go
@@ -117,9 +117,9 @@ var assets = func() http.FileSystem {
                "/crd/bases/camel.apache.org_builds.yaml": 
&vfsgen۰CompressedFileInfo{
                        name:             "camel.apache.org_builds.yaml",
                        modTime:          time.Time{},
-                       uncompressedSize: 43400,
+                       uncompressedSize: 43674,
 
-                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x3d\x5d\x73\x23\x39\x6e\xef\xfe\x15\xa8\xf5\xc3\x78\xaa\x2c\x69\x77\xef\x23\x1b\xa7\x52\x29\x9f\x67\x77\xcf\x99\x0f\x3b\x23\xef\xdc\xdd\x9b\xa9\x6e\x48\xe2\xa9\x9b\xec\x90\x6c\x6b\x74\xa9\xfc\xf7\x14\x41\xb2\xd5\x92\xfa\x83\xed\x8f\x99\xcd\xad\xf8\x32\xe3\x16\x09\x02\x20\x09\x80\x20\x08\x9e\xc2\xe8\xf9\xca\xc9\x29\xbc\xe3\x09\x0a\x8d\x29\x18\x09\x66\x89\x70\x59\xb0\x64\x89\x30\x95\x73\xb3\x66\x0a\xe1\x27\x59\x8a\x
 [...]
+                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x3d\x5d\x73\x23\x39\x6e\xef\xfe\x15\xa8\xf5\xc3\x78\xaa\x2c\x69\x77\xef\x23\x1b\xa7\x52\x29\x9f\x67\x77\xcf\x99\x0f\x3b\x23\xef\xdc\xdd\x9b\xa9\x6e\x48\xe2\xaa\x9b\xec\x90\x6c\x6b\x74\xa9\xfc\xf7\x14\x41\xb2\xd5\x92\xfa\x83\xed\x8f\x99\xcb\xad\xf8\x32\xe3\x16\x09\x82\x20\x08\x80\x20\x08\x9e\xc2\xe8\xf9\xca\xc9\x29\xbc\xe3\x09\x0a\x8d\x29\x18\x09\x66\x89\x70\x59\xb0\x64\x89\x30\x95\x73\xb3\x66\x0a\xe1\x27\x59\x8a\x
 [...]
                },
                "/crd/bases/camel.apache.org_camelcatalogs.yaml": 
&vfsgen۰CompressedFileInfo{
                        name:             "camel.apache.org_camelcatalogs.yaml",
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 9a90e7d46..912b8942d 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -104,7 +104,11 @@ func (t *builderTrait) Apply(e *Environment) error {
                } else {
                        t.L.Infof("User defined %s platform, will be used from 
buildah!", platform)
                }
-
+               var executorImage string
+               if image, found := 
e.Platform.Status.Build.PublishStrategyOptions[builder.BuildahImage]; found {
+                       executorImage = image
+                       t.L.Infof("User defined executor image %s will be used 
for buildah", image)
+               }
                e.BuildTasks = append(e.BuildTasks, v1.Task{Buildah: 
&v1.BuildahTask{
                        Platform: platform,
                        BaseTask: v1.BaseTask{
@@ -114,7 +118,8 @@ func (t *builderTrait) Apply(e *Environment) error {
                                Image:    getImageName(e),
                                Registry: e.Platform.Status.Build.Registry,
                        },
-                       Verbose: t.Verbose,
+                       Verbose:       t.Verbose,
+                       ExecutorImage: executorImage,
                }})
        //nolint: staticcheck,nolintlint
        case v1.IntegrationPlatformBuildPublishStrategyKaniko:
@@ -129,6 +134,12 @@ func (t *builderTrait) Apply(e *Environment) error {
                        cacheEnabled = *e.Platform.Status.Build.KanikoBuildCache
                }
 
+               var executorImage string
+               if image, found := 
e.Platform.Status.Build.PublishStrategyOptions[builder.KanikoExecutorImage]; 
found {
+                       executorImage = image
+                       t.L.Infof("User defined executor image %s will be used 
for kaniko", image)
+               }
+
                e.BuildTasks = append(e.BuildTasks, v1.Task{Kaniko: 
&v1.KanikoTask{
                        BaseTask: v1.BaseTask{
                                Name: "kaniko",
@@ -141,7 +152,8 @@ func (t *builderTrait) Apply(e *Environment) error {
                                Enabled:               &cacheEnabled,
                                PersistentVolumeClaim: persistentVolumeClaim,
                        },
-                       Verbose: t.Verbose,
+                       Verbose:       t.Verbose,
+                       ExecutorImage: executorImage,
                }})
        }
 

Reply via email to