/
informerManager.go
204 lines (187 loc) · 6.53 KB
/
informerManager.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package controller
import (
"context"
cisapiv1 "github.com/F5Networks/k8s-bigip-ctlr/v3/config/apis/cis/v1"
"github.com/F5Networks/k8s-bigip-ctlr/v3/pkg/networkmanager"
log "github.com/F5Networks/k8s-bigip-ctlr/v3/pkg/vlogger"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"strings"
)
func (ctlr *Controller) setupInformers() error {
for n := range ctlr.namespaces {
if err := ctlr.addNamespacedInformers(n, false); err != nil {
log.Errorf("Unable to setup informer for namespace: %v, Error:%v", n, err)
return err
}
}
nodeInf := ctlr.getNodeInformer("")
ctlr.multiClusterNodeInformers[""] = &nodeInf
ctlr.addNodeEventUpdateHandler(&nodeInf)
return nil
}
func (ctlr *Controller) initController() {
// Initialize the controller with base resources in CIS config CR
key := strings.Split(ctlr.CISConfigCRKey, "/")
configCR, err := ctlr.clientsets.kubeCRClient.CisV1().DeployConfigs(key[0]).Get(context.TODO(), key[1], metaV1.GetOptions{})
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
ctlr.updateResourceSelectorConfig(configCR.Spec.BaseConfig)
ctlr.updateBigIpConfigMap(configCR.Spec.BigIpConfig)
// process the CNI config
ctlr.processCNIConfig(configCR)
// create the network manager if required
if ctlr.StaticRoutingMode && ctlr.PoolMemberType != NodePort {
// create a new network manager
ctlr.networkManager = networkmanager.NewNetworkManager(ctlr.CMTokenManager, ctlr.ControllerIdentifier)
}
// update the agent params
ctlr.PostParams.AS3Config = configCR.Spec.AS3Config
ctlr.PostParams.tokenManager = ctlr.CMTokenManager
if ctlr.managedResources.ManageRoutes {
// initialize the processed host-path map
var processedHostPath ProcessedHostPath
processedHostPath.processedHostPathMap = make(map[string]metaV1.Time)
ctlr.processedHostPath = &processedHostPath
}
// initialize the informer maps
ctlr.comInformers = make(map[string]*CommonInformer)
ctlr.multiClusterPoolInformers = make(map[string]map[string]*MultiClusterPoolInformer)
ctlr.multiClusterNodeInformers = make(map[string]*NodeInformer)
ctlr.nrInformers = make(map[string]*NRInformer)
ctlr.crInformers = make(map[string]*CRInformer)
ctlr.nsInformers = make(map[string]*NSInformer)
ctlr.namespaces = make(map[string]bool)
if ctlr.resourceSelectorConfig.NamespaceLabel == "" {
ctlr.namespaces[""] = true
log.Debug("No namespaces provided. Watching all namespaces")
} else {
err2 := ctlr.createNamespaceLabeledInformer(ctlr.resourceSelectorConfig.NamespaceLabel)
if err2 != nil {
log.Errorf("%v", err2)
for _, nsInf := range ctlr.nsInformers {
for _, v := range nsInf.nsInformer.GetIndexer().List() {
ns := v.(*v1.Namespace)
ctlr.namespaces[ns.ObjectMeta.Name] = true
}
}
}
}
}
// Start the informers for controller
func (ctlr *Controller) startInformers() {
// start nsinformer in all modes
for _, nsInf := range ctlr.nsInformers {
nsInf.start()
}
// start nodeinformer in all modes
for _, nodeInf := range ctlr.multiClusterNodeInformers {
nodeInf.start()
}
// start comInformers for all modes
for _, inf := range ctlr.comInformers {
inf.start()
}
if ctlr.managedResources.ManageRoutes { // nrInformers only with openShiftMode
for _, inf := range ctlr.nrInformers {
inf.start()
}
}
if ctlr.managedResources.ManageCustomResources { // start customer resource informers in custom resource mode only
for _, inf := range ctlr.crInformers {
inf.start()
}
}
}
// stop the informers for controller
func (ctlr *Controller) stopInformers() {
if ctlr.managedResources.ManageRoutes { // stop native resource informers
for _, inf := range ctlr.nrInformers {
inf.stop()
}
}
if ctlr.managedResources.ManageCustomResources { // stop custom resource informers
for _, inf := range ctlr.crInformers {
inf.stop()
}
}
// stop common informers & namespace informers in all modes
for ns, inf := range ctlr.comInformers {
inf.stop(ns)
}
for ns, nsInf := range ctlr.nsInformers {
nsInf.stop(ns)
}
// stop node Informer
for _, nodeInf := range ctlr.multiClusterNodeInformers {
nodeInf.stop()
}
// stop multi cluster informers
for _, poolInformers := range ctlr.multiClusterPoolInformers {
for _, inf := range poolInformers {
inf.stop()
}
}
}
func (ctlr *Controller) updateResourceSelectorConfig(config cisapiv1.BaseConfig) {
ctlr.resourceSelectorConfig = ResourceSelectorConfig{
NodeLabel: config.NodeLabel,
NamespaceLabel: config.NamespaceLabel,
RouteLabel: config.RouteLabel,
}
ctlr.ControllerIdentifier = config.ControllerIdentifier
ctlr.resourceSelectorConfig.nativeResourceSelector, _ = createLabelSelector(DefaultNativeResourceLabel)
ctlr.resourceSelectorConfig.customResourceSelector, _ = createLabelSelector(DefaultCustomResourceLabel)
}
func (ctlr *Controller) updateBigIpConfigMap(config []cisapiv1.BigIpConfig) {
for _, bigipconfig := range config {
//initialize map with empty bigipconfig.will be updated after resource processing
ctlr.bigIpMap[bigipconfig] = BigIpResourceConfig{ltmConfig: make(LTMConfig), gtmConfig: make(GTMConfig)}
}
}
func (ctlr *Controller) resetControllerForNodeLabel() {
for clusterName, inf := range ctlr.multiClusterNodeInformers {
log.Debugf("Resetting node informer %v", getClusterLog(inf.clusterName))
inf.stop()
inf.oldNodes = []Node{}
newInf := ctlr.getNodeInformer(clusterName)
ctlr.multiClusterNodeInformers[clusterName] = &newInf
ctlr.addNodeEventUpdateHandler(&newInf)
newInf.start()
}
}
func (ctlr *Controller) resetControllerForRouteLabel() {
ctlr.resources.processedNativeResources = make(map[resourceRef]struct{})
var processedHostPath ProcessedHostPath
processedHostPath.processedHostPathMap = make(map[string]metaV1.Time)
ctlr.processedHostPath = &processedHostPath
for namespace, inf := range ctlr.nrInformers {
inf.stop()
newInf := ctlr.newNamespacedNativeResourceInformer(namespace)
ctlr.addNativeResourceEventHandlers(newInf)
ctlr.nrInformers[namespace] = newInf
newInf.start()
}
}
func (ctlr *Controller) resetControllerForNamespaceLabel() {
// stop the older informers
ctlr.stopInformers()
// create new resource store
ctlr.resources = NewResourceStore()
// reinitialize the informers
ctlr.initController()
ctlr.setupInformers()
ctlr.startInformers()
// process the resources
ctlr.initState = true
ctlr.setInitialResourceCount()
// process the DeployConfig CR if present
if ctlr.CISConfigCRKey != "" {
ctlr.processGlobalDeployConfigCR()
}
// process static routes after DeployConfig CR if present is processed to support external cluster static routes
ctlr.processStaticRouteUpdate()
}