Skip to content

Commit

Permalink
storageclasses view keeps the same output as kubectl get sc (derailed…
Browse files Browse the repository at this point in the history
…#2132)

* storageclasses view keeps the same output as kubectl get sc

* fix sc test

* rollback labels and valid field and add test case
  • Loading branch information
wjiec authored Nov 12, 2023
1 parent 858fe6a commit 069c5ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
7 changes: 7 additions & 0 deletions internal/render/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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 {
Expand Down
21 changes: 18 additions & 3 deletions internal/render/sc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (

"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"
)

// StorageClass renders a K8s StorageClass to screen.
Expand All @@ -19,14 +21,17 @@ func (StorageClass) Header(ns string) Header {
return Header{
HeaderColumn{Name: "NAME"},
HeaderColumn{Name: "PROVISIONER"},
HeaderColumn{Name: "RECLAIMPOLICY"},
HeaderColumn{Name: "VOLUMEBINDINGMODE"},
HeaderColumn{Name: "ALLOWVOLUMEEXPANSION"},
HeaderColumn{Name: "LABELS", Wide: true},
HeaderColumn{Name: "VALID", Wide: true},
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)
Expand All @@ -39,12 +44,22 @@ 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),
s.nameWithDefault(sc.ObjectMeta),
sc.Provisioner,
strPtrToStr((*string)(sc.ReclaimPolicy)),
strPtrToStr((*string)(sc.VolumeBindingMode)),
boolPtrToStr(sc.AllowVolumeExpansion),
mapToStr(sc.Labels),
"",
toAge(sc.GetCreationTimestamp()),
}

return nil
}

func (StorageClass) nameWithDefault(meta metav1.ObjectMeta) string {
if storage.IsDefaultAnnotationText(meta) == "Yes" {
return meta.Name + " (default)"
}
return meta.Name
}
2 changes: 1 addition & 1 deletion internal/render/sc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,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", "kubernetes.io/gce-pd"}, r.Fields[:2])
assert.Equal(t, render.Fields{"standard (default)", "kubernetes.io/gce-pd", "Delete", "Immediate", "true"}, r.Fields[:5])
}
3 changes: 2 additions & 1 deletion internal/render/testdata/sc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
},
"provisioner": "kubernetes.io/gce-pd",
"reclaimPolicy": "Delete",
"volumeBindingMode": "Immediate"
"volumeBindingMode": "Immediate",
"allowVolumeExpansion": true
}

0 comments on commit 069c5ed

Please sign in to comment.