Repository: doitintl/kube-no-trouble
Pull request #156 (2021-06-10 15:19:55)

feat: Add coverage for deprecated Storage resources - `storage.k8s.io/v1beta1` API group
This PR adds coverage for deprecated Storage resources - `storage.k8s.io/v1beta1` API group.


Patch:
diff --git a/fixtures/csidriver-v1beta1.yaml b/fixtures/csidriver-v1beta1.yaml
new file mode 100644
index 0000000..5fe53a1
--- /dev/null
+++ b/fixtures/csidriver-v1beta1.yaml
@@ -0,0 +1,7 @@
+apiVersion: storage.k8s.io/v1beta1
+kind: CSIDriver
+metadata:
+        name: fake-csi-driver
+spec:
+  attachRequired: true
+  podInfoOnMount: false
diff --git a/fixtures/csinode-v1beta1.yaml b/fixtures/csinode-v1beta1.yaml
new file mode 100644
index 0000000..ed11b55
--- /dev/null
+++ b/fixtures/csinode-v1beta1.yaml
@@ -0,0 +1,9 @@
+apiVersion: storage.k8s.io/v1beta1
+kind: CSINode
+metadata:
+  name: my-node
+spec:
+  drivers:
+  - name: my-csi-driver.example.com
+    nodeID: storageNodeID1
+    topologyKeys: ['mycsidriver.example.com/regions', "mycsidriver.example.com/zones"]
diff --git a/fixtures/storageclass-v1beta1.yaml b/fixtures/storageclass-v1beta1.yaml
new file mode 100644
index 0000000..10f0c96
--- /dev/null
+++ b/fixtures/storageclass-v1beta1.yaml
@@ -0,0 +1,8 @@
+kind: StorageClass
+apiVersion: storage.k8s.io/v1beta1
+metadata:
+  name: slow
+provisioner: kubernetes.io/gce-pd
+parameters:
+  type: pd-standard
+  zone: us-east1-d
diff --git a/fixtures/volumeattachment-v1beta1.yaml b/fixtures/volumeattachment-v1beta1.yaml
new file mode 100644
index 0000000..cb06f78
--- /dev/null
+++ b/fixtures/volumeattachment-v1beta1.yaml
@@ -0,0 +1,4 @@
+kind: VolumeAttachment
+apiVersion: storage.k8s.io/v1beta1
+metadata:
+  name: test
diff --git a/pkg/collector/cluster.go b/pkg/collector/cluster.go
index d06590c..ae4a010 100644
--- a/pkg/collector/cluster.go
+++ b/pkg/collector/cluster.go
@@ -78,6 +78,10 @@ func (c *ClusterCollector) Get() ([]map[string]interface{}, error) {
 		schema.GroupVersionResource{Group: "networking.k8s.io", Version: "v1", Resource: "networkpolicies"},
 		schema.GroupVersionResource{Group: "policy", Version: "v1beta1", Resource: "podsecuritypolicies"},
 		schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "ingresses"},
+		schema.GroupVersionResource{Group: "storage.k8s.io", Version: "v1", Resource: "csidrivers"},
+		schema.GroupVersionResource{Group: "storage.k8s.io", Version: "v1", Resource: "csinodes"},
+		schema.GroupVersionResource{Group: "storage.k8s.io", Version: "v1", Resource: "storageclasses"},
+		schema.GroupVersionResource{Group: "storage.k8s.io", Version: "v1", Resource: "volumeattachments"},
 	}
 	gvrs = append(gvrs, c.additionalResources...)
 
diff --git a/pkg/rules/rego/deprecated-1-22.rego b/pkg/rules/rego/deprecated-1-22.rego
index a1aafb8..c8beaed 100644
--- a/pkg/rules/rego/deprecated-1-22.rego
+++ b/pkg/rules/rego/deprecated-1-22.rego
@@ -46,6 +46,26 @@ deprecated_api(kind, api_version) = api {
 			"new": "coordination.k8s.io/v1",
 			"since": "1.19",
 		},
+		"CSIDriver": {
+			"old": ["storage.k8s.io/v1beta1"],
+			"new": "storage.k8s.io/v1",
+			"since": "1.19",
+		},
+		"CSINode": {
+			"old": ["storage.k8s.io/v1beta1"],
+			"new": "storage.k8s.io/v1",
+			"since": "1.17",
+		},
+		"StorageClass": {
+			"old": ["storage.k8s.io/v1beta1"],
+			"new": "storage.k8s.io/v1",
+			"since": "1.6",
+		},
+		"VolumeAttachment": {
+			"old": ["storage.k8s.io/v1beta1"],
+			"new": "storage.k8s.io/v1",
+			"since": "1.13",
+		},
 	}
 
 	deprecated_apis[kind].old[_] == api_version


Test patch:
diff --git a/test/rules_122_test.go b/test/rules_122_test.go
new file mode 100644
index 0000000..1be7c03
--- /dev/null
+++ b/test/rules_122_test.go
@@ -0,0 +1,45 @@
+package test
+
+import (
+	"testing"
+
+	"github.com/doitintl/kube-no-trouble/pkg/collector"
+)
+
+func TestRego116(t *testing.T) {
+	testCases := []struct {
+		name          string
+		manifests     []string
+		expectedKinds []string // kinds of objects
+	}{
+		{"CSIDriver", []string{"../fixtures/csidriver-v1beta1.yaml"}, []string{"CSIDriver"}},
+		{"CSINode", []string{"../fixtures/csinode-v1beta1.yaml"}, []string{"CSINode"}},
+		{"StorageClass", []string{"../fixtures/storageclass-v1beta1.yaml"}, []string{"StorageClass"}},
+		{"VolumeAttachment", []string{"../fixtures/volumeattachment-v1beta1.yaml"}, []string{"VolumeAttachment"}},
+	}
+
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			c, err := collector.NewFileCollector(
+				&collector.FileOpts{Filenames: tc.manifests},
+			)
+
+			if err != nil {
+				t.Errorf("Expected to succeed for %s, failed: %s", tc.manifests, err)
+			}
+
+			manifests, err := c.Get()
+			if err != nil {
+				t.Errorf("Expected to succeed for %s, failed: %s", tc.manifests, err)
+			} else if len(manifests) != len(tc.expectedKinds) {
+				t.Errorf("Expected to get %d, got %d", len(tc.expectedKinds), len(manifests))
+			}
+
+			for i := range manifests {
+				if manifests[i]["kind"] != tc.expectedKinds[i] {
+					t.Errorf("Expected to get %s, instead got: %s", tc.expectedKinds[i], manifests[i]["kind"])
+				}
+			}
+		})
+	}
+}