Skip to content

Commit 7a2bc9c

Browse files
jlpetterssontekton-robot
authored andcommittedApr 22, 2020
Allow omitting metadata in volumeclaimtemplate
Workspaces can be backed by a PVC that is created from a user provied template, e.g. workspaces: - name: ws volumeClaimTemplate: metadata: name: ws-pvc spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi but this might fail if the user did not provide a `metadata.name` on the template, since the PVC name would be `-<ws-name>-<run-name>` and names starting with a `-` is not allowed for PVCs. This commits omit the first `-` in the PVC name, if the user did not provide a name or metadata part of the volumeclaimtemplate. I also added license banners to the two pvc-related go files. Fixes #2450
1 parent f583d07 commit 7a2bc9c

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
 

‎pkg/reconciler/volumeclaim/pvchandler.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2020 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package volumeclaim
218

319
import (
@@ -74,5 +90,8 @@ func getPersistentVolumeClaims(workspaceBindings []v1alpha1.WorkspaceBinding, ow
7490
// GetPersistentVolumeClaimName gets the name of PersistentVolumeClaim for a Workspace and PipelineRun or TaskRun. claim
7591
// must be a PersistentVolumeClaim from set's VolumeClaims template.
7692
func GetPersistentVolumeClaimName(claim *corev1.PersistentVolumeClaim, wb v1alpha1.WorkspaceBinding, owner metav1.OwnerReference) string {
93+
if claim.Name == "" {
94+
return fmt.Sprintf("%s-%s", wb.Name, owner.Name)
95+
}
7796
return fmt.Sprintf("%s-%s-%s", claim.Name, wb.Name, owner.Name)
7897
}

‎pkg/reconciler/volumeclaim/pvchandler_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2020 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package volumeclaim
218

319
import (
@@ -94,3 +110,44 @@ func TestCreatePersistentVolumeClaimsForWorkspaces(t *testing.T) {
94110
t.Fatalf("unexptected name in ownerreference on created PVC; expected: %s got %s", ownerName, pvc.OwnerReferences[0].Name)
95111
}
96112
}
113+
114+
// TestCreatePersistentVolumeClaimsForWorkspacesWithoutMetadata tests that given a volumeClaimTemplate workspace
115+
// without a metadata part, a PVC is created, with the expected name.
116+
func TestCreatePersistentVolumeClaimsForWorkspacesWithoutMetadata(t *testing.T) {
117+
118+
// given
119+
120+
// workspace with volumeClaimTemplate without metadata
121+
workspaceName := "ws-with-volume-claim-template-without-metadata"
122+
ownerName := "taskrun1"
123+
workspaces := []v1alpha1.WorkspaceBinding{{
124+
Name: workspaceName,
125+
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
126+
Spec: corev1.PersistentVolumeClaimSpec{},
127+
},
128+
}}
129+
130+
ownerRef := metav1.OwnerReference{Name: ownerName}
131+
namespace := "ns"
132+
fakekubeclient := fakek8s.NewSimpleClientset()
133+
pvcHandler := defaultPVCHandler{fakekubeclient, zap.NewExample().Sugar()}
134+
135+
// when
136+
137+
err := pvcHandler.CreatePersistentVolumeClaimsForWorkspaces(workspaces, ownerRef, namespace)
138+
if err != nil {
139+
t.Fatalf("unexpexted error: %v", err)
140+
}
141+
142+
expectedPVCName := fmt.Sprintf("%s-%s", workspaceName, ownerName)
143+
pvc, err := fakekubeclient.CoreV1().PersistentVolumeClaims(namespace).Get(expectedPVCName, metav1.GetOptions{})
144+
if err != nil {
145+
t.Fatalf("unexpected error: %v", err)
146+
}
147+
148+
// that
149+
150+
if pvc.Name != expectedPVCName {
151+
t.Fatalf("unexpected PVC name on created PVC; exptected: %s got: %s", expectedPVCName, pvc.Name)
152+
}
153+
}

0 commit comments

Comments
 (0)
Please sign in to comment.