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

Migrate pkg to slog #30708

Merged
merged 32 commits into from
Apr 11, 2025
Merged

Migrate pkg to slog #30708

merged 32 commits into from
Apr 11, 2025

Conversation

benoittgt
Copy link
Contributor

@benoittgt benoittgt commented Mar 25, 2025

What this PR does / why we need it:

tl;dr : migrate packages and cmd from log to slog for logs

While trying to implement #30689 but for V4. It was mentionned that I should use slog. So I tried to make a first try in
#30685, but it requires a change accross packages and cmd.

This PR migrates packages and cmd to slog.

  • Migrate all log to slog key-value, with everything in Debug, except logs that start with warning:
  • Start all logs phrase with lowercase letter like recommend in Go
  • Try to make logs more easily grepable by adding more often the name and the namespace of the resource
  • Follow same construction in struct as before, so SDK's users can pass custom slogger with a custom handler to process logs

This code still need to be fully manually tested but first I would like to get some feedbacks.

cc @robertsirc @TerryHowe

Close: #30698

Special notes for your reviewer:

If applicable:

  • this PR contains user facing changes (the docs needed label should be applied if so)
  • this PR contains unit tests
  • this PR has been tested for backwards compatibility

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 25, 2025
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from dca38af to df264d2 Compare March 25, 2025 16:48
@benoittgt benoittgt marked this pull request as ready for review March 26, 2025 08:42
@benoittgt benoittgt changed the title Migrate kube pkg to slog [WIP] Migrate kube pkg to slog Mar 31, 2025
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 31, 2025
@benoittgt benoittgt changed the title [WIP] Migrate kube pkg to slog [WIP] Migrate pkg to slog Mar 31, 2025
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from a4e9552 to 359f585 Compare March 31, 2025 09:38
@benoittgt benoittgt changed the title [WIP] Migrate pkg to slog Migrate pkg to slog Mar 31, 2025
@benoittgt benoittgt mentioned this pull request Apr 7, 2025
3 tasks
benoittgt added 17 commits April 7, 2025 15:35
As for helm v4. We want to migrate logs to slog.

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
```
helmClient.Log = NewSlogAdapter(slog.Default())
```

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
See: helm#30698 (comment)
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from 0edab58 to b238072 Compare April 7, 2025 14:46
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
}

// Create a handler that removes timestamps
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I am more curious why add the logger rather than just use slog directly? I thought I mentioned that on another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought we wanted to chose extactly the output, like do not have the timestamp. Also to handle debug loglevel like it was done before it seems we need to have a custom slog Handler that handle the debug flag just before log is processed.

@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from 6e684c8 to bf3d011 Compare April 7, 2025 15:46
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from bf3d011 to 710770e Compare April 7, 2025 15:49
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from 87846ec to 0c85456 Compare April 7, 2025 16:26
@benoittgt benoittgt requested a review from robertsirc April 7, 2025 16:45
Copy link
Contributor

@robertsirc robertsirc left a comment

Choose a reason for hiding this comment

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

First let me thank you for all your hard work on this. Where we are calling logging looks good but how we implement the logging i think needs to change. Rather than passing around a logger Log as to just call slog directly. Would love to setup some time to disucss if you are up to it.

}

// Create a handler that removes timestamps
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Copy link
Contributor

Choose a reason for hiding this comment

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

I am more curious why add the logger rather than just use slog directly? I thought I mentioned that on another PR.

So we should use dynamic handler to set the log level after. With this
patch we can clearly see the output. Before we were always stuck in log
level "info" and not seeing debug log level

```
bin/helm upgrade --install --debug --wait frontend \
--namespace test \
--set replicaCount=2 \
--set backend=http://backend-podinfo:9898/echo \
podinfo/podinfo
level=DEBUG msg="getting history for release" release=frontend
level=DEBUG msg="preparing upgrade" name=frontend
level=DEBUG msg="performing update" name=frontend
level=DEBUG msg="creating upgraded release" name=frontend
level=DEBUG msg="checking resources for changes" resources=2
level=DEBUG msg="no changes detected" kind=Service name=frontend-podinfo
level=DEBUG msg="patching resource" kind=Deployment name=frontend-podinfo namespace=test
level=DEBUG msg="waiting for resources" count=2 timeout=5m0s
level=DEBUG msg="waiting for resource" name=frontend-podinfo kind=Deployment expectedStatus=Current actualStatus=Unknown
level=DEBUG msg="updating status for upgraded release" name=frontend
Release "frontend" has been upgraded. Happy Helming!
NAME: frontend
LAST DEPLOYED: Thu Apr 10 09:56:25 2025
NAMESPACE: test
STATUS: deployed
REVISION: 6
DESCRIPTION: Upgrade complete
```

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@benoittgt
Copy link
Contributor Author

benoittgt commented Apr 10, 2025

Would love to setup some time to disucss if you are up to it.

I will ping you on Slack. ;)

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@@ -148,6 +140,9 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
settings.AddFlags(flags)
addKlogFlags(flags)

logger := logging.NewLogger(func() bool { return settings.Debug })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to be dynamic with the settings because it is set later. Maybe we could simplify, if you have an idea maybe.

@benoittgt
Copy link
Contributor Author

benoittgt commented Apr 10, 2025

After a quick call with @robertsirc I removed all mention of Log and instead leveral slog directly + slog.SetDefault.

  1. SDK developer will still be able to set a custom default logger with a dedicated handler thank to SetDefault
  2. I choose to make a Handler for slog to do not loose the --debug behavior.
  3. We loose the call location with the logging with slog 7f02e89
    , but I think log message are pretty uniq and it's easy to find the location of the call with basic text search on Github.
  4. Error trace are one-lined but I think it's ok.
    benoit.tigeot » bin/helm uninstall --namespace test podinfo 
    
    Error: uninstall: Release not loaded: podinfo: release: not found
    
    benoit.tigeot » bin/helm uninstall --namespace test podinfo --debug
    
    level=DEBUG msg="getting release history" name=podinfo
    Error: uninstall: Release not loaded: podinfo: release: not found
    level=DEBUG msg=error error="release: not found\nhelm.sh/helm/v4/pkg/storage/driver.init\n\t<autogenerated>:1\nruntime.doInit1\n\truntime/proc.go:7291\nruntime.doInit\n\truntime/proc.go:7258\nruntime.main\n\truntime/proc.go:254\nruntime.goexit\n\truntime/asm_arm64.s:1223\nuninstall: Release not loaded: podinfo\nhelm.sh/helm/v4/pkg/action.(*Uninstall).Run\n\thelm.sh/helm/v4/pkg/action/uninstall.go:87\nhelm.sh/helm/v4/pkg/cmd.newUninstallCmd.func2\n\thelm.sh/helm/v4/pkg/cmd/uninstall.go:60\ngithub.com/spf13/cobra.(*Command).execute\n\tgithub.com/spf13/cobra@v1.9.1/command.go:1015\ngithub.com/spf13/cobra.(*Command).ExecuteC\n\tgithub.com/spf13/cobra@v1.9.1/command.go:1148\ngithub.com/spf13/cobra.(*Command).Execute\n\tgithub.com/spf13/cobra@v1.9.1/command.go:1071\nmain.main\n\thelm.sh/helm/v4/cmd/helm/helm.go:48\nruntime.main\n\truntime/proc.go:272\nruntime.goexit\n\truntime/asm_arm64.s:1223"
    

Current output example:

» bin/helm upgrade --install --debug --wait frontend --namespace test --set replicaCount=2 --set backend=http://backend-podinfo:9898/echo podinfo/podinfo
level=DEBUG msg="getting history for release" release=frontend
level=DEBUG msg="getting release history" name=frontend
level=DEBUG msg="preparing upgrade" name=frontend
level=DEBUG msg="getting last revision" name=frontend
....

Sorry again for the size of the PR.

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@benoittgt benoittgt force-pushed the migrate-kube-pkg-to-slog branch from 7bcf82a to c05bcbd Compare April 10, 2025 14:40
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@robertsirc robertsirc self-requested a review April 11, 2025 19:35
Copy link
Contributor

@robertsirc robertsirc left a comment

Choose a reason for hiding this comment

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

LGTM, there are some issues that we are going to want to follow up with another PR to fix but since this is part of Helm4, we have some liberties.

@@ -237,24 +238,24 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma
// Check reachability of cluster unless in client-only mode (e.g. `helm template` without `--validate`)
if !i.ClientOnly {
if err := i.cfg.KubeClient.IsReachable(); err != nil {
i.cfg.Log(fmt.Sprintf("ERROR: Cluster reachability check failed: %v", err))
slog.Error(fmt.Sprintf("cluster reachability check failed: %v", err))
return nil, errors.Wrap(err, "cluster reachability check failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

So, we log this as an error here but then return the error below it, it will be logged again. It could make sense to change this but let me bring this back others before you change anything.

Comment on lines -722 to -724
if err != nil {
return errors.Errorf("Failed to close reader for pod: %s, container: %s", podName, containerName)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note, this was unreachable code so it's safe to remove.

@mattfarina mattfarina merged commit 7a1eb77 into helm:main Apr 11, 2025
5 checks passed
@benoittgt benoittgt deleted the migrate-kube-pkg-to-slog branch April 12, 2025 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants