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>
pull/31560/head
Matt Farina 1 month ago
parent 61d289c119
commit 936cd328ac
No known key found for this signature in database
GPG Key ID: 92C44A3D421FF7F9

@ -179,6 +179,16 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
logSetup(settings.Debug)
// newRootCmdWithConfig is only called from NewRootCmd. NewRootCmd sets up
// NewConfiguration without a custom logger. So, the slog default is used. logSetup
// can change the default logger to the one in the logger package. This happens for
// the Helm client. This means the actionConfig logger is different from the slog
// default logger. If they are different we sync the actionConfig logger to the slog
// current default one.
if actionConfig.Logger() != slog.Default() {
actionConfig.SetLogger(slog.Default().Handler())
}
// Validate color mode setting
switch settings.ColorMode {
case "never", "auto", "always":

@ -17,11 +17,14 @@ limitations under the License.
package cmd
import (
"bytes"
"log/slog"
"os"
"path/filepath"
"testing"
"helm.sh/helm/v4/internal/test/ensure"
"helm.sh/helm/v4/pkg/action"
"helm.sh/helm/v4/pkg/helmpath"
"helm.sh/helm/v4/pkg/helmpath/xdg"
)
@ -129,3 +132,20 @@ func TestUnknownSubCmd(t *testing.T) {
// func TestRootFileCompletion(t *testing.T) {
// checkFileCompletion(t, "", false)
// }
func TestRootCmdLogger(t *testing.T) {
args := []string{}
buf := new(bytes.Buffer)
actionConfig := action.NewConfiguration()
_, err := newRootCmdWithConfig(actionConfig, buf, args, SetupLogging)
if err != nil {
t.Errorf("expected no error, got: '%v'", err)
}
l1 := actionConfig.Logger()
l2 := slog.Default()
if l1.Handler() != l2.Handler() {
t.Error("expected actionConfig logger to be the slog default logger")
}
}

Loading…
Cancel
Save