Debian Codegraph @

storage/volume/helpers.go

raw symbols SCIP
1 /*
2 Copyright 2020 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package volume
18
19 import (
20 "fmt"
21
22 v1 "k8s.io/api/core/v1"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/component-helpers/scheduling/corev1"
25 )
26
27 // PersistentVolumeClaimHasClass returns true if given claim has set StorageClassName field.
28 func PersistentVolumeClaimHasClass(claim *v1.PersistentVolumeClaim) bool {
29 // Use beta annotation first
30 if _, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found {
31 return true
32 }
33
34 if claim.Spec.StorageClassName != nil {
35 return true
36 }
37
38 return false
39 }
40
41 // GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was
42 // requested, it returns "".
43 func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
44 // Use beta annotation first
45 if class, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found {
46 return class
47 }
48
49 if claim.Spec.StorageClassName != nil {
50 return *claim.Spec.StorageClassName
51 }
52
53 return ""
54 }
55
56 // GetPersistentVolumeClass returns StorageClassName.
57 func GetPersistentVolumeClass(volume *v1.PersistentVolume) string {
58 // Use beta annotation first
59 if class, found := volume.Annotations[v1.BetaStorageClassAnnotation]; found {
60 return class
61 }
62
63 return volume.Spec.StorageClassName
64 }
65
66 // CheckNodeAffinity looks at the PV node affinity, and checks if the node has the same corresponding labels
67 // This ensures that we don't mount a volume that doesn't belong to this node
68 func CheckNodeAffinity(pv *v1.PersistentVolume, nodeLabels map[string]string) error {
69 if pv.Spec.NodeAffinity == nil {
70 return nil
71 }
72
73 if pv.Spec.NodeAffinity.Required != nil {
74 node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: nodeLabels}}
75 terms := pv.Spec.NodeAffinity.Required
76 if matches, err := corev1.MatchNodeSelectorTerms(node, terms); err != nil {
77 return err
78 } else if !matches {
79 return fmt.Errorf("no matching NodeSelectorTerms")
80 }
81 }
82
83 return nil
84 }