Skip to content

Commit 33ea1a0

Browse files
Fabian-Ktekton-robot
authored andcommitted
Allow the customization of the storage class for the PVC of pipelines
This commit enables to use storage classes other than the cluster-wide default storage class for the PVC of pipelines. The customization can be defined in the config map config-artifact-pvc.yaml - similar to how the size of the PVC can already be defined. By default, it will fallback to the cluster-wide default storage class. fixes #1101
1 parent 7a11326 commit 33ea1a0

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

config/config-artifact-pvc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ data:
2121
# size of the PVC volume
2222
# size: 5Gi
2323

24+
# storage class of the PVC volume
25+
# storageClassName: storage-class-name

docs/install.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The PVC option can be configured using a ConfigMap with the name
109109
`config-artifact-pvc` and the following attributes:
110110

111111
- size: the size of the volume (5Gi by default)
112+
- storageClassName: the storage class of the volume (default storage class by default)
112113

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

pkg/artifacts/artifact_storage_test.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ var (
3939
Name: "pipelineruntest",
4040
},
4141
}
42-
persistentVolumeClaim = GetPersistentVolumeClaim(DefaultPvcSize)
43-
quantityComparer = cmp.Comparer(func(x, y resource.Quantity) bool {
42+
defaultStorageClass *string = nil
43+
customStorageClass = "custom-storage-class"
44+
persistentVolumeClaim = GetPersistentVolumeClaim(DefaultPvcSize, defaultStorageClass)
45+
quantityComparer = cmp.Comparer(func(x, y resource.Quantity) bool {
4446
return x.Cmp(y) == 0
4547
})
4648
)
4749

48-
func GetPersistentVolumeClaim(size string) *corev1.PersistentVolumeClaim {
50+
func GetPersistentVolumeClaim(size string, storageClassName *string) *corev1.PersistentVolumeClaim {
4951
pvc := &corev1.PersistentVolumeClaim{
5052
ObjectMeta: metav1.ObjectMeta{Name: "pipelineruntest-pvc", Namespace: "foo", OwnerReferences: pipelinerun.GetOwnerReference()},
5153
Spec: corev1.PersistentVolumeClaimSpec{
52-
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
53-
Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse(size)}},
54+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
55+
Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse(size)}},
56+
StorageClassName: storageClassName,
5457
},
5558
}
5659
return pvc
@@ -147,7 +150,7 @@ func TestInitializeArtifactStorageWithConfigMap(t *testing.T) {
147150
expectedArtifactStorage ArtifactStorageInterface
148151
storagetype string
149152
}{{
150-
desc: "pvc configmap",
153+
desc: "pvc configmap size",
151154
configMap: &corev1.ConfigMap{
152155
ObjectMeta: metav1.ObjectMeta{
153156
Namespace: system.GetNamespace(),
@@ -160,7 +163,24 @@ func TestInitializeArtifactStorageWithConfigMap(t *testing.T) {
160163
pipelinerun: pipelinerun,
161164
expectedArtifactStorage: &v1alpha1.ArtifactPVC{
162165
Name: "pipelineruntest",
163-
PersistentVolumeClaim: GetPersistentVolumeClaim("10Gi"),
166+
PersistentVolumeClaim: GetPersistentVolumeClaim("10Gi", defaultStorageClass),
167+
},
168+
storagetype: "pvc",
169+
}, {
170+
desc: "pvc configmap storageclass",
171+
configMap: &corev1.ConfigMap{
172+
ObjectMeta: metav1.ObjectMeta{
173+
Namespace: system.GetNamespace(),
174+
Name: PvcConfigName,
175+
},
176+
Data: map[string]string{
177+
PvcStorageClassNameKey: customStorageClass,
178+
},
179+
},
180+
pipelinerun: pipelinerun,
181+
expectedArtifactStorage: &v1alpha1.ArtifactPVC{
182+
Name: "pipelineruntest",
183+
PersistentVolumeClaim: GetPersistentVolumeClaim("5Gi", &customStorageClass),
164184
},
165185
storagetype: "pvc",
166186
}, {
@@ -341,7 +361,7 @@ func TestCleanupArtifactStorage(t *testing.T) {
341361
},
342362
}} {
343363
t.Run(c.desc, func(t *testing.T) {
344-
fakekubeclient := fakek8s.NewSimpleClientset(c.configMap, GetPVCSpec(c.pipelinerun, persistentVolumeClaim.Spec.Resources.Requests["storage"]))
364+
fakekubeclient := fakek8s.NewSimpleClientset(c.configMap, GetPVCSpec(c.pipelinerun, persistentVolumeClaim.Spec.Resources.Requests["storage"], defaultStorageClass))
345365
_, err := fakekubeclient.CoreV1().PersistentVolumeClaims(c.pipelinerun.Namespace).Get(GetPVCName(c.pipelinerun), metav1.GetOptions{})
346366
if err != nil {
347367
t.Fatalf("Error getting expected PVC %s for PipelineRun %s: %s", GetPVCName(c.pipelinerun), c.pipelinerun.Name, err)

pkg/artifacts/artifacts_storage.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const (
4141

4242
// DefaultPvcSize is the default size of the PVC to create
4343
DefaultPvcSize = "5Gi"
44+
45+
// PvcStorageClassNameKey is the name of the configmap entry that specifies the storage class of the PVC to create
46+
PvcStorageClassNameKey = "storageClassName"
4447
)
4548

4649
// ArtifactStorageInterface is an interface to define the steps to copy
@@ -163,8 +166,10 @@ func createPVC(pr *v1alpha1.PipelineRun, c kubernetes.Interface) (*corev1.Persis
163166
return nil, xerrors.Errorf("failed to get PVC ConfigMap %s for %q due to error: %w", PvcConfigName, pr.Name, err)
164167
}
165168
var pvcSizeStr string
169+
var pvcStorageClassNameStr string
166170
if configMap != nil {
167171
pvcSizeStr = configMap.Data[PvcSizeKey]
172+
pvcStorageClassNameStr = configMap.Data[PvcStorageClassNameKey]
168173
}
169174
if pvcSizeStr == "" {
170175
pvcSizeStr = DefaultPvcSize
@@ -173,7 +178,14 @@ func createPVC(pr *v1alpha1.PipelineRun, c kubernetes.Interface) (*corev1.Persis
173178
if err != nil {
174179
return nil, xerrors.Errorf("failed to create Persistent Volume spec for %q due to error: %w", pr.Name, err)
175180
}
176-
pvcSpec := GetPVCSpec(pr, pvcSize)
181+
var pvcStorageClassName *string
182+
if pvcStorageClassNameStr == "" {
183+
pvcStorageClassName = nil
184+
} else {
185+
pvcStorageClassName = &pvcStorageClassNameStr
186+
}
187+
188+
pvcSpec := GetPVCSpec(pr, pvcSize, pvcStorageClassName)
177189
pvc, err := c.CoreV1().PersistentVolumeClaims(pr.Namespace).Create(pvcSpec)
178190
if err != nil {
179191
return nil, xerrors.Errorf("failed to claim Persistent Volume %q due to error: %w", pr.Name, err)
@@ -197,7 +209,7 @@ func deletePVC(pr *v1alpha1.PipelineRun, c kubernetes.Interface) error {
197209
}
198210

199211
// GetPVCSpec returns the PVC to create for a given PipelineRun
200-
func GetPVCSpec(pr *v1alpha1.PipelineRun, pvcSize resource.Quantity) *corev1.PersistentVolumeClaim {
212+
func GetPVCSpec(pr *v1alpha1.PipelineRun, pvcSize resource.Quantity, storageClassName *string) *corev1.PersistentVolumeClaim {
201213
return &corev1.PersistentVolumeClaim{
202214
ObjectMeta: metav1.ObjectMeta{
203215
Namespace: pr.Namespace,
@@ -211,6 +223,7 @@ func GetPVCSpec(pr *v1alpha1.PipelineRun, pvcSize resource.Quantity) *corev1.Per
211223
corev1.ResourceStorage: pvcSize,
212224
},
213225
},
226+
StorageClassName: storageClassName,
214227
},
215228
}
216229
}

0 commit comments

Comments
 (0)