-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
123 lines (110 loc) · 3.33 KB
/
options.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
package snapshotter
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
)
// Option is used for snapshot configuration.
type Option func(opts *Options) error
// Options defines all snapshot parameters.
type Options struct {
// Name is a snapshot name.
Name string
// Namespace is a snapshot namespace.
Namespace string
// PVCName is a persistent volume claim name to make snapshot from or restore to.
PVCName string
// PVCName is a persistent volume claim namespace to make snapshot from or restore to.
PVCNamespace string
// PVC is a persistent volume claim spec to restore snapshot to.
PVC v1.PersistentVolumeClaim
// RestoreNamespace is the namespace to which a snapshot should be restored to.
RestoreNamespace string
// SnapshotClassName is the name of the VolumeSnapshotClass requested by the VolumeSnapshot.
SnapshotClassName string
// RestoreSnapshotName is the name of the snapshot from which restore will be performed
RestoreSnapshotName string
// Annotations are the annotations that can be applied on the snapshot related objects
Annotations map[string]string
}
// Name is used to set a snapshot name.
func Name(name string) Option {
return func(opts *Options) error {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("snapshot name is empty")
}
opts.Name = name
return nil
}
}
// Namespace is used to set a snapshot namespace.
func Namespace(name string) Option {
return func(opts *Options) error {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("snapshot namespace is empty")
}
opts.Namespace = name
return nil
}
}
// PVCName is a persistent volume claim name to make snapshot from.
func PVCName(name string) Option {
return func(opts *Options) error {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("persistent volume claim name is empty")
}
opts.PVCName = name
return nil
}
}
// PVCNamespace is a persistent volume claim namespace to make snapshot from.
func PVCNamespace(ns string) Option {
return func(opts *Options) error {
if strings.TrimSpace(ns) == "" {
return fmt.Errorf("persistent volume claim namespace is empty")
}
opts.PVCNamespace = ns
return nil
}
}
// PVC is a persistent volume claim spec to restore snapshot to.
func PVC(pvc v1.PersistentVolumeClaim) Option {
return func(opts *Options) error {
opts.PVC = pvc
return nil
}
}
// RestoreNamespace is the namespace to which a snapshot should be restored to.
func RestoreNamespace(namespace string) Option {
return func(opts *Options) error {
opts.RestoreNamespace = namespace
return nil
}
}
// SnapshotClassName is the name of the VolumeSnapshotClass requested by the VolumeSnapshot.
func SnapshotClassName(name string) Option {
return func(opts *Options) error {
opts.SnapshotClassName = name
return nil
}
}
// RestoreSnapshotName is the snapshot name from which a PVC will be restored
func RestoreSnapshotName(name string) Option {
return func(opts *Options) error {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("restore snapshot name is empty")
}
opts.RestoreSnapshotName = name
return nil
}
}
// Annotations are the annotations applied on the snapshot related objects
func Annotations(annotations map[string]string) Option {
return func(opts *Options) error {
opts.Annotations = make(map[string]string)
for k, v := range annotations {
opts.Annotations[k] = v
}
return nil
}
}