forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replenishment_controller.go
68 lines (60 loc) · 2.39 KB
/
replenishment_controller.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
package controller
import (
"fmt"
"reflect"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/controller/framework"
kresourcequota "k8s.io/kubernetes/pkg/controller/resourcequota"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
osclient "github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// replenishmentControllerFactory implements ReplenishmentControllerFactory
type replenishmentControllerFactory struct {
osClient osclient.Interface
}
var _ kresourcequota.ReplenishmentControllerFactory = &replenishmentControllerFactory{}
// NewReplenishmentControllerFactory returns a factory that knows how to build controllers
// to replenish resources when updated or deleted
func NewReplenishmentControllerFactory(osClient osclient.Interface) kresourcequota.ReplenishmentControllerFactory {
return &replenishmentControllerFactory{
osClient: osClient,
}
}
func (r *replenishmentControllerFactory) NewController(options *kresourcequota.ReplenishmentControllerOptions) (*framework.Controller, error) {
var result *framework.Controller
switch options.GroupKind {
case imageapi.Kind("ImageStream"):
_, result = framework.NewInformer(
&cache.ListWatch{
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
return r.osClient.ImageStreams(api.NamespaceAll).List(options)
},
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
return r.osClient.ImageStreams(api.NamespaceAll).Watch(options)
},
},
&imageapi.ImageStream{},
options.ResyncPeriod(),
framework.ResourceEventHandlerFuncs{
UpdateFunc: ImageStreamReplenishmentUpdateFunc(options),
DeleteFunc: kresourcequota.ObjectReplenishmentDeleteFunc(options),
},
)
default:
return nil, fmt.Errorf("no replenishment controller available for %s", options.GroupKind)
}
return result, nil
}
// ImageStreamReplenishmentUpdateFunc will replenish if the old image stream was quota tracked but the new is not
func ImageStreamReplenishmentUpdateFunc(options *kresourcequota.ReplenishmentControllerOptions) func(oldObj, newObj interface{}) {
return func(oldObj, newObj interface{}) {
oldIS := oldObj.(*imageapi.ImageStream)
newIS := newObj.(*imageapi.ImageStream)
if !reflect.DeepEqual(oldIS.Status.Tags, newIS.Status.Tags) {
options.ReplenishmentFunc(options.GroupKind, newIS.Namespace, newIS)
}
}
}