generated from ironcore-dev/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
217 lines (193 loc) · 6.03 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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/runtime"
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
"github.com/ironcore-dev/metal-provider/internal/log"
"github.com/ironcore-dev/metal-provider/servers"
metalv1alpha4 "github.com/ironcore-dev/metal/apis/metal/v1alpha4"
)
func usage() {
name := filepath.Base(os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [--option]...\n", name)
_, _ = fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults()
}
func exitUsage(err error) {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), err)
}
pflag.Usage()
os.Exit(2)
}
type params struct {
dev bool
leaderElect bool
healthProbeBindAddress string
metricsBindAddress string
kubeconfig string
namespace string
gRPCAddr string
}
func parseCmdLine() params {
pflag.Usage = usage
pflag.ErrHelp = nil
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
pflag.Bool("dev", false, "Log human-readable messages at debug level.")
pflag.Bool("leader-elect", false, "Enable leader election for controller manager to ensure there is only one active controller manager.")
pflag.String("health-probe-bind-address", "", "The address that the health probe server will listen on.")
pflag.String("metrics-bind-address", "0", "The address that the metrics server will listen on.")
pflag.String("kubeconfig", "", "Use a kubeconfig to run out of cluster.")
pflag.String("namespace", "", "Limit monitoring to a specific namespace.")
pflag.String("grpc-address", "/run/metal-provider.sock", "The address that the gRPC server will listen on.")
var help bool
pflag.BoolVarP(&help, "help", "h", false, "Show this help message.")
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
exitUsage(err)
}
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
err = pflag.CommandLine.Parse(os.Args[1:])
if err != nil {
exitUsage(err)
}
if help {
exitUsage(nil)
}
return params{
dev: viper.GetBool("dev"),
leaderElect: viper.GetBool("leader-elect"),
healthProbeBindAddress: viper.GetString("health-probe-bind-address"),
metricsBindAddress: viper.GetString("metrics-bind-address"),
kubeconfig: viper.GetString("kubeconfig"),
namespace: viper.GetString("namespace"),
gRPCAddr: viper.GetString("grpc-address"),
}
}
func main() {
p := parseCmdLine()
var exitCode int
defer func() { os.Exit(exitCode) }()
ctx, stop := signal.NotifyContext(log.Setup(context.Background(), p.dev, false, os.Stderr), syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP)
defer stop()
log.Info(ctx, "Starting ORI machine provider")
defer func() { log.Info(ctx, "Exiting", "exitCode", exitCode) }()
l := logr.FromContextOrDiscard(ctx)
klog.SetLogger(l)
ctrl.SetLogger(l)
if p.namespace == "" {
ns, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil && !os.IsNotExist(err) {
log.Error(ctx, fmt.Errorf("cannot determine in-cluster namespace: %w", err))
exitCode = 1
return
}
p.namespace = string(ns)
if p.namespace == "" {
log.Error(ctx, fmt.Errorf("namespace must be specified when running outside of a Kubernetes cluster"))
exitCode = 1
return
}
log.Debug(ctx, "Using in-cluster namespace", "namespace", p.namespace)
}
log.Debug(ctx, "Loading kubeconfig")
kcfg, err := ctrl.GetConfig()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot load kubeconfig: %w", err))
exitCode = 1
return
}
scheme := runtime.NewScheme()
err = kscheme.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = computev1alpha1.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = metalv1alpha4.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = metalv1alpha4.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
var mgr manager.Manager
mgr, err = ctrl.NewManager(kcfg, ctrl.Options{
BaseContext: func() context.Context {
return ctx
},
Scheme: scheme,
LeaderElection: p.leaderElect,
LeaderElectionID: "metal-provider.ironcore.dev",
HealthProbeBindAddress: p.healthProbeBindAddress,
Metrics: server.Options{
BindAddress: p.metricsBindAddress,
},
})
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create manager: %w", err))
exitCode = 1
return
}
err = mgr.AddHealthzCheck("health", healthz.Ping)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot set up health check: %w", err))
exitCode = 1
return
}
err = mgr.AddReadyzCheck("check", healthz.Ping)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot set up ready check: %w", err))
exitCode = 1
return
}
var grpcServer *servers.GRPCServer
grpcServer, err = servers.NewGRPCServer(p.gRPCAddr, p.namespace)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create server: %w", err), "server", "gRPC")
exitCode = 1
return
}
err = grpcServer.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create server: %w", err), "server", "gRPC")
exitCode = 1
return
}
log.Info(ctx, "Starting manager")
err = mgr.Start(ctx)
if err != nil {
log.Error(ctx, err)
exitCode = 1
return
}
}