-
Notifications
You must be signed in to change notification settings - Fork 94
/
main.go
167 lines (145 loc) · 4.88 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
package main
/*
Copyright 2022 The k8gb Contributors.
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.
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/
import (
"context"
"os"
k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
"github.com/k8gb-io/k8gb/controllers"
"github.com/k8gb-io/k8gb/controllers/depresolver"
"github.com/k8gb-io/k8gb/controllers/logging"
"github.com/k8gb-io/k8gb/controllers/providers/dns"
"github.com/k8gb-io/k8gb/controllers/providers/metrics"
"github.com/k8gb-io/k8gb/controllers/tracing"
istio "istio.io/client-go/pkg/apis/networking/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/scheme"
externaldns "sigs.k8s.io/external-dns/endpoint"
// +kubebuilder:scaffold:imports
)
var (
runtimescheme = runtime.NewScheme()
version = "development"
commit = "none"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(runtimescheme))
utilruntime.Must(k8gbv1beta1.AddToScheme(runtimescheme))
utilruntime.Must(istio.AddToScheme(runtimescheme))
// +kubebuilder:scaffold:scheme
}
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
var f *dns.ProviderFactory
resolver := depresolver.NewDependencyResolver()
config, err := resolver.ResolveOperatorConfig()
deprecations := resolver.GetDeprecations()
// Initialize desired log or default log in case of configuration failed.
logging.Init(config)
log := logging.Logger()
log.Info().
Str("version", version).
Str("commit", commit).
Msg("k8gb info")
if err != nil {
log.Err(err).Msg("Can't resolve environment variables")
return err
}
log.Debug().
Interface("config", config).
Msg("Resolved config")
ctrl.SetLogger(logging.NewLogrAdapter(log))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: runtimescheme,
Metrics: metricsserver.Options{
BindAddress: config.MetricsAddress,
},
LeaderElection: false,
LeaderElectionID: "8020e9ff.absa.oss",
})
if err != nil {
log.Err(err).Msg("Unable to create k8gb operator manager")
return err
}
for _, d := range deprecations {
log.Warn().Msg(d)
}
log.Info().Msg("Registering components")
// Add external-dns DNSEndpoints resource
// https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#adding-3rd-party-resources-to-your-operator
schemeBuilder := &scheme.Builder{GroupVersion: schema.GroupVersion{Group: "externaldns.k8s.io", Version: "v1alpha1"}}
schemeBuilder.Register(&externaldns.DNSEndpoint{}, &externaldns.DNSEndpointList{})
if err := schemeBuilder.AddToScheme(mgr.GetScheme()); err != nil {
log.Err(err).Msg("Unable to register ExternalDNS resource schemas")
return err
}
reconciler := &controllers.GslbReconciler{
Config: config,
Client: mgr.GetClient(),
DepResolver: resolver,
Scheme: mgr.GetScheme(),
}
log.Info().Msg("Starting metrics")
metrics.Init(config)
defer metrics.Metrics().Unregister()
err = metrics.Metrics().Register()
if err != nil {
log.Err(err).Msg("Unable to register metrics")
return err
}
log.Info().Msg("Resolving DNS provider")
f, err = dns.NewDNSProviderFactory(reconciler.Client, *reconciler.Config)
if err != nil {
log.Err(err).Msg("Unable to create DNS provider factory")
return err
}
reconciler.DNSProvider = f.Provider()
log.Info().
Str("provider", reconciler.DNSProvider.String()).
Msg("Started DNS provider")
if err = reconciler.SetupWithManager(mgr); err != nil {
log.Err(err).Msg("Unable to create Gslb controller")
return err
}
metrics.Metrics().SetRuntimeInfo(version, commit)
// tracing
cfg := tracing.Settings{
Enabled: config.TracingEnabled,
Endpoint: config.OtelExporterOtlpEndpoint,
SamplingRatio: config.TracingSamplingRatio,
Commit: commit,
AppVersion: version,
}
cleanup, tracer := tracing.SetupTracing(context.Background(), cfg, log)
reconciler.Tracer = tracer
defer cleanup()
// +kubebuilder:scaffold:builder
log.Info().Msg("Starting k8gb")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Err(err).Msg("Problem running k8gb")
return err
}
log.Info().Msg("Gracefully finished, bye!")
return nil
}