From b2d9b3abe643efb0658ae8d22e1fddce710f6e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Jim=C3=A9nez=20S=C3=A1nchez?= Date: Thu, 27 Mar 2025 11:07:06 +0100 Subject: [PATCH] Remote provisioning: use client discovery for sync job (#102885) * Use client discovery for folders and dashboards synchronization * Remove unused dashboards client * Add folders import again --- .../apis/provisioning/jobs/sync/worker.go | 29 +++++-------------- .../apis/provisioning/resources/client.go | 3 ++ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/pkg/registry/apis/provisioning/jobs/sync/worker.go b/pkg/registry/apis/provisioning/jobs/sync/worker.go index 82565d752e6..ebeacbe2e29 100644 --- a/pkg/registry/apis/provisioning/jobs/sync/worker.go +++ b/pkg/registry/apis/provisioning/jobs/sync/worker.go @@ -8,11 +8,10 @@ import ( "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/dynamic" "github.com/grafana/grafana-app-sdk/logging" - dashboard "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1" folders "github.com/grafana/grafana/pkg/apis/folder/v0alpha1" provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" client "github.com/grafana/grafana/pkg/generated/clientset/versioned/typed/provisioning/v0alpha1" @@ -145,18 +144,12 @@ func (r *SyncWorker) createJob(ctx context.Context, repo repository.Reader, prog return nil, fmt.Errorf("unable to get folder client: %w", err) } - dashboardClient, err := parser.Clients().Dashboard() - if err != nil { - return nil, fmt.Errorf("unable to get dashboard client: %w", err) - } - job := &syncJob{ repository: repo, progress: progress, parser: parser, lister: r.lister, folders: resources.NewFolderManager(repo, folderClient), - dashboards: dashboardClient, resourcesLookup: map[resourceID]string{}, } @@ -191,7 +184,6 @@ type syncJob struct { parser *resources.Parser lister resources.ResourceLister folders *resources.FolderManager - dashboards dynamic.ResourceInterface folderLookup *resources.FolderTree resourcesLookup map[resourceID]string // the path with this k8s name } @@ -285,7 +277,12 @@ func (r *syncJob) applyChanges(ctx context.Context, changes []ResourceFileChange continue } - client, err := r.client(change.Existing.Resource) + versionlessGVR := schema.GroupVersionResource{ + Group: change.Existing.Group, + Resource: change.Existing.Resource, + } + + client, _, err := r.parser.Clients().ForResource(versionlessGVR) if err != nil { result.Error = fmt.Errorf("unable to get client for deleted object: %w", err) r.progress.Record(ctx, result) @@ -435,7 +432,7 @@ func (r *syncJob) deleteObject(ctx context.Context, path string, ref string) job result.Resource = gvk.Kind result.Group = gvk.Group - client, err := r.client(gvk.Kind) + client, _, err := r.parser.Clients().ForKind(*gvk) if err != nil { result.Error = fmt.Errorf("unable to get client for deleted object: %w", err) return result @@ -502,13 +499,3 @@ func (r *syncJob) writeResourceFromFile(ctx context.Context, path string, ref st result.Error = err return result } - -func (r *syncJob) client(kind string) (dynamic.ResourceInterface, error) { - switch kind { - case dashboard.GROUP, dashboard.DASHBOARD_RESOURCE, "Dashboard": - return r.dashboards, nil - case folders.GROUP, folders.RESOURCE, "Folder": - return r.folders.Client(), nil - } - return nil, fmt.Errorf("unsupported resource: %s", kind) -} diff --git a/pkg/registry/apis/provisioning/resources/client.go b/pkg/registry/apis/provisioning/resources/client.go index 860f7ce9154..8222146cdfa 100644 --- a/pkg/registry/apis/provisioning/resources/client.go +++ b/pkg/registry/apis/provisioning/resources/client.go @@ -93,6 +93,9 @@ func (c *ResourceClients) ForKind(gvk schema.GroupVersionKind) (dynamic.Resource return info.client, info.gvr, nil } +// ForResource returns a client for a resource. +// If the resource has a version, it will be used. +// If the resource does not have a version, the preferred version will be used. func (c *ResourceClients) ForResource(gvr schema.GroupVersionResource) (dynamic.ResourceInterface, schema.GroupVersionKind, error) { c.mutex.Lock() defer c.mutex.Unlock()