Skip to content

Commit

Permalink
Allow configuration of PVC size from ConfigMap
Browse files Browse the repository at this point in the history
PVCs are hardcoded to 5Gi and that should be configurable.
For instance Alibaba Container Service does not allow volumes smaller than 20Gi

Add a `config-artifact-pvc` ConfigMap that can be used to store such configuration
  • Loading branch information
carlossg committed May 15, 2019
1 parent 4dcc0d9 commit 932b53d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
23 changes: 23 additions & 0 deletions config/config-artifact-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2018 The Knative Authors
#
# Licensed 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
#
# https://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.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-artifact-bucket
namespace: tekton-pipelines
data:
# size of the PVC volume
# size: 5Gi

9 changes: 7 additions & 2 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ Pipelines need a way to share resources between tasks. The alternatives are a
[Persistent volume](https://kubernetes.io/docs/concepts/storage/persistent-volumes/)
or a [GCS storage bucket](https://cloud.google.com/storage/)

The PVC option does not require any configuration, but the GCS storage bucket
can be configured using a ConfigMap with the name `config-artifact-bucket` with
The PVC option can be configured using a ConfigMap with the name
`config-artifact-pvc` and the following attributes:

- size: the size of the volume

The GCS storage bucket can be configured
using a ConfigMap with the name `config-artifact-bucket` with
the following attributes:

- location: the address of the bucket (for example gs://mybucket)
Expand Down
36 changes: 36 additions & 0 deletions pkg/artifacts/artifact_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,39 @@ func TestGetArtifactStorageWithoutConfigMap(t *testing.T) {
t.Fatalf("want %v, but got %v", expectedArtifactPVC, pvc)
}
}

func TestGetArtifactStorageWithPvcConfigMap(t *testing.T) {
logger := logtesting.TestLogger(t)
prName := "pipelineruntest"
for _, c := range []struct {
desc string
configMap *corev1.ConfigMap
expectedArtifactStorage ArtifactStorageInterface
}{{
desc: "valid bucket",
configMap: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: system.GetNamespace(),
Name: PvcConfigName,
},
Data: map[string]string{
PvcSizeKey: "10Gi",
},
},
expectedArtifactStorage: &v1alpha1.ArtifactPVC{},
},
} {
t.Run(c.desc, func(t *testing.T) {
fakekubeclient := fakek8s.NewSimpleClientset(c.configMap)

bucket, err := GetArtifactStorage(prName, fakekubeclient, logger)
if err != nil {
t.Fatalf("Somehow had error initializing artifact storage run out of fake client: %s", err)
}

if diff := cmp.Diff(bucket, c.expectedArtifactStorage); diff != "" {
t.Fatalf("want %v, but got %v", c.expectedArtifactStorage, bucket)
}
})
}
}
36 changes: 31 additions & 5 deletions pkg/artifacts/artifacts_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import (
"k8s.io/client-go/kubernetes"
)

const (
// PvcConfigName is the name of the configmap containing all
// customizations for the storage PVC.
PvcConfigName = "config-artifact-pvc"

// PvcSizeKey is the name of the configmap entry that specifies the size of the PVC to create
PvcSizeKey = "size"

// DefaultPvcSize is the default size of the PVC to create
DefaultPvcSize = "5Gi"
)

// ArtifactStorageInterface is an interface to define the steps to copy
// an pipeline artifact to/from temporary storage
type ArtifactStorageInterface interface {
Expand Down Expand Up @@ -125,7 +137,23 @@ func NewArtifactBucketConfigFromConfigMap(configMap *corev1.ConfigMap) (*v1alpha
func createPVC(pr *v1alpha1.PipelineRun, c kubernetes.Interface) error {
if _, err := c.CoreV1().PersistentVolumeClaims(pr.Namespace).Get(getPVCName(pr), metav1.GetOptions{}); err != nil {
if errors.IsNotFound(err) {
pvc := getPVCSpec(pr)

configMap, err := c.CoreV1().ConfigMaps(system.GetNamespace()).Get(PvcConfigName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to get PVC ConfigMap %s for %q due to error: %s", PvcConfigName, pr.Name, err)
}
var pvcSizeStr string
if configMap != nil {
pvcSizeStr = configMap.Data[PvcSizeKey]
}
if pvcSizeStr == "" {
pvcSizeStr = DefaultPvcSize
}
pvcSize, err := resource.ParseQuantity(pvcSizeStr)
if err != nil {
return fmt.Errorf("failed to create Persistent Volume spec for %q due to error: %s", pr.Name, err)
}
pvc := getPVCSpec(pr, pvcSize)
if _, err := c.CoreV1().PersistentVolumeClaims(pr.Namespace).Create(pvc); err != nil {
return fmt.Errorf("failed to claim Persistent Volume %q due to error: %s", pr.Name, err)
}
Expand All @@ -136,9 +164,7 @@ func createPVC(pr *v1alpha1.PipelineRun, c kubernetes.Interface) error {
return nil
}

func getPVCSpec(pr *v1alpha1.PipelineRun) *corev1.PersistentVolumeClaim {
// TODO(shashwathi): make this value configurable
pvcSizeBytes := int64(5 * 1024 * 1024 * 1024) // 5 GBs
func getPVCSpec(pr *v1alpha1.PipelineRun, pvcSize resource.Quantity) *corev1.PersistentVolumeClaim {
return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: pr.Namespace,
Expand All @@ -149,7 +175,7 @@ func getPVCSpec(pr *v1alpha1.PipelineRun) *corev1.PersistentVolumeClaim {
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: *resource.NewQuantity(pvcSizeBytes, resource.BinarySI),
corev1.ResourceStorage: pvcSize,
},
},
},
Expand Down

0 comments on commit 932b53d

Please sign in to comment.