-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (175 loc) · 6.13 KB
/
main.go
File metadata and controls
195 lines (175 loc) · 6.13 KB
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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-logr/logr"
"github.com/krateoplatformops/plumbing/env"
"github.com/krateoplatformops/plumbing/ptr"
prettylog "github.com/krateoplatformops/plumbing/slogs/pretty"
"github.com/krateoplatformops/unstructured-runtime/pkg/controller/builder"
"github.com/krateoplatformops/unstructured-runtime/pkg/logging"
"github.com/krateoplatformops/unstructured-runtime/pkg/metrics/server"
restResources "github.com/krateoplatformops/rest-dynamic-controller/internal/controllers"
getter "github.com/krateoplatformops/rest-dynamic-controller/internal/tools/definitiongetter"
"github.com/krateoplatformops/unstructured-runtime/pkg/controller"
"github.com/krateoplatformops/unstructured-runtime/pkg/pluralizer"
"github.com/krateoplatformops/unstructured-runtime/pkg/workqueue"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
serviceName = "rest-dynamic-controller"
)
var (
Build string
)
func main() {
// Flags
kubeconfig := flag.String("kubeconfig",
env.String("KUBECONFIG", ""),
"absolute path to the kubeconfig file")
debug := flag.Bool("debug",
env.Bool("REST_CONTROLLER_DEBUG", false),
"dump verbose output")
prettyJSONDebug := flag.Bool("pretty-json-debug",
env.Bool("REST_CONTROLLER_PRETTY_JSON_DEBUG", false),
"globally enable pretty-print JSON formatting in HTTP debug output (response bodies)")
workers := flag.Int("workers",
env.Int("REST_CONTROLLER_WORKERS", 5),
"number of workers")
resyncInterval := flag.Duration("resync-interval",
env.Duration("REST_CONTROLLER_RESYNC_INTERVAL", time.Minute*3),
"resync interval")
resourceGroup := flag.String("group",
env.String("REST_CONTROLLER_GROUP", ""),
"resource api group")
resourceVersion := flag.String("version",
env.String("REST_CONTROLLER_VERSION", ""),
"resource api version")
resourceName := flag.String("resource",
env.String("REST_CONTROLLER_RESOURCE", ""),
"resource plural name")
namespace := flag.String("namespace",
env.String("REST_CONTROLLER_NAMESPACE", ""),
"namespace to watch, empty for all namespaces")
maxErrorRetryInterval := flag.Duration("max-error-retry-interval",
env.Duration("REST_CONTROLLER_MAX_ERROR_RETRY_INTERVAL", 90*time.Second),
"The maximum interval between retries when an error occurs. This should be less than the half of the resync interval.")
minErrorRetryInterval := flag.Duration("min-error-retry-interval",
env.Duration("REST_CONTROLLER_MIN_ERROR_RETRY_INTERVAL", 1*time.Second),
"The minimum interval between retries when an error occurs. This should be less than max-error-retry-interval.")
maxErrorRetry := flag.Int("max-error-retries",
env.Int("REST_CONTROLLER_MAX_ERROR_RETRIES", 5),
"How many times to retry the processing of a resource when an error occurs before giving up and dropping the resource.")
metricsServerPort := flag.Int("metrics-server-port",
env.Int("REST_CONTROLLER_METRICS_SERVER_PORT", 0),
"The address to bind the metrics server to. If empty, metrics server is disabled.")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Flags:")
flag.PrintDefaults()
}
flag.Parse()
logLevel := slog.LevelInfo
if *debug {
logLevel = slog.LevelDebug
}
lh := prettylog.New(&slog.HandlerOptions{
Level: logLevel,
AddSource: false,
},
prettylog.WithDestinationWriter(os.Stderr),
prettylog.WithColor(),
prettylog.WithOutputEmptyAttrs(),
)
log := logging.NewLogrLogger(logr.FromSlogHandler(slog.New(lh).Handler()))
// Kubernetes configuration
var cfg *rest.Config
var err error
if len(*kubeconfig) > 0 {
cfg, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
} else {
cfg, err = builder.GetConfig()
}
if err != nil {
log.Error(err, "Building kubeconfig.")
os.Exit(1)
}
var handler controller.ExternalClient
pluralizer := pluralizer.New()
var swg getter.Getter
swg, err = getter.Dynamic(cfg, pluralizer)
if err != nil {
log.Error(err, "Creating definition getter.")
os.Exit(1)
}
log.WithValues("build", Build).
WithValues("debug", *debug).
WithValues("prettyJSONDebug", *prettyJSONDebug).
WithValues("resyncInterval", *resyncInterval).
WithValues("group", *resourceGroup).
WithValues("version", *resourceVersion).
WithValues("resource", *resourceName).
WithValues("namespace", *namespace).
WithValues("maxErrorRetryInterval", *maxErrorRetryInterval).
WithValues("minErrorRetryInterval", *minErrorRetryInterval).
WithValues("maxErrorRetry", *maxErrorRetry).
WithValues("workers", *workers).
Info("Starting.", "serviceName", serviceName)
handler = restResources.NewHandler(cfg, log, swg, *pluralizer, *prettyJSONDebug)
if handler == nil {
log.Error(fmt.Errorf("handler is nil"), "Creating handler for controller.")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), []os.Signal{
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGQUIT,
}...)
defer cancel()
opts := []builder.FuncOption{
builder.WithLogger(log),
builder.WithNamespace(*namespace),
builder.WithResyncInterval(*resyncInterval),
builder.WithGlobalRateLimiter(workqueue.NewExponentialTimedFailureRateLimiter[any](*minErrorRetryInterval, *maxErrorRetryInterval)),
builder.WithMaxRetries(*maxErrorRetry),
}
metricsServerBindAddress := ""
if ptr.Deref(metricsServerPort, 0) != 0 {
log.Info("Metrics server enabled", "bindAddress", fmt.Sprintf(":%d", *metricsServerPort))
metricsServerBindAddress = fmt.Sprintf(":%d", *metricsServerPort)
opts = append(opts, builder.WithMetrics(server.Options{
BindAddress: metricsServerBindAddress,
}))
} else {
log.Info("Metrics server disabled")
}
controller, err := builder.Build(ctx, builder.Configuration{
Config: cfg,
GVR: schema.GroupVersionResource{
Group: *resourceGroup,
Version: *resourceVersion,
Resource: *resourceName,
},
ProviderName: serviceName,
}, opts...)
if err != nil {
log.Error(err, "Building controller.")
os.Exit(1)
}
controller.SetExternalClient(handler)
err = controller.Run(ctx, *workers)
if err != nil {
log.Error(err, "Running controller.")
os.Exit(1)
}
}