-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
247 lines (214 loc) · 7.58 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
/*
Copyright 2022 The Polykube Authors.
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 main
import (
"flag"
"github.com/polycube-network/polykube/controllers"
"github.com/polycube-network/polykube/node"
"github.com/polycube-network/polykube/polycube"
"net/http"
"os"
"os/signal"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"syscall"
"time"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
//+kubebuilder:scaffold:imports
)
const (
polycubeBasePath = "http://127.0.0.1:9000/polycube/v1"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func exit(code int) {
node.DeleteVxlanIface()
node.DeletePolykubeVethPair()
os.Exit(code)
}
// setupSignalHandler sets up a handler for handling cleanup if SIGTERM or SIGINT interrupts occur
func setupSignalHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-c
exit(5)
}()
}
// ensureDeployment ensures the connection with polycubed, creates all the necessary polycube cubes and connects them
// together and with the existing pods in order to allow network communications
func ensureDeployment() error {
if err := polycube.EnsureConnection(); err != nil {
return err
}
if err := polycube.EnsureCubes(); err != nil {
return err
}
if err := polycube.EnsureCubesConnections(); err != nil {
return err
}
// the first time ensureDeployment is called, the Pod default gateway MAC must be retrieved from the infrastructure
// and its value complete the information required for writing the CNI configuration file. If ensureDeployment
// is called after a polycubed disconnection, simply the old MAC is set to the new polycube router port.
podGwMAC := node.Conf.PodGwInfo.MAC
if podGwMAC == nil {
podGwMAC, err := polycube.GetRouterToIntLbrpPortMAC()
if err != nil {
return err
}
node.Conf.PodGwInfo.MAC = podGwMAC
if err := node.EnsureCNIConf(); err != nil {
return err
}
} else {
if err := polycube.SetRouterToIntLbrpPortMAC(podGwMAC); err != nil {
return err
}
}
pods, err := node.GetPods(node.Conf.Node.Name)
if err != nil {
return err
}
if err := polycube.EnsureIntLbrpMissingFrontendPorts(pods.Items); err != nil {
return err
}
setupLog.Info("ensured network infrastructure deployment")
return nil
}
// startPolycubedPolling starts an asynchronous task in order to periodically queries polycubed. If for some reason
// polycubed is not reachable, an attempts to restore the connection and the polycube network infrastructure is made
// by calling internally ensureDeployment
func startPolycubedPolling() {
go func() {
for {
time.Sleep(10 * time.Second)
if _, err := http.Get(polycubeBasePath); err != nil {
setupLog.Info("lost polycubed connection, waiting for it...")
if err := ensureDeployment(); err != nil {
setupLog.Error(err, "failed to network infrastructure deployment")
exit(13)
}
setupLog.Info("re-established network infrastructure deployment after connection loss")
}
}
}()
}
func main() {
var metricsAddr string
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
// obtaining environment configuration by taking it from env variables
if err := node.LoadEnvironment(); err != nil {
setupLog.Error(err, "failed to load environment configuration from environment variables")
os.Exit(1)
}
// loading node configuration details
if err := node.LoadConfig(); err != nil {
setupLog.Error(err, "failed to load cluster node configuration")
os.Exit(2)
}
// ensuring vxlan interface on the node
if err := node.EnsureVxlanIface(); err != nil {
setupLog.Error(err, "failed to ensure vxlan interface")
os.Exit(3)
}
// ensuring polykube veth pair on the node
if err := node.EnsurePolykubeVethPair(); err != nil {
setupLog.Error(err, "failed to ensure polykube veth pair")
exit(4)
}
setupSignalHandler()
// initialize polycube package internal state
polycube.InitConf()
// ensure that the network infrastructure is correctly deployed and connected to the already existing entities
if err := ensureDeployment(); err != nil {
setupLog.Error(err, "failed to ensure network infrastructure deployment")
exit(6)
}
mgrConfig := ctrl.GetConfigOrDie()
// changing the config host in such a way that the manager can talk directly with the API server
// without using the "kubernetes" ClusterIP Service
mgrConfig.Host = node.Env.APIServerEndpoint()
mgr, err := ctrl.NewManager(mgrConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: false,
})
if err != nil {
setupLog.Error(err, "failed to create manager")
exit(7)
}
if err = (&controllers.NodeReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "failed to create controller", "controller", "node-controller")
exit(8)
}
if err = (&controllers.ServiceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "failed to create controller", "controller", "service-controller")
exit(9)
}
if err = (&controllers.EndpointsReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "failed to create controller", "controller", "endpoints-controller")
exit(10)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "failed to set up health check")
exit(11)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "failed to set up ready check")
exit(12)
}
// the following is used in order to react to a possible polycubed crash
startPolycubedPolling()
setupLog.Info("starting manager...")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "failed to start manager")
exit(14)
}
// If this point is reached, a SIGTERM or a SIGNINT signal has occurred. The termination must be handled by
// the goroutine created by the setupSignalHandler function, that calls os.Exit at the end of a cleanup procedure.
// In order to avoid that the main goroutine is destroyed before the cleanup procedure and the call to os.Exit,
// the following is used in order to stop it undefinetely
<-make(chan struct{})
}