-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
195 lines (166 loc) · 5.92 KB
/
util.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
/*
Copyright 2017 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 attacher
import (
"encoding/json"
"fmt"
"regexp"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/glog"
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1beta1"
"k8s.io/client-go/kubernetes"
csiapi "k8s.io/csi-api/pkg/apis/csi/v1alpha1"
)
func markAsAttached(client kubernetes.Interface, va *storage.VolumeAttachment, metadata map[string]string) (*storage.VolumeAttachment, error) {
glog.V(4).Infof("Marking as attached %q", va.Name)
clone := va.DeepCopy()
clone.Status.Attached = true
clone.Status.AttachmentMetadata = metadata
clone.Status.AttachError = nil
// TODO: use patch to save us from VersionError
newVA, err := client.StorageV1beta1().VolumeAttachments().Update(clone)
if err != nil {
return va, err
}
glog.V(4).Infof("Marked as attached %q", va.Name)
return newVA, nil
}
func markAsDetached(client kubernetes.Interface, va *storage.VolumeAttachment) (*storage.VolumeAttachment, error) {
finalizerName := GetFinalizerName(va.Spec.Attacher)
// Prepare new array of finalizers
newFinalizers := make([]string, 0, len(va.Finalizers))
found := false
for _, f := range va.Finalizers {
if f == finalizerName {
found = true
continue
}
newFinalizers = append(newFinalizers, f)
}
// Mostly to simplify unit tests, but it won't harm in production too
if len(newFinalizers) == 0 {
newFinalizers = nil
}
if !found && !va.Status.Attached {
// Finalizer was not present, nothing to update
glog.V(4).Infof("Already fully detached %q", va.Name)
return va, nil
}
glog.V(4).Infof("Marking as detached %q", va.Name)
clone := va.DeepCopy()
clone.Finalizers = newFinalizers
clone.Status.Attached = false
clone.Status.DetachError = nil
clone.Status.AttachmentMetadata = nil
// TODO: use patch to save us from VersionError
newVA, err := client.StorageV1beta1().VolumeAttachments().Update(clone)
if err != nil {
return va, err
}
glog.V(4).Infof("Finalizer removed from %q", va.Name)
return newVA, nil
}
const (
defaultFSType = "ext4"
nodeIDAnnotation = "csi.volume.kubernetes.io/nodeid"
csiVolAttribsAnnotationKey = "csi.volume.kubernetes.io/volume-attributes"
)
func SanitizeDriverName(driver string) string {
re := regexp.MustCompile("[^a-zA-Z0-9-]")
name := re.ReplaceAllString(driver, "-")
if name[len(name)-1] == '-' {
// name must not end with '-'
name = name + "X"
}
return name
}
// getFinalizerName returns Attacher name suitable to be used as finalizer
func GetFinalizerName(driver string) string {
return "external-attacher/" + SanitizeDriverName(driver)
}
func GetNodeIDFromNode(driver string, node *v1.Node) (string, error) {
nodeIDJSON, ok := node.Annotations[nodeIDAnnotation]
if !ok {
return "", fmt.Errorf("node %q has no NodeID annotation", node.Name)
}
var nodeIDs map[string]string
if err := json.Unmarshal([]byte(nodeIDJSON), &nodeIDs); err != nil {
return "", fmt.Errorf("cannot parse NodeID annotation on node %q: %s", node.Name, err)
}
nodeID, ok := nodeIDs[driver]
if !ok {
return "", fmt.Errorf("cannot find NodeID for driver %q for node %q", driver, node.Name)
}
return nodeID, nil
}
func GetNodeIDFromNodeInfo(driver string, nodeInfo *csiapi.CSINodeInfo) (string, bool) {
for _, d := range nodeInfo.CSIDrivers {
if d.Driver == driver {
return d.NodeID, true
}
}
return "", false
}
func GetVolumeCapabilities(pv *v1.PersistentVolume) (*csi.VolumeCapability, error) {
m := map[v1.PersistentVolumeAccessMode]bool{}
for _, mode := range pv.Spec.AccessModes {
m[mode] = true
}
if pv.Spec.PersistentVolumeSource.CSI == nil {
return nil, fmt.Errorf("persistent volume does not contain CSI volume source")
}
fsType := pv.Spec.CSI.FSType
if len(fsType) == 0 {
fsType = defaultFSType
}
cap := &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
FsType: fsType,
MountFlags: pv.Spec.MountOptions,
},
},
AccessMode: &csi.VolumeCapability_AccessMode{},
}
// Translate array of modes into single VolumeCapability
switch {
case m[v1.ReadWriteMany]:
// ReadWriteMany trumps everything, regardless what other modes are set
cap.AccessMode.Mode = csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER
case m[v1.ReadOnlyMany] && m[v1.ReadWriteOnce]:
// This is no way how to translate this to CSI...
return nil, fmt.Errorf("CSI does not support ReadOnlyMany and ReadWriteOnce on the same PersistentVolume")
case m[v1.ReadOnlyMany]:
// There is only ReadOnlyMany set
cap.AccessMode.Mode = csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY
case m[v1.ReadWriteOnce]:
// There is only ReadWriteOnce set
cap.AccessMode.Mode = csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER
default:
return nil, fmt.Errorf("unsupported AccessMode combination: %+v", pv.Spec.AccessModes)
}
return cap, nil
}
func GetVolumeHandle(pv *v1.PersistentVolume) (string, bool, error) {
if pv.Spec.PersistentVolumeSource.CSI == nil {
return "", false, fmt.Errorf("persistent volume does not contain CSI volume source")
}
return pv.Spec.PersistentVolumeSource.CSI.VolumeHandle, pv.Spec.PersistentVolumeSource.CSI.ReadOnly, nil
}
func GetVolumeAttributes(pv *v1.PersistentVolume) (map[string]string, error) {
if pv.Spec.PersistentVolumeSource.CSI == nil {
return nil, fmt.Errorf("persistent volume does not contain CSI volume source")
}
return pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes, nil
}