-
Notifications
You must be signed in to change notification settings - Fork 153
/
snapshot_stable.go
178 lines (153 loc) · 8.28 KB
/
snapshot_stable.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
// Copyright 2021 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package snapshot
import (
"context"
"github.com/kanisterio/errkit"
v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"github.com/kanisterio/kanister/pkg/blockstorage"
)
const (
// VolumeSnapshotContentResourcePlural is "volumesnapshotcontents"
VolumeSnapshotContentResourcePlural = "volumesnapshotcontents"
// VolumeSnapshotResourcePlural is "volumesnapshots"
VolumeSnapshotResourcePlural = "volumesnapshots"
// VolumeSnapshotClassResourcePlural is "volumesnapshotclasses"
VolumeSnapshotClassResourcePlural = "volumesnapshotclasses"
GroupName = "snapshot.storage.k8s.io"
Version = "v1"
)
var (
// VolSnapGVR specifies GVR schema for VolumeSnapshots
VolSnapGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotResourcePlural}
// VolSnapClassGVR specifies GVR schema for VolumeSnapshotClasses
VolSnapClassGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotClassResourcePlural}
// VolSnapContentGVR specifies GVR schema for VolumeSnapshotContents
VolSnapContentGVR = schema.GroupVersionResource{Group: GroupName, Version: Version, Resource: VolumeSnapshotContentResourcePlural}
)
type SnapshotStable struct {
kubeCli kubernetes.Interface
dynCli dynamic.Interface
}
func NewSnapshotStable(kubeCli kubernetes.Interface, dynCli dynamic.Interface) Snapshotter {
return &SnapshotStable{kubeCli: kubeCli, dynCli: dynCli}
}
// CloneVolumeSnapshotClass creates a copy of the source volume snapshot class
func (sna *SnapshotStable) CloneVolumeSnapshotClass(ctx context.Context, sourceClassName, targetClassName, newDeletionPolicy string, excludeAnnotations []string) error {
return cloneSnapshotClass(ctx, sna.dynCli, VolSnapClassGVR, sourceClassName, targetClassName, newDeletionPolicy, excludeAnnotations)
}
// GetVolumeSnapshotClass returns VolumeSnapshotClass name which is annotated with given key.
func (sna *SnapshotStable) GetVolumeSnapshotClass(ctx context.Context, annotationKey, annotationValue, storageClassName string) (string, error) {
return GetSnapshotClassbyAnnotation(ctx, sna.dynCli, sna.kubeCli, VolSnapClassGVR, annotationKey, annotationValue, storageClassName)
}
// Create creates a VolumeSnapshot and returns it or any error happened meanwhile.
func (sna *SnapshotStable) Create(ctx context.Context, name, namespace, volumeName string, snapshotClass *string, waitForReady bool, labels map[string]string) error {
return createSnapshot(ctx, sna.dynCli, sna.kubeCli, VolSnapGVR, name, namespace, volumeName, snapshotClass, waitForReady, labels)
}
// Get will return the VolumeSnapshot in the 'namespace' with given 'name'.
func (sna *SnapshotStable) Get(ctx context.Context, name, namespace string) (*v1.VolumeSnapshot, error) {
return getSnapshot(ctx, sna.dynCli, VolSnapGVR, name, namespace)
}
func (sna *SnapshotStable) List(ctx context.Context, namespace string, labels map[string]string) (*v1.VolumeSnapshotList, error) {
return listSnapshots(ctx, sna.dynCli, VolSnapGVR, namespace, labels)
}
// Delete will delete the VolumeSnapshot and returns any error as a result.
func (sna *SnapshotStable) Delete(ctx context.Context, name, namespace string) (*v1.VolumeSnapshot, error) {
return deleteSnapshot(ctx, sna.dynCli, VolSnapGVR, name, namespace)
}
// DeleteContent will delete the specified VolumeSnapshotContent
func (sna *SnapshotStable) DeleteContent(ctx context.Context, name string) error {
if err := sna.dynCli.Resource(VolSnapContentGVR).Delete(ctx, name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return errkit.Wrap(err, "Failed to delete", "volumeSnapshotContent", name)
}
// If the Snapshot Content does not exist, that's an acceptable error and we ignore it
return nil
}
// Clone will clone the VolumeSnapshot to namespace 'cloneNamespace'.
// Underlying VolumeSnapshotContent will be cloned with a different name.
func (sna *SnapshotStable) Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool, labels map[string]string) error {
_, err := sna.Get(ctx, cloneName, cloneNamespace)
if err == nil {
return errkit.New("Target snapshot already exists in target namespace", "volumeSnapshot", cloneName, "namespace", cloneNamespace)
}
if !apierrors.IsNotFound(err) {
return errkit.Wrap(err, "Failed to query target", "volumeSnapshot", cloneName, "namespace", cloneNamespace)
}
src, err := sna.GetSource(ctx, name, namespace)
if err != nil {
return errkit.New("Failed to get source")
}
return sna.CreateFromSource(ctx, src, cloneName, cloneNamespace, waitForReady, labels)
}
// GetSource will return the CSI source that backs the volume snapshot.
func (sna *SnapshotStable) GetSource(ctx context.Context, snapshotName, namespace string) (*Source, error) {
return getSnapshotSource(ctx, sna.dynCli, VolSnapGVR, VolSnapContentGVR, snapshotName, namespace)
}
// CreateFromSource will create a 'Volumesnapshot' and 'VolumesnaphotContent' pair for the underlying snapshot source.
func (sna *SnapshotStable) CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool, labels map[string]string) error {
deletionPolicy, err := getDeletionPolicyFromClass(sna.dynCli, VolSnapClassGVR, source.VolumeSnapshotClassName)
if err != nil {
return errkit.Wrap(err, "Failed to get DeletionPolicy from VolumeSnapshotClass")
}
contentName := snapshotName + "-content-" + string(uuid.NewUUID())
snap := UnstructuredVolumeSnapshot(
VolSnapGVR,
snapshotName,
namespace,
"",
contentName,
source.VolumeSnapshotClassName,
blockstorage.SanitizeTags(labels),
)
if err := sna.CreateContentFromSource(ctx, source, contentName, snapshotName, namespace, deletionPolicy); err != nil {
return err
}
if _, err := sna.dynCli.Resource(VolSnapGVR).Namespace(namespace).Create(ctx, snap, metav1.CreateOptions{}); err != nil {
return errkit.Wrap(err, "Failed to create content", "volumeSnapshot", snap.GetName())
}
if !waitForReady {
return nil
}
err = sna.WaitOnReadyToUse(ctx, snapshotName, namespace)
return err
}
// UpdateVolumeSnapshotStatusStable sets the readyToUse valuse of a VolumeSnapshot.
func (sna *SnapshotStable) UpdateVolumeSnapshotStatusStable(ctx context.Context, namespace string, snapshotName string, readyToUse bool) error {
return updateVolumeSnapshotStatus(ctx, sna.dynCli, VolSnapGVR, namespace, snapshotName, readyToUse)
}
// CreateContentFromSource will create a 'VolumesnaphotContent' for the underlying snapshot source.
func (sna *SnapshotStable) CreateContentFromSource(ctx context.Context, source *Source, contentName, snapshotName, namespace, deletionPolicy string) error {
content := UnstructuredVolumeSnapshotContent(VolSnapContentGVR, contentName, snapshotName, namespace, deletionPolicy, source.Driver, source.Handle, source.VolumeSnapshotClassName)
if _, err := sna.dynCli.Resource(VolSnapContentGVR).Create(ctx, content, metav1.CreateOptions{}); err != nil {
return errkit.Wrap(err, "Failed to create content", "volumeSnapshotContent", content.GetName())
}
return nil
}
// WaitOnReadyToUse will block until the Volumesnapshot in 'namespace' with name 'snapshotName'
// has status 'ReadyToUse' or 'ctx.Done()' is signalled.
func (sna *SnapshotStable) WaitOnReadyToUse(ctx context.Context, snapshotName, namespace string) error {
return waitOnReadyToUse(ctx, sna.dynCli, VolSnapGVR, snapshotName, namespace, isReadyToUseBeta)
}
func (sna *SnapshotStable) GroupVersion(ctx context.Context) schema.GroupVersion {
return schema.GroupVersion{
Group: GroupName,
Version: Version,
}
}