-
Notifications
You must be signed in to change notification settings - Fork 187
/
azure_snapshot_utils.go
139 lines (118 loc) · 4.65 KB
/
azure_snapshot_utils.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
/*
Copyright 2021 The Kubernetes 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 azureutils
import (
"fmt"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-08-01/compute"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
volumehelper "sigs.k8s.io/azuredisk-csi-driver/pkg/util"
)
func GenerateCSISnapshot(sourceVolumeID string, snapshot *compute.Snapshot) (*csi.Snapshot, error) {
if snapshot == nil || snapshot.SnapshotProperties == nil {
return nil, fmt.Errorf("snapshot property is nil")
}
if snapshot.SnapshotProperties.TimeCreated == nil {
return nil, fmt.Errorf("TimeCreated of snapshot property is nil")
}
if snapshot.SnapshotProperties.DiskSizeGB == nil {
return nil, fmt.Errorf("diskSizeGB of snapshot property is nil")
}
ready, _ := isCSISnapshotReady(*snapshot.SnapshotProperties.ProvisioningState)
if sourceVolumeID == "" {
sourceVolumeID = GetSourceVolumeID(snapshot)
}
return &csi.Snapshot{
SizeBytes: volumehelper.GiBToBytes(int64(*snapshot.SnapshotProperties.DiskSizeGB)),
SnapshotId: *snapshot.ID,
SourceVolumeId: sourceVolumeID,
CreationTime: timestamppb.New(snapshot.SnapshotProperties.TimeCreated.ToTime()),
ReadyToUse: ready,
}, nil
}
// There are 4 scenarios for listing snapshots.
// 1. StartingToken is null, and MaxEntries is null. Return all snapshots from zero.
// 2. StartingToken is null, and MaxEntries is not null. Return `MaxEntries` snapshots from zero.
// 3. StartingToken is not null, and MaxEntries is null. Return all snapshots from `StartingToken`.
// 4. StartingToken is not null, and MaxEntries is not null. Return `MaxEntries` snapshots from `StartingToken`.
func GetEntriesAndNextToken(req *csi.ListSnapshotsRequest, snapshots []compute.Snapshot) (*csi.ListSnapshotsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.Aborted, "request is nil")
}
var err error
start := 0
if req.StartingToken != "" {
start, err = strconv.Atoi(req.StartingToken)
if err != nil {
return nil, status.Errorf(codes.Aborted, "ListSnapshots starting token(%s) parsing with error: %v", req.StartingToken, err)
}
if start >= len(snapshots) {
return nil, status.Errorf(codes.Aborted, "ListSnapshots starting token(%d) is greater than total number of snapshots", start)
}
if start < 0 {
return nil, status.Errorf(codes.Aborted, "ListSnapshots starting token(%d) can not be negative", start)
}
}
maxEntries := len(snapshots) - start
if req.MaxEntries > 0 && int(req.MaxEntries) < maxEntries {
maxEntries = int(req.MaxEntries)
}
entries := []*csi.ListSnapshotsResponse_Entry{}
for count := 0; start < len(snapshots) && count < maxEntries; start++ {
if (req.SourceVolumeId != "" && req.SourceVolumeId == GetSourceVolumeID(&snapshots[start])) || req.SourceVolumeId == "" {
csiSnapshot, err := GenerateCSISnapshot(req.SourceVolumeId, &snapshots[start])
if err != nil {
return nil, fmt.Errorf("failed to generate snapshot entry: %v", err)
}
entries = append(entries, &csi.ListSnapshotsResponse_Entry{Snapshot: csiSnapshot})
count++
}
}
nextToken := len(snapshots)
if start < len(snapshots) {
nextToken = start
}
listSnapshotResp := &csi.ListSnapshotsResponse{
Entries: entries,
NextToken: strconv.Itoa(nextToken),
}
return listSnapshotResp, nil
}
func GetSnapshotNameFromURI(snapshotURI string) (string, error) {
matches := diskSnapshotPathRE.FindStringSubmatch(snapshotURI)
if len(matches) != 2 {
return "", fmt.Errorf("could not get snapshot name from %s, correct format: %s", snapshotURI, diskSnapshotPathRE)
}
return matches[1], nil
}
func GetSourceVolumeID(snapshot *compute.Snapshot) string {
if snapshot != nil &&
snapshot.SnapshotProperties != nil &&
snapshot.SnapshotProperties.CreationData != nil &&
snapshot.SnapshotProperties.CreationData.SourceResourceID != nil {
return *snapshot.SnapshotProperties.CreationData.SourceResourceID
}
return ""
}
func isCSISnapshotReady(state string) (bool, error) {
switch strings.ToLower(state) {
case "succeeded":
return true, nil
default:
return false, nil
}
}