Skip to content

add logs format for leader elections#1619

Merged
Uburro merged 1 commit intomainfrom
uburro/add-logs-format-to-leaderelection
Sep 24, 2025
Merged

add logs format for leader elections#1619
Uburro merged 1 commit intomainfrom
uburro/add-logs-format-to-leaderelection

Conversation

@Uburro
Copy link
Collaborator

@Uburro Uburro commented Sep 24, 2025

Problem

Leader election logs from Kubernetes components (like leaderelection.go) were always output in plain text format (I0924 10:23:13.769662 1 leaderelection.go:257] attempting to acquire leader lease...), even when the soperator was configured to use JSON logging format. This created inconsistent log formats in structured logging environments where all logs should be in JSON format for proper parsing and processing by log aggregation systems.

Solution

Configured klog (Kubernetes logging library) to use the same structured logger as controller-runtime across all main.go files that use controller-runtime:

  • Added klog.SetLogger() calls in cmd/main.go, cmd/soperatorchecks/main.go, cmd/sconfigcontroller/main.go, and cmd/rebooter/main.go
  • The klog logger now inherits the same format configuration (JSON/plain text) as the main application logger
  • Leader election logs and other Kubernetes client-go logs now respect the --log-format flag setting

Testing

  • Verified compilation of all modified main.go files
  • Ran make lint to ensure no formatting or import issues
  • Tested that the applications start successfully with the new klog configuration
  • Confirmed that klog imports are properly used and no unused import warnings
  • Manual verification that leader election logs now follow the same format as application logs when JSON format is enabled
IS_PROMETHEUS_CRD_INSTALLED=true IS_MARIADB_CRD_INSTALLED=true ENABLE_WEBHOOKS=false IS_APPARMOR_CRD_INSTALLED=true go run cmd/main.go \
-log-level=debug -leader-elect=true -operator-namespace=soperator-system --enable-topology-controller=true | grep leader
{"level":"info","ts":"2025-09-24T15:25:04+02:00","logger":"setup","msg":"starting manager"}
{"level":"info","ts":"2025-09-24T15:25:04+02:00","msg":"starting server","name":"health probe","addr":"[::]:8081"}
{"level":"info","ts":"2025-09-24T15:25:04+02:00","logger":"controller-runtime.metrics","msg":"Starting metrics server"}
{"level":"info","ts":"2025-09-24T15:25:04+02:00","logger":"controller-runtime.metrics","msg":"Serving metrics server","bindAddress":":8080","secure":false}
{"level":"info","ts":"2025-09-24T15:25:04+02:00","logger":"klog","msg":"attempting to acquire leader lease soperator-system/e21479ae.nebius.ai..."}

Release Notes

Feature: Leader election and Kubernetes client logs now respect the --log-format flag setting, providing consistent JSON or plain text formatting across all soperator components. This improves log consistency in structured logging environments without any breaking changes to existing deployments.

@Uburro Uburro added go Pull requests that update Go code fix labels Sep 24, 2025
@Uburro Uburro requested a review from Copilot September 24, 2025 13:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR configures klog (Kubernetes logging library) to use the same structured logger as controller-runtime to ensure consistent log formatting across all soperator components. Previously, leader election logs from Kubernetes components were always in plain text format even when JSON logging was configured.

  • Added klog.SetLogger() calls to inherit the same format configuration as the main application logger
  • Modified go.mod to make k8s.io/klog/v2 a direct dependency instead of indirect
  • Updated Makefile to enable leader election during local development runs

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
go.mod Changed k8s.io/klog/v2 from indirect to direct dependency
cmd/main.go Added klog configuration and leader election namespace
cmd/soperatorchecks/main.go Added klog configuration for consistent log formatting
cmd/sconfigcontroller/main.go Added klog configuration for consistent log formatting
cmd/rebooter/main.go Added klog configuration for consistent log formatting
Makefile Changed leader election flag from false to true for local runs

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Uburro Uburro merged commit 6695d9b into main Sep 24, 2025
5 of 7 checks passed
@Uburro Uburro deleted the uburro/add-logs-format-to-leaderelection branch September 24, 2025 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants