Skip to content

Commit

Permalink
fix:the pvc creation failure does not print the success log
Browse files Browse the repository at this point in the history
  • Loading branch information
cugykw authored and tekton-robot committed Feb 8, 2023
1 parent 5b702f9 commit d7961fc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pkg/reconciler/volumeclaim/pvchandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ func (c *defaultPVCHandler) CreatePersistentVolumeClaimsForWorkspaces(ctx contex
switch {
case apierrors.IsNotFound(err):
_, err := c.clientset.CoreV1().PersistentVolumeClaims(claim.Namespace).Create(ctx, claim, metav1.CreateOptions{})
if err != nil {
if err != nil && !apierrors.IsAlreadyExists(err) {
errs = append(errs, fmt.Errorf("failed to create PVC %s: %s", claim.Name, err))
}
if err == nil || !apierrors.IsAlreadyExists(err) {

if apierrors.IsAlreadyExists(err) {
c.logger.Infof("Tried to create PersistentVolumeClaim %s in namespace %s, but it already exists",
claim.Name, claim.Namespace)
}

if err == nil {
c.logger.Infof("Created PersistentVolumeClaim %s in namespace %s", claim.Name, claim.Namespace)
}
case err != nil:
Expand Down
78 changes: 77 additions & 1 deletion pkg/reconciler/volumeclaim/pvchandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
fakek8s "k8s.io/client-go/kubernetes/fake"
client_go_testing "k8s.io/client-go/testing"
)

const actionCreate = "create"
const (
actionCreate = "create"
actionGet = "get"
)

// check that defaultPVCHandler implements PvcHandler
var _ PvcHandler = (*defaultPVCHandler)(nil)
Expand Down Expand Up @@ -157,3 +164,72 @@ func TestCreatePersistentVolumeClaimsForWorkspacesWithoutMetadata(t *testing.T)
t.Fatalf("unexpected PVC name on created PVC; exptected: %s got: %s", expectedPVCName, pvc.Name)
}
}

// TestCreateExistPersistentVolumeClaims tests that given a volumeClaimTemplate workspace.
// At first, a pvc could not be obtained, but when creating a pvc, it already existed.
// In this scenario, no new pvc will be created.
func TestCreateExistPersistentVolumeClaims(t *testing.T) {
workspaceName := "ws-with-volume-claim-template"
ownerName := "taskrun1"
workspaces := []v1beta1.WorkspaceBinding{{
Name: workspaceName,
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{},
},
}}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

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

for _, claim := range getPersistentVolumeClaims(workspaces, ownerRef, namespace) {
_, err := fakekubeclient.CoreV1().PersistentVolumeClaims(namespace).Create(ctx, claim, metav1.CreateOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

// A new ReactionFunc implementation.
// This function is called when a get action occurs and returns a not found error
fn := func(action client_go_testing.Action) (bool, runtime.Object, error) {
switch action := action.(type) {
case client_go_testing.GetActionImpl:
return true, nil, errors.NewNotFound(schema.GroupResource{}, action.Name)
default:
return false, nil, fmt.Errorf("no reaction implemented for %s", action)
}
}
fakekubeclient.Fake.PrependReactor(actionGet, "*", fn)

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

if len(fakekubeclient.Fake.Actions()) != 3 {
t.Fatalf("unexpected numer of actions; expected: %d got: %d", 3, len(fakekubeclient.Fake.Actions()))
}

actions := []string{actionCreate, actionGet, actionCreate}
for i, action := range fakekubeclient.Fake.Actions() {
if actions[i] != action.GetVerb() {
t.Fatalf("PVC action, expected: %s got: %s", actions[i], action.GetVerb())
}
}

expectedPVCName := fmt.Sprintf("%s-%s", "pvc", "5435cf73f0")
pvcList, err := fakekubeclient.CoreV1().PersistentVolumeClaims(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(pvcList.Items) != 1 {
t.Fatalf("unexpected number of created PVC; expected: %d got %d", 1, len(pvcList.Items))
}
if pvcList.Items[0].Name != expectedPVCName {
t.Fatalf("unexpected PVC name on created PVC; expected: %s got: %s", expectedPVCName, pvcList.Items[0].Name)
}
}

0 comments on commit d7961fc

Please sign in to comment.