Skip to content

Commit

Permalink
fix render dashboard
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Nov 18, 2024
1 parent 396cf54 commit bbd08db
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 48 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 2 additions & 7 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 20 additions & 6 deletions pkg/graph/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -52,15 +51,15 @@ 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
}
return rd, dg.Response.Dashboards[0].URL, nil
}
}

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)
}
Expand Down Expand Up @@ -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
}
30 changes: 14 additions & 16 deletions pkg/graph/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/graph/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/registry/meta/render/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions pkg/registry/meta/renderdashboard/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -39,7 +38,6 @@ import (

type Storage struct {
kc client.Client
oc openvizcs.Interface
}

var (
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions vendor/kmodules.xyz/client-go/client/delegated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbd08db

Please sign in to comment.