-
Notifications
You must be signed in to change notification settings - Fork 204
/
main.go
257 lines (226 loc) · 7.7 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// 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.
//nolint:forbidigo
package main
import (
_ "embed"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
//nolint:gosec
_ "net/http/pprof"
"k8s.io/apimachinery/pkg/watch"
"github.com/go-test/deep"
"github.com/jessevdk/go-flags"
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
"github.com/haproxytech/kubernetes-ingress/pkg/controller"
"github.com/haproxytech/kubernetes-ingress/pkg/job"
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
"github.com/haproxytech/kubernetes-ingress/pkg/k8s/meta"
k8ssync "github.com/haproxytech/kubernetes-ingress/pkg/k8s/sync"
"github.com/haproxytech/kubernetes-ingress/pkg/store"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
"github.com/haproxytech/kubernetes-ingress/pkg/version"
_ "go.uber.org/automaxprocs"
)
//go:embed fs/usr/local/etc/haproxy/haproxy.cfg
var haproxyConf []byte
func main() {
exitCode := 0
defer func() {
if r := recover(); r != nil {
fmt.Println("Error : ", r)
}
os.Exit(exitCode)
}()
// Parse Controller Args
var osArgs utils.OSArgs
var err error
parser := flags.NewParser(&osArgs, flags.IgnoreUnknown)
if _, err = parser.Parse(); err != nil {
fmt.Println(err)
exitCode = 1
return
}
// Set Logger
logger := utils.GetLogger()
err = version.Set()
if err != nil {
logger.Error(err)
os.Exit(1) //nolint:gocritic
}
logger.SetLevel(osArgs.LogLevel.LogLevel)
if len(osArgs.Help) > 0 && osArgs.Help[0] {
parser.WriteHelp(os.Stdout)
return
}
if osArgs.JobCheckCRD {
logger.Print(version.IngressControllerInfo)
logger.Print(job.IngressControllerCRDUpdater)
logger.Infof("HAProxy Ingress Controller CRD Updater %s %s", version.GitTag, version.GitCommit)
logger.Infof("Build from: %s", version.GitRepo)
logger.Infof("Build date: %s\n", version.GitCommitDate)
err := job.CRDRefresh(logger, osArgs)
if err != nil {
logger.Error(err)
os.Exit(1)
}
// exit, this is just a job
os.Exit(0)
}
if osArgs.InitialSyncPeriod == 0 {
osArgs.InitialSyncPeriod = osArgs.SyncPeriod
}
logger.ShowFilename(false)
exit := logInfo(logger, osArgs)
if exit {
return
}
logger.ShowFilename(true)
annotations.DisableConfigSnippets(osArgs.DisableConfigSnippets)
// backwards compatibility with 1.7
if osArgs.PprofEnabled && osArgs.ControllerPort == 0 {
osArgs.ControllerPort = 6060
}
if osArgs.PrometheusEnabled && osArgs.ControllerPort == 0 {
osArgs.ControllerPort = 6060
}
deep.NilMapsAreEmpty = true
deep.NilSlicesAreEmpty = true
// Default annotations
defaultBackendSvc := fmt.Sprint(osArgs.DefaultBackendService)
defaultCertificate := fmt.Sprint(osArgs.DefaultCertificate)
annotations.SetDefaultValue("default-backend-service", defaultBackendSvc)
annotations.SetDefaultValue("default-backend-port", strconv.Itoa(osArgs.DefaultBackendPort))
annotations.SetDefaultValue("ssl-certificate", defaultCertificate)
// Start Controller
var chanSize int64 = int64(watch.DefaultChanSize * 6)
if osArgs.ChannelSize > 0 {
chanSize = osArgs.ChannelSize
}
eventChan := make(chan k8ssync.SyncDataEvent, chanSize)
stop := make(chan struct{})
publishService := getNamespaceValue(osArgs.PublishService)
s := store.NewK8sStore(osArgs)
k := k8s.New(
osArgs,
s.NamespacesAccess.Whitelist,
publishService,
)
if osArgs.Test {
meta.GetMetaStore().ProcessedResourceVersion.SetTestMode()
}
c := controller.NewBuilder().
WithHaproxyCfgFile(haproxyConf).
WithEventChan(eventChan).
WithStore(s).
WithClientSet(k.GetClientset()).
WithRestClientSet(k.GetRestClientset()).
WithArgs(osArgs).Build()
isGatewayAPIInstalled := k.IsGatewayAPIInstalled(osArgs.GatewayControllerName)
c.SetGatewayAPIInstalled(isGatewayAPIInstalled)
go k.MonitorChanges(eventChan, stop, osArgs, isGatewayAPIInstalled)
go c.Start()
// Catch QUIT signals
signalC := make(chan os.Signal, 1)
signal.Notify(signalC, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
<-signalC
c.Stop()
close(stop)
}
func logInfo(logger utils.Logger, osArgs utils.OSArgs) bool {
if len(osArgs.Version) > 0 {
fmt.Printf("HAProxy Ingress Controller %s %s\n", version.GitTag, version.GitCommit)
fmt.Printf("Build from: %s\n", version.GitRepo)
fmt.Printf("Git commit date: %s\n", version.GitCommitDate)
if len(osArgs.Version) > 1 {
fmt.Printf("ConfigMap: %s\n", osArgs.ConfigMap)
fmt.Printf("Ingress class: %s\n", osArgs.IngressClass)
fmt.Printf("Empty Ingress class: %t\n", osArgs.EmptyIngressClass)
}
return true
}
logger.Print(version.IngressControllerInfo)
logger.Printf("HAProxy Ingress Controller %s %s", version.GitTag, version.GitCommit)
logger.Printf("Build from: %s", version.GitRepo)
logger.Printf("Git commit date: %s", version.GitCommitDate)
logger.Printf("ConfigMap: %s", osArgs.ConfigMap)
logger.Printf("Ingress class: %s", osArgs.IngressClass)
logger.Printf("Empty Ingress class: %t", osArgs.EmptyIngressClass)
if osArgs.GatewayControllerName != "" {
// display log message only if Gateway API is used
logger.Printf("Gateway API controller name: %s", osArgs.GatewayControllerName)
}
logger.Printf("Publish service: %s", osArgs.PublishService)
if osArgs.DefaultBackendService.String() != "" {
logger.Printf("Default backend service: %s", osArgs.DefaultBackendService)
} else {
logger.Printf("Using local backend service on port: %d", osArgs.DefaultBackendPort)
}
logger.Printf("Default ssl certificate: %s", osArgs.DefaultCertificate)
if !osArgs.DisableHTTP {
logger.Printf("Frontend HTTP listening on: %s:%d", osArgs.IPV4BindAddr, osArgs.HTTPBindPort)
}
if !osArgs.DisableHTTPS {
logger.Printf("Frontend HTTPS listening on: %s:%d", osArgs.IPV4BindAddr, osArgs.HTTPSBindPort)
}
if osArgs.DisableHTTP {
logger.Printf("Disabling HTTP frontend")
}
if osArgs.DisableHTTPS {
logger.Printf("Disabling HTTPS frontend")
}
if osArgs.DisableIPV4 {
logger.Printf("Disabling IPv4 support")
}
if osArgs.DisableIPV6 {
logger.Printf("Disabling IPv6 support")
}
if osArgs.ConfigMapTCPServices.Name != "" {
logger.Printf("TCP Services provided in '%s'", osArgs.ConfigMapTCPServices)
}
if osArgs.ConfigMapErrorFiles.Name != "" {
logger.Printf("Errorfiles provided in '%s'", osArgs.ConfigMapErrorFiles)
}
if osArgs.ConfigMapPatternFiles.Name != "" {
logger.Printf("Pattern files provided in '%s'", osArgs.ConfigMapPatternFiles)
}
if osArgs.DisableConfigSnippets != "" {
logger.Printf("Disabling config snippets for [%s]", osArgs.DisableConfigSnippets)
}
if osArgs.DisableDelayedWritingOnlyIfReload {
logger.Printf("Disabling the delayed writing of files to disk only in case of haproxy reload (write to disk even if no reload)")
}
logger.Debugf("Kubernetes Informers resync period: %s", osArgs.CacheResyncPeriod.String())
logger.Printf("Controller initial sync period: %s", osArgs.InitialSyncPeriod.String())
logger.Printf("Controller sync period: %s\n", osArgs.SyncPeriod.String())
hostname, err := os.Hostname()
logger.Error(err)
logger.Printf("Running on %s", hostname)
return false
}
func getNamespaceValue(name string) *utils.NamespaceValue {
parts := strings.Split(name, "/")
var result *utils.NamespaceValue
if len(parts) == 2 {
result = &utils.NamespaceValue{
Namespace: parts[0],
Name: parts[1],
}
}
return result
}