-
Notifications
You must be signed in to change notification settings - Fork 195
/
monitor.go
134 lines (127 loc) · 4.56 KB
/
monitor.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
// Copyright 2019 HAProxy Technologies LLC
//
// 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.
package controller
import (
"os"
v1 "github.com/haproxytech/kubernetes-ingress/crs/api/ingress/v1"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/instance"
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
"github.com/haproxytech/kubernetes-ingress/pkg/store"
)
// SyncData gets all kubernetes changes, aggregates them and apply to HAProxy.
// All the changes must come through this function
func (c *HAProxyController) SyncData() {
hadChanges := false
for job := range c.eventChan {
ns := c.store.GetNamespace(job.Namespace)
change := false
switch job.SyncType {
case k8s.COMMAND:
c.auxCfgManager()
// create a NeedAction function.
if hadChanges || instance.NeedAction() {
c.updateHAProxy()
hadChanges = false
continue
}
case k8s.CR_GLOBAL:
var data *v1.Global
if job.Data != nil {
data = job.Data.(*v1.Global) //nolint:forcetypeassert
}
change = c.store.EventGlobalCR(job.Namespace, job.Name, data)
case k8s.CR_DEFAULTS:
var data *v1.Defaults
if job.Data != nil {
data = job.Data.(*v1.Defaults) //nolint:forcetypeassert
}
change = c.store.EventDefaultsCR(job.Namespace, job.Name, data)
case k8s.CR_BACKEND:
var data *v1.Backend
if job.Data != nil {
data = job.Data.(*v1.Backend) //nolint:forcetypeassert
}
change = c.store.EventBackendCR(job.Namespace, job.Name, data)
case k8s.NAMESPACE:
change = c.store.EventNamespace(ns, job.Data.(*store.Namespace)) //nolint:forcetypeassert
case k8s.INGRESS:
change = c.store.EventIngress(ns, job.Data.(*store.Ingress)) //nolint:forcetypeassert
case k8s.INGRESS_CLASS:
change = c.store.EventIngressClass(job.Data.(*store.IngressClass)) //nolint:forcetypeassert
case k8s.ENDPOINTS:
change = c.store.EventEndpoints(ns, job.Data.(*store.Endpoints), c.haproxy.SyncBackendSrvs) //nolint:forcetypeassert
case k8s.SERVICE:
change = c.store.EventService(ns, job.Data.(*store.Service)) //nolint:forcetypeassert
case k8s.CONFIGMAP:
change = c.store.EventConfigMap(ns, job.Data.(*store.ConfigMap)) //nolint:forcetypeassert
case k8s.SECRET:
change = c.store.EventSecret(ns, job.Data.(*store.Secret)) //nolint:forcetypeassert
case k8s.POD:
change = c.store.EventPod(job.Data.(store.PodEvent)) //nolint:forcetypeassert
case k8s.PUBLISH_SERVICE:
change = c.store.EventPublishService(ns, job.Data.(*store.Service)) //nolint:forcetypeassert
case k8s.GATEWAYCLASS:
change = c.store.EventGatewayClass(job.Data.(*store.GatewayClass))
case k8s.GATEWAY:
change = c.store.EventGateway(ns, job.Data.(*store.Gateway))
case k8s.TCPROUTE:
change = c.store.EventTCPRoute(ns, job.Data.(*store.TCPRoute))
case k8s.REFERENCEGRANT:
change = c.store.EventReferenceGrant(ns, job.Data.(*store.ReferenceGrant))
}
hadChanges = hadChanges || change
if job.EventProcessed != nil {
close(job.EventProcessed)
}
}
}
// auxCfgManager returns restart or reload requirement based on state and transition of auxiliary configuration file.
func (c *HAProxyController) auxCfgManager() {
info, errStat := os.Stat(c.haproxy.AuxCFGFile)
var (
modifTime int64
auxCfgFile = c.haproxy.AuxCFGFile
useAuxFile bool
)
defer func() {
// Nothing changed
if c.auxCfgModTime == modifTime {
return
}
// Apply decisions
c.haproxy.SetAuxCfgFile(auxCfgFile)
c.haproxy.UseAuxFile(useAuxFile)
// The file exists now (modifTime !=0 otherwise nothing changed case).
instance.RestartIf(c.auxCfgModTime == 0, "auxiliary configuration file created")
instance.ReloadIf(c.auxCfgModTime != 0, "auxiliary configuration file modified")
c.auxCfgModTime = modifTime
if c.auxCfgModTime != 0 {
logger.Infof("Auxiliary HAProxy config '%s' updated", auxCfgFile)
}
}()
// File does not exist
if errStat != nil {
// nullify it
auxCfgFile = ""
if c.auxCfgModTime == 0 {
// never existed before
return
}
instance.Restart("Auxiliary HAProxy config '%s' removed", c.haproxy.AuxCFGFile)
return
}
// File exists
useAuxFile = true
modifTime = info.ModTime().Unix()
}