Skip to content

Commit

Permalink
Convert lib/versioncontrol to use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Dec 16, 2024
1 parent 0644b99 commit 2d26118
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions lib/versioncontrol/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@ package github

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

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

"github.com/gravitational/teleport"
logutils "github.com/gravitational/teleport/lib/utils/log"
vc "github.com/gravitational/teleport/lib/versioncontrol"
)

// NOTE: when making modifications to package, make sure to run tests with
// `TEST_GITHUB_API=yes`. this will enable some additional tests that are not
// run as part of normal CI.

var log = logrus.WithFields(logrus.Fields{
teleport.ComponentKey: teleport.ComponentVersionControl,
})
var logger = logutils.NewPackageLogger(teleport.ComponentKey, teleport.ComponentVersionControl)

// Visit uses the supplied visitor to aggregate release info from the github releases api.
func Visit(visitor *vc.Visitor) error {
Expand Down Expand Up @@ -195,7 +194,7 @@ func (i *Iterator) Next() bool {
i.page = make([]vc.Target, 0, len(page))
for _, r := range page {
if !semver.IsValid(r.TagName) {
log.Debugf("Skipping non-semver release tag: %q\n", r.TagName)
logger.DebugContext(context.Background(), "Skipping non-semver release tag", "tag_name", r.TagName)
continue
}
labels := parseReleaseNoteLabels(r.Body)
Expand Down Expand Up @@ -246,21 +245,21 @@ func parseReleaseNoteLabels(notes string) map[string]string {
l = strings.TrimPrefix(l, labelPrefix)
for _, kv := range strings.Split(l, ",") {
if !strings.Contains(kv, "=") {
log.Debugf("Skipping invalid release label keypair: %q", kv)
logger.DebugContext(context.Background(), "Skipping invalid release label keypair", "label", kv)
continue
}

parts := strings.SplitN(kv, "=", 2)
if len(parts) != 2 {
log.Debugf("Skipping invalid release label keypair: %q", kv)
logger.DebugContext(context.Background(), "Skipping invalid release label keypair", "label", kv)
continue
}

key := strings.TrimSpace(parts[0])
val := strings.TrimSpace(parts[1])

if !vc.IsValidTargetKey(key) || !vc.IsValidTargetVal(val) {
log.Debugf("Skipping invalid release label keypair: %q", kv)
logger.DebugContext(context.Background(), "Skipping invalid release label keypair", "label", kv)
// NOTE: we are skipping invalid keypairs for github release scraping
// because github releases are using a generally simplistic release representation.
// The TUF implementation will not skip invalid keypairs, preferring to
Expand Down
12 changes: 6 additions & 6 deletions lib/versioncontrol/upgradewindow/upgradewindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package upgradewindow

import (
"context"
"log/slog"
"os"
"path/filepath"
"time"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client/proto"
Expand Down Expand Up @@ -232,7 +232,7 @@ Outer:
// if we lose connectivity with auth for too long, forcibly reset any existing schedule.
// this frees up the upgrader to attempt an upgrade at its discretion.
if err := e.cfg.Driver.Reset(ctx); err != nil {
log.Warnf("Failed to perform %q maintenance window reset: %v", e.cfg.Driver.Kind(), err)
slog.WarnContext(ctx, "Failed to perform maintenance window reset", "upgrader_kind", e.cfg.Driver.Kind(), "error", err)
}
e.event(resetFromRun)
case <-ctx.Done():
Expand All @@ -252,7 +252,7 @@ func (e *Exporter[C]) exportWithRetry(ctx context.Context) {
// failure state appears persistent. reset and yield back
// to outer loop to wait for our next scheduled attempt.
if err := e.cfg.Driver.Reset(ctx); err != nil {
log.Warnf("Failed to perform %q maintenance window reset: %v", e.cfg.Driver.Kind(), err)
slog.WarnContext(ctx, "Failed to perform maintenance window reset", "upgrader_kind", e.cfg.Driver.Kind(), "error", err)
}
e.event(resetFromExport)
e.event(exportFailure)
Expand All @@ -278,21 +278,21 @@ func (e *Exporter[C]) exportWithRetry(ctx context.Context) {
})

if err != nil {
log.Warnf("Failed to import %q maintenance window from auth: %v", e.cfg.Driver.Kind(), err)
slog.WarnContext(ctx, "Failed to import maintenance window from auth", "upgrader_kind", e.cfg.Driver.Kind(), "error", err)
e.retry.Inc()
e.event(getExportErr)
continue
}

// sync exported windows out to our upgrader
if err := e.cfg.Driver.Sync(ctx, rsp); err != nil {
log.Warnf("Failed to sync %q maintenance window: %v", e.cfg.Driver.Kind(), err)
slog.WarnContext(ctx, "Failed to sync %q maintenance window", "upgrader_kind", e.cfg.Driver.Kind(), "error", err)
e.retry.Inc()
e.event(syncExportErr)
continue
}

log.Infof("Successfully synced %q upgrader maintenance window value.", e.cfg.Driver.Kind())
slog.InfoContext(ctx, "Successfully synced upgrader maintenance window value", "upgrader_kind", e.cfg.Driver.Kind())
e.event(exportSuccess)
return
}
Expand Down

0 comments on commit 2d26118

Please sign in to comment.