forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
virtinformers.go
174 lines (145 loc) · 6.22 KB
/
virtinformers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Red Hat, Inc.
*
*/
package controller
import (
"math/rand"
"sync"
"time"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
kubev1 "kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/kubecli"
"kubevirt.io/kubevirt/pkg/log"
)
type newSharedInformer func() cache.SharedIndexInformer
type KubeInformerFactory interface {
// Starts any informers that have not been started yet
// This function is thread safe and idempotent
Start(stopCh <-chan struct{})
// Watches for vm objects
VM() cache.SharedIndexInformer
// Watches for VirtualMachineReplicaSet objects
VMReplicaSet() cache.SharedIndexInformer
// Watches for VirtualMachinePreset objects
VirtualMachinePreset() cache.SharedIndexInformer
// Watches for pods related only to kubevirt
KubeVirtPod() cache.SharedIndexInformer
// Watches for nodes
KubeVirtNode() cache.SharedIndexInformer
// OfflineVirtualMachine handles the VMs that are stopped or not running
OfflineVirtualMachine() cache.SharedIndexInformer
}
type kubeInformerFactory struct {
restClient *rest.RESTClient
clientSet kubecli.KubevirtClient
lock sync.Mutex
defaultResync time.Duration
informers map[string]cache.SharedIndexInformer
startedInformers map[string]bool
}
func NewKubeInformerFactory(restClient *rest.RESTClient, clientSet kubecli.KubevirtClient) KubeInformerFactory {
return &kubeInformerFactory{
restClient: restClient,
clientSet: clientSet,
// Resulting resync period will be between 12 and 24 hours, like the default for k8s
defaultResync: resyncPeriod(12 * time.Hour),
informers: make(map[string]cache.SharedIndexInformer),
startedInformers: make(map[string]bool),
}
}
// Start can be called from multiple controllers in different go routines safely.
// Only informers that have not started are triggered by this function.
// Multiple calls to this function are idempotent.
func (f *kubeInformerFactory) Start(stopCh <-chan struct{}) {
f.lock.Lock()
defer f.lock.Unlock()
for name, informer := range f.informers {
if f.startedInformers[name] {
// skip informers that have already started.
log.Log.Infof("SKIPPING informer %s", name)
continue
}
log.Log.Infof("STARTING informer %s", name)
go informer.Run(stopCh)
f.startedInformers[name] = true
}
}
// internal function used to retrieve an already created informer
// or create a new informer if one does not already exist.
// Thread safe
func (f *kubeInformerFactory) getInformer(key string, newFunc newSharedInformer) cache.SharedIndexInformer {
f.lock.Lock()
defer f.lock.Unlock()
informer, exists := f.informers[key]
if exists {
return informer
}
informer = newFunc()
f.informers[key] = informer
return informer
}
func (f *kubeInformerFactory) VM() cache.SharedIndexInformer {
return f.getInformer("vmInformer", func() cache.SharedIndexInformer {
lw := cache.NewListWatchFromClient(f.restClient, "virtualmachines", k8sv1.NamespaceAll, fields.Everything())
return cache.NewSharedIndexInformer(lw, &kubev1.VirtualMachine{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}
func (f *kubeInformerFactory) VMReplicaSet() cache.SharedIndexInformer {
return f.getInformer("vmrsInformer", func() cache.SharedIndexInformer {
lw := cache.NewListWatchFromClient(f.restClient, "virtualmachinereplicasets", k8sv1.NamespaceAll, fields.Everything())
return cache.NewSharedIndexInformer(lw, &kubev1.VirtualMachineReplicaSet{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}
func (f *kubeInformerFactory) VirtualMachinePreset() cache.SharedIndexInformer {
return f.getInformer("vmPresetInformer", func() cache.SharedIndexInformer {
lw := cache.NewListWatchFromClient(f.restClient, "virtualmachinepresets", k8sv1.NamespaceAll, fields.Everything())
return cache.NewSharedIndexInformer(lw, &kubev1.VirtualMachinePreset{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}
func (f *kubeInformerFactory) KubeVirtPod() cache.SharedIndexInformer {
return f.getInformer("kubeVirtPodInformer", func() cache.SharedIndexInformer {
// Watch all pods with the kubevirt app label
labelSelector, err := labels.Parse(kubev1.AppLabel)
if err != nil {
panic(err)
}
lw := NewListWatchFromClient(f.clientSet.CoreV1().RESTClient(), "pods", k8sv1.NamespaceAll, fields.Everything(), labelSelector)
return cache.NewSharedIndexInformer(lw, &k8sv1.Pod{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}
func (f *kubeInformerFactory) KubeVirtNode() cache.SharedIndexInformer {
return f.getInformer("kubeVirtNodeInformer", func() cache.SharedIndexInformer {
lw := NewListWatchFromClient(f.clientSet.CoreV1().RESTClient(), "nodes", k8sv1.NamespaceAll, fields.Everything(), labels.Everything())
return cache.NewSharedIndexInformer(lw, &k8sv1.Node{}, f.defaultResync, cache.Indexers{})
})
}
func (f *kubeInformerFactory) OfflineVirtualMachine() cache.SharedIndexInformer {
return f.getInformer("ovmInformer", func() cache.SharedIndexInformer {
lw := cache.NewListWatchFromClient(f.restClient, "offlinevirtualmachines", k8sv1.NamespaceAll, fields.Everything())
return cache.NewSharedIndexInformer(lw, &kubev1.OfflineVirtualMachine{}, f.defaultResync, cache.Indexers{})
})
}
// resyncPeriod computes the time interval a shared informer waits before resyncing with the api server
func resyncPeriod(minResyncPeriod time.Duration) time.Duration {
factor := rand.Float64() + 1
return time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor)
}