From b025bd07e5f2703b6919d27835b21dae31231596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Wed, 9 Dec 2020 22:42:27 +0100 Subject: [PATCH] Attempt to render deployments in xray apps --- go.mod | 1 + go.sum | 2 ++ internal/xray/application.go | 46 +++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 383ff638..7c254e97 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/derailed/tview v0.4.9 github.com/drone/envsubst v1.0.2 // indirect github.com/fatih/color v1.10.0 + github.com/fatih/structs v1.1.0 github.com/fsnotify/fsnotify v1.4.9 github.com/fvbommel/sortorder v1.0.2 github.com/gdamore/tcell v1.1.2 // indirect diff --git a/go.sum b/go.sum index 709079c1..ebea277f 100644 --- a/go.sum +++ b/go.sum @@ -244,6 +244,8 @@ github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwo github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= +github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= diff --git a/internal/xray/application.go b/internal/xray/application.go index 48f56b2a..335d73bd 100644 --- a/internal/xray/application.go +++ b/internal/xray/application.go @@ -7,6 +7,7 @@ import ( v1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" "github.com/derailed/k9s/internal/client" + "github.com/fatih/structs" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -36,11 +37,30 @@ func (a *Application) Render(ctx context.Context, ns string, o interface{}) erro root := NewTreeNode("argoproj.io/v1alpha1/applications", client.FQN(app.Namespace, app.Name)) ctx = context.WithValue(ctx, KeyParent, root) - var re ApplicationResource + var ar ApplicationResource + var dp Deployment for _, res := range app.Status.Resources { - if err := re.Render(ctx, app.Namespace, res); err != nil { - return err + gvr := gvkToGvr(res.GroupVersionKind()) + switch gvr.String() { + case "apps/v1/deployments": + if err := dp.Render(ctx, app.Namespace, toUnstructured(res)); err != nil { + return err + } + + default: + if err := ar.Render(ctx, app.Namespace, res); err != nil { + return err + } } + /* + if meta, ok := model.Registry[gvr.String()]; ok { + if meta.TreeRenderer != nil { + if err := meta.TreeRenderer.Render(ctx, app.Namespace, res); err != nil { + return err + } + } + } + */ } gvr, nsID := "v1/namespaces", client.FQN(client.ClusterScope, app.Namespace) @@ -61,3 +81,23 @@ func gvkToGvr(gvk schema.GroupVersionKind) client.GVR { } return client.NewGVR(gvr) } + +func toUnstructured(in interface{}) *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: structs.Map(in), + } + /* + var inInterface map[string]interface{} + inrec, _ := json.Marshal(in) + json.Unmarshal(inrec, &inInterface) + + // iterate through inrecs + for field, val := range inInterface { + fmt.Println("KV Pair: ", field, val) + } + + return &unstructured.Unstructured{ + Object: inInterface, + } + */ +}