Skip to content

Commit 936cd32

Browse files
committed
Fix kube client logging
The kube client logging is based on the actionConfig logging. This is setup to use slog.Default() before the logging flags are parsed and logging is setup. newRootCmdWithConfig changes the logging but it wasn't picked up for actionConfig or the kube client. This change updates the logging to include any changes. Signed-off-by: Matt Farina <matt.farina@suse.com>
1 parent 61d289c commit 936cd32

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pkg/cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
179179

180180
logSetup(settings.Debug)
181181

182+
// newRootCmdWithConfig is only called from NewRootCmd. NewRootCmd sets up
183+
// NewConfiguration without a custom logger. So, the slog default is used. logSetup
184+
// can change the default logger to the one in the logger package. This happens for
185+
// the Helm client. This means the actionConfig logger is different from the slog
186+
// default logger. If they are different we sync the actionConfig logger to the slog
187+
// current default one.
188+
if actionConfig.Logger() != slog.Default() {
189+
actionConfig.SetLogger(slog.Default().Handler())
190+
}
191+
182192
// Validate color mode setting
183193
switch settings.ColorMode {
184194
case "never", "auto", "always":

pkg/cmd/root_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"bytes"
21+
"log/slog"
2022
"os"
2123
"path/filepath"
2224
"testing"
2325

2426
"helm.sh/helm/v4/internal/test/ensure"
27+
"helm.sh/helm/v4/pkg/action"
2528
"helm.sh/helm/v4/pkg/helmpath"
2629
"helm.sh/helm/v4/pkg/helmpath/xdg"
2730
)
@@ -129,3 +132,20 @@ func TestUnknownSubCmd(t *testing.T) {
129132
// func TestRootFileCompletion(t *testing.T) {
130133
// checkFileCompletion(t, "", false)
131134
// }
135+
136+
func TestRootCmdLogger(t *testing.T) {
137+
args := []string{}
138+
buf := new(bytes.Buffer)
139+
actionConfig := action.NewConfiguration()
140+
_, err := newRootCmdWithConfig(actionConfig, buf, args, SetupLogging)
141+
if err != nil {
142+
t.Errorf("expected no error, got: '%v'", err)
143+
}
144+
145+
l1 := actionConfig.Logger()
146+
l2 := slog.Default()
147+
148+
if l1.Handler() != l2.Handler() {
149+
t.Error("expected actionConfig logger to be the slog default logger")
150+
}
151+
}

0 commit comments

Comments
 (0)