forked from tsuru/rpaas-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
121 lines (102 loc) · 4.5 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
// Copyright 2020 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/tsuru/rpaas-operator/controllers"
"github.com/tsuru/rpaas-operator/internal/registry"
"github.com/tsuru/rpaas-operator/pkg/controllerapi"
extensionsruntime "github.com/tsuru/rpaas-operator/pkg/runtime"
// +kubebuilder:scaffold:imports
)
var setupLog = ctrl.Log.WithName("setup")
type configOpts struct {
metricsAddr string
healthAddr string
internalAPIAddr string
leaderElection bool
leaderElectionNamespace string
leaderElectionResourceName string
namespace string
syncPeriod time.Duration
portRangeMin int
portRangeMax int
}
func (o *configOpts) bindFlags(fs *flag.FlagSet) {
// Following the standard of flags on Kubernetes.
// See more: https://github.com/kubernetes-sigs/kubebuilder/issues/1839
fs.StringVar(&o.metricsAddr, "metrics-bind-address", ":8080", "The TCP address that controller should bind to for serving Prometheus metrics. It can be set to \"0\" to disable the metrics serving.")
fs.StringVar(&o.healthAddr, "health-probe-bind-address", ":8081", "The TCP address that controller should bind to for serving health probes.")
fs.StringVar(&o.internalAPIAddr, "internal-api-address", ":8082", "The TCP address that controller should bind to for internal controller API.")
fs.BoolVar(&o.leaderElection, "leader-elect", true, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.")
fs.StringVar(&o.leaderElectionResourceName, "leader-elect-resource-name", "rpaas-operator-lock", "The name of resource object that is used for locking during leader election.")
fs.StringVar(&o.leaderElectionNamespace, "leader-elect-resource-namespace", "", "The namespace of resource object that is used for locking during leader election.")
fs.DurationVar(&o.syncPeriod, "sync-period", 10*time.Hour, "The resync period for reconciling manager resources.")
fs.StringVar(&o.namespace, "namespace", "", "Limit the observed RpaasInstance resources from specific namespace (empty means all namespaces)")
fs.IntVar(&o.portRangeMin, "port-range-min", 20000, "Allocated port range start")
fs.IntVar(&o.portRangeMax, "port-range-max", 30000, "Allocated port range end")
}
func (o *configOpts) validate() error {
if o.portRangeMin >= o.portRangeMax {
return fmt.Errorf("invalid port range, min: %d, max: %d", o.portRangeMin, o.portRangeMax)
}
return nil
}
func main() {
var opts configOpts
opts.bindFlags(flag.CommandLine)
zapOpts := zap.Options{}
zapOpts.BindFlags(flag.CommandLine)
flag.Parse()
err := opts.validate()
if err != nil {
setupLog.Error(err, "invalid args")
os.Exit(1)
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: extensionsruntime.NewScheme(),
MetricsBindAddress: opts.metricsAddr,
LeaderElectionResourceLock: "leases",
LeaderElection: opts.leaderElection,
LeaderElectionID: opts.leaderElectionResourceName,
LeaderElectionNamespace: opts.leaderElectionNamespace,
SyncPeriod: &opts.syncPeriod,
HealthProbeBindAddress: opts.healthAddr,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controllers.RpaasInstanceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("RpaasInstance"),
Scheme: mgr.GetScheme(),
PortRangeMin: int32(opts.portRangeMin),
PortRangeMax: int32(opts.portRangeMax),
ImageMetadata: registry.NewImageMetadata(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RpaasInstance")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
// controllerapi
go func() {
setupLog.Info("starting internalapi", "addr", opts.internalAPIAddr)
log.Fatal(http.ListenAndServe(opts.internalAPIAddr, controllerapi.New(mgr.GetClient())))
}()
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}