/
blockstore-plugin.go
159 lines (136 loc) · 4.38 KB
/
blockstore-plugin.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
package main
import (
"errors"
"math/rand"
"strconv"
"github.com/heptio/velero/pkg/util/collections"
"github.com/heptio/velero/pkg/cloudprovider"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
)
// Volume keeps track of volumes created by this plugin
type Volume struct {
volType, az string
iops int64
}
// Snapshot keeps track of snapshots created by this plugin
type Snapshot struct {
volID, az string
tags map[string]string
}
// Plugin for containing state for the blockstore plugin
type NoOpBlockStore struct {
config map[string]string
logrus.FieldLogger
volumes map[string]Volume
snapshots map[string]Snapshot
}
var _ cloudprovider.BlockStore = (*NoOpBlockStore)(nil)
// Init the plugin. Note that after v0.10.0, this will happen multiple times.
func (p *NoOpBlockStore) Init(config map[string]string) error {
p.Infof("Init called", config)
p.config = config
// Make sure we don't overwrite data, now that we can re-initialize the plugin
if p.volumes == nil {
p.volumes = make(map[string]Volume)
}
if p.snapshots == nil {
p.snapshots = make(map[string]Snapshot)
}
return nil
}
// CreateVolumeFromSnapshot Create a volume from given snapshot.
func (p *NoOpBlockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ string, iops *int64) (string, error) {
p.Infof("CreateVolumeFromSnapshot called", snapshotID, volumeType, volumeAZ, *iops)
var volumeID string
for {
volumeID := snapshotID + ".vol." + strconv.FormatUint(rand.Uint64(), 10)
if _, ok := p.volumes[volumeID]; ok {
// Duplicate ? Retry
continue
}
break
}
p.volumes[volumeID] = Volume{
volType: volumeType,
az: volumeAZ,
iops: *iops,
}
return volumeID, nil
}
// GetVolumeInfo Get information about the volume.
func (p *NoOpBlockStore) GetVolumeInfo(volumeID, volumeAZ string) (string, *int64, error) {
p.Infof("GetVolumeInfo called", volumeID, volumeAZ)
if val, ok := p.volumes[volumeID]; ok {
iops := val.iops
return val.volType, &iops, nil
}
return "", nil, errors.New("Volume " + volumeID + " not found")
}
// IsVolumeReady Check if the volume is ready.
func (p *NoOpBlockStore) IsVolumeReady(volumeID, volumeAZ string) (ready bool, err error) {
p.Infof("IsVolumeReady called", volumeID, volumeAZ)
return true, nil
}
// CreateSnapshot Create a snapshot.
func (p *NoOpBlockStore) CreateSnapshot(volumeID, volumeAZ string, tags map[string]string) (string, error) {
p.Infof("CreateSnapshot called", volumeID, volumeAZ, tags)
var snapshotID string
for {
snapshotID = volumeID + ".snap." + strconv.FormatUint(rand.Uint64(), 10)
p.Infof("CreateSnapshot trying to create snapshot", snapshotID)
if _, ok := p.snapshots[snapshotID]; ok {
// Duplicate ? Retry
continue
}
break
}
// Remember the "original" volume, only required for the first
// time.
if _, exists := p.volumes[volumeID]; !exists {
p.volumes[volumeID] = Volume{
volType: "orignalVolumeType",
az: volumeAZ,
iops: 100,
}
}
// Remember the snapshot
p.snapshots[snapshotID] = Snapshot{volID: volumeID,
az: volumeAZ,
tags: tags}
p.Infof("CreateSnapshot returning", snapshotID)
return snapshotID, nil
}
// DeleteSnapshot Delete a snapshot.
func (p *NoOpBlockStore) DeleteSnapshot(snapshotID string) error {
p.Infof("DeleteSnapshot called", snapshotID)
delete(p.snapshots, snapshotID)
return nil
}
// GetVolumeID Get the volume ID from the spec.
func (p *NoOpBlockStore) GetVolumeID(pv runtime.Unstructured) (string, error) {
p.Infof("GetVolumeID called", pv)
if !collections.Exists(pv.UnstructuredContent(), "spec.hostPath.path") {
return "", errors.New("Example plugin failed to get volume ID. ")
}
// Seed the volume info so that GetVolumeInfo doesn't fail later.
volumeID, _ := collections.GetString(pv.UnstructuredContent(), "spec.hostPath.path")
if _, exists := p.volumes[volumeID]; !exists {
p.Infof("L134")
p.volumes[volumeID] = Volume{
volType: "orignalVolumeType",
iops: 100,
}
}
return collections.GetString(pv.UnstructuredContent(), "spec.hostPath.path")
}
// SetVolumeID Set the volume ID in the spec.
func (p *NoOpBlockStore) SetVolumeID(pv runtime.Unstructured, volumeID string) (runtime.Unstructured, error) {
p.Infof("SetVolumeID called", pv, volumeID)
metadataMap, err := collections.GetMap(pv.UnstructuredContent(), "spec.hostPath.path")
if err != nil {
return nil, err
}
metadataMap["volumeID"] = volumeID
return pv, nil
}