This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
166 lines (143 loc) · 4.93 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
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"
"github.com/k8gb-io/k8gb-light/controllers"
"github.com/k8gb-io/k8gb-light/controllers/depresolver"
"github.com/k8gb-io/k8gb-light/controllers/logging"
"github.com/k8gb-io/k8gb-light/controllers/mapper"
"github.com/k8gb-io/k8gb-light/controllers/providers/dns"
"github.com/k8gb-io/k8gb-light/controllers/providers/metrics"
"github.com/k8gb-io/k8gb-light/controllers/tracing"
"github.com/k8gb-io/k8gb-light/controllers/utils"
"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"
ctrl "sigs.k8s.io/controller-runtime"
sch "sigs.k8s.io/controller-runtime/pkg/scheme"
externaldns "sigs.k8s.io/external-dns/endpoint"
)
var (
scheme = runtime.NewScheme()
version = "development"
commit = "none"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
}
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
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: scheme,
MetricsBindAddress: config.MetricsAddress,
Port: 9443,
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 := &sch.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
}
log.Info().Msg("Starting metrics")
metrics.Init(config)
defer metrics.Prometheus().Unregister()
err = metrics.Prometheus().Register()
if err != nil {
log.Err(err).Msg("Unable to register metrics")
return err
}
reconciler := &controllers.AnnoReconciler{
Config: config,
Client: mgr.GetClient(),
DepResolver: resolver,
Scheme: mgr.GetScheme(),
Mapper: mapper.NewCommonProvider(mgr.GetClient(), config),
ReconcilerResult: utils.NewReconcileResultHandler(config.ReconcileRequeueSeconds),
Log: log,
Metrics: metrics.Prometheus(),
}
log.Info().Msg("Resolving DNS provider")
var f *dns.ProviderFactory
f, err = dns.NewDNSProviderFactory(reconciler.Client, *reconciler.Config, log, reconciler.Metrics)
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.Prometheus().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
}