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

Properly set logger before creating a typed client #988

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading