-
Notifications
You must be signed in to change notification settings - Fork 220
/
volume.go
202 lines (177 loc) · 6.81 KB
/
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2019 NetApp, Inc. All Rights Reserved.
package storage
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
"strings"
"github.com/netapp/trident/config"
"github.com/netapp/trident/utils"
)
type VolumeConfig struct {
Version string `json:"version"`
Name string `json:"name"`
InternalName string `json:"internalName"`
Size string `json:"size"`
Protocol config.Protocol `json:"protocol"`
SpaceReserve string `json:"spaceReserve"`
SecurityStyle string `json:"securityStyle"`
SnapshotPolicy string `json:"snapshotPolicy,omitempty"`
SnapshotReserve string `json:"snapshotReserve,omitempty"`
SnapshotDir string `json:"snapshotDirectory,omitempty"`
ExportPolicy string `json:"exportPolicy,omitempty"`
UnixPermissions string `json:"unixPermissions,omitempty"`
StorageClass string `json:"storageClass,omitempty"`
AccessMode config.AccessMode `json:"accessMode,omitempty"`
VolumeMode config.VolumeMode `json:"volumeMode,omitempty"`
AccessInfo utils.VolumeAccessInfo `json:"accessInformation"`
BlockSize string `json:"blockSize"`
FileSystem string `json:"fileSystem"`
Encryption string `json:"encryption"`
CloneSourceVolume string `json:"cloneSourceVolume"`
CloneSourceVolumeInternal string `json:"cloneSourceVolumeInternal"`
CloneSourceSnapshot string `json:"cloneSourceSnapshot"`
SplitOnClone string `json:"splitOnClone"`
QoS string `json:"qos,omitempty"`
QoSType string `json:"type,omitempty"`
ServiceLevel string `json:"serviceLevel,omitempty"`
Network string `json:"network,omitempty"`
ImportOriginalName string `json:"importOriginalName,omitempty"`
ImportBackendUUID string `json:"importBackendUUID,omitempty"`
ImportNotManaged bool `json:"importNotManaged,omitempty"`
}
func (c *VolumeConfig) Validate() error {
if c.Name == "" || c.Size == "" {
return fmt.Errorf("the following fields for \"Volume\" are mandatory: name and size")
}
if !config.IsValidProtocol(c.Protocol) {
return fmt.Errorf("%v is an usupported protocol! Acceptable values: "+
"%s", c.Protocol,
strings.Join([]string(config.GetValidProtocolNames()), ", "),
)
}
return nil
}
func (c *VolumeConfig) ConstructClone() *VolumeConfig {
clone := &VolumeConfig{}
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
_ = enc.Encode(c)
_ = dec.Decode(clone)
return clone
}
type Volume struct {
Config *VolumeConfig
BackendUUID string // UUID of the storage backend
Pool string // Name of the pool on which this volume was first provisioned
Orphaned bool // An Orphaned volume isn't currently tracked by the storage backend
State VolumeState
}
type VolumeState string
const (
VolumeStateUnknown = VolumeState("unknown")
VolumeStateOnline = VolumeState("online")
VolumeStateDeleting = VolumeState("deleting")
VolumeStateUpgrading = VolumeState("upgrading")
VolumeStateMissingBackend = VolumeState("missing_backend")
// TODO should Orphaned be moved to a VolumeState?
)
func (s VolumeState) String() string {
switch s {
case VolumeStateUnknown, VolumeStateOnline, VolumeStateDeleting:
return string(s)
default:
return "unknown"
}
}
func (s VolumeState) IsUnknown() bool {
switch s {
case VolumeStateOnline, VolumeStateDeleting:
return false
case VolumeStateUnknown:
return true
default:
return true
}
}
func (s VolumeState) IsOnline() bool {
return s == VolumeStateOnline
}
func (s VolumeState) IsDeleting() bool {
return s == VolumeStateDeleting
}
func (s VolumeState) IsMissingBackend() bool {
return s == VolumeStateMissingBackend
}
func NewVolume(conf *VolumeConfig, backendUUID string, pool string, orphaned bool) *Volume {
return &Volume{
Config: conf,
BackendUUID: backendUUID,
Pool: pool,
Orphaned: orphaned,
State: VolumeStateOnline,
}
}
type VolumeExternal struct {
Config *VolumeConfig
Backend string `json:"backend"` // replaced w/ backendUUID, remains to read old records
BackendUUID string `json:"backendUUID"` // UUID of the storage backend
Pool string `json:"pool"`
Orphaned bool `json:"orphaned"`
State VolumeState `json:"state"`
}
func (v *VolumeExternal) GetCHAPSecretName() string {
secretName := fmt.Sprintf("trident-chap-%v-%v", v.BackendUUID, v.Config.AccessInfo.IscsiUsername)
secretName = strings.Replace(secretName, "_", "-", -1)
secretName = strings.Replace(secretName, ".", "-", -1)
secretName = strings.ToLower(secretName)
return secretName
}
func (v *Volume) ConstructExternal() *VolumeExternal {
return &VolumeExternal{
Config: v.Config,
BackendUUID: v.BackendUUID,
Pool: v.Pool,
Orphaned: v.Orphaned,
State: v.State,
}
}
// VolumeExternalWrapper is used to return volumes and errors via channels between goroutines
type VolumeExternalWrapper struct {
Volume *VolumeExternal
Error error
}
type ImportVolumeRequest struct {
Backend string `json:"backend"`
InternalName string `json:"internalName"`
NoManage bool `json:"noManage"`
PVCData string `json:"pvcData"` // Opaque, base64-encoded
}
func (r *ImportVolumeRequest) Validate() error {
if r.Backend == "" || r.InternalName == "" {
return fmt.Errorf("the following fields are mandatory: backend and internalName")
}
if _, err := base64.StdEncoding.DecodeString(r.PVCData); err != nil {
return fmt.Errorf("the pvcData field does not contain valid base64-encoded data: %v", err)
}
return nil
}
type UpgradeVolumeRequest struct {
Type string `json:"type"`
Volume string `json:"volume"`
}
func (r *UpgradeVolumeRequest) Validate() error {
if r.Volume == "" {
return fmt.Errorf("the following field is mandatory: volume")
}
if r.Type != "csi" {
return fmt.Errorf("the only supported type for volume upgrade is 'csi'")
}
return nil
}
type ByVolumeExternalName []*VolumeExternal
func (a ByVolumeExternalName) Len() int { return len(a) }
func (a ByVolumeExternalName) Less(i, j int) bool { return a[i].Config.Name < a[j].Config.Name }
func (a ByVolumeExternalName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }