-
Notifications
You must be signed in to change notification settings - Fork 375
/
default.go
136 lines (123 loc) · 4.41 KB
/
default.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package driver
import (
"context"
"path/filepath"
"time"
"github.com/erda-project/erda/modules/scheduler/impl/volume"
"github.com/erda-project/erda/pkg/jsonstore"
"github.com/pkg/errors"
)
var (
ExistedVolumeID = errors.New("Existed Volume ID")
NotFoundVolume = errors.New("Not found volume")
VolumeNameReferNilVolume = errors.New("volume name refer to nil volume")
)
// etcd Volume information stored in etcd
// key: /volume/<id>
// val: VolumeInfo
type etcdVolumeInfo volume.VolumeInfo
// defaultCreate Store volume metadata in etcd
func defaultCreate(js jsonstore.JsonStore, config volume.VolumeCreateConfig) (volume.VolumeInfo, error) {
id, err := volume.NewVolumeID(config)
if err != nil {
return volume.VolumeInfo{}, err
}
info := volume.VolumeInfo{
ID: id.String(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Size: config.Size,
Type: config.Type,
}
// TODO(zj): Here there is a concurrency problem with Get first and then Set
// Later, STM support will be added to jsonstore to facilitate the use and avoid directly using the stm of the etcd library
IDPath := mkIndexEtcdPath(volume.ETCDVolumeMetadataDir, id.String())
infoToEtcd := etcdVolumeInfo(info)
notfound, err := js.Notfound(context.Background(), IDPath)
if err != nil {
return volume.VolumeInfo{}, err
}
if !notfound {
return volume.VolumeInfo{}, ExistedVolumeID
}
if err = js.Put(context.Background(), IDPath, infoToEtcd); err != nil {
return volume.VolumeInfo{}, err
}
return info, nil
}
func defaultInfo(js jsonstore.JsonStore, ID volume.VolumeIdentity) (volume.VolumeInfo, error) {
path := mkIndexEtcdPath(volume.ETCDVolumeMetadataDir, ID.String())
var v etcdVolumeInfo
if err := js.Get(context.Background(), path, &v); err != nil {
if err == jsonstore.NotFoundErr {
return volume.VolumeInfo{}, NotFoundVolume
}
return volume.VolumeInfo{}, err
}
return volume.VolumeInfo(v), nil
}
func defaultDelete(js jsonstore.JsonStore, ID volume.VolumeIdentity) (volume.VolumeInfo, error) {
path := mkIndexEtcdPath(volume.ETCDVolumeMetadataDir, ID.String())
var info etcdVolumeInfo
if err := js.Get(context.Background(), path, &info); err != nil {
if err == jsonstore.NotFoundErr {
return volume.VolumeInfo{}, NotFoundVolume
}
return volume.VolumeInfo{}, err
}
var unused interface{}
if err := js.Remove(context.Background(), path, &unused); err != nil {
return volume.VolumeInfo{}, err
}
return volume.VolumeInfo(info), nil
}
// defaultUpdate Currently only the References value in VolumeInfo will be updated,
// Return volumeinfo before update
func defaultUpdate(js jsonstore.JsonStore, ID volume.VolumeIdentity, vlm volume.VolumeInfo) (volume.VolumeInfo, error) {
path := mkIndexEtcdPath(volume.ETCDVolumeMetadataDir, ID.String())
var info etcdVolumeInfo
if err := js.Get(context.Background(), path, &info); err != nil {
if err == jsonstore.NotFoundErr {
return volume.VolumeInfo{}, NotFoundVolume
}
return volume.VolumeInfo{}, err
}
old := volume.VolumeInfo(info)
info.UpdatedAt = time.Now()
info.References = vlm.References
var err error
if err = js.Put(context.Background(), path, info); err != nil {
return volume.VolumeInfo{}, err
}
return old, nil
}
func defaultSoftDelete(js jsonstore.JsonStore, ID volume.VolumeIdentity) (volume.VolumeInfo, error) {
path := mkIndexEtcdPath(volume.ETCDVolumeMetadataDir, ID.String())
var info etcdVolumeInfo
if err := js.Get(context.Background(), path, &info); err != nil {
if err == jsonstore.NotFoundErr {
return volume.VolumeInfo{}, NotFoundVolume
}
return volume.VolumeInfo{}, err
}
info.DeletedAt = time.Now()
if err := js.Put(context.Background(), path, info); err != nil {
return volume.VolumeInfo{}, err
}
return volume.VolumeInfo(info), nil
}
func mkIndexEtcdPath(dir string, name string) string {
return filepath.Join(dir, name)
}