Skip to content

Commit

Permalink
[KOGITO-8298] Improve the Kaniko build time using a pre-warmed cache …
Browse files Browse the repository at this point in the history
…and desired resources (apache#51)

Signed-off-by: Davide Salerno <[email protected]>
  • Loading branch information
davidesalerno authored Dec 21, 2022
1 parent e09cdde commit ea8c6b2
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 10 deletions.
44 changes: 44 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func (b *Builder) getImageBuilder(workflowID string, imageTag string, workflowDe
return ib
}

func (b *Builder) getImageBuilderForKaniko(workflowID string, imageTag string, workflowDefinition []byte, task *api.KanikoTask) ImageBuilder {
containerFile := b.commonConfig.Data[b.commonConfig.Data[constants.DEFAULT_BUILDER_RESOURCE_NAME_KEY]]
ib := NewImageBuilder(workflowID, workflowDefinition, []byte(containerFile))
ib.OnNamespace(b.customConfig[constants.CUSTOM_NS_KEY])
ib.WithPodMiddleName(workflowID)
ib.WithInsecureRegistry(false)
ib.WithImageName(workflowID + imageTag)
ib.WithSecret(b.customConfig[constants.CUSTOM_REG_CRED_KEY])
ib.WithRegistryAddress(b.customConfig[constants.CUSTOM_REG_ADDRESS_KEY])
ib.WithCache(task.Cache)
ib.WithResources(task.Resources)
ib.WithAdditionalFlags(task.AdditionalFlags)
return ib
}

func (b *Builder) ScheduleNewBuildWithTimeout(workflowName string, imageTag string, workflowDefinition []byte, timeout time.Duration) (*api.Build, error) {
ib := b.getImageBuilder(workflowName, imageTag, workflowDefinition)
ib.WithTimeout(timeout)
Expand All @@ -72,6 +87,12 @@ func (b *Builder) ScheduleNewBuildWithContainerFile(workflowName string, imageTa
return b.BuildImage(ib.Build())
}

func (b *Builder) ScheduleNewKanikoBuildWithContainerFile(workflowName string, imageTag string, workflowDefinition []byte, build api.BuildSpec) (*api.Build, error) {
ib := b.getImageBuilderForKaniko(workflowName, imageTag, workflowDefinition, build.Tasks[0].Kaniko)
ib.WithTimeout(5 * time.Minute)
return b.BuildImage(ib.Build())
}

func (b *Builder) ReconcileBuild(build *api.Build, cli client.Client) (*api.Build, error) {
result, err := builder.FromBuild(build).WithClient(cli).Reconcile()
return result, err
Expand Down Expand Up @@ -100,6 +121,9 @@ func (b *Builder) BuildImage(kb KogitoBuilder) (*api.Build, error) {
}

build, err := builder.NewBuild(platform, kb.ImageName, kb.PodMiddleName).
WithKanikoCache(kb.Cache).
WithKanikoResources(kb.Resources).
WithKanikoAdditionalArgs(kb.AdditionalFlags).
WithResource(constants.BUILDER_RESOURCE_NAME_DEFAULT, kb.ContainerFile).
WithResource(kb.WorkflowID+b.commonConfig.Data[constants.DEFAULT_WORKFLOW_EXTENSION_KEY], kb.WorkflowDefinition).
WithClient(cli).
Expand Down Expand Up @@ -134,6 +158,9 @@ func (b *Builder) ScheduleBuild(kb KogitoBuilder) (*api.Build, error) {
}

build, err := builder.NewBuild(platform, kb.ImageName, kb.PodMiddleName).
WithKanikoCache(kb.Cache).
WithKanikoResources(kb.Resources).
WithKanikoAdditionalArgs(kb.AdditionalFlags).
WithResource(constants.BUILDER_RESOURCE_NAME_DEFAULT, kb.ContainerFile).
WithResource(kb.WorkflowID+b.commonConfig.Data[constants.DEFAULT_WORKFLOW_EXTENSION_KEY], kb.WorkflowDefinition).
WithClient(cli).
Expand All @@ -158,6 +185,9 @@ type KogitoBuilder struct {
RegistryAddress string
RegistryOrganization string
Secret string
Cache api.KanikoTaskCache
Resources corev1.ResourceRequirements
AdditionalFlags []string
}

type ImageBuilder struct {
Expand Down Expand Up @@ -203,6 +233,20 @@ func (ib *ImageBuilder) WithImageName(imageName string) *ImageBuilder {
return ib
}

func (ib *ImageBuilder) WithCache(cache api.KanikoTaskCache) *ImageBuilder {
ib.KogitoBuilder.Cache = cache
return ib
}
func (ib *ImageBuilder) WithResources(resources corev1.ResourceRequirements) *ImageBuilder {
ib.KogitoBuilder.Resources = resources
return ib
}

func (ib *ImageBuilder) WithAdditionalFlags(flags []string) *ImageBuilder {
ib.KogitoBuilder.AdditionalFlags = flags
return ib
}

func (ib *ImageBuilder) Build() KogitoBuilder {
return *ib.KogitoBuilder
}
35 changes: 35 additions & 0 deletions bundle/manifests/sw.kogito.kie.org_kogitoserverlessbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ spec:
kaniko:
description: a KanikoTask, for Kaniko strategy
properties:
additionalFlags:
description: AdditionalFlags -- List of additional
flags for the Kaniko process (see https://github.com/GoogleContainerTools/kaniko/blob/main/README.md#additional-flags)
items:
type: string
type: array
baseImage:
description: base image layer
type: string
Expand Down Expand Up @@ -136,6 +142,35 @@ spec:
stored
type: string
type: object
resources:
description: Resources -- optional compute resource
requirements for the Kaniko container
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount
of compute resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum
amount of compute resources required. If Requests
is omitted for a container, it defaults to
Limits if that is explicitly specified, otherwise
to an implementation-defined value. More info:
https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
verbose:
description: log more information
type: boolean
Expand Down
68 changes: 68 additions & 0 deletions bundle/manifests/sw.kogito.kie.org_kogitoserverlessplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ spec:
kaniko:
description: a KanikoTask, for Kaniko strategy
properties:
additionalFlags:
description: AdditionalFlags -- List of additional flags
for the Kaniko process (see https://github.com/GoogleContainerTools/kaniko/blob/main/README.md#additional-flags)
items:
type: string
type: array
baseImage:
description: base image layer
type: string
Expand Down Expand Up @@ -100,6 +106,34 @@ spec:
description: the secret where credentials are stored
type: string
type: object
resources:
description: Resources -- optional compute resource
requirements for the Kaniko container
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount
of compute resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount
of compute resources required. If Requests is
omitted for a container, it defaults to Limits
if that is explicitly specified, otherwise to
an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
verbose:
description: log more information
type: boolean
Expand Down Expand Up @@ -241,6 +275,12 @@ spec:
kaniko:
description: a KanikoTask, for Kaniko strategy
properties:
additionalFlags:
description: AdditionalFlags -- List of additional flags
for the Kaniko process (see https://github.com/GoogleContainerTools/kaniko/blob/main/README.md#additional-flags)
items:
type: string
type: array
baseImage:
description: base image layer
type: string
Expand Down Expand Up @@ -285,6 +325,34 @@ spec:
description: the secret where credentials are stored
type: string
type: object
resources:
description: Resources -- optional compute resource
requirements for the Kaniko container
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount
of compute resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount
of compute resources required. If Requests is
omitted for a container, it defaults to Limits
if that is explicitly specified, otherwise to
an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
verbose:
description: log more information
type: boolean
Expand Down
35 changes: 35 additions & 0 deletions config/crd/bases/sw.kogito.kie.org_kogitoserverlessbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ spec:
kaniko:
description: a KanikoTask, for Kaniko strategy
properties:
additionalFlags:
description: AdditionalFlags -- List of additional
flags for the Kaniko process (see https://github.com/GoogleContainerTools/kaniko/blob/main/README.md#additional-flags)
items:
type: string
type: array
baseImage:
description: base image layer
type: string
Expand Down Expand Up @@ -137,6 +143,35 @@ spec:
stored
type: string
type: object
resources:
description: Resources -- optional compute resource
requirements for the Kaniko container
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount
of compute resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum
amount of compute resources required. If Requests
is omitted for a container, it defaults to
Limits if that is explicitly specified, otherwise
to an implementation-defined value. More info:
https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
verbose:
description: log more information
type: boolean
Expand Down
Loading

0 comments on commit ea8c6b2

Please sign in to comment.