Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting klog.Fatalf to structured logging equivalent #7646

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions vertical-pod-autoscaler/pkg/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func main() {
klog.V(1).InfoS("Starting Vertical Pod Autoscaler Admission Controller", "version", common.VerticalPodAutoscalerVersion)

if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
os.Exit(255)
}

healthCheck := metrics.NewHealthCheck(time.Minute)
Expand Down Expand Up @@ -113,7 +114,8 @@ func main() {

hostname, err := os.Hostname()
if err != nil {
klog.Fatalf("Unable to get hostname: %v", err)
klog.ErrorS(err, "Unable to get hostname")
os.Exit(255)
}

statusNamespace := status.AdmissionControllerStatusNamespace
Expand Down Expand Up @@ -151,6 +153,7 @@ func main() {
}()

if err = server.ListenAndServeTLS("", ""); err != nil {
klog.Fatalf("HTTPS Error: %s", err)
klog.ErrorS(err, "Failed to start HTTPS server")
os.Exit(255)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package metrics

import (
"context"
"os"
"time"

k8sapiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -70,7 +71,8 @@ type ExternalClientOptions struct {
func NewExternalClient(c *rest.Config, clusterState *model.ClusterState, options ExternalClientOptions) PodMetricsLister {
extClient, err := external_metrics.NewForConfig(c)
if err != nil {
klog.Fatalf("Failed initializing external metrics client: %v", err)
klog.ErrorS(err, "Failed initializing external metrics client")
os.Exit(255)
}
return &externalMetricsClient{
externalClient: extClient,
Expand Down
15 changes: 10 additions & 5 deletions vertical-pod-autoscaler/pkg/recommender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func main() {
klog.V(1).InfoS("Vertical Pod Autoscaler Recommender", "version", common.VerticalPodAutoscalerVersion, "recommenderName", *recommenderName)

if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
os.Exit(255)
}

healthCheck := metrics.NewHealthCheck(*metricsFetcherInterval * 5)
Expand All @@ -140,7 +141,8 @@ func main() {
} else {
id, err := os.Hostname()
if err != nil {
klog.Fatalf("Unable to get hostname: %v", err)
klog.ErrorS(err, "Unable to get hostname")
os.Exit(255)
}
id = id + "_" + string(uuid.NewUUID())

Expand All @@ -158,7 +160,8 @@ func main() {
},
)
if err != nil {
klog.Fatalf("Unable to create leader election lock: %v", err)
klog.ErrorS(err, "Unable to create leader election lock")
os.Exit(255)
}

leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Expand Down Expand Up @@ -266,7 +269,8 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {

promQueryTimeout, err := time.ParseDuration(*queryTimeout)
if err != nil {
klog.Fatalf("Could not parse --prometheus-query-timeout as a time.Duration: %v", err)
klog.ErrorS(err, "Could not parse --prometheus-query-timeout as a time.Duration")
os.Exit(255)
}

if useCheckpoints {
Expand All @@ -293,7 +297,8 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
}
provider, err := history.NewPrometheusHistoryProvider(config)
if err != nil {
klog.Fatalf("Could not initialize history provider: %v", err)
klog.ErrorS(err, "Could not initialize history provider")
os.Exit(255)
}
recommender.GetClusterStateFeeder().InitFromHistoryProvider(provider)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package model
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -354,7 +355,8 @@ func addVpaObject(cluster *ClusterState, id VpaID, vpa *vpa_types.VerticalPodAut
parsedSelector, _ := metav1.LabelSelectorAsSelector(labelSelector)
err := cluster.AddOrUpdateVpa(vpa, parsedSelector)
if err != nil {
klog.Fatalf("AddOrUpdateVpa() failed: %v", err)
klog.ErrorS(err, "AddOrUpdateVpa() failed")
os.Exit(255)
}
return cluster.Vpas[id]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"os"
"time"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -115,7 +116,8 @@ func (f *controllerFetcher) Start(ctx context.Context, loopPeriod time.Duration)
func NewControllerFetcher(config *rest.Config, kubeClient kube_client.Interface, factory informers.SharedInformerFactory, betweenRefreshes, lifeTime time.Duration, jitterFactor float64) *controllerFetcher {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
klog.Fatalf("Could not create discoveryClient: %v", err)
klog.ErrorS(err, "Could not create discoveryClient")
os.Exit(255)
}
resolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
restClient := kubeClient.CoreV1().RESTClient()
Expand Down
7 changes: 5 additions & 2 deletions vertical-pod-autoscaler/pkg/target/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package target
import (
"context"
"fmt"
"os"
"time"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -69,7 +70,8 @@ const (
func NewVpaTargetSelectorFetcher(config *rest.Config, kubeClient kube_client.Interface, factory informers.SharedInformerFactory) VpaTargetSelectorFetcher {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
klog.Fatalf("Could not create discoveryClient: %v", err)
klog.ErrorS(err, "Could not create discoveryClient")
os.Exit(255)
}
resolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
restClient := kubeClient.CoreV1().RESTClient()
Expand All @@ -94,7 +96,8 @@ func NewVpaTargetSelectorFetcher(config *rest.Config, kubeClient kube_client.Int
go informer.Run(stopCh)
synced := cache.WaitForCacheSync(stopCh, informer.HasSynced)
if !synced {
klog.Fatalf("Could not sync cache for %s: %v", kind, err)
klog.ErrorS(nil, "Could not sync cache for "+string(kind))
os.Exit(255)
} else {
klog.InfoS("Initial sync completed", "kind", kind)
}
Expand Down
7 changes: 5 additions & 2 deletions vertical-pod-autoscaler/pkg/updater/logic/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package logic
import (
"context"
"fmt"
"os"
"slices"
"time"

Expand Down Expand Up @@ -135,7 +136,8 @@ func (u *updater) RunOnce(ctx context.Context) {

vpaList, err := u.vpaLister.List(labels.Everything())
if err != nil {
klog.Fatalf("failed get VPA list: %v", err)
klog.ErrorS(err, "Failed to get VPA list")
os.Exit(255)
}
timer.ObserveStep("ListVPAs")

Expand Down Expand Up @@ -318,7 +320,8 @@ func newEventRecorder(kubeClient kube_client.Interface) record.EventRecorder {

vpascheme := scheme.Scheme
if err := corescheme.AddToScheme(vpascheme); err != nil {
klog.Fatalf("Error adding core scheme: %v", err)
klog.ErrorS(err, "Error adding core scheme")
os.Exit(255)
}

return eventBroadcaster.NewRecorder(vpascheme, apiv1.EventSource{Component: "vpa-updater"})
Expand Down
12 changes: 8 additions & 4 deletions vertical-pod-autoscaler/pkg/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func main() {
klog.V(1).InfoS("Vertical Pod Autoscaler Updater", "version", common.VerticalPodAutoscalerVersion)

if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 {
klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
klog.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
os.Exit(255)
}

healthCheck := metrics.NewHealthCheck(*updaterInterval * 5)
Expand All @@ -105,7 +106,8 @@ func main() {
} else {
id, err := os.Hostname()
if err != nil {
klog.Fatalf("Unable to get hostname: %v", err)
klog.ErrorS(err, "Unable to get hostname")
os.Exit(255)
}
id = id + "_" + string(uuid.NewUUID())

Expand All @@ -123,7 +125,8 @@ func main() {
},
)
if err != nil {
klog.Fatalf("Unable to create leader election lock: %v", err)
klog.ErrorS(err, "Unable to create leader election lock")
os.Exit(255)
}

leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Expand Down Expand Up @@ -201,7 +204,8 @@ func run(healthCheck *metrics.HealthCheck, commonFlag *common.CommonFlags) {
ignoredNamespaces,
)
if err != nil {
klog.Fatalf("Failed to create updater: %v", err)
klog.ErrorS(err, "Failed to create updater")
os.Exit(255)
}

// Start updating health check endpoint.
Expand Down
4 changes: 3 additions & 1 deletion vertical-pod-autoscaler/pkg/utils/metrics/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package metrics
import (
"fmt"
"net/http"
"os"
"sync"
"time"

Expand Down Expand Up @@ -75,7 +76,8 @@ func (hc *HealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write([]byte("OK"))
if err != nil {
klog.Fatalf("Failed to write response message: %v", err)
klog.ErrorS(err, "Failed to write response message")
os.Exit(255)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion vertical-pod-autoscaler/pkg/utils/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package server
import (
"net/http"
"net/http/pprof"
"os"

"k8s.io/klog/v2"

Expand All @@ -46,6 +47,7 @@ func Initialize(enableProfiling *bool, healthCheck *metrics.HealthCheck, address
}

err := http.ListenAndServe(*address, mux)
klog.Fatalf("Failed to start metrics: %v", err)
klog.ErrorS(err, "Failed to start metrics")
os.Exit(255)
}()
}
Loading