Skip to content

Commit 4a63ef2

Browse files
committed
Support specifying StorageClass while creating volumes
Add support for specifying StorageClass when user is creating volumes via oc set volume command
1 parent 5134734 commit 4a63ef2

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

pkg/cmd/cli/cmd/set/volume.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
3030
)
3131

32-
const volumePrefix = "volume-"
32+
const (
33+
volumePrefix = "volume-"
34+
storageAnnClass = "volume.beta.kubernetes.io/storage-class"
35+
)
3336

3437
var (
3538
volumeLong = templates.LongDesc(`
@@ -136,6 +139,7 @@ type AddVolumeOptions struct {
136139
ClaimName string
137140
ClaimSize string
138141
ClaimMode string
142+
ClaimClass string
139143

140144
TypeChanged bool
141145
}
@@ -186,6 +190,7 @@ func NewCmdVolume(fullName string, f *clientcmd.Factory, out, errOut io.Writer)
186190
cmd.Flags().StringVar(&addOpts.ConfigMapName, "configmap-name", "", "Name of the persisted config map. Must be provided for configmap volume type")
187191
cmd.Flags().StringVar(&addOpts.SecretName, "secret-name", "", "Name of the persisted secret. Must be provided for secret volume type")
188192
cmd.Flags().StringVar(&addOpts.ClaimName, "claim-name", "", "Persistent volume claim name. Must be provided for persistentVolumeClaim volume type")
193+
cmd.Flags().StringVar(&addOpts.ClaimClass, "claim-class", "", "StorageClass to use for provisioning the persistent volume.")
189194
cmd.Flags().StringVar(&addOpts.ClaimSize, "claim-size", "", "If specified along with a persistent volume type, create a new claim with the given size in bytes. Accepts SI notation: 10, 10G, 10Gi")
190195
cmd.Flags().StringVar(&addOpts.ClaimMode, "claim-mode", "ReadWriteOnce", "Set the access mode of the claim to be created. Valid values are ReadWriteOnce (rwo), ReadWriteMany (rwm), or ReadOnlyMany (rom)")
191196
cmd.Flags().StringVar(&addOpts.Source, "source", "", "Details of volume source as json string. This can be used if the required volume type is not supported by --type option. (e.g.: '{\"gitRepo\": {\"repository\": <git-url>, \"revision\": <commit-hash>}}')")
@@ -314,6 +319,15 @@ func (a *AddVolumeOptions) Validate(isAddOp bool) error {
314319
return err
315320
}
316321
}
322+
if len(a.ClaimClass) > 0 {
323+
selectedLowerType := strings.ToLower(a.Type)
324+
if selectedLowerType != "persistentvolumeclaim" && selectedLowerType != "pvc" {
325+
return errors.New("must provide --type as persistentVolumeClaim")
326+
}
327+
if len(a.ClaimSize) == 0 {
328+
return errors.New("must provide --claim-size to create new pvc with claim-class")
329+
}
330+
}
317331
} else if len(a.Source) > 0 || len(a.Path) > 0 || len(a.SecretName) > 0 || len(a.ConfigMapName) > 0 || len(a.ClaimName) > 0 || a.Overwrite {
318332
return errors.New("--type|--path|--configmap-name|--secret-name|--claim-name|--source|--overwrite are only valid for --add operation")
319333
}
@@ -563,7 +577,7 @@ func (v *VolumeOptions) printVolumes(infos []*resource.Info) []error {
563577
}
564578

565579
func (v *AddVolumeOptions) createClaim() *kapi.PersistentVolumeClaim {
566-
return &kapi.PersistentVolumeClaim{
580+
pvc := &kapi.PersistentVolumeClaim{
567581
ObjectMeta: kapi.ObjectMeta{
568582
Name: v.ClaimName,
569583
},
@@ -576,6 +590,12 @@ func (v *AddVolumeOptions) createClaim() *kapi.PersistentVolumeClaim {
576590
},
577591
},
578592
}
593+
if len(v.ClaimClass) > 0 {
594+
pvc.Annotations = map[string]string{
595+
storageAnnClass: v.ClaimClass,
596+
}
597+
}
598+
return pvc
579599
}
580600

581601
func (v *VolumeOptions) setVolumeSource(kv *kapi.Volume) error {

pkg/cmd/cli/cmd/set/volume_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package set
22

33
import (
4+
"errors"
45
"net/http"
56
"testing"
67

@@ -161,3 +162,72 @@ func TestAddVolume(t *testing.T) {
161162
t.Error(patchError)
162163
}
163164
}
165+
166+
func TestCreateClaim(t *testing.T) {
167+
addOpts := &AddVolumeOptions{
168+
Type: "persistentVolumeClaim",
169+
ClaimClass: "foobar",
170+
ClaimName: "foo-vol",
171+
ClaimSize: "5G",
172+
MountPath: "/sandbox",
173+
}
174+
175+
pvc := addOpts.createClaim()
176+
if len(pvc.Annotations) == 0 {
177+
t.Errorf("Expected storage class annotation")
178+
}
179+
180+
if pvc.Annotations[storageAnnClass] != "foobar" {
181+
t.Errorf("Expected storage annotated class to be %s", addOpts.ClaimClass)
182+
}
183+
}
184+
185+
func TestValidateAddOptions(t *testing.T) {
186+
tests := []struct {
187+
name string
188+
addOpts *AddVolumeOptions
189+
expectedError error
190+
}{
191+
{
192+
"using existing pvc",
193+
&AddVolumeOptions{Type: "persistentVolumeClaim"},
194+
errors.New("must provide --claim-name or --claim-size (to create a new claim) for --type=pvc"),
195+
},
196+
{
197+
"creating new pvc",
198+
&AddVolumeOptions{Type: "persistentVolumeClaim", ClaimName: "sandbox-pvc", ClaimSize: "5G"},
199+
nil,
200+
},
201+
{
202+
"error creating pvc with storage class",
203+
&AddVolumeOptions{Type: "persistentVolumeClaim", ClaimName: "sandbox-pvc", ClaimClass: "slow"},
204+
errors.New("must provide --claim-size to create new pvc with claim-class"),
205+
},
206+
{
207+
"creating pvc with storage class",
208+
&AddVolumeOptions{Type: "persistentVolumeClaim", ClaimName: "sandbox-pvc", ClaimClass: "slow", ClaimSize: "5G"},
209+
nil,
210+
},
211+
}
212+
213+
for _, testCase := range tests {
214+
addOpts := testCase.addOpts
215+
err := addOpts.Validate(true)
216+
if testCase.expectedError == nil && err != nil {
217+
t.Errorf("Expected nil error for %s got %s", testCase.name, err)
218+
continue
219+
}
220+
221+
if testCase.expectedError != nil {
222+
if err == nil {
223+
t.Errorf("Expected %s, got nil", testCase.expectedError)
224+
continue
225+
}
226+
227+
if testCase.expectedError.Error() != err.Error() {
228+
t.Errorf("Expected %s, got %s", testCase.expectedError, err)
229+
}
230+
}
231+
232+
}
233+
}

0 commit comments

Comments
 (0)