Skip to content

Commit

Permalink
Refactor kaniko settings
Browse files Browse the repository at this point in the history
fix #3005
  • Loading branch information
mmelko authored and squakez committed Mar 11, 2022
1 parent 5b08eed commit a79f832
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 41 deletions.
24 changes: 18 additions & 6 deletions config/crd/bases/camel.apache.org_integrationplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ spec:
build:
description: specify how to build the Integration/IntegrationKits
properties:
PublishStrategyOptions:
additionalProperties:
type: string
type: object
baseImage:
description: a base image that can be used as base layer for all
images. It can be useful if you want to provide some custom
Expand All @@ -83,7 +87,8 @@ spec:
- pod
type: string
kanikoBuildCache:
description: enables Kaniko publish strategy cache
description: 'Deprecated: Use PublishStrategyOptions instead enables
Kaniko publish strategy cache'
type: boolean
maven:
description: Maven configuration used to build the Camel/Camel-Quarkus
Expand Down Expand Up @@ -192,8 +197,9 @@ spec:
type: object
type: object
persistentVolumeClaim:
description: the Persistent Volume Claim used by Kaniko publish
strategy, if cache is enabled
description: 'Deprecated: Use PublishStrategyOptions instead the
Persistent Volume Claim used by Kaniko publish strategy, if
cache is enabled'
type: string
publishStrategy:
description: the strategy to adopt for publishing an Integration
Expand Down Expand Up @@ -311,6 +317,10 @@ spec:
build:
description: specify how to build the Integration/IntegrationKits
properties:
PublishStrategyOptions:
additionalProperties:
type: string
type: object
baseImage:
description: a base image that can be used as base layer for all
images. It can be useful if you want to provide some custom
Expand All @@ -324,7 +334,8 @@ spec:
- pod
type: string
kanikoBuildCache:
description: enables Kaniko publish strategy cache
description: 'Deprecated: Use PublishStrategyOptions instead enables
Kaniko publish strategy cache'
type: boolean
maven:
description: Maven configuration used to build the Camel/Camel-Quarkus
Expand Down Expand Up @@ -433,8 +444,9 @@ spec:
type: object
type: object
persistentVolumeClaim:
description: the Persistent Volume Claim used by Kaniko publish
strategy, if cache is enabled
description: 'Deprecated: Use PublishStrategyOptions instead the
Persistent Volume Claim used by Kaniko publish strategy, if
cache is enabled'
type: string
publishStrategy:
description: the strategy to adopt for publishing an Integration
Expand Down
9 changes: 9 additions & 0 deletions docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1603,15 +1603,24 @@ bool
|
Deprecated: Use PublishStrategyOptions instead
enables Kaniko publish strategy cache
|`persistentVolumeClaim` +
string
|
Deprecated: Use PublishStrategyOptions instead
the Persistent Volume Claim used by Kaniko publish strategy, if cache is enabled
|`PublishStrategyOptions` +
map[string]string
|
|===
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,7 @@ k8s.io/code-generator v0.18.2/go.mod h1:+UHX5rSbxmR8kzS+FAv7um6dtYrZokQvjHpDSYRV
k8s.io/code-generator v0.18.6/go.mod h1:TgNEVx9hCyPGpdtCWA34olQYLkh3ok9ar7XfSsr8b6c=
k8s.io/code-generator v0.19.2/go.mod h1:moqLn7w0t9cMs4+5CQyxnfA/HV8MF6aAVENF+WZZhgk=
k8s.io/code-generator v0.22.2/go.mod h1:eV77Y09IopzeXOJzndrDyCI88UBok2h6WxAlBwpxa+o=
k8s.io/code-generator v0.22.5 h1:jn+mYXI5q7rzo7Bz/n8xZIgbe61SeXlIjU5jA8jLVps=
k8s.io/code-generator v0.22.5/go.mod h1:sbdWCOVob+KaQ5O7xs8PNNaCTpbWVqNgA6EPwLOmRNk=
k8s.io/component-base v0.18.2/go.mod h1:kqLlMuhJNHQ9lz8Z7V5bxUUtjFZnrypArGl58gmDfUM=
k8s.io/component-base v0.18.6/go.mod h1:knSVsibPR5K6EW2XOjEHik6sdU5nCvKMrzMt2D4In14=
Expand Down
24 changes: 18 additions & 6 deletions helm/camel-k/crds/crd-integration-platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ spec:
build:
description: specify how to build the Integration/IntegrationKits
properties:
PublishStrategyOptions:
additionalProperties:
type: string
type: object
baseImage:
description: a base image that can be used as base layer for all
images. It can be useful if you want to provide some custom
Expand All @@ -83,7 +87,8 @@ spec:
- pod
type: string
kanikoBuildCache:
description: enables Kaniko publish strategy cache
description: 'Deprecated: Use PublishStrategyOptions instead enables
Kaniko publish strategy cache'
type: boolean
maven:
description: Maven configuration used to build the Camel/Camel-Quarkus
Expand Down Expand Up @@ -192,8 +197,9 @@ spec:
type: object
type: object
persistentVolumeClaim:
description: the Persistent Volume Claim used by Kaniko publish
strategy, if cache is enabled
description: 'Deprecated: Use PublishStrategyOptions instead the
Persistent Volume Claim used by Kaniko publish strategy, if
cache is enabled'
type: string
publishStrategy:
description: the strategy to adopt for publishing an Integration
Expand Down Expand Up @@ -311,6 +317,10 @@ spec:
build:
description: specify how to build the Integration/IntegrationKits
properties:
PublishStrategyOptions:
additionalProperties:
type: string
type: object
baseImage:
description: a base image that can be used as base layer for all
images. It can be useful if you want to provide some custom
Expand All @@ -324,7 +334,8 @@ spec:
- pod
type: string
kanikoBuildCache:
description: enables Kaniko publish strategy cache
description: 'Deprecated: Use PublishStrategyOptions instead enables
Kaniko publish strategy cache'
type: boolean
maven:
description: Maven configuration used to build the Camel/Camel-Quarkus
Expand Down Expand Up @@ -433,8 +444,9 @@ spec:
type: object
type: object
persistentVolumeClaim:
description: the Persistent Volume Claim used by Kaniko publish
strategy, if cache is enabled
description: 'Deprecated: Use PublishStrategyOptions instead the
Persistent Volume Claim used by Kaniko publish strategy, if
cache is enabled'
type: string
publishStrategy:
description: the strategy to adopt for publishing an Integration
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/camel/v1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ type IntegrationPlatformBuildSpec struct {
Timeout *metav1.Duration `json:"timeout,omitempty"`
// Maven configuration used to build the Camel/Camel-Quarkus applications
Maven MavenSpec `json:"maven,omitempty"`
// Deprecated: Use PublishStrategyOptions instead
// enables Kaniko publish strategy cache
KanikoBuildCache *bool `json:"kanikoBuildCache,omitempty"`
// the Persistent Volume Claim used by Kaniko publish strategy, if cache is enabled
// Deprecated: Use PublishStrategyOptions instead
//the Persistent Volume Claim used by Kaniko publish strategy, if cache is enabled
PersistentVolumeClaim string `json:"persistentVolumeClaim,omitempty"`
//
PublishStrategyOptions map[string]string `json:"PublishStrategyOptions,omitempty"`
}

// IntegrationPlatformKameletSpec define the behavior for all the Kamelets controller by the IntegrationPlatform
Expand Down
17 changes: 11 additions & 6 deletions pkg/apis/camel/v1/integrationplatform_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package v1

import (
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -178,13 +179,17 @@ func (in *IntegrationPlatformStatus) RemoveCondition(condType IntegrationPlatfor
in.Conditions = newConditions
}

// IsKanikoCacheEnabled tells if the KanikoCache is enabled on the integration platform build spec
func (b IntegrationPlatformBuildSpec) IsKanikoCacheEnabled() bool {
if b.KanikoBuildCache == nil {
// Cache is disabled by default
return false
// IsOptionEnabled tells if provided option key is present in PublishStrategyOptions and enabled
func (b IntegrationPlatformBuildSpec) IsOptionEnabled(option string) bool {
//Key defined in builder/kaniko.go
if enabled, ok := b.PublishStrategyOptions[option]; ok {
res, err := strconv.ParseBool(enabled)
if err != nil {
return false
}
return res
}
return *b.KanikoBuildCache
return false
}

// GetTimeout returns the specified duration or a default one
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/camel/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/builder/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ package builder

// KanikoCacheDir is the cache directory for Kaniko builds (mounted into the Kaniko pod).
const KanikoCacheDir = "/kaniko/cache"
const KanikoPVCName = "KanikoPersistentVolumeClaim"
const KanikoBuildCacheEnabled = "KanikoBuildCacheEnabled"
8 changes: 8 additions & 0 deletions pkg/client/camel/informers/externalversions/factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -39,6 +40,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/builder"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/install"
"github.com/apache/camel-k/pkg/util"
Expand Down Expand Up @@ -443,7 +445,7 @@ func (o *installCmdOptions) install(cobraCmd *cobra.Command, _ []string) error {

kanikoBuildCacheFlag := cobraCmd.Flags().Lookup("kaniko-build-cache")
if kanikoBuildCacheFlag.Changed {
platform.Spec.Build.KanikoBuildCache = &o.KanikoBuildCache
platform.Spec.Build.PublishStrategyOptions[builder.KanikoBuildCacheEnabled] = strconv.FormatBool(o.KanikoBuildCache)
}

// Always create a platform in the namespace where the operator is located
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/trait_help_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build common
// +build common

/*
Expand Down
16 changes: 13 additions & 3 deletions pkg/controller/integrationplatform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/builder"
"github.com/apache/camel-k/pkg/client"
platformutil "github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/defaults"
Expand Down Expand Up @@ -68,9 +69,13 @@ func (action *initializeAction) Handle(ctx context.Context, platform *v1.Integra
if err = platformutil.ConfigureDefaults(ctx, action.client, platform, true); err != nil {
return nil, err
}

// nolint: staticcheck
if platform.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyKaniko {
if platform.Status.Build.IsKanikoCacheEnabled() {
cacheEnabled := platform.Status.Build.IsOptionEnabled(builder.KanikoBuildCacheEnabled)
if _, found := platform.Status.Build.PublishStrategyOptions[builder.KanikoBuildCacheEnabled]; !found {
cacheEnabled = *platform.Status.Build.KanikoBuildCache
}
if cacheEnabled {
// Create the persistent volume claim used by the Kaniko cache
action.L.Info("Create persistent volume claim")
err := createPersistentVolumeClaim(ctx, action.client, platform)
Expand Down Expand Up @@ -120,6 +125,11 @@ func createPersistentVolumeClaim(ctx context.Context, client client.Client, plat
if err != nil {
return err
}
// nolint: staticcheck
pvcName := platform.Status.Build.PersistentVolumeClaim
if persistentVolumeClaim, found := platform.Status.Build.PublishStrategyOptions[builder.KanikoPVCName]; found {
pvcName = persistentVolumeClaim
}

pvc := &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
Expand All @@ -128,7 +138,7 @@ func createPersistentVolumeClaim(ctx context.Context, client client.Client, plat
},
ObjectMeta: metav1.ObjectMeta{
Namespace: platform.Namespace,
Name: platform.Status.Build.PersistentVolumeClaim,
Name: pvcName,
Labels: map[string]string{
"app": "camel-k",
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/integrationplatform/kaniko_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func createKanikoCacheWarmerPod(ctx context.Context, client client.Client, platf
// See:
// - https://kubernetes.io/docs/concepts/storage/persistent-volumes/#node-affinity
// - https://kubernetes.io/docs/concepts/storage/volumes/#local
// nolint: staticcheck
pvcName := platform.Status.Build.PersistentVolumeClaim
if persistentVolumeClaim, found := platform.Status.Build.PublishStrategyOptions[builder.KanikoPVCName]; found {
pvcName = persistentVolumeClaim
}

pod := corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Expand Down Expand Up @@ -91,7 +97,7 @@ func createKanikoCacheWarmerPod(ctx context.Context, client client.Client, platf
Name: "kaniko-cache",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: platform.Status.Build.PersistentVolumeClaim,
ClaimName: pvcName,
},
},
},
Expand Down
Loading

0 comments on commit a79f832

Please sign in to comment.