forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagestream_limits.go
91 lines (75 loc) · 2.42 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
package admission
import (
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
kerrutil "k8s.io/kubernetes/pkg/util/errors"
imageapi "github.com/openshift/origin/pkg/image/api"
)
type LimitVerifier interface {
VerifyLimits(namespace string, is *imageapi.ImageStream) error
}
type NamespaceLimiter interface {
LimitsForNamespace(namespace string) (kapi.ResourceList, error)
}
// NewLimitVerifier accepts a NamespaceLimiter
func NewLimitVerifier(limiter NamespaceLimiter) LimitVerifier {
return &limitVerifier{
limiter: limiter,
}
}
type limitVerifier struct {
limiter NamespaceLimiter
}
func (v *limitVerifier) VerifyLimits(namespace string, is *imageapi.ImageStream) error {
limits, err := v.limiter.LimitsForNamespace(namespace)
if err != nil || len(limits) == 0 {
return err
}
usage := GetImageStreamUsage(is)
if err := verifyImageStreamUsage(usage, limits); err != nil {
return kapierrors.NewForbidden(imageapi.Resource("ImageStream"), is.Name, err)
}
return nil
}
func verifyImageStreamUsage(isUsage kapi.ResourceList, limits kapi.ResourceList) error {
var 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)
}
type LimitRangesForNamespaceFunc func(namespace string) ([]*kapi.LimitRange, error)
func (fn LimitRangesForNamespaceFunc) LimitsForNamespace(namespace string) (kapi.ResourceList, error) {
items, err := fn(namespace)
if err != nil {
return nil, err
}
var res kapi.ResourceList
for _, limitRange := range items {
res = getMaxLimits(limitRange, res)
}
return res, nil
}
// getMaxLimits updates the resource list to include the max allowed image count
// TODO: use the existing Max function for resource lists.
func getMaxLimits(limit *kapi.LimitRange, current kapi.ResourceList) kapi.ResourceList {
res := current
for _, item := range limit.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 {
if res == nil {
res = make(kapi.ResourceList)
}
res[resource] = max
}
}
}
}
return res
}