forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplenishment_controller.go
61 lines (52 loc) · 2.6 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
package controller
import (
"fmt"
"reflect"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kresourcequota "k8s.io/kubernetes/pkg/controller/resourcequota"
osclient "github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/controller/shared"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// replenishmentControllerFactory implements ReplenishmentControllerFactory
type replenishmentControllerFactory struct {
isInformer shared.ImageStreamInformer
}
var _ kresourcequota.ReplenishmentControllerFactory = &replenishmentControllerFactory{}
// NewReplenishmentControllerFactory returns a factory that knows how to build controllers
// to replenish resources when updated or deleted
func NewReplenishmentControllerFactory(isInformer shared.ImageStreamInformer) kresourcequota.ReplenishmentControllerFactory {
return &replenishmentControllerFactory{
isInformer: isInformer,
}
}
func (r *replenishmentControllerFactory) NewController(options *kresourcequota.ReplenishmentControllerOptions) (cache.ControllerInterface, error) {
switch options.GroupKind {
case imageapi.Kind("ImageStream"):
r.isInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: ImageStreamReplenishmentUpdateFunc(options),
DeleteFunc: kresourcequota.ObjectReplenishmentDeleteFunc(options),
})
return r.isInformer.Informer().GetController(), nil
default:
return nil, fmt.Errorf("no replenishment controller available for %s", options.GroupKind)
}
}
// 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)
}
}
}
// NewAllResourceReplenishmentControllerFactory returns a ReplenishmentControllerFactory that knows how to replenish all known resources
func NewAllResourceReplenishmentControllerFactory(informerFactory shared.InformerFactory, osClient osclient.Interface, kubeClientSet clientset.Interface) kresourcequota.ReplenishmentControllerFactory {
return kresourcequota.UnionReplenishmentControllerFactory{
kresourcequota.NewReplenishmentControllerFactory(informerFactory.KubernetesInformers(), kubeClientSet),
NewReplenishmentControllerFactory(informerFactory.ImageStreams()),
}
}