Skip to content

Commit

Permalink
Migrate to slog (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Feb 6, 2024
1 parent 1e01830 commit 751e006
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 661 deletions.
18 changes: 9 additions & 9 deletions cmd/internal/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backup
import (
"context"
"fmt"
"log/slog"
"os"
"path"

Expand All @@ -12,12 +13,11 @@ import (
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/metrics"
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
cron "github.com/robfig/cron/v3"
"go.uber.org/zap"
"golang.org/x/sync/semaphore"
)

type BackuperConfig struct {
Log *zap.SugaredLogger
Log *slog.Logger
BackupSchedule string
DatabaseProber database.DatabaseProber
BackupProvider backuproviders.BackupProvider
Expand All @@ -26,7 +26,7 @@ type BackuperConfig struct {
}

type Backuper struct {
log *zap.SugaredLogger
log *slog.Logger
backupSchedule string
db database.DatabaseProber
bp backuproviders.BackupProvider
Expand Down Expand Up @@ -57,19 +57,19 @@ func (b *Backuper) Start(ctx context.Context) error {
id, err := c.AddFunc(b.backupSchedule, func() {
err := b.CreateBackup(ctx)
if err != nil {
b.log.Errorw("error creating backup", "error", err)
b.log.Error("error creating backup", "error", err)
}

for _, e := range c.Entries() {
b.log.Infow("scheduling next backup", "at", e.Next.String())
b.log.Info("scheduling next backup", "at", e.Next.String())
}
})
if err != nil {
return err
}

c.Start()
b.log.Infow("scheduling next backup", "at", c.Entry(id).Next.String())
b.log.Info("scheduling next backup", "at", c.Entry(id).Next.String())
<-ctx.Done()
c.Stop()
return nil
Expand All @@ -87,7 +87,7 @@ func (b *Backuper) CreateBackup(ctx context.Context) error {
return fmt.Errorf("database backup failed: %w", err)
}

b.log.Infow("successfully backed up database")
b.log.Info("successfully backed up database")

backupArchiveName := b.bp.GetNextBackupName(ctx)

Expand Down Expand Up @@ -118,9 +118,9 @@ func (b *Backuper) CreateBackup(ctx context.Context) error {
err = b.bp.CleanupBackups(ctx)
if err != nil {
b.metrics.CountError("cleanup")
b.log.Errorw("cleaning up backups failed", "error", err)
b.log.Error("cleaning up backups failed", "error", err)
} else {
b.log.Infow("cleaned up backups")
b.log.Info("cleaned up backups")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions cmd/internal/backup/providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"path"
"path/filepath"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"github.com/spf13/afero"

"go.uber.org/zap"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
Expand All @@ -31,7 +31,7 @@ const (
// BackupProviderGCP implements the backup provider interface for GCP
type BackupProviderGCP struct {
fs afero.Fs
log *zap.SugaredLogger
log *slog.Logger
c *storage.Client
config *BackupProviderConfigGCP
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *BackupProviderConfigGCP) validate() error {
}

// New returns a GCP backup provider
func New(ctx context.Context, log *zap.SugaredLogger, config *BackupProviderConfigGCP) (*BackupProviderGCP, error) {
func New(ctx context.Context, log *slog.Logger, config *BackupProviderConfigGCP) (*BackupProviderGCP, error) {
if config == nil {
return nil, errors.New("gcp backup provider requires a provider config")
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (b *BackupProviderGCP) DownloadBackup(ctx context.Context, version *provide
}
backupFilePath := path.Join(constants.DownloadDir, downloadFileName)

b.log.Infow("downloading", "object", version.Name, "gen", gen, "to", backupFilePath)
b.log.Info("downloading", "object", version.Name, "gen", gen, "to", backupFilePath)

r, err := bucket.Object(version.Name).Generation(gen).NewReader(ctx)
if err != nil {
Expand Down Expand Up @@ -199,7 +199,7 @@ func (b *BackupProviderGCP) UploadBackup(ctx context.Context, sourcePath string)
destination = b.config.ObjectPrefix + "/" + destination
}

b.log.Debugw("uploading object", "src", sourcePath, "dest", destination)
b.log.Debug("uploading object", "src", sourcePath, "dest", destination)

obj := bucket.Object(destination)
w := obj.NewWriter(ctx)
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/backup/providers/gcp/gcp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
iofs "io/fs"
"log/slog"
"net/http"
"path"
"strings"
Expand All @@ -21,14 +22,13 @@ import (
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.uber.org/zap/zaptest"
"google.golang.org/api/option"
)

func Test_BackupProviderGCP(t *testing.T) {
var (
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Minute)
log = zaptest.NewLogger(t).Sugar()
log = slog.Default()
)

defer cancel()
Expand Down
17 changes: 8 additions & 9 deletions cmd/internal/backup/providers/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package local
import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strconv"
Expand All @@ -13,8 +14,6 @@ import (
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils"
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"github.com/spf13/afero"

"go.uber.org/zap"
)

const (
Expand All @@ -24,7 +23,7 @@ const (
// BackupProviderLocal implements the backup provider interface for no backup provider (useful to disable sidecar functionality in development environments)
type BackupProviderLocal struct {
fs afero.Fs
log *zap.SugaredLogger
log *slog.Logger
config *BackupProviderConfigLocal
nextBackupCount int64
}
Expand All @@ -41,7 +40,7 @@ func (c *BackupProviderConfigLocal) validate() error {
}

// New returns a Local backup provider
func New(log *zap.SugaredLogger, config *BackupProviderConfigLocal) (*BackupProviderLocal, error) {
func New(log *slog.Logger, config *BackupProviderConfigLocal) (*BackupProviderLocal, error) {
if config == nil {
return nil, errors.New("local backup provider requires a provider config")
}
Expand Down Expand Up @@ -70,7 +69,7 @@ func New(log *zap.SugaredLogger, config *BackupProviderConfigLocal) (*BackupProv

// EnsureBackupBucket ensures a backup bucket at the backup provider
func (b *BackupProviderLocal) EnsureBackupBucket(_ context.Context) error {
b.log.Infow("ensuring backup bucket called for provider local")
b.log.Info("ensuring backup bucket called for provider local")

if err := b.fs.MkdirAll(b.config.LocalBackupPath, 0777); err != nil {
return fmt.Errorf("could not create local backup directory: %w", err)
Expand All @@ -81,14 +80,14 @@ func (b *BackupProviderLocal) EnsureBackupBucket(_ context.Context) error {

// CleanupBackups cleans up backups according to the given backup cleanup policy at the backup provider
func (b *BackupProviderLocal) CleanupBackups(_ context.Context) error {
b.log.Infow("cleanup backups called for provider local")
b.log.Info("cleanup backups called for provider local")

return nil
}

// DownloadBackup downloads the given backup version to the restoration folder
func (b *BackupProviderLocal) DownloadBackup(_ context.Context, version *providers.BackupVersion) error {
b.log.Infow("download backup called for provider local")
b.log.Info("download backup called for provider local")

source := filepath.Join(b.config.LocalBackupPath, version.Name)
destination := filepath.Join(constants.DownloadDir, version.Name)
Expand All @@ -103,7 +102,7 @@ func (b *BackupProviderLocal) DownloadBackup(_ context.Context, version *provide

// UploadBackup uploads a backup to the backup provider
func (b *BackupProviderLocal) UploadBackup(_ context.Context, sourcePath string) error {
b.log.Infow("upload backups called for provider local")
b.log.Info("upload backups called for provider local")

destination := filepath.Join(b.config.LocalBackupPath, filepath.Base(sourcePath))

Expand All @@ -125,7 +124,7 @@ func (b *BackupProviderLocal) GetNextBackupName(_ context.Context) string {

// ListBackups lists the available backups of the backup provider
func (b *BackupProviderLocal) ListBackups(_ context.Context) (providers.BackupVersions, error) {
b.log.Infow("listing backups called for provider local")
b.log.Info("listing backups called for provider local")

d, err := b.fs.Open(b.config.LocalBackupPath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/backup/providers/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
iofs "io/fs"
"log/slog"
"path"
"strings"
"testing"
Expand All @@ -12,14 +13,13 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func Test_BackupProviderLocal(t *testing.T) {
var (
ctx = context.Background()
localProviderBackupPath = defaultLocalBackupPath
log = zaptest.NewLogger(t).Sugar()
log = slog.Default()
)

for _, backupAmount := range []int{0, 1, 5, constants.DefaultObjectsToKeep + 5} {
Expand Down
9 changes: 4 additions & 5 deletions cmd/internal/backup/providers/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package s3

import (
"context"
"log/slog"
"path"
"path/filepath"
"strings"
Expand All @@ -12,8 +13,6 @@ import (
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"github.com/spf13/afero"

"go.uber.org/zap"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand All @@ -29,7 +28,7 @@ const (
// BackupProviderS3 implements the backup provider interface for S3
type BackupProviderS3 struct {
fs afero.Fs
log *zap.SugaredLogger
log *slog.Logger
c *s3.S3
sess *session.Session
config *BackupProviderConfigS3
Expand Down Expand Up @@ -66,7 +65,7 @@ func (c *BackupProviderConfigS3) validate() error {
}

// New returns a S3 backup provider
func New(log *zap.SugaredLogger, config *BackupProviderConfigS3) (*BackupProviderS3, error) {
func New(log *slog.Logger, config *BackupProviderConfigS3) (*BackupProviderS3, error) {
if config == nil {
return nil, errors.New("s3 backup provider requires a provider config")
}
Expand Down Expand Up @@ -242,7 +241,7 @@ func (b *BackupProviderS3) UploadBackup(ctx context.Context, sourcePath string)
destination = b.config.ObjectPrefix + "/" + destination
}

b.log.Debugw("uploading object", "src", sourcePath, "dest", destination)
b.log.Debug("uploading object", "src", sourcePath, "dest", destination)

uploader := s3manager.NewUploader(b.sess)
_, err = uploader.UploadWithContext(ctx, &s3manager.UploadInput{
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/backup/providers/s3/s3_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
iofs "io/fs"
"log/slog"
"path"
"strings"
"testing"
Expand All @@ -18,13 +19,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.uber.org/zap/zaptest"
)

func Test_BackupProviderS3(t *testing.T) {
var (
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Minute)
log = zaptest.NewLogger(t).Sugar()
log = slog.Default()
)

defer cancel()
Expand Down
16 changes: 8 additions & 8 deletions cmd/internal/database/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package etcd
import (
"context"
"fmt"
"log/slog"
"os"
"path"

"github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils"
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"go.uber.org/zap"
)

const (
Expand All @@ -21,7 +21,7 @@ type Etcd struct {
caCert string
cert string
endpoints string
log *zap.SugaredLogger
log *slog.Logger
key string
name string

Expand All @@ -30,7 +30,7 @@ type Etcd struct {
}

// New instantiates a new etcd database
func New(log *zap.SugaredLogger, datadir, caCert, cert, key, endpoints, name string) *Etcd {
func New(log *slog.Logger, datadir, caCert, cert, key, endpoints, name string) *Etcd {
return &Etcd{
log: log,
datadir: datadir,
Expand Down Expand Up @@ -74,7 +74,7 @@ func (db *Etcd) Backup(ctx context.Context) error {
return fmt.Errorf("error running backup command: %s", out)
}

db.log.Infow("took backup of etcd database", "output", out)
db.log.Info("took backup of etcd database", "output", out)

if _, err := os.Stat(snapshotFileName); os.IsNotExist(err) {
return fmt.Errorf("backup file was not created: %s", snapshotFileName)
Expand All @@ -85,7 +85,7 @@ func (db *Etcd) Backup(ctx context.Context) error {
return fmt.Errorf("backup was not created correct: %s", out)
}

db.log.Infow("successfully took backup of etcd database, snapshot status is", "status", out)
db.log.Info("successfully took backup of etcd database, snapshot status is", "status", out)

return nil
}
Expand All @@ -102,7 +102,7 @@ func (db *Etcd) Recover(ctx context.Context) error {
return fmt.Errorf("restored backup file was not created correct: %s", out)
}

db.log.Infow("successfully pulled backup of etcd database, snapshot status is", "status", out)
db.log.Info("successfully pulled backup of etcd database, snapshot status is", "status", out)

if err := utils.RemoveContents(db.datadir); err != nil {
return fmt.Errorf("could not clean database data directory %w", err)
Expand All @@ -117,13 +117,13 @@ func (db *Etcd) Recover(ctx context.Context) error {
return fmt.Errorf("unable to restore:%w", err)
}

db.log.Infow("restored etcd base backup", "output", out)
db.log.Info("restored etcd base backup", "output", out)

if err := os.RemoveAll(snapshotFileName); err != nil {
return fmt.Errorf("could not remove snapshot %w", err)
}

db.log.Infow("successfully restored etcd database")
db.log.Info("successfully restored etcd database")

return nil
}
Expand Down
Loading

0 comments on commit 751e006

Please sign in to comment.