Skip to content

Commit

Permalink
Allow omitting metadata in volumeclaimtemplate
Browse files Browse the repository at this point in the history
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 tektoncd#2450
  • Loading branch information
jlpettersson committed Apr 21, 2020
1 parent 1346656 commit 9307b1a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/reconciler/volumeclaim/pvchandler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 The Tekton 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
http://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.
*/

package volumeclaim

import (
Expand Down Expand Up @@ -74,5 +90,8 @@ func getPersistentVolumeClaims(workspaceBindings []v1alpha1.WorkspaceBinding, ow
// GetPersistentVolumeClaimName gets the name of PersistentVolumeClaim for a Workspace and PipelineRun or TaskRun. claim
// must be a PersistentVolumeClaim from set's VolumeClaims template.
func GetPersistentVolumeClaimName(claim *corev1.PersistentVolumeClaim, wb v1alpha1.WorkspaceBinding, owner metav1.OwnerReference) string {
if claim.Name == "" {
return fmt.Sprintf("%s-%s", wb.Name, owner.Name)
}
return fmt.Sprintf("%s-%s-%s", claim.Name, wb.Name, owner.Name)
}
57 changes: 57 additions & 0 deletions pkg/reconciler/volumeclaim/pvchandler_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 The Tekton 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
http://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.
*/

package volumeclaim

import (
Expand Down Expand Up @@ -94,3 +110,44 @@ func TestCreatePersistentVolumeClaimsForWorkspaces(t *testing.T) {
t.Fatalf("unexptected name in ownerreference on created PVC; expected: %s got %s", ownerName, pvc.OwnerReferences[0].Name)
}
}

// TestCreatePersistentVolumeClaimsForWorkspaces tests that given a TaskRun with volumeClaimTemplate workspace,
// a PVC is created, with the expected name and that it has the expected OwnerReference.
func TestCreatePersistentVolumeClaimsForWorkspacesWithoutMetadata(t *testing.T) {

// given

// workspace with volumeClaimTemplate without metadata
workspaceName := "ws-with-volume-claim-template-without-metadata"
ownerName := "taskrun1"
workspaces := []v1alpha1.WorkspaceBinding{{
Name: workspaceName,
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{},
},
}}

ownerRef := metav1.OwnerReference{Name: ownerName}
namespace := "ns"
fakekubeclient := fakek8s.NewSimpleClientset()
pvcHandler := defaultPVCHandler{fakekubeclient, zap.NewExample().Sugar()}

// when

err := pvcHandler.CreatePersistentVolumeClaimsForWorkspaces(workspaces, ownerRef, namespace)
if err != nil {
t.Fatalf("unexpexted error: %v", err)
}

expectedPVCName := fmt.Sprintf("%s-%s", workspaceName, ownerName)
pvc, err := fakekubeclient.CoreV1().PersistentVolumeClaims(namespace).Get(expectedPVCName, metav1.GetOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

// that

if pvc.Name != expectedPVCName {
t.Fatalf("unexpected PVC name on created PVC; exptected: %s got: %s", expectedPVCName, pvc.Name)
}
}

0 comments on commit 9307b1a

Please sign in to comment.