forked from strangelove-ventures/cosmos-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (217 loc) · 8.37 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
/*
Copyright 2022 Strangelove Ventures 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.
*/
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/go-logr/zapr"
"github.com/pkg/profile"
"github.com/spf13/cobra"
"github.com/spf13/viper"
opcmd "github.com/strangelove-ventures/cosmos-operator/cmd"
"github.com/strangelove-ventures/cosmos-operator/controllers"
"github.com/strangelove-ventures/cosmos-operator/internal/cosmos"
"github.com/strangelove-ventures/cosmos-operator/internal/fullnode"
"github.com/strangelove-ventures/cosmos-operator/internal/version"
"sigs.k8s.io/controller-runtime/pkg/healthz"
// 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"
// Add Pprof endpoints.
_ "net/http/pprof"
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1"
cosmosv1alpha1 "github.com/strangelove-ventures/cosmos-operator/api/v1alpha1"
"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"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(snapshotv1.AddToScheme(scheme))
utilruntime.Must(cosmosv1.AddToScheme(scheme))
utilruntime.Must(cosmosv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
root := rootCmd()
ctx := ctrl.SetupSignalHandler()
if err := root.ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}
// root command flags
var (
metricsAddr string
enableLeaderElection bool
probeAddr string
profileMode string
logLevel string
logFormat string
)
func rootCmd() *cobra.Command {
root := &cobra.Command{
Short: "Run the operator",
Use: "manager",
Version: version.AppVersion(),
RunE: startManager,
SilenceUsage: true,
}
root.Flags().StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
root.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
root.Flags().BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
root.Flags().StringVar(&profileMode, "profile", "", "Enable profiling and save profile to working dir. (Must be one of 'cpu', or 'mem'.)")
root.Flags().StringVar(&logLevel, "log-level", "info", "Logging level one of 'error', 'info', 'debug'")
root.Flags().StringVar(&logFormat, "log-format", "console", "Logging format one of 'console' or 'json'")
if err := viper.BindPFlags(root.Flags()); err != nil {
panic(err)
}
// Add subcommands here
root.AddCommand(opcmd.HealthCheckCmd())
root.AddCommand(opcmd.VersionCheckCmd(scheme))
root.AddCommand(&cobra.Command{
Short: "Print the version",
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("App Version:", version.AppVersion())
fmt.Println("Docker Tag:", version.DockerTag())
},
})
return root
}
func startManager(cmd *cobra.Command, args []string) error {
go func() {
setupLog.Info("Serving pprof endpoints at localhost:6060/debug/pprof")
if err := http.ListenAndServe("localhost:6060", nil); err != nil {
setupLog.Error(err, "Pprof server exited with error")
}
}()
logger := opcmd.ZapLogger(logLevel, logFormat)
defer func() { _ = logger.Sync() }()
ctrl.SetLogger(zapr.NewLogger(logger))
if profileMode != "" {
defer profile.Start(profileOpts(profileMode)...).Stop()
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "16e1bc09.strange.love",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
return fmt.Errorf("unable to start manager: %w", err)
}
ctx := cmd.Context()
// CacheController which fetches CometBFT status in the background.
httpClient := &http.Client{Timeout: 30 * time.Second}
statusClient := fullnode.NewStatusClient(mgr.GetClient())
cometClient := cosmos.NewCometClient(httpClient)
cacheController := cosmos.NewCacheController(
cosmos.NewStatusCollector(cometClient, 5*time.Second),
mgr.GetClient(),
mgr.GetEventRecorderFor(cosmos.CacheControllerName),
)
defer func() { _ = cacheController.Close() }()
if err = cacheController.SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create CosmosCache controller: %w", err)
}
// The primary controller for CosmosFullNode.
if err = controllers.NewFullNode(
mgr.GetClient(),
mgr.GetEventRecorderFor(cosmosv1.CosmosFullNodeController),
statusClient,
cacheController,
).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create CosmosFullNode controller: %w", err)
}
// An ancillary controller that supports CosmosFullNode.
if err = controllers.NewSelfHealing(
mgr.GetClient(),
mgr.GetEventRecorderFor(cosmosv1.SelfHealingController),
statusClient,
httpClient,
cacheController,
).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create SelfHealing controller: %w", err)
}
// Test for presence of VolumeSnapshot CRD.
snapshotErr := controllers.IndexVolumeSnapshots(ctx, mgr)
if snapshotErr != nil {
setupLog.Info("Warning: VolumeSnapshot CRD not found, StatefulJob and ScheduledVolumeSnapshot controllers will be disabled")
}
// StatefulJobs
jobCtl := controllers.NewStatefulJob(
mgr.GetClient(),
mgr.GetEventRecorderFor(cosmosv1alpha1.StatefulJobController),
snapshotErr != nil,
)
if err = jobCtl.SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create StatefulJob controller: %w", err)
}
// ScheduledVolumeSnapshots
if err = controllers.NewScheduledVolumeSnapshotReconciler(
mgr.GetClient(),
mgr.GetEventRecorderFor(cosmosv1alpha1.ScheduledVolumeSnapshotController),
statusClient,
cacheController,
snapshotErr != nil,
).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create ScheduledVolumeSnapshot controller: %w", err)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return fmt.Errorf("unable to set up health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
return fmt.Errorf("unable to set up ready check: %w", err)
}
setupLog.Info("Starting Cosmos Operator manager", "version", version.AppVersion())
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("problem running manager: %w", err)
}
return nil
}
func profileOpts(mode string) []func(*profile.Profile) {
opts := []func(*profile.Profile){profile.ProfilePath("."), profile.NoShutdownHook}
switch mode {
case "cpu":
return append(opts, profile.CPUProfile)
case "mem":
return append(opts, profile.MemProfile)
default:
panic(fmt.Errorf("unknown profile mode %q", mode))
}
}