From c7e6dd851e0215ab2e7e09413fe5d1be7774b4c5 Mon Sep 17 00:00:00 2001 From: jayson wang Date: Thu, 15 Jun 2023 22:53:22 +0800 Subject: [PATCH 1/3] storageclasses view keeps the same output as kubectl get sc --- internal/render/helpers.go | 10 +++++++++- internal/render/sc.go | 28 ++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/internal/render/helpers.go b/internal/render/helpers.go index 1988633b09..84e93da708 100644 --- a/internal/render/helpers.go +++ b/internal/render/helpers.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/derailed/k9s/internal/client" "github.com/derailed/tview" runewidth "github.com/mattn/go-runewidth" "github.com/rs/zerolog/log" @@ -14,6 +13,8 @@ import ( "golang.org/x/text/message" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" + + "github.com/derailed/k9s/internal/client" ) func runesToNum(rr []rune) int64 { @@ -298,6 +299,13 @@ func boolPtrToStr(b *bool) string { return boolToStr(*b) } +func strPtrToStr(s *string) string { + if s == nil { + return "" + } + return *s +} + // Check if string is in a string list. func in(ll []string, s string) bool { for _, l := range ll { diff --git a/internal/render/sc.go b/internal/render/sc.go index dc6de66c7c..7067cccae3 100644 --- a/internal/render/sc.go +++ b/internal/render/sc.go @@ -3,10 +3,13 @@ package render import ( "fmt" - "github.com/derailed/k9s/internal/client" storagev1 "k8s.io/api/storage/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kubectl/pkg/util/storage" + + "github.com/derailed/k9s/internal/client" ) // StorageClass renders a K8s StorageClass to screen. @@ -19,14 +22,15 @@ func (StorageClass) Header(ns string) Header { return Header{ HeaderColumn{Name: "NAME"}, HeaderColumn{Name: "PROVISIONER"}, - HeaderColumn{Name: "LABELS", Wide: true}, - HeaderColumn{Name: "VALID", Wide: true}, + HeaderColumn{Name: "RECLAIMPOLICY"}, + HeaderColumn{Name: "VOLUMEBINDINGMODE"}, + HeaderColumn{Name: "ALLOWVOLUMEEXPANSION"}, HeaderColumn{Name: "AGE", Time: true}, } } // Render renders a K8s resource to screen. -func (StorageClass) Render(o interface{}, ns string, r *Row) error { +func (s StorageClass) Render(o interface{}, ns string, r *Row) error { raw, ok := o.(*unstructured.Unstructured) if !ok { return fmt.Errorf("Expected StorageClass, but got %T", o) @@ -39,12 +43,20 @@ func (StorageClass) Render(o interface{}, ns string, r *Row) error { r.ID = client.FQN(client.ClusterScope, sc.ObjectMeta.Name) r.Fields = Fields{ - sc.Name, - string(sc.Provisioner), - mapToStr(sc.Labels), - "", + s.nameWithDefault(sc.ObjectMeta), + sc.Provisioner, + strPtrToStr((*string)(sc.ReclaimPolicy)), + strPtrToStr((*string)(sc.VolumeBindingMode)), + boolPtrToStr(sc.AllowVolumeExpansion), toAge(sc.GetCreationTimestamp()), } return nil } + +func (StorageClass) nameWithDefault(meta metav1.ObjectMeta) string { + if storage.IsDefaultAnnotationText(meta) == "Yes" { + return meta.Name + " (default)" + } + return meta.Name +} From 724cb202a66be661bc80aceb2c8ee2d00dfe3402 Mon Sep 17 00:00:00 2001 From: jayson wang Date: Thu, 15 Jun 2023 23:11:43 +0800 Subject: [PATCH 2/3] fix sc test --- internal/render/sc_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/render/sc_test.go b/internal/render/sc_test.go index 80e13393d0..0a15d09be6 100644 --- a/internal/render/sc_test.go +++ b/internal/render/sc_test.go @@ -3,8 +3,9 @@ package render_test import ( "testing" - "github.com/derailed/k9s/internal/render" "github.com/stretchr/testify/assert" + + "github.com/derailed/k9s/internal/render" ) func TestStorageClassRender(t *testing.T) { @@ -13,5 +14,5 @@ func TestStorageClassRender(t *testing.T) { assert.NoError(t, c.Render(load(t, "sc"), "", &r)) assert.Equal(t, "-/standard", r.ID) - assert.Equal(t, render.Fields{"standard", "kubernetes.io/gce-pd"}, r.Fields[:2]) + assert.Equal(t, render.Fields{"standard (default)", "kubernetes.io/gce-pd"}, r.Fields[:2]) } From cbd05913a57bf6d70fdcd384843785a4b01f8119 Mon Sep 17 00:00:00 2001 From: jayson Date: Thu, 19 Oct 2023 13:11:30 +0800 Subject: [PATCH 3/3] rollback labels and valid field and add test case --- internal/render/helpers.go | 3 +-- internal/render/sc.go | 7 +++++-- internal/render/sc_test.go | 5 ++--- internal/render/testdata/sc.json | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/render/helpers.go b/internal/render/helpers.go index 84e93da708..d96ebad8f0 100644 --- a/internal/render/helpers.go +++ b/internal/render/helpers.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/derailed/k9s/internal/client" "github.com/derailed/tview" runewidth "github.com/mattn/go-runewidth" "github.com/rs/zerolog/log" @@ -13,8 +14,6 @@ import ( "golang.org/x/text/message" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" - - "github.com/derailed/k9s/internal/client" ) func runesToNum(rr []rune) int64 { diff --git a/internal/render/sc.go b/internal/render/sc.go index 7067cccae3..a2995eab7e 100644 --- a/internal/render/sc.go +++ b/internal/render/sc.go @@ -3,13 +3,12 @@ package render import ( "fmt" + "github.com/derailed/k9s/internal/client" storagev1 "k8s.io/api/storage/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubectl/pkg/util/storage" - - "github.com/derailed/k9s/internal/client" ) // StorageClass renders a K8s StorageClass to screen. @@ -25,6 +24,8 @@ func (StorageClass) Header(ns string) Header { HeaderColumn{Name: "RECLAIMPOLICY"}, HeaderColumn{Name: "VOLUMEBINDINGMODE"}, HeaderColumn{Name: "ALLOWVOLUMEEXPANSION"}, + HeaderColumn{Name: "LABELS", Wide: true}, + HeaderColumn{Name: "VALID", Wide: true}, HeaderColumn{Name: "AGE", Time: true}, } } @@ -48,6 +49,8 @@ func (s StorageClass) Render(o interface{}, ns string, r *Row) error { strPtrToStr((*string)(sc.ReclaimPolicy)), strPtrToStr((*string)(sc.VolumeBindingMode)), boolPtrToStr(sc.AllowVolumeExpansion), + mapToStr(sc.Labels), + "", toAge(sc.GetCreationTimestamp()), } diff --git a/internal/render/sc_test.go b/internal/render/sc_test.go index 0a15d09be6..60cd56f242 100644 --- a/internal/render/sc_test.go +++ b/internal/render/sc_test.go @@ -3,9 +3,8 @@ package render_test import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/derailed/k9s/internal/render" + "github.com/stretchr/testify/assert" ) func TestStorageClassRender(t *testing.T) { @@ -14,5 +13,5 @@ func TestStorageClassRender(t *testing.T) { assert.NoError(t, c.Render(load(t, "sc"), "", &r)) assert.Equal(t, "-/standard", r.ID) - assert.Equal(t, render.Fields{"standard (default)", "kubernetes.io/gce-pd"}, r.Fields[:2]) + assert.Equal(t, render.Fields{"standard (default)", "kubernetes.io/gce-pd", "Delete", "Immediate", "true"}, r.Fields[:5]) } diff --git a/internal/render/testdata/sc.json b/internal/render/testdata/sc.json index afd1d892c3..c8ad58efe6 100644 --- a/internal/render/testdata/sc.json +++ b/internal/render/testdata/sc.json @@ -20,5 +20,6 @@ }, "provisioner": "kubernetes.io/gce-pd", "reclaimPolicy": "Delete", - "volumeBindingMode": "Immediate" + "volumeBindingMode": "Immediate", + "allowVolumeExpansion": true } \ No newline at end of file