Skip to content

Commit f9e12e1

Browse files
committed
Log API warnings at a higher verbosity
Since v0.19.0, controller-runtime has configured its REST clients to log API warnings at the INFO level. This changes the CRUNCHY_DEBUG environment variable to accept an integer that increases the verbosity of logs. API warnings are logged when verbosity is 2 or higher.
1 parent 46ae646 commit f9e12e1

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,15 @@ undeploy: ## Undeploy the PostgreSQL Operator
107107

108108
.PHONY: deploy-dev
109109
deploy-dev: ## Deploy the PostgreSQL Operator locally
110-
deploy-dev: PGO_FEATURE_GATES ?= "AllAlpha=true"
111110
deploy-dev: get-pgmonitor
112111
deploy-dev: createnamespaces
113112
kubectl apply --server-side -k ./config/dev
114113
hack/create-kubeconfig.sh postgres-operator pgo
115114
env \
116-
QUERIES_CONFIG_DIR="${QUERIES_CONFIG_DIR}" \
117-
CRUNCHY_DEBUG=true \
118-
PGO_FEATURE_GATES="${PGO_FEATURE_GATES}" \
119-
CHECK_FOR_UPGRADES='$(if $(CHECK_FOR_UPGRADES),$(CHECK_FOR_UPGRADES),false)' \
115+
QUERIES_CONFIG_DIR='$(QUERIES_CONFIG_DIR)' \
116+
CRUNCHY_DEBUG="$${CRUNCHY_DEBUG:-true}" \
117+
PGO_FEATURE_GATES="$${PGO_FEATURE_GATES:-AllAlpha=true}" \
118+
CHECK_FOR_UPGRADES="$${CHECK_FOR_UPGRADES:-false}" \
120119
KUBECONFIG=hack/.kube/postgres-operator/pgo \
121120
PGO_NAMESPACE='postgres-operator' \
122121
PGO_INSTALLER='deploy-dev' \

cmd/postgres-operator/main.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,41 @@ func need[V any](v V, err error) V {
4949
return v
5050
}
5151

52+
func initClient() (*rest.Config, error) {
53+
config, err := runtime.GetConfig()
54+
55+
if err == nil && userAgent == "" {
56+
err = errors.New("call initVersion first")
57+
}
58+
if err == nil {
59+
config.UserAgent = userAgent
60+
config.Wrap(otelTransportWrapper())
61+
62+
// Log Kubernetes API warnings encountered by client-go at a high verbosity.
63+
// See [rest.WarningLogger].
64+
handler := runtime.WarningHandler(func(ctx context.Context, code int, _ string, message string) {
65+
if code == 299 && len(message) != 0 {
66+
logging.FromContext(ctx).V(2).WithName("client-go").Info(message)
67+
}
68+
})
69+
config.WarningHandler = handler
70+
config.WarningHandlerWithContext = handler
71+
}
72+
73+
return config, err
74+
}
75+
5276
func initLogging() {
53-
// Configure a singleton that treats logging.Logger.V(1) as logrus.DebugLevel.
77+
debug := strings.TrimSpace(os.Getenv("CRUNCHY_DEBUG"))
78+
5479
var verbosity int
55-
if strings.EqualFold(os.Getenv("CRUNCHY_DEBUG"), "true") {
80+
if strings.EqualFold(debug, "true") {
5681
verbosity = 1
82+
} else if i, err := strconv.Atoi(debug); err == nil && i > 0 {
83+
verbosity = i
5784
}
85+
86+
// Configure a singleton that treats logging.Logger.V(1) as logrus.DebugLevel.
5887
logging.SetLogSink(logging.Logrus(os.Stdout, versionString, 1, verbosity))
5988

6089
global := logging.FromContext(context.Background())
@@ -190,19 +219,10 @@ func main() {
190219

191220
tracing.SetDefaultTracer(tracing.New("github.com/CrunchyData/postgres-operator"))
192221

193-
cfg := need(runtime.GetConfig())
194-
cfg.UserAgent = userAgent
195-
cfg.Wrap(otelTransportWrapper())
196-
197-
// TODO(controller-runtime): Set config.WarningHandler instead after v0.19.0.
198-
// Configure client-go to suppress warnings when warning headers are encountered. This prevents
199-
// warnings from being logged over and over again during reconciliation (e.g. this will suppress
200-
// deprecation warnings when using an older version of a resource for backwards compatibility).
201-
rest.SetDefaultWarningHandler(rest.NoWarnings{})
202-
203-
k8s := need(kubernetes.NewDiscoveryRunner(cfg))
222+
// Load Kubernetes client configuration and ensure it works.
223+
config := need(initClient())
224+
k8s := need(kubernetes.NewDiscoveryRunner(config))
204225
must(k8s.Read(running))
205-
206226
log.Info("connected to Kubernetes", "api", k8s.Version().String(), "openshift", k8s.IsOpenShift())
207227

208228
options := need(initManager(running))
@@ -216,7 +236,7 @@ func main() {
216236
return ctx
217237
}
218238

219-
manager := need(runtime.NewManager(cfg, options))
239+
manager := need(runtime.NewManager(config, options))
220240
must(manager.Add(k8s))
221241

222242
registrar := need(registration.NewRunner(os.Getenv("RSA_KEY"), os.Getenv("TOKEN_PATH"), stopRunning))

internal/controller/runtime/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime
77
import (
88
"context"
99

10+
"k8s.io/client-go/rest"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112
)
1213

@@ -74,3 +75,15 @@ func (fn ClientPatch) Patch(ctx context.Context, obj client.Object, patch client
7475
func (fn ClientUpdate) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
7576
return fn(ctx, obj, opts...)
7677
}
78+
79+
type WarningHandler func(ctx context.Context, code int, agent string, text string)
80+
81+
func (fn WarningHandler) HandleWarningHeader(code int, agent string, text string) {
82+
fn(context.Background(), code, agent, text)
83+
}
84+
func (fn WarningHandler) HandleWarningHeaderWithContext(ctx context.Context, code int, agent string, text string) {
85+
fn(ctx, code, agent, text)
86+
}
87+
88+
var _ rest.WarningHandler = WarningHandler(nil)
89+
var _ rest.WarningHandlerWithContext = WarningHandler(nil)

0 commit comments

Comments
 (0)