diff --git a/checks.md b/checks.md index 51f6ba6..591034d 100644 --- a/checks.md +++ b/checks.md @@ -252,3 +252,16 @@ spec: readOnly: true ``` +###### Unused Persistent Volume + +Name: `unused-pv` + +Group: `basic` + +Description: This check reports all the persistent volumes in the cluster that are not claimed by persistent volume claims in any namespace. The cluster can be cleaned up based on this information and there will be fewer objects to manage. + +How to fix: + +```bash +kubectl delete pv +``` diff --git a/checks/basic/unused_pv.go b/checks/basic/unused_pv.go new file mode 100644 index 0000000..6e18cb6 --- /dev/null +++ b/checks/basic/unused_pv.go @@ -0,0 +1,51 @@ +package basic + +import ( + "fmt" + + "github.com/digitalocean/clusterlint/checks" + "github.com/digitalocean/clusterlint/kube" +) + +func init() { + checks.Register(&unusedPVCheck{}) +} + +type unusedPVCheck struct{} + +// Name returns a unique name for this check. +func (pv *unusedPVCheck) Name() string { + return "unused-pv" +} + +// Groups returns a list of group names this check should be part of. +func (pvh *unusedPVCheck) Groups() []string { + return []string{"basic"} +} + +// Description returns a detailed human-readable description of what this check +// does. +func (pv *unusedPVCheck) Description() string { + return "Check if there are unused persistent volumes in the cluster" +} + +// Run runs this check on a set of Kubernetes objects. It can return warnings +// (low-priority problems) and errors (high-priority problems) as well as an +// error value indicating that the check failed to run. +func (pv *unusedPVCheck) Run(objects *kube.Objects) ([]checks.Diagnostic, error) { + var diagnostics []checks.Diagnostic + for _, pv := range objects.PersistentVolumes.Items { + if pv.Spec.ClaimRef == nil { + d := checks.Diagnostic{ + Severity: checks.Warning, + Message: fmt.Sprintf("Unused Persistent Volume '%s'.", pv.GetName()), + Kind: checks.PV, + Object: &pv.ObjectMeta, + Owners: pv.ObjectMeta.GetOwnerReferences(), + } + diagnostics = append(diagnostics, d) + } + } + + return diagnostics, nil +} diff --git a/checks/basic/unused_pv_test.go b/checks/basic/unused_pv_test.go new file mode 100644 index 0000000..e8d73de --- /dev/null +++ b/checks/basic/unused_pv_test.go @@ -0,0 +1,99 @@ +package basic + +import ( + "testing" + + "github.com/digitalocean/clusterlint/checks" + "github.com/digitalocean/clusterlint/kube" + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestUnusedPVCheckMeta(t *testing.T) { + unusedPVCheck := unusedPVCheck{} + assert.Equal(t, "unused-pv", unusedPVCheck.Name()) + assert.Equal(t, []string{"basic"}, unusedPVCheck.Groups()) + assert.NotEmpty(t, unusedPVCheck.Description()) +} + +func TestUnusedPVCheckRegistration(t *testing.T) { + unusedPVCheck := &unusedPVCheck{} + check, err := checks.Get("unused-pv") + assert.NoError(t, err) + assert.Equal(t, check, unusedPVCheck) +} + +func TestUnusedPVWarning(t *testing.T) { + tests := []struct { + name string + objs *kube.Objects + expected []checks.Diagnostic + }{ + { + name: "no pvs", + objs: &kube.Objects{PersistentVolumes: &corev1.PersistentVolumeList{}}, + expected: nil, + }, + { + name: "bound pv", + objs: bound(), + expected: nil, + }, + { + name: "unused pv", + objs: unused(), + expected: []checks.Diagnostic{ + { + Severity: checks.Warning, + Message: "Unused Persistent Volume 'pv_foo'.", + Kind: checks.PV, + Object: &metav1.ObjectMeta{Name: "pv_foo"}, + Owners: GetOwners(), + }, + }, + }, + } + + unusedPVCheck := unusedPVCheck{} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + d, err := unusedPVCheck.Run(test.objs) + assert.NoError(t, err) + assert.ElementsMatch(t, test.expected, d) + }) + } +} + +func initPV() *kube.Objects { + objs := &kube.Objects{ + PersistentVolumes: &corev1.PersistentVolumeList{ + Items: []corev1.PersistentVolume{ + { + TypeMeta: metav1.TypeMeta{Kind: "PersistentVolume", APIVersion: "v1"}, + ObjectMeta: metav1.ObjectMeta{Name: "pv_foo"}, + }, + }, + }, + } + return objs +} + +func bound() *kube.Objects { + objs := initPV() + objs.PersistentVolumes.Items[0].Spec = corev1.PersistentVolumeSpec{ + ClaimRef: &corev1.ObjectReference{ + Kind: "PersistentVolumeClaim", + Name: "foo", + Namespace: "k8s", + }, + } + return objs +} + +func unused() *kube.Objects { + objs := initPV() + objs.PersistentVolumes.Items[0].Spec = corev1.PersistentVolumeSpec{} + return objs +} diff --git a/checks/diagnostic.go b/checks/diagnostic.go index 976b96a..8542ecb 100644 --- a/checks/diagnostic.go +++ b/checks/diagnostic.go @@ -34,4 +34,5 @@ const ( Service Kind = "service" Secret Kind = "secret" SA Kind = "service account" + PV Kind = "persistent volume" )