Skip to content

Commit fabcbe5

Browse files
authored
Merge pull request #8159 from bedsteye/migrate-klog-flush
migrate os.Exit(255) to klog.FlushAndExit
2 parents adf59d4 + 1c79586 commit fabcbe5

File tree

12 files changed

+24
-33
lines changed

12 files changed

+24
-33
lines changed

vertical-pod-autoscaler/pkg/admission-controller/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989

9090
if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
9191
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
92-
os.Exit(255)
92+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
9393
}
9494

9595
healthCheck := metrics.NewHealthCheck(time.Minute)
@@ -118,7 +118,7 @@ func main() {
118118
hostname, err := os.Hostname()
119119
if err != nil {
120120
klog.ErrorS(err, "Unable to get hostname")
121-
os.Exit(255)
121+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
122122
}
123123

124124
statusNamespace := status.AdmissionControllerStatusNamespace
@@ -157,6 +157,6 @@ func main() {
157157

158158
if err = server.ListenAndServeTLS("", ""); err != nil {
159159
klog.ErrorS(err, "Failed to start HTTPS server")
160-
os.Exit(255)
160+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
161161
}
162162
}

vertical-pod-autoscaler/pkg/recommender/input/cluster_feeder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package input
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"slices"
2423
"time"
2524

@@ -183,7 +182,7 @@ func newPodClients(kubeClient kube_client.Interface, resourceEventHandler cache.
183182
indexer, ok := store.(cache.Indexer)
184183
if !ok {
185184
klog.ErrorS(nil, "Expected Indexer, but got a Store that does not implement Indexer")
186-
os.Exit(255)
185+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
187186
}
188187
podLister := v1lister.NewPodLister(indexer)
189188
go controller.Run(stopCh)

vertical-pod-autoscaler/pkg/recommender/input/metrics/metrics_source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package metrics
1818

1919
import (
2020
"context"
21-
"os"
2221
"time"
2322

2423
k8sapiv1 "k8s.io/api/core/v1"
@@ -73,7 +72,7 @@ func NewExternalClient(c *rest.Config, clusterState model.ClusterState, options
7372
extClient, err := external_metrics.NewForConfig(c)
7473
if err != nil {
7574
klog.ErrorS(err, "Failed initializing external metrics client")
76-
os.Exit(255)
75+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
7776
}
7877
return &externalMetricsClient{
7978
externalClient: extClient,

vertical-pod-autoscaler/pkg/recommender/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func main() {
141141

142142
if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
143143
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
144-
os.Exit(255)
144+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
145145
}
146146

147147
if *routines.MinCheckpointsPerRun != 10 { // Default value is 10
@@ -162,7 +162,7 @@ func main() {
162162
id, err := os.Hostname()
163163
if err != nil {
164164
klog.ErrorS(err, "Unable to get hostname")
165-
os.Exit(255)
165+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
166166
}
167167
id = id + "_" + string(uuid.NewUUID())
168168

@@ -181,7 +181,7 @@ func main() {
181181
)
182182
if err != nil {
183183
klog.ErrorS(err, "Unable to create leader election lock")
184-
os.Exit(255)
184+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
185185
}
186186

187187
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
@@ -296,7 +296,7 @@ func run(ctx context.Context, healthCheck *metrics.HealthCheck, commonFlag *comm
296296
promQueryTimeout, err := time.ParseDuration(*queryTimeout)
297297
if err != nil {
298298
klog.ErrorS(err, "Could not parse --prometheus-query-timeout as a time.Duration")
299-
os.Exit(255)
299+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
300300
}
301301

302302
if useCheckpoints {
@@ -324,7 +324,7 @@ func run(ctx context.Context, healthCheck *metrics.HealthCheck, commonFlag *comm
324324
provider, err := history.NewPrometheusHistoryProvider(config)
325325
if err != nil {
326326
klog.ErrorS(err, "Could not initialize history provider")
327-
os.Exit(255)
327+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
328328
}
329329
recommender.GetClusterStateFeeder().InitFromHistoryProvider(provider)
330330
}

vertical-pod-autoscaler/pkg/recommender/model/cluster_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package model
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"testing"
2423
"time"
2524

@@ -354,7 +353,7 @@ func addVpaObject(cluster ClusterState, id VpaID, vpa *vpa_types.VerticalPodAuto
354353
err := cluster.AddOrUpdateVpa(vpa, parsedSelector)
355354
if err != nil {
356355
klog.ErrorS(err, "AddOrUpdateVpa() failed")
357-
os.Exit(255)
356+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
358357
}
359358
return cluster.VPAs()[id]
360359
}

vertical-pod-autoscaler/pkg/target/controller_fetcher/controller_fetcher.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"os"
2423
"time"
2524

2625
appsv1 "k8s.io/api/apps/v1"
@@ -117,7 +116,7 @@ func NewControllerFetcher(config *rest.Config, kubeClient kube_client.Interface,
117116
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
118117
if err != nil {
119118
klog.ErrorS(err, "Could not create discoveryClient")
120-
os.Exit(255)
119+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
121120
}
122121
resolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
123122
restClient := kubeClient.CoreV1().RESTClient()

vertical-pod-autoscaler/pkg/target/fetcher.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package target
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"time"
2423

2524
appsv1 "k8s.io/api/apps/v1"
@@ -72,7 +71,7 @@ func NewVpaTargetSelectorFetcher(config *rest.Config, kubeClient kube_client.Int
7271
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
7372
if err != nil {
7473
klog.ErrorS(err, "Could not create discoveryClient")
75-
os.Exit(255)
74+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
7675
}
7776
resolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
7877
restClient := kubeClient.CoreV1().RESTClient()
@@ -98,7 +97,7 @@ func NewVpaTargetSelectorFetcher(config *rest.Config, kubeClient kube_client.Int
9897
synced := cache.WaitForCacheSync(stopCh, informer.HasSynced)
9998
if !synced {
10099
klog.ErrorS(nil, "Could not sync cache for "+string(kind))
101-
os.Exit(255)
100+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
102101
} else {
103102
klog.InfoS("Initial sync completed", "kind", kind)
104103
}

vertical-pod-autoscaler/pkg/updater/logic/updater.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package logic
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"slices"
2423
"time"
2524

@@ -152,7 +151,7 @@ func (u *updater) RunOnce(ctx context.Context) {
152151
vpaList, err := u.vpaLister.List(labels.Everything())
153152
if err != nil {
154153
klog.ErrorS(err, "Failed to get VPA list")
155-
os.Exit(255)
154+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
156155
}
157156
timer.ObserveStep("ListVPAs")
158157

@@ -406,7 +405,7 @@ func newEventRecorder(kubeClient kube_client.Interface) record.EventRecorder {
406405
vpascheme := scheme.Scheme
407406
if err := corescheme.AddToScheme(vpascheme); err != nil {
408407
klog.ErrorS(err, "Error adding core scheme")
409-
os.Exit(255)
408+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
410409
}
411410

412411
return eventBroadcaster.NewRecorder(vpascheme, apiv1.EventSource{Component: "vpa-updater"})

vertical-pod-autoscaler/pkg/updater/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func main() {
9999

100100
if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
101101
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
102-
os.Exit(255)
102+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
103103
}
104104

105105
healthCheck := metrics.NewHealthCheck(*updaterInterval * 5)
@@ -113,7 +113,7 @@ func main() {
113113
id, err := os.Hostname()
114114
if err != nil {
115115
klog.ErrorS(err, "Unable to get hostname")
116-
os.Exit(255)
116+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
117117
}
118118
id = id + "_" + string(uuid.NewUUID())
119119

@@ -132,7 +132,7 @@ func main() {
132132
)
133133
if err != nil {
134134
klog.ErrorS(err, "Unable to create leader election lock")
135-
os.Exit(255)
135+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
136136
}
137137

138138
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
@@ -216,7 +216,7 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
216216
)
217217
if err != nil {
218218
klog.ErrorS(err, "Failed to create updater")
219-
os.Exit(255)
219+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
220220
}
221221

222222
// Start updating health check endpoint.

vertical-pod-autoscaler/pkg/utils/metrics/healthcheck.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package metrics
1919
import (
2020
"fmt"
2121
"net/http"
22-
"os"
2322
"sync"
2423
"time"
2524

@@ -77,7 +76,7 @@ func (hc *HealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7776
_, err := w.Write([]byte("OK"))
7877
if err != nil {
7978
klog.ErrorS(err, "Failed to write response message")
80-
os.Exit(255)
79+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
8180
}
8281
}
8382
}

vertical-pod-autoscaler/pkg/utils/server/server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package server
2020
import (
2121
"net/http"
2222
"net/http/pprof"
23-
"os"
2423

2524
"k8s.io/klog/v2"
2625

@@ -49,6 +48,6 @@ func Initialize(enableProfiling *bool, healthCheck *metrics.HealthCheck, address
4948

5049
err := http.ListenAndServe(*address, mux)
5150
klog.ErrorS(err, "Failed to start metrics")
52-
os.Exit(255)
51+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
5352
}()
5453
}

vertical-pod-autoscaler/pkg/utils/vpa/api.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24-
"os"
2524
"strings"
2625
"time"
2726

@@ -95,13 +94,13 @@ func NewVpasLister(vpaClient *vpa_clientset.Clientset, stopChannel <-chan struct
9594
indexer, ok := store.(cache.Indexer)
9695
if !ok {
9796
klog.ErrorS(nil, "Expected Indexer, but got a Store that does not implement Indexer")
98-
os.Exit(255)
97+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
9998
}
10099
vpaLister := vpa_lister.NewVerticalPodAutoscalerLister(indexer)
101100
go controller.Run(stopChannel)
102101
if !cache.WaitForCacheSync(stopChannel, controller.HasSynced) {
103102
klog.ErrorS(nil, "Failed to sync VPA cache during initialization")
104-
os.Exit(255)
103+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
105104
} else {
106105
klog.InfoS("Initial VPA synced successfully")
107106
}

0 commit comments

Comments
 (0)