Skip to content

Commit

Permalink
Adding accessmode to Create PVCs and PVs (#919)
Browse files Browse the repository at this point in the history
* adding accessmode to Create PVCs and PVs

* missed function calls

* After carls review
  • Loading branch information
bathina2 committed Mar 5, 2021
1 parent 66c42f4 commit af35060
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/function/create_volume_from_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func createVolumeFromSnapshot(ctx context.Context, cli kubernetes.Interface, nam
}

annotations := map[string]string{}
pvc, err := kubevolume.CreatePVC(ctx, cli, namespace, pvcName, vol.SizeInBytes, vol.ID, annotations)
pvc, err := kubevolume.CreatePVC(ctx, cli, namespace, pvcName, vol.SizeInBytes, vol.ID, annotations, nil)
if err != nil {
return nil, errors.Wrapf(err, "Unable to create PVC for volume %v", *vol)
}
pv, err := kubevolume.CreatePV(ctx, cli, vol, vol.Type, annotations)
pv, err := kubevolume.CreatePV(ctx, cli, vol, vol.Type, annotations, nil)
if err != nil {
return nil, errors.Wrapf(err, "Unable to create PV for volume %v", *vol)
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/kube/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ const (
// An empty 'targetVolID' indicates the caller would like the PV to be dynamically provisioned
// An empty 'name' indicates the caller would like the name to be auto-generated
// An error indicating that the PVC already exists is ignored (for idempotency)
func CreatePVC(ctx context.Context, kubeCli kubernetes.Interface, ns string, name string, sizeInBytes int64, targetVolID string, annotations map[string]string) (string, error) {
func CreatePVC(ctx context.Context, kubeCli kubernetes.Interface, ns string, name string, sizeInBytes int64, targetVolID string, annotations map[string]string, accessmodes []v1.PersistentVolumeAccessMode) (string, error) {
sizeFmt := fmt.Sprintf("%d", sizeInBytes)
size, err := resource.ParseQuantity(sizeFmt)
emptyStorageClass := ""
if err != nil {
return "", errors.Wrapf(err, "Unable to parse sizeFmt %s", sizeFmt)
}
if len(accessmodes) == 0 {
accessmodes = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
}
pvc := v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Annotations: annotations,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
AccessModes: accessmodes,
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): size,
Expand Down Expand Up @@ -108,6 +111,7 @@ type CreatePVCFromSnapshotArgs struct {
SnapshotName string
RestoreSize string
Labels map[string]string
AccessModes []v1.PersistentVolumeAccessMode
}

// CreatePVCFromSnapshot will restore a volume and returns the resulting
Expand All @@ -134,14 +138,17 @@ func CreatePVCFromSnapshot(ctx context.Context, args *CreatePVCFromSnapshotArgs)
return "", fmt.Errorf("Restore size is empty and no restore size argument given, Volumesnapshot: %s", args.SnapshotName)
}

if len(args.AccessModes) == 0 {
args.AccessModes = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
}
snapshotKind := "VolumeSnapshot"
snapshotAPIGroup := "snapshot.storage.k8s.io"
pvc := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Labels: args.Labels,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
AccessModes: args.AccessModes,
DataSource: &v1.TypedLocalObjectReference{
APIGroup: &snapshotAPIGroup,
Kind: snapshotKind,
Expand Down Expand Up @@ -175,7 +182,7 @@ func CreatePVCFromSnapshot(ctx context.Context, args *CreatePVCFromSnapshotArgs)

// CreatePV creates a PersistentVolume and returns its name
// For retry idempotency, checks whether PV associated with volume already exists
func CreatePV(ctx context.Context, kubeCli kubernetes.Interface, vol *blockstorage.Volume, volType blockstorage.Type, annotations map[string]string) (string, error) {
func CreatePV(ctx context.Context, kubeCli kubernetes.Interface, vol *blockstorage.Volume, volType blockstorage.Type, annotations map[string]string, accessmodes []v1.PersistentVolumeAccessMode) (string, error) {
sizeFmt := fmt.Sprintf("%d", vol.SizeInBytes)
size, err := resource.ParseQuantity(sizeFmt)
if err != nil {
Expand All @@ -191,6 +198,10 @@ func CreatePV(ctx context.Context, kubeCli kubernetes.Interface, vol *blockstora
return pvl.Items[0].Name, nil
}

if len(accessmodes) == 0 {
accessmodes = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}
}

pv := v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "kanister-pv-",
Expand All @@ -201,7 +212,7 @@ func CreatePV(ctx context.Context, kubeCli kubernetes.Interface, vol *blockstora
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): size,
},
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
AccessModes: accessmodes,
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
},
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/kube/volume/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *TestVolSuite) TestCreatePVC(c *C) {
targetVolID := "testVolID"
annotations := map[string]string{"a1": "foo"}
cli := fake.NewSimpleClientset()
pvcName, err := CreatePVC(ctx, cli, ns, NoPVCNameSpecified, pvcSize, targetVolID, annotations)
pvcName, err := CreatePVC(ctx, cli, ns, NoPVCNameSpecified, pvcSize, targetVolID, annotations, []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce})
c.Assert(err, IsNil)
pvc, err := cli.CoreV1().PersistentVolumeClaims(ns).Get(ctx, pvcName, metav1.GetOptions{})
c.Assert(err, IsNil)
Expand All @@ -56,4 +56,10 @@ func (s *TestVolSuite) TestCreatePVC(c *C) {
c.Assert(len(pvc.Spec.Selector.MatchLabels) >= 1, Equals, true)
label := pvc.Spec.Selector.MatchLabels[pvMatchLabelName]
c.Assert(label, Equals, filepath.Base(targetVolID))

_, err = CreatePVC(ctx, cli, ns, "pvc2", pvcSize, targetVolID, annotations, nil)
c.Assert(err, IsNil)
pvc2, err := cli.CoreV1().PersistentVolumeClaims(ns).Get(ctx, "pvc2", metav1.GetOptions{})
c.Assert(err, IsNil)
c.Assert(len(pvc2.Spec.AccessModes) >= 1, Equals, true)
}

0 comments on commit af35060

Please sign in to comment.