Skip to content

Commit

Permalink
Add buildCacheMB helm chart value
Browse files Browse the repository at this point in the history
- This value is used to set the size of the persistent disk we allocate
  for caching when building images with kpack.
- it defaults to 2GiB which is similar to the 2GB default size that
  kpack allocates.
- The new setting allows customers building large applicaitons to take
  advantage of caching.

  #2634
  • Loading branch information
julian-hj committed Jun 27, 2023
1 parent f52cf8e commit cb13ea1
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Here are all the values that can be set for the chart:
- `lifecycle`: Default lifecycle for apps.
- `stack` (_String_): Stack.
- `stagingRequirements`:
- `buildCacheMB` (_Integer_): Persistent disk in MB for caching staging artifacts across builds.
- `diskMB` (_Integer_): Disk in MB for staging.
- `memoryMB` (_Integer_): Memory in MB for staging.
- `type` (_String_): Lifecycle type (only `buildpack` accepted currently).
Expand Down
12 changes: 9 additions & 3 deletions controllers/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ControllerConfig struct {
BuilderReadinessTimeout string `yaml:"builderReadinessTimeout"`
ContainerRepositoryPrefix string `yaml:"containerRepositoryPrefix"`
ContainerRegistryType string `yaml:"containerRegistryType"`
BuildCacheMB int `yaml:"buildCacheMB"`
}

type CFProcessDefaults struct {
Expand All @@ -50,9 +51,10 @@ type CFProcessDefaults struct {
}

const (
defaultTaskTTL = 30 * 24 * time.Hour
defaultTimeout int64 = 60
defaultJobTTL = 24 * time.Hour
defaultTaskTTL = 30 * 24 * time.Hour
defaultTimeout int64 = 60
defaultJobTTL = 24 * time.Hour
defaultBuildCacheMB = 2048
)

func LoadFromPath(path string) (*ControllerConfig, error) {
Expand All @@ -70,6 +72,10 @@ func LoadFromPath(path string) (*ControllerConfig, error) {
config.SpaceFinalizerAppDeletionTimeout = tools.PtrTo(defaultTimeout)
}

if config.BuildCacheMB == 0 {
config.BuildCacheMB = defaultBuildCacheMB
}

return &config, nil
}

Expand Down
12 changes: 12 additions & 0 deletions controllers/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _ = Describe("LoadFromPath", func() {
JobTTL: "jobTTL",
LogLevel: zapcore.DebugLevel,
SpaceFinalizerAppDeletionTimeout: tools.PtrTo(int64(42)),
BuildCacheMB: 1024,
}
})

Expand Down Expand Up @@ -80,6 +81,7 @@ var _ = Describe("LoadFromPath", func() {
JobTTL: "jobTTL",
LogLevel: zapcore.DebugLevel,
SpaceFinalizerAppDeletionTimeout: tools.PtrTo(int64(42)),
BuildCacheMB: 1024,
}))
})

Expand Down Expand Up @@ -112,6 +114,16 @@ var _ = Describe("LoadFromPath", func() {
Expect(retConfig.SpaceFinalizerAppDeletionTimeout).To(gstruct.PointTo(Equal(int64(60))))
})
})

When("the staging build cache size is not set", func() {
BeforeEach(func() {
cfg.BuildCacheMB = 0
})

It("uses the default", func() {
Expect(retConfig.BuildCacheMB).To(Equal(2048))
})
})
})

var _ = Describe("ParseTaskTTL", func() {
Expand Down
1 change: 1 addition & 0 deletions helm/korifi/controllers/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ data:
builderReadinessTimeout: {{ required "builderReadinessTimeout is required" .Values.kpackImageBuilder.builderReadinessTimeout }}
containerRepositoryPrefix: {{ .Values.global.containerRepositoryPrefix | quote }}
builderServiceAccount: kpack-service-account
buildCacheMB: {{ .Values.api.lifecycle.stagingRequirements.buildCacheMB }}
{{- if .Values.global.eksContainerRegistryRoleARN }}
containerRegistryType: "ECR"
{{- end }}
Expand Down
6 changes: 5 additions & 1 deletion helm/korifi/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@
"diskMB": {
"description": "Disk in MB for staging.",
"type": "integer"
},
"buildCacheMB": {
"description": "Persistent disk in MB for caching staging artifacts across builds.",
"type": "integer"
}
},
"required": ["memoryMB", "diskMB"]
"required": ["memoryMB", "diskMB", "buildCacheMB"]
}
},
"required": ["type", "stack", "stagingRequirements"]
Expand Down
1 change: 1 addition & 0 deletions helm/korifi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ api:
stagingRequirements:
memoryMB: 1024
diskMB: 1024
buildCacheMB: 2048

builderName: kpack-image-builder
userCertificateExpirationWarningDuration: 168h
Expand Down
15 changes: 13 additions & 2 deletions kpack-image-builder/controllers/buildworkload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -648,7 +649,12 @@ func (r *BuildWorkloadReconciler) reconcileKpackImage(
return err
}

_, err := controllerutil.CreateOrPatch(ctx, r.k8sClient, &desiredKpackImage, func() error {
cacheSize, err := resource.ParseQuantity(fmt.Sprintf("%dMi", r.controllerConfig.BuildCacheMB))
if err != nil {
log.Error(err, "failed to parse image cache size")
return err
}
_, err = controllerutil.CreateOrPatch(ctx, r.k8sClient, &desiredKpackImage, func() error {
desiredKpackImage.Labels = map[string]string{
BuildWorkloadLabelKey: buildWorkload.Name,
}
Expand All @@ -671,6 +677,11 @@ func (r *BuildWorkloadReconciler) reconcileKpackImage(
Services: buildWorkload.Spec.Services,
Env: buildWorkload.Spec.Env,
},
Cache: &buildv1alpha2.ImageCacheConfig{
Volume: &buildv1alpha2.ImagePersistentVolumeCache{
Size: &cacheSize,
},
},
}
if customBuilderName != "" {
desiredKpackImage.Spec.Builder.Kind = "Builder"
Expand All @@ -679,7 +690,7 @@ func (r *BuildWorkloadReconciler) reconcileKpackImage(
}

// Cannot use SetControllerReference here as multiple BuildWorkloads can "own" the same Image.
err := controllerutil.SetOwnerReference(buildWorkload, &desiredKpackImage, r.scheme)
err = controllerutil.SetOwnerReference(buildWorkload, &desiredKpackImage, r.scheme)
if err != nil {
log.Info("failed to set OwnerRef on Kpack Image", "reason", err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -160,6 +161,7 @@ var _ = Describe("BuildWorkloadReconciler", func() {

g.Expect(kpackImage.Spec.Builder.Kind).To(Equal("ClusterBuilder"))
g.Expect(kpackImage.Spec.Builder.Name).To(Equal("cf-kpack-builder"))
g.Expect(kpackImage.Spec.Cache.Volume.Size.Equal(resource.MustParse("1024Mi"))).To(BeTrue())
}).Should(Succeed())
})

Expand Down
1 change: 1 addition & 0 deletions kpack-image-builder/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var _ = BeforeSuite(func() {
ClusterBuilderName: "cf-kpack-builder",
ContainerRepositoryPrefix: "image/registry/tag",
BuilderServiceAccount: "builder-service-account",
BuildCacheMB: 1024,
}

imageRepoCreator = new(fake.RepositoryCreator)
Expand Down
4 changes: 4 additions & 0 deletions scripts/assets/values-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ api:
apiServer:
url: localhost
image: cloudfoundry/korifi-api:latest
lifecycle:
stagingRequirements:
buildCacheMB: 1024


controllers:
taskTTL: 5s
Expand Down

0 comments on commit cb13ea1

Please sign in to comment.