Repository: grafana/tanka
Pull request #796 (2022-12-09 23:06:56)

Update resources.go to take output from kubectl@1.26
Fixes https://github.com/grafana/tanka/issues/795.


Patch:
diff --git a/pkg/kubernetes/client/resources.go b/pkg/kubernetes/client/resources.go
index 9b4d5bc8..f29d5011 100644
--- a/pkg/kubernetes/client/resources.go
+++ b/pkg/kubernetes/client/resources.go
@@ -36,6 +36,7 @@ type Resource struct {
 	Namespaced bool   `json:"NAMESPACED,string"`
 	Shortnames string `json:"SHORTNAMES"`
 	Verbs      string `json:"VERBS"`
+	Categories string `json:"CATEGORIES"`
 }
 
 func (r Resource) FQN() string {
@@ -134,8 +135,13 @@ func splitRow(s string, header []string) (elems []string) {
 		}
 
 		lim := len(h)
-		elems = append(elems, s[pos:pos+lim])
-		pos += lim
+		endPos := pos + lim
+		if endPos >= len(s) {
+			endPos = len(s)
+		}
+
+		elems = append(elems, s[pos:endPos])
+		pos = endPos
 	}
 	return elems
 }


Test patch:
diff --git a/pkg/kubernetes/client/resources_test.go b/pkg/kubernetes/client/resources_test.go
index c384f14d..f08107c6 100644
--- a/pkg/kubernetes/client/resources_test.go
+++ b/pkg/kubernetes/client/resources_test.go
@@ -35,7 +35,24 @@ func TestUnmarshalTable(t *testing.T) {
 	}{
 		{
 			name: "normal",
-			tbl:  strings.TrimSpace(tblNormal),
+			tbl:  strings.TrimSpace(tblv126),
+			want: &Resources{
+				{APIVersion: "v1", Kind: "Binding", Name: "bindings", Shortnames: "", Namespaced: true, Verbs: "create"},
+				{APIVersion: "v1", Kind: "ComponentStatus", Name: "componentstatuses", Shortnames: "cs", Namespaced: false, Verbs: "get,list"},
+				{APIVersion: "v1", Kind: "ConfigMap", Name: "configmaps", Shortnames: "cm", Namespaced: true, Verbs: "create,delete,deletecollection,get,list,patch,update,watch"},
+				{APIVersion: "v1", Kind: "Pod", Name: "pods", Shortnames: "po", Namespaced: true, Verbs: "create,delete,deletecollection,get,list,patch,update,watch", Categories: "all"},
+			},
+			wantFQNs: []string{
+				"bindings",
+				"componentstatuses",
+				"configmaps",
+				"pods",
+			},
+			dest: &Resources{},
+		},
+		{
+			name: "normal-v1.18-to-v.1.25",
+			tbl:  strings.TrimSpace(tblv121),
 			want: &Resources{
 				{APIVersion: "v1", Kind: "Namespace", Name: "namespaces", Shortnames: "ns", Namespaced: false},
 				{APIVersion: "apps/v1", Kind: "DaemonSet", Name: "daemonsets", Shortnames: "ds", Namespaced: true},
@@ -52,7 +69,7 @@ func TestUnmarshalTable(t *testing.T) {
 		},
 		{
 			name: "normal-v1.18-and-older",
-			tbl:  strings.TrimSpace(tblNormal1Dot18AndOlder),
+			tbl:  strings.TrimSpace(tblv118),
 			want: &Resources{
 				{APIGroup: "", Kind: "Namespace", Name: "namespaces", Shortnames: "ns", Namespaced: false},
 				{APIGroup: "apps", Kind: "DaemonSet", Name: "daemonsets", Shortnames: "ds", Namespaced: true},
@@ -97,9 +114,19 @@ func TestUnmarshalTable(t *testing.T) {
 	}
 }
 
+// This is a snippet that was pulled from output generated with kubectl v1.26.0
+// $ kubectl api-resources --cached --output=wide
+var tblv126 = `
+NAME                              SHORTNAMES   APIVERSION                        NAMESPACED   KIND                             VERBS                                                        CATEGORIES
+bindings                                       v1                                true         Binding                          create
+componentstatuses                 cs           v1                                false        ComponentStatus                  get,list
+configmaps                        cm           v1                                true         ConfigMap                        create,delete,deletecollection,get,list,patch,update,watch
+pods                              po           v1                                true         Pod                              create,delete,deletecollection,get,list,patch,update,watch   all
+`
+
 // this output was generated with kubectl v1.21.1
 // $ kubectl api-resources | grep -e "Deployment\|DaemonSet\|Namespace\|networking.k8s.io.*Ingress$\|KIND"
-var tblNormal = `
+var tblv121 = `
 NAME                                SHORTNAMES                             APIVERSION                             NAMESPACED   KIND
 namespaces                          ns                                     v1                                     false        Namespace
 daemonsets                          ds                                     apps/v1                                true         DaemonSet
@@ -109,7 +136,7 @@ ingresses                           ing                                    netwo
 
 // this output was generated with kubectl v1.18.10
 // $ kubectl api-resources | grep -e "Deployment\|DaemonSet\|Namespace\|networking.k8s.io.*Ingress$\|KIND"
-var tblNormal1Dot18AndOlder = `
+var tblv118 = `
 NAME                                SHORTNAMES                             APIGROUP                       NAMESPACED   KIND
 namespaces                          ns                                                                    false        Namespace
 daemonsets                          ds                                     apps                           true         DaemonSet