Skip to content

Commit

Permalink
Convert lib/automaticupgrades to use slog (#50312)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy authored Dec 17, 2024
1 parent 86b8ce5 commit 2071797
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
8 changes: 4 additions & 4 deletions lib/automaticupgrades/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package automaticupgrades

import (
"context"
"log/slog"
"net/url"
"strconv"
"strings"
"sync"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/mod/semver"

"github.com/gravitational/teleport"
Expand Down Expand Up @@ -57,12 +57,12 @@ func (c Channels) CheckAndSetDefaults() error {
// Else if cloud/stable channel is specified in the config, we use it as default.
// Else, we build a default channel based on the teleport binary version.
if _, ok := c[DefaultChannelName]; ok {
log.Debugln("'default' automatic update channel manually specified, honoring it.")
slog.DebugContext(context.Background(), "'default' automatic update channel manually specified, honoring it")
} else if cloudDefaultChannel, ok := c[DefaultCloudChannelName]; ok {
log.Debugln("'default' automatic update channel not specified, but 'stable/cloud' is, using the cloud default channel by default.")
slog.DebugContext(context.Background(), "'default' automatic update channel not specified, but 'stable/cloud' is, using the cloud default channel by default")
c[DefaultChannelName] = cloudDefaultChannel
} else {
log.Debugln("'default' automatic update channel not specified, teleport will serve its version by default.")
slog.DebugContext(context.Background(), "'default' automatic update channel not specified, teleport will serve its version by default")
c[DefaultChannelName] = defaultChannel
}

Expand Down
9 changes: 4 additions & 5 deletions lib/automaticupgrades/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ package automaticupgrades
import (
"bytes"
"context"
"log/slog"
"os"
"os/exec"
"strconv"

log "github.com/sirupsen/logrus"

"github.com/gravitational/teleport/lib/automaticupgrades/version"
)

Expand Down Expand Up @@ -60,7 +59,7 @@ func IsEnabled() bool {

automaticUpgrades, err := strconv.ParseBool(autoUpgradesEnv)
if err != nil {
log.Warnf("unexpected value for ENV:%s: %v", automaticUpgradesEnvar, err)
slog.WarnContext(context.Background(), "unexpected value for TELEPORT_AUTOMATIC_UPGRADES environment variable", "error", err)
return false
}

Expand All @@ -79,12 +78,12 @@ func GetUpgraderVersion(ctx context.Context) string {
if os.Getenv(EnvUpgrader) == "unit" {
out, err := exec.CommandContext(ctx, teleportUpgradeScript, "version").Output()
if err != nil {
log.WithError(err).Debug("Failed to exec /usr/sbin/teleport-upgrade version command.")
slog.DebugContext(ctx, "Failed to exec /usr/sbin/teleport-upgrade version command", "error", err)
return ""
}
ver, err := version.EnsureSemver(string(bytes.TrimSpace(out)))
if err != nil {
log.WithError(err).Debug("Unexpected teleport-upgrade version.")
slog.DebugContext(ctx, "Unexpected teleport-upgrade version", "error", err)
return ""
}
return ver
Expand Down
11 changes: 1 addition & 10 deletions lib/automaticupgrades/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package automaticupgrades
import (
"testing"

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -47,15 +45,8 @@ func TestIsEnabled(t *testing.T) {
require.False(t, IsEnabled())
})

t.Run("invalid value returns false and logs a warning message", func(t *testing.T) {
hook := test.NewGlobal()
defer hook.Reset()

t.Run("invalid value returns false", func(t *testing.T) {
t.Setenv(automaticUpgradesEnvar, "foo")
require.False(t, IsEnabled())

require.Len(t, hook.Entries, 1)
require.Equal(t, logrus.WarnLevel, hook.LastEntry().Level)
require.Equal(t, `unexpected value for ENV:TELEPORT_AUTOMATIC_UPGRADES: strconv.ParseBool: parsing "foo": invalid syntax`, hook.LastEntry().Message)
})
}

0 comments on commit 2071797

Please sign in to comment.