-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstorageclass_finder.go
91 lines (73 loc) · 2.79 KB
/
storageclass_finder.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
/*
Copyright (c) 2020-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
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 k8s
import (
v1 "k8s.io/api/storage/v1"
)
// StorageClassGetter is an interface for getting a list of storage class information
//
//go:generate mockgen -destination=mocks/storage_class_getter_mocks.go -package=mocks github.com/dell/karavi-metrics-powerflex/internal/k8s StorageClassGetter
type StorageClassGetter interface {
GetStorageClasses() (*v1.StorageClassList, error)
}
// StorageSystemID contains ID, whether is default and associated drivernames
type StorageSystemID struct {
ID string
IsDefault bool
DriverNames []string
}
// StorageClassFinder is a storage class finder that will query the Kubernetes API for storage classes provisioned by a matching DriverName and StorageSystemID
type StorageClassFinder struct {
API StorageClassGetter
StorageSystemID []StorageSystemID
}
// GetStorageClasses will return a list of storage classes that match the given DriverName in Kubernetes
func (f *StorageClassFinder) GetStorageClasses() ([]v1.StorageClass, error) {
var storageClasses []v1.StorageClass
classes, err := f.API.GetStorageClasses()
if err != nil {
return nil, err
}
for _, class := range classes.Items {
if f.isMatch(class) {
storageClasses = append(storageClasses, class)
}
}
return storageClasses, nil
}
func (f *StorageClassFinder) isMatch(class v1.StorageClass) bool {
for _, storage := range f.StorageSystemID {
if !Contains(storage.DriverNames, class.Provisioner) {
continue
}
systemID := class.Parameters["systemID"]
if systemID == storage.ID {
return true
}
}
for _, storage := range f.StorageSystemID {
if !Contains(storage.DriverNames, class.Provisioner) {
continue
}
systemID, systemIDExists := class.Parameters["systemID"]
// if a storage system is marked as default, the StorageClass is a match if either the 'systemID' key does not exist or if it matches the storage system ID
if storage.IsDefault && (!systemIDExists || systemID == storage.ID) {
return true
}
}
return false
}
// GetStoragePools will return a list of storage pool names from a given Kubernetes storage class
func GetStoragePools(storageClass v1.StorageClass) []string {
return []string{storageClass.Parameters["storagepool"]}
}