This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
resource.go
87 lines (73 loc) · 1.91 KB
/
resource.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
package pvc
import (
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
const (
// Name is the identifier of the resource.
Name = "pvc"
// EtcdStorageClass is the storage class annotation persistent volume claims are
// configured with.
EtcdStorageClass = "g8s-storage"
LocalStorageClass = "local-storage"
)
// Config represents the configuration used to create a new PVC resource.
type Config struct {
// Dependencies.
K8sClient kubernetes.Interface
Logger micrologger.Logger
}
// DefaultConfig provides a default configuration to create a new PVC
// resource by best effort.
func DefaultConfig() Config {
return Config{
// Dependencies.
K8sClient: nil,
Logger: nil,
}
}
// Resource implements the PVC resource.
type Resource struct {
// Dependencies.
k8sClient kubernetes.Interface
logger micrologger.Logger
}
// New creates a new configured PVC resource.
func New(config Config) (*Resource, error) {
// Dependencies.
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "config.K8sClient must not be empty")
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "config.Logger must not be empty")
}
newResource := &Resource{
// Dependencies.
k8sClient: config.K8sClient,
logger: config.Logger,
}
return newResource, nil
}
func (r *Resource) Name() string {
return Name
}
func containsPVC(list []corev1.PersistentVolumeClaim, item corev1.PersistentVolumeClaim) bool {
for _, l := range list {
if l.Name == item.Name {
return true
}
}
return false
}
func toPVCs(v interface{}) ([]corev1.PersistentVolumeClaim, error) {
if v == nil {
return nil, nil
}
PVCs, ok := v.([]corev1.PersistentVolumeClaim)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", []corev1.PersistentVolumeClaim{}, v)
}
return PVCs, nil
}