From bbd08dbc37caf9ba4c307e02ec4eaae0645779e0 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Mon, 18 Nov 2024 02:42:12 -0800 Subject: [PATCH] fix render dashboard Signed-off-by: Tamal Saha --- go.mod | 2 +- go.sum | 4 +-- pkg/apiserver/apiserver.go | 9 ++---- pkg/graph/dashboard.go | 26 ++++++++++++---- pkg/graph/renderer.go | 30 +++++++++---------- pkg/graph/setup.go | 9 +++--- pkg/registry/meta/render/storage.go | 6 ++-- pkg/registry/meta/renderdashboard/storage.go | 13 ++++---- .../client-go/client/delegated.go | 5 ++-- vendor/modules.txt | 2 +- 10 files changed, 58 insertions(+), 48 deletions(-) diff --git a/go.mod b/go.mod index 26bddc0c0..7b92bc73c 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 kmodules.xyz/apiversion v0.2.0 kmodules.xyz/authorizer v0.29.1 - kmodules.xyz/client-go v0.30.38-0.20241118091954-9f659a58032e + kmodules.xyz/client-go v0.30.38-0.20241118102436-54d0f570c289 kmodules.xyz/custom-resources v0.30.0 kmodules.xyz/go-containerregistry v0.0.12 kmodules.xyz/monitoring-agent-api v0.30.2 diff --git a/go.sum b/go.sum index 475d26351..f4843dba2 100644 --- a/go.sum +++ b/go.sum @@ -927,8 +927,8 @@ kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk= kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80= kmodules.xyz/authorizer v0.29.1 h1:uByGGoryKbZcfiEAhjcK/Y345I9mygNQP7DVpkMbNQQ= kmodules.xyz/authorizer v0.29.1/go.mod h1:kZRhclL8twzyt2bQuJQJbpYww2sc+qFr8I5PPoq/sWY= -kmodules.xyz/client-go v0.30.38-0.20241118091954-9f659a58032e h1:M++YOemIa3px37C984O7eiDaJjEKNV5EP+QV9z6ic3w= -kmodules.xyz/client-go v0.30.38-0.20241118091954-9f659a58032e/go.mod h1:CAu+JlA8RVGtj6LQHu0Q1w2mnFUajuti49c7T1AvGdM= +kmodules.xyz/client-go v0.30.38-0.20241118102436-54d0f570c289 h1:96BXpu5pXeO1zx07Nk3jJRNQGUOT/qLTBjjn+rboKCM= +kmodules.xyz/client-go v0.30.38-0.20241118102436-54d0f570c289/go.mod h1:CAu+JlA8RVGtj6LQHu0Q1w2mnFUajuti49c7T1AvGdM= kmodules.xyz/crd-schema-fuzz v0.29.1 h1:zJTlWYOrT5dsVVHW8HGcnR/vaWfxQfNh11QwTtkYpcs= kmodules.xyz/crd-schema-fuzz v0.29.1/go.mod h1:n708z9YQqLMP2KNLQVgBcRJw1QpSWLvpNCEi+KJDOYE= kmodules.xyz/custom-resources v0.30.0 h1:vR3CbseHMLwR4GvtcJJuRuwIV8voKqFqNii27rMcm1o= diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 99acffcb0..287338e31 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -77,7 +77,6 @@ import ( "github.com/pkg/errors" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" openvizapi "go.openviz.dev/apimachinery/apis/openviz/v1alpha1" - openvizcs "go.openviz.dev/apimachinery/client/clientset/versioned" crdinstall "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -242,10 +241,6 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) { if err != nil { return nil, fmt.Errorf("unable to create discovery client, reason: %v", err) } - oc, err := openvizcs.NewForConfig(cfg) - if err != nil { - return nil, fmt.Errorf("unable to create openviz client, reason: %v", err) - } cid, err := clustermeta.ClusterUID(mgr.GetAPIReader()) if err != nil { @@ -331,9 +326,9 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) { v1alpha1storage := map[string]rest.Storage{} v1alpha1storage[rsapi.ResourceChartPresetQueries] = chartpresetquery.NewStorage(ctrlClient) v1alpha1storage[rsapi.ResourceClusterStatuses] = clusterstatusstorage.NewStorage(ctrlClient, kc) - v1alpha1storage[rsapi.ResourceRenderDashboards] = renderdashboard.NewStorage(ctrlClient, oc) + v1alpha1storage[rsapi.ResourceRenderDashboards] = renderdashboard.NewStorage(ctrlClient) v1alpha1storage[rsapi.ResourceRenderRawGraphs] = renderrawgraph.NewStorage(ctrlClient) - v1alpha1storage[rsapi.ResourceRenders] = render.NewStorage(ctrlClient, oc, rbacAuthorizer) + v1alpha1storage[rsapi.ResourceRenders] = render.NewStorage(ctrlClient, rbacAuthorizer) v1alpha1storage[rsapi.ResourceResourceBlockDefinitions] = resourceblockdefinition.NewStorage() v1alpha1storage[rsapi.ResourceResourceCalculators] = resourcecalculatorstorage.NewStorage(ctrlClient, cid, rbacAuthorizer) v1alpha1storage[rsapi.ResourceResourceDescriptors] = resourcedescriptor.NewStorage() diff --git a/pkg/graph/dashboard.go b/pkg/graph/dashboard.go index b8e75f484..e79d58c16 100644 --- a/pkg/graph/dashboard.go +++ b/pkg/graph/dashboard.go @@ -26,9 +26,8 @@ import ( "github.com/pkg/errors" openvizauipi "go.openviz.dev/apimachinery/apis/ui/v1alpha1" - openvizcs "go.openviz.dev/apimachinery/client/clientset/versioned" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" kmapi "kmodules.xyz/client-go/api/v1" sharedapi "kmodules.xyz/resource-metadata/apis/shared" uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1" @@ -37,7 +36,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -func renderDashboard(ctx context.Context, kc client.Client, oc openvizcs.Interface, srcObj *unstructured.Unstructured) tableconvertor.DashboardRendererFunc { +func renderDashboard(ctx context.Context, kc client.Client, srcObj *unstructured.Unstructured) tableconvertor.DashboardRendererFunc { return func(name string) (*uiapi.ResourceDashboard, string, error) { rd, err := resourcedashboards.LoadByName(kc, name) if err != nil { @@ -52,7 +51,7 @@ func renderDashboard(ctx context.Context, kc client.Client, oc openvizcs.Interfa if len(rd.Spec.Dashboards) > 1 { return nil, "", fmt.Errorf("multiple dashboards configured for %s", name) } - dg, err := RenderDashboard(ctx, kc, oc, rd, srcObj, false) + dg, err := RenderDashboard(ctx, kc, rd, srcObj, false) if err != nil { return nil, "", err } @@ -60,7 +59,7 @@ func renderDashboard(ctx context.Context, kc client.Client, oc openvizcs.Interfa } } -func RenderDashboard(ctx context.Context, kc client.Client, oc openvizcs.Interface, rd *uiapi.ResourceDashboard, src *unstructured.Unstructured, embeddedLink bool) (*openvizauipi.DashboardGroup, error) { +func RenderDashboard(ctx context.Context, kc client.Client, rd *uiapi.ResourceDashboard, src *unstructured.Unstructured, embeddedLink bool) (*openvizauipi.DashboardGroup, error) { if rd.Spec.Provider != uiapi.DashboardProviderGrafana { return nil, fmt.Errorf("dashboard %s uses unsupported provider %q", rd.Name, rd.Spec.Provider) } @@ -124,5 +123,20 @@ func RenderDashboard(ctx context.Context, kc client.Client, oc openvizcs.Interfa } dg.Request.Dashboards = append(dg.Request.Dashboards, out) } - return oc.UiV1alpha1().DashboardGroups().Create(ctx, dg, metav1.CreateOptions{}) + + content, err := runtime.DefaultUnstructuredConverter.ToUnstructured(dg) + if err != nil { + return nil, err + } + req := unstructured.Unstructured{Object: content} + req.SetGroupVersionKind(openvizauipi.SchemeGroupVersion.WithKind(openvizauipi.ResourceKindDashboardGroup)) + err = kc.Create(ctx, &req) + if err != nil { + return nil, err + } + err = runtime.DefaultUnstructuredConverter.FromUnstructured(req.UnstructuredContent(), dg) + if err != nil { + return nil, err + } + return dg, err } diff --git a/pkg/graph/renderer.go b/pkg/graph/renderer.go index 067164c1c..5caad4f86 100644 --- a/pkg/graph/renderer.go +++ b/pkg/graph/renderer.go @@ -27,7 +27,6 @@ import ( "kubeops.dev/ui-server/pkg/shared" "github.com/pkg/errors" - openvizcs "go.openviz.dev/apimachinery/client/clientset/versioned" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" @@ -45,7 +44,6 @@ import ( func RenderLayout( ctx context.Context, kc client.Client, - oc openvizcs.Interface, src kmapi.ObjectInfo, layoutName string, // optional pageName string, // optional @@ -119,14 +117,14 @@ func RenderLayout( } if layout.Spec.Header != nil && okToRender(layout.Spec.Header.Kind, renderBlocks) { - if bv, err := renderPageBlock(ctx, kc, oc, srcRID, &srcObj, layout.Spec.Header, convertToTable); err != nil { + if bv, err := renderPageBlock(ctx, kc, srcRID, &srcObj, layout.Spec.Header, convertToTable); err != nil { return nil, err } else { out.Header = bv } } if layout.Spec.TabBar != nil && okToRender(layout.Spec.TabBar.Kind, renderBlocks) { - if bv, err := renderPageBlock(ctx, kc, oc, srcRID, &srcObj, layout.Spec.TabBar, convertToTable); err != nil { + if bv, err := renderPageBlock(ctx, kc, srcRID, &srcObj, layout.Spec.TabBar, convertToTable); err != nil { return nil, err } else { out.TabBar = bv @@ -152,14 +150,14 @@ func RenderLayout( Blocks: nil, } if sectionLayout.Info != nil && okToRender(sectionLayout.Info.Kind, renderBlocks) { - if bv, err := renderPageBlock(ctx, kc, oc, srcRID, &srcObj, sectionLayout.Info, convertToTable); err != nil { + if bv, err := renderPageBlock(ctx, kc, srcRID, &srcObj, sectionLayout.Info, convertToTable); err != nil { return nil, err } else { section.Info = bv } } if sectionLayout.Insight != nil && okToRender(sectionLayout.Insight.Kind, renderBlocks) { - if bv, err := renderPageBlock(ctx, kc, oc, srcRID, &srcObj, sectionLayout.Insight, convertToTable); err != nil { + if bv, err := renderPageBlock(ctx, kc, srcRID, &srcObj, sectionLayout.Insight, convertToTable); err != nil { return nil, err } else { section.Insight = bv @@ -169,7 +167,7 @@ func RenderLayout( blocks := make([]rsapi.PageBlockView, 0, len(sectionLayout.Blocks)) for _, block := range sectionLayout.Blocks { if okToRender(block.Kind, renderBlocks) { - if bv, err := renderPageBlock(ctx, kc, oc, srcRID, &srcObj, &block, convertToTable); err != nil { + if bv, err := renderPageBlock(ctx, kc, srcRID, &srcObj, &block, convertToTable); err != nil { return nil, err } else { blocks = append(blocks, *bv) @@ -191,7 +189,7 @@ func okToRender(kind rsapi.TableKind, renderBlocks sets.Set[string]) bool { return renderBlocks.Len() == 0 || renderBlocks.Has(string(kind)) } -func RenderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interface, src kmapi.ObjectInfo, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { +func RenderPageBlock(ctx context.Context, kc client.Client, src kmapi.ObjectInfo, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { srcRID, err := kmapi.ExtractResourceID(kc.RESTMapper(), src.Resource) if err != nil { return nil, errors.Wrap(err, "failed to detect src resource id") @@ -203,11 +201,11 @@ func RenderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interfa return nil, err } - return renderPageBlock(ctx, kc, oc, srcRID, &srcObj, block, convertToTable) + return renderPageBlock(ctx, kc, srcRID, &srcObj, block, convertToTable) } -func renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interface, srcRID *kmapi.ResourceID, srcObj *unstructured.Unstructured, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { - bv, err := _renderPageBlock(ctx, kc, oc, srcRID, srcObj, block, convertToTable) +func renderPageBlock(ctx context.Context, kc client.Client, srcRID *kmapi.ResourceID, srcObj *unstructured.Unstructured, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { + bv, err := _renderPageBlock(ctx, kc, srcRID, srcObj, block, convertToTable) if err != nil { bv.Result = rsapi.RenderResult{ Status: rsapi.RenderError, @@ -221,12 +219,12 @@ func renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interfa return bv, nil } -func _renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interface, srcRID *kmapi.ResourceID, srcObj *unstructured.Unstructured, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { +func _renderPageBlock(ctx context.Context, kc client.Client, srcRID *kmapi.ResourceID, srcObj *unstructured.Unstructured, block *rsapi.PageBlockLayout, convertToTable bool) (*rsapi.PageBlockView, error) { var impersonate bool if block != nil && block.ResourceLocator != nil && block.Impersonate { impersonate = true } - cc, err := getClient(ctx, kc, impersonate) + cc, err := NewClient(ctx, kc, impersonate) if err != nil { return nil, err } @@ -241,7 +239,7 @@ func _renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interf if block.Kind == rsapi.TableKindSelf || block.Kind == rsapi.TableKindSubTable { out.Resource = srcRID if convertToTable { - converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, oc, srcObj), RenderExec(nil, &srcGVR)) + converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, srcObj), RenderExec(nil, &srcGVR)) if err != nil { return &out, err } @@ -314,7 +312,7 @@ func _renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interf } if convertToTable { - converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, oc, srcObj), RenderExec(&srcGVR, &mapping.Resource)) + converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, srcObj), RenderExec(&srcGVR, &mapping.Resource)) if err != nil { return &out, err } @@ -343,7 +341,7 @@ func _renderPageBlock(ctx context.Context, kc client.Client, oc openvizcs.Interf } if convertToTable { - converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, oc, srcObj), RenderExec(&srcGVR, &mapping.Resource)) + converter, err := tableconvertor.New(block.FieldPath, block.View.Columns, renderDashboard(ctx, cc, srcObj), RenderExec(&srcGVR, &mapping.Resource)) if err != nil { return &out, err } diff --git a/pkg/graph/setup.go b/pkg/graph/setup.go index b299aed0a..8ecbda269 100644 --- a/pkg/graph/setup.go +++ b/pkg/graph/setup.go @@ -250,7 +250,7 @@ func extractRefs(data map[string]interface{}, result ksets.ObjectReference) erro } func ExecRawQuery(ctx context.Context, kc client.Client, src kmapi.OID, target sharedapi.ResourceLocator) (*kmapi.ResourceID, []kmapi.ObjectReference, error) { - cc, err := getClient(ctx, kc, target.Impersonate) + cc, err := NewClient(ctx, kc, target.Impersonate) if err != nil { return nil, nil, err } @@ -286,7 +286,7 @@ func ExecRawQuery(ctx context.Context, kc client.Client, src kmapi.OID, target s } func ExecQuery(ctx context.Context, kc client.Client, src kmapi.OID, target sharedapi.ResourceLocator) (*kmapi.ResourceID, []unstructured.Unstructured, error) { - cc, err := getClient(ctx, kc, target.Impersonate) + cc, err := NewClient(ctx, kc, target.Impersonate) if err != nil { return nil, nil, err } @@ -332,7 +332,7 @@ func execRestQuery(ctx context.Context, kc client.Client, q string, gvk schema.G return &out, nil } -func getClient(ctx context.Context, kc client.Client, impersonate bool) (client.Client, error) { +func NewClient(ctx context.Context, kc client.Client, impersonate bool) (client.Client, error) { u, found := request.UserFrom(ctx) if !impersonate || !found || len(u.GetExtra()[kmapi.AceOrgIDKey]) != 1 { @@ -342,7 +342,8 @@ func getClient(ctx context.Context, kc client.Client, impersonate bool) (client. fmt.Printf("impersonating: %v\n", u.GetName()) if rw, ok := kc.(*cu.DelegatingClient); ok { - return rw.Impersonate(u) + _, cc, err := rw.Impersonate(u) + return cc, err } return nil, fmt.Errorf("can't impersonate client") } diff --git a/pkg/registry/meta/render/storage.go b/pkg/registry/meta/render/storage.go index bad2974f2..959cec55c 100644 --- a/pkg/registry/meta/render/storage.go +++ b/pkg/registry/meta/render/storage.go @@ -51,10 +51,9 @@ var ( _ rest.SingularNameProvider = &Storage{} ) -func NewStorage(kc client.Client, oc openvizcs.Interface, a authorizer.Authorizer) *Storage { +func NewStorage(kc client.Client, a authorizer.Authorizer) *Storage { return &Storage{ kc: kc, - oc: oc, a: a, } } @@ -107,7 +106,7 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat autoColumns = true } - bv, err := graph.RenderPageBlock(graph.NewUserContext(ctx), r.kc, r.oc, req.Source, req.Block, req.ConvertToTable) + bv, err := graph.RenderPageBlock(graph.NewUserContext(ctx), r.kc, req.Source, req.Block, req.ConvertToTable) if err != nil { return nil, err } @@ -123,7 +122,6 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat rv, err := graph.RenderLayout( graph.NewUserContext(ctx), r.kc, - r.oc, req.Source, req.LayoutName, // optional req.PageName, // optional diff --git a/pkg/registry/meta/renderdashboard/storage.go b/pkg/registry/meta/renderdashboard/storage.go index 239eff6da..62462d022 100644 --- a/pkg/registry/meta/renderdashboard/storage.go +++ b/pkg/registry/meta/renderdashboard/storage.go @@ -23,7 +23,6 @@ import ( "kubeops.dev/ui-server/pkg/graph" - openvizcs "go.openviz.dev/apimachinery/client/clientset/versioned" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -39,7 +38,6 @@ import ( type Storage struct { kc client.Client - oc openvizcs.Interface } var ( @@ -50,10 +48,9 @@ var ( _ rest.SingularNameProvider = &Storage{} ) -func NewStorage(kc client.Client, oc openvizcs.Interface) *Storage { +func NewStorage(kc client.Client) *Storage { return &Storage{ kc: kc, - oc: oc, } } @@ -132,7 +129,13 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat } } - dg, err := graph.RenderDashboard(graph.NewUserContext(ctx), r.kc, r.oc, rd, src, req.EmbeddedLink) + utx := graph.NewUserContext(ctx) + cc, err := graph.NewClient(utx, r.kc, true) + if err != nil { + return nil, err + } + + dg, err := graph.RenderDashboard(utx, cc, rd, src, req.EmbeddedLink) if err != nil { return nil, err } diff --git a/vendor/kmodules.xyz/client-go/client/delegated.go b/vendor/kmodules.xyz/client-go/client/delegated.go index 021e45d44..6a4c4eca7 100644 --- a/vendor/kmodules.xyz/client-go/client/delegated.go +++ b/vendor/kmodules.xyz/client-go/client/delegated.go @@ -100,7 +100,7 @@ func (d *DelegatingClient) RestConfig() *restclient.Config { return d.config } -func (d *DelegatingClient) Impersonate(u user.Info) (client.Client, error) { +func (d *DelegatingClient) Impersonate(u user.Info) (*restclient.Config, client.Client, error) { config := restclient.CopyConfig(d.config) config.Impersonate = restclient.ImpersonationConfig{ UserName: u.GetName(), @@ -121,7 +121,8 @@ func (d *DelegatingClient) Impersonate(u user.Info) (client.Client, error) { }, d.options.HTTPClient.Transport), } } - return NewClient(config, optionsShallowCopy) + cc, err := NewClient(config, optionsShallowCopy) + return config, cc, err } // GroupVersionKindFor returns the GroupVersionKind for the given object. diff --git a/vendor/modules.txt b/vendor/modules.txt index 78cc1de9c..44b78f91f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -2020,7 +2020,7 @@ kmodules.xyz/authorizer/apiserver kmodules.xyz/authorizer/rbac kmodules.xyz/authorizer/rbac/helpers kmodules.xyz/authorizer/rbac/validation -# kmodules.xyz/client-go v0.30.38-0.20241118091954-9f659a58032e +# kmodules.xyz/client-go v0.30.38-0.20241118102436-54d0f570c289 ## explicit; go 1.22.0 kmodules.xyz/client-go kmodules.xyz/client-go/api/v1