Skip to content

Commit

Permalink
Merge pull request #988 from k8up-io/fix/logger_not_set
Browse files Browse the repository at this point in the history
Properly set logger before creating a typed client
  • Loading branch information
Kidswiss authored Jul 26, 2024
2 parents 1426f00 + 9614a93 commit e904627
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/restic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func backupAnnotatedPods(ctx context.Context, resticCLI *resticCli.Restic, mainL
return nil
}

k8cli, err := kubernetes.NewTypedClient()
k8cli, err := kubernetes.NewTypedClient(mainLogger)
if err != nil {
return fmt.Errorf("Could not create kubernetes client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion restic/cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (r *Restic) sendSnapshotList() {
r.logger.Error(err, "webhook send failed")
}

err = kubernetes.SyncSnapshotList(r.ctx, r.snapshots, cfg.Config.Hostname, cfg.Config.ResticRepository)
err = kubernetes.SyncSnapshotList(r.ctx, r.snapshots, cfg.Config.Hostname, cfg.Config.ResticRepository, r.logger)
if err != nil {
r.logger.Error(err, "cannot sync snapshots to the cluster")
}
Expand Down
7 changes: 6 additions & 1 deletion restic/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/go-logr/logr"
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
"github.com/k8up-io/k8up/v2/restic/cfg"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -28,7 +30,10 @@ func getClientConfig() (*rest.Config, error) {
return config, nil
}

func NewTypedClient() (client.Client, error) {
func NewTypedClient(l logr.Logger) (client.Client, error) {

log.SetLogger(l)

scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(k8upv1.AddToScheme(scheme))
Expand Down
5 changes: 3 additions & 2 deletions restic/kubernetes/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"context"

"github.com/go-logr/logr"
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
"github.com/k8up-io/k8up/v2/restic/dto"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -11,12 +12,12 @@ import (

// SyncSnapshotList will take a k8upv1.SnapshotList and apply them to the k8s cluster.
// It will remove any snapshots on the cluster that are not present in the list.
func SyncSnapshotList(ctx context.Context, list []dto.Snapshot, namespace, repository string) error {
func SyncSnapshotList(ctx context.Context, list []dto.Snapshot, namespace, repository string, l logr.Logger) error {

newList := filterAndConvert(list, namespace, repository)
oldList := &k8upv1.SnapshotList{}

kube, err := NewTypedClient()
kube, err := NewTypedClient(l)
if err != nil {
return err
}
Expand Down

0 comments on commit e904627

Please sign in to comment.