/
helpers.go
139 lines (123 loc) · 3.9 KB
/
helpers.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
package v1
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
quotav1 "github.com/openshift/api/quota/v1"
)
func GetResourceQuotasStatusByNamespace(namespaceStatuses quotav1.ResourceQuotasStatusByNamespace, namespace string) (corev1.ResourceQuotaStatus, bool) {
for i := range namespaceStatuses {
curr := namespaceStatuses[i]
if curr.Namespace == namespace {
return curr.Status, true
}
}
return corev1.ResourceQuotaStatus{}, false
}
func RemoveResourceQuotasStatusByNamespace(namespaceStatuses *quotav1.ResourceQuotasStatusByNamespace, namespace string) {
newNamespaceStatuses := quotav1.ResourceQuotasStatusByNamespace{}
for i := range *namespaceStatuses {
curr := (*namespaceStatuses)[i]
if curr.Namespace == namespace {
continue
}
newNamespaceStatuses = append(newNamespaceStatuses, curr)
}
*namespaceStatuses = newNamespaceStatuses
}
func InsertResourceQuotasStatus(namespaceStatuses *quotav1.ResourceQuotasStatusByNamespace, newStatus quotav1.ResourceQuotaStatusByNamespace) {
newNamespaceStatuses := quotav1.ResourceQuotasStatusByNamespace{}
found := false
for i := range *namespaceStatuses {
curr := (*namespaceStatuses)[i]
if curr.Namespace == newStatus.Namespace {
// do this so that we don't change serialization order
newNamespaceStatuses = append(newNamespaceStatuses, newStatus)
found = true
continue
}
newNamespaceStatuses = append(newNamespaceStatuses, curr)
}
if !found {
newNamespaceStatuses = append(newNamespaceStatuses, newStatus)
}
*namespaceStatuses = newNamespaceStatuses
}
var accessor = meta.NewAccessor()
func GetMatcher(selector quotav1.ClusterResourceQuotaSelector) (func(obj runtime.Object) (bool, error), error) {
var labelSelector labels.Selector
if selector.LabelSelector != nil {
var err error
labelSelector, err = metav1.LabelSelectorAsSelector(selector.LabelSelector)
if err != nil {
return nil, err
}
}
var annotationSelector map[string]string
if len(selector.AnnotationSelector) > 0 {
// ensure our matcher has a stable copy of the map
annotationSelector = make(map[string]string, len(selector.AnnotationSelector))
for k, v := range selector.AnnotationSelector {
annotationSelector[k] = v
}
}
return func(obj runtime.Object) (bool, error) {
if labelSelector != nil {
objLabels, err := accessor.Labels(obj)
if err != nil {
return false, err
}
if !labelSelector.Matches(labels.Set(objLabels)) {
return false, nil
}
}
if annotationSelector != nil {
objAnnotations, err := accessor.Annotations(obj)
if err != nil {
return false, err
}
for k, v := range annotationSelector {
if objValue, exists := objAnnotations[k]; !exists || objValue != v {
return false, nil
}
}
}
return true, nil
}, nil
}
func GetObjectMatcher(selector quotav1.ClusterResourceQuotaSelector) (func(obj metav1.Object) (bool, error), error) {
var labelSelector labels.Selector
if selector.LabelSelector != nil {
var err error
labelSelector, err = metav1.LabelSelectorAsSelector(selector.LabelSelector)
if err != nil {
return nil, err
}
}
var annotationSelector map[string]string
if len(selector.AnnotationSelector) > 0 {
// ensure our matcher has a stable copy of the map
annotationSelector = make(map[string]string, len(selector.AnnotationSelector))
for k, v := range selector.AnnotationSelector {
annotationSelector[k] = v
}
}
return func(obj metav1.Object) (bool, error) {
if labelSelector != nil {
if !labelSelector.Matches(labels.Set(obj.GetLabels())) {
return false, nil
}
}
if annotationSelector != nil {
objAnnotations := obj.GetAnnotations()
for k, v := range annotationSelector {
if objValue, exists := objAnnotations[k]; !exists || objValue != v {
return false, nil
}
}
}
return true, nil
}, nil
}