-
Notifications
You must be signed in to change notification settings - Fork 75
/
controller_create_volume.go
133 lines (111 loc) · 3.39 KB
/
controller_create_volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"context"
"errors"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/container-storage-interface/spec/lib/go/csi"
)
var createVolume struct {
reqBytes int64
limBytes int64
caps volumeCapabilitySliceArg
params mapOfStringArg
sourceVol string
sourceSnap string
}
var createVolumeCmd = &cobra.Command{
Use: "create-volume",
Aliases: []string{"n", "c", "new", "create"},
Short: `invokes the rpc "CreateVolume"`,
Example: `
CREATING MULTIPLE VOLUMES
The following example illustrates how to create two volumes with the
same characteristics at the same time:
csc controller new --endpoint /csi/server.sock
--cap 1,block \
--cap MULTI_NODE_MULTI_WRITER,mount,xfs,uid=500 \
--params region=us,zone=texas
--params disabled=false
MyNewVolume1 MyNewVolume2
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
req := csi.CreateVolumeRequest{
VolumeCapabilities: createVolume.caps.data,
Parameters: createVolume.params.data,
Secrets: root.secrets,
}
if createVolume.reqBytes > 0 || createVolume.limBytes > 0 {
req.CapacityRange = &csi.CapacityRange{}
if v := createVolume.reqBytes; v > 0 {
req.CapacityRange.RequiredBytes = v
}
if v := createVolume.limBytes; v > 0 {
req.CapacityRange.LimitBytes = v
}
}
if createVolume.sourceVol != "" && createVolume.sourceSnap != "" {
return errors.New(
"--source-volume and --source-snapshot are mutually exclusive")
}
if createVolume.sourceVol != "" {
req.VolumeContentSource = &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Volume{
Volume: &csi.VolumeContentSource_VolumeSource{
VolumeId: createVolume.sourceVol,
},
},
}
} else if createVolume.sourceSnap != "" {
req.VolumeContentSource = &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: createVolume.sourceSnap,
},
},
}
}
for i := range args {
ctx, cancel := context.WithTimeout(root.ctx, root.timeout)
defer cancel()
// Set the volume name for the current request.
req.Name = args[i]
log.WithField("request", req).Debug("creating volume")
rep, err := controller.client.CreateVolume(ctx, &req)
if err != nil {
return err
}
if err := root.tpl.Execute(os.Stdout, rep.Volume); err != nil {
return err
}
}
return nil
},
}
func init() {
controllerCmd.AddCommand(createVolumeCmd)
flagRequiredBytes(createVolumeCmd.Flags(), &createVolume.reqBytes)
flagLimitBytes(createVolumeCmd.Flags(), &createVolume.limBytes)
flagParameters(createVolumeCmd.Flags(), &createVolume.params)
flagVolumeCapabilities(createVolumeCmd.Flags(), &createVolume.caps)
createVolumeCmd.Flags().StringVar(
&createVolume.sourceVol,
"source-volume",
"",
"Pre-populate data using a source volume")
createVolumeCmd.Flags().StringVar(
&createVolume.sourceSnap,
"source-snapshot",
"",
"Pre-populate data using a source snapshot")
flagWithRequiresVolContext(
createVolumeCmd.Flags(),
&root.withRequiresVolContext,
false)
flagWithRequiresCreds(
createVolumeCmd.Flags(),
&root.withRequiresCreds,
"")
}