Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow omitting metadata in volumeclaimtemplate #2453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}

// TestCreatePersistentVolumeClaimsForWorkspacesWithoutMetadata tests that given a volumeClaimTemplate workspace
// without a metadata part, a PVC is created, with the expected name.
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)
}
}