-
Notifications
You must be signed in to change notification settings - Fork 499
/
storage.go
221 lines (189 loc) · 7.17 KB
/
storage.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"fmt"
"sync"
"github.com/juju/errors"
core "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/juju/juju/caas/kubernetes/provider/constants"
"github.com/juju/juju/caas/kubernetes/provider/storage"
jujucontext "github.com/juju/juju/environs/envcontext"
jujustorage "github.com/juju/juju/internal/storage"
)
func validateStorageAttributes(attributes map[string]interface{}) error {
if _, err := storage.ParseStorageConfig(attributes); err != nil {
return errors.Trace(err)
}
if _, err := storage.ParseStorageMode(attributes); err != nil {
return errors.Trace(err)
}
return nil
}
type storageProvider struct {
client *kubernetesClient
}
var _ jujustorage.Provider = (*storageProvider)(nil)
// ValidateStorageProvider returns an error if the storage type and config is not valid
// for a Kubernetes deployment.
func (g *storageProvider) ValidateForK8s(attributes map[string]any) error {
if attributes == nil {
return nil
}
if mediumValue, ok := attributes[constants.StorageMedium]; ok {
medium := core.StorageMedium(fmt.Sprintf("%v", mediumValue))
if medium != core.StorageMediumMemory && medium != core.StorageMediumHugePages {
return errors.NotValidf("storage medium %q", mediumValue)
}
}
if err := validateStorageAttributes(attributes); err != nil {
return errors.Trace(err)
}
return nil
}
// ValidateConfig is defined on the jujustorage.Provider interface.
func (g *storageProvider) ValidateConfig(cfg *jujustorage.Config) error {
return errors.Trace(validateStorageAttributes(cfg.Attrs()))
}
// Supports is defined on the jujustorage.Provider interface.
func (g *storageProvider) Supports(k jujustorage.StorageKind) bool {
return k == jujustorage.StorageKindBlock
}
// Scope is defined on the jujustorage.Provider interface.
func (g *storageProvider) Scope() jujustorage.Scope {
return jujustorage.ScopeEnviron
}
// Dynamic is defined on the jujustorage.Provider interface.
func (g *storageProvider) Dynamic() bool {
return true
}
// Releasable is defined on the jujustorage.Provider interface.
func (g *storageProvider) Releasable() bool {
return true
}
// DefaultPools is defined on the jujustorage.Provider interface.
func (g *storageProvider) DefaultPools() []*jujustorage.Config {
return nil
}
// VolumeSource is defined on the jujustorage.Provider interface.
func (g *storageProvider) VolumeSource(cfg *jujustorage.Config) (jujustorage.VolumeSource, error) {
return &volumeSource{
client: g.client,
}, nil
}
// FilesystemSource is defined on the jujustorage.Provider interface.
func (g *storageProvider) FilesystemSource(providerConfig *jujustorage.Config) (jujustorage.FilesystemSource, error) {
return nil, errors.NotSupportedf("filesystems")
}
type volumeSource struct {
client *kubernetesClient
}
var _ jujustorage.VolumeSource = (*volumeSource)(nil)
// CreateVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) CreateVolumes(ctx jujucontext.ProviderCallContext, params []jujustorage.VolumeParams) (_ []jujustorage.CreateVolumesResult, err error) {
// noop
return nil, nil
}
// ListVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) ListVolumes(ctx jujucontext.ProviderCallContext) ([]string, error) {
pVolumes := v.client.client().CoreV1().PersistentVolumes()
vols, err := pVolumes.List(ctx, v1.ListOptions{})
if err != nil {
return nil, errors.Trace(err)
}
volumeIds := make([]string, 0, len(vols.Items))
for _, v := range vols.Items {
volumeIds = append(volumeIds, v.Name)
}
return volumeIds, nil
}
// DescribeVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) DescribeVolumes(ctx jujucontext.ProviderCallContext, volIds []string) ([]jujustorage.DescribeVolumesResult, error) {
pVolumes := v.client.client().CoreV1().PersistentVolumes()
vols, err := pVolumes.List(ctx, v1.ListOptions{
// TODO(caas) - filter on volumes for the current model
})
if err != nil {
return nil, errors.Trace(err)
}
byID := make(map[string]core.PersistentVolume)
for _, vol := range vols.Items {
byID[vol.Name] = vol
}
results := make([]jujustorage.DescribeVolumesResult, len(volIds))
for i, volID := range volIds {
vol, ok := byID[volID]
if !ok {
results[i].Error = errors.NotFoundf("%s", volID)
continue
}
results[i].VolumeInfo = &jujustorage.VolumeInfo{
Size: uint64(vol.Size()),
VolumeId: vol.Name,
Persistent: true,
}
}
return results, nil
}
// DestroyVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) DestroyVolumes(ctx jujucontext.ProviderCallContext, volIds []string) ([]error, error) {
logger.Debugf("destroy k8s volumes: %v", volIds)
pVolumes := v.client.client().CoreV1().PersistentVolumes()
return foreachVolume(volIds, func(volumeId string) error {
vol, err := pVolumes.Get(ctx, volumeId, v1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return errors.Annotatef(err, "getting volume %v to delete", volumeId)
}
if err == nil && vol.Spec.ClaimRef != nil {
claimRef := vol.Spec.ClaimRef
pClaims := v.client.client().CoreV1().PersistentVolumeClaims(claimRef.Namespace)
logger.Infof("deleting PVC %s due to call to volumeSource.DestroyVolumes(%q)", claimRef.Name, volumeId)
err := pClaims.Delete(ctx, claimRef.Name, v1.DeleteOptions{PropagationPolicy: constants.DefaultPropagationPolicy()})
if err != nil && !k8serrors.IsNotFound(err) {
return errors.Annotatef(err, "destroying volume claim %v", claimRef.Name)
}
}
if err := pVolumes.Delete(ctx,
volumeId,
v1.DeleteOptions{PropagationPolicy: constants.DefaultPropagationPolicy()},
); !k8serrors.IsNotFound(err) {
return errors.Annotate(err, "destroying k8s volumes")
}
return nil
}), nil
}
// ReleaseVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) ReleaseVolumes(ctx jujucontext.ProviderCallContext, volIds []string) ([]error, error) {
// noop
return make([]error, len(volIds)), nil
}
// ValidateVolumeParams is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) ValidateVolumeParams(params jujustorage.VolumeParams) error {
// TODO(caas) - we need to validate params based on the underlying substrate
return nil
}
// AttachVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) AttachVolumes(ctx jujucontext.ProviderCallContext, attachParams []jujustorage.VolumeAttachmentParams) ([]jujustorage.AttachVolumesResult, error) {
// noop
return nil, nil
}
// DetachVolumes is specified on the jujustorage.VolumeSource interface.
func (v *volumeSource) DetachVolumes(ctx jujucontext.ProviderCallContext, attachParams []jujustorage.VolumeAttachmentParams) ([]error, error) {
// noop
return make([]error, len(attachParams)), nil
}
func foreachVolume(volumeIds []string, f func(string) error) []error {
results := make([]error, len(volumeIds))
var wg sync.WaitGroup
for i, volumeId := range volumeIds {
wg.Add(1)
go func(i int, volumeId string) {
defer wg.Done()
results[i] = f(volumeId)
}(i, volumeId)
}
wg.Wait()
return results
}