forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagestream_limits.go
95 lines (81 loc) · 2.6 KB
/
imagestream_limits.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
package admission
import (
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/runtime"
kerrutil "k8s.io/kubernetes/pkg/util/errors"
watch "k8s.io/kubernetes/pkg/watch"
imageapi "github.com/openshift/origin/pkg/image/api"
)
type LimitVerifier interface {
VerifyLimits(namespace string, is *imageapi.ImageStream) error
}
func NewLimitVerifier(client kclient.LimitRangesNamespacer) LimitVerifier {
lw := &cache.ListWatch{
ListFunc: func(options kapi.ListOptions) (runtime.Object, error) {
return client.LimitRanges(kapi.NamespaceAll).List(options)
},
WatchFunc: func(options kapi.ListOptions) (watch.Interface, error) {
return client.LimitRanges(kapi.NamespaceAll).Watch(options)
},
}
indexer, reflector := cache.NewNamespaceKeyedIndexerAndReflector(lw, &kapi.LimitRange{}, 0)
reflector.Run()
return &limitVerifier{
client: client,
indexer: indexer,
}
}
type limitVerifier struct {
client kclient.LimitRangesNamespacer
indexer cache.Indexer
}
func (v *limitVerifier) VerifyLimits(namespace string, is *imageapi.ImageStream) error {
items, err := v.indexer.Index("namespace", &kapi.LimitRange{ObjectMeta: kapi.ObjectMeta{Namespace: namespace}})
if err != nil {
return fmt.Errorf("error resolving limit ranges: %v", err)
}
if len(items) == 0 {
return nil
}
limits := getMaxLimits(items)
if len(limits) == 0 {
return nil
}
usage := GetImageStreamUsage(is)
if err := verifyImageStreamUsage(usage, limits); err != nil {
return kapierrors.NewForbidden(imageapi.Resource("ImageStream"), is.Name, err)
}
return nil
}
func getMaxLimits(limits []interface{}) kapi.ResourceList {
res := kapi.ResourceList{}
for _, limitObject := range limits {
lr := limitObject.(*kapi.LimitRange)
for _, item := range lr.Spec.Limits {
if item.Type != imageapi.LimitTypeImageStream {
continue
}
for _, resource := range []kapi.ResourceName{imageapi.ResourceImageStreamImages, imageapi.ResourceImageStreamTags} {
if max, ok := item.Max[resource]; ok {
if oldMax, exists := res[resource]; !exists || oldMax.Cmp(max) > 0 {
res[resource] = max
}
}
}
}
}
return res
}
func verifyImageStreamUsage(isUsage kapi.ResourceList, limits kapi.ResourceList) error {
errs := []error{}
for resource, limit := range limits {
if usage, ok := isUsage[resource]; ok && usage.Cmp(limit) > 0 {
errs = append(errs, newLimitExceededError(imageapi.LimitTypeImageStream, resource, &usage, &limit))
}
}
return kerrutil.NewAggregate(errs)
}