Skip to content

Commit

Permalink
Merge pull request #97 from openshift-cherrypick-robot/cherry-pick-96…
Browse files Browse the repository at this point in the history
…-to-release-4.12

[release-4.12] OCPBUGS-5509: Add a count of zonal volumes
  • Loading branch information
openshift-merge-robot committed Jan 31, 2023
2 parents 2b91d55 + 537d0e6 commit 7328d21
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
31 changes: 23 additions & 8 deletions pkg/check/count_pv_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,53 @@ var (
},
[]string{},
)
zonalPVCountMetric = metrics.NewGaugeVec(
&metrics.GaugeOpts{
Name: "vsphere_zonal_volumes_total",
Help: "Number of zonal vSphere volumes in cluster",
StabilityLevel: metrics.ALPHA,
}, []string{},
)
rwxVolumeRegx = regexp.MustCompile(`file\:`)
)

func init() {
legacyregistry.MustRegister(rwxPVCountMetric)
legacyregistry.MustRegister(zonalPVCountMetric)
}

func CountRWXVolumes(ctx *CheckContext) error {
rwxPVCount, err := countRWXPVsFromCluster(ctx)
func CountPVTypes(ctx *CheckContext) error {
rwxPVCount, zonalPVCount, err := countVolumeTypes(ctx)
if err != nil {
return err
}

rwxPVCountMetric.WithLabelValues().Set(float64(rwxPVCount))
zonalPVCountMetric.WithLabelValues().Set(float64(zonalPVCount))
return nil
}

func countRWXPVsFromCluster(ctx *CheckContext) (int, error) {
rwxPVCount := 0
func countVolumeTypes(ctx *CheckContext) (rwxCount int, zonalPVCount int, err error) {
pvs, err := ctx.KubeClient.ListPVs(ctx.Context)
if err != nil {
return rwxPVCount, err
return
}

for i := range pvs {
pv := pvs[i]
csi := pv.Spec.CSI
if csi != nil && csi.Driver == vSphereCSIDdriver {
volumeHandle := csi.VolumeHandle
if rwxVolumeRegx.MatchString(volumeHandle) {
rwxPVCount += 1
rwxCount += 1
}

if pv.Spec.NodeAffinity != nil && pv.Spec.NodeAffinity.Required != nil {
nodeSelectors := pv.Spec.NodeAffinity.Required
if len(nodeSelectors.NodeSelectorTerms) > 0 {
zonalPVCount += 1
}
}
}
}
return rwxPVCount, nil
return
}
41 changes: 37 additions & 4 deletions pkg/check/count_pv_types_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package check

import (
"testing"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestCountRWXVolumes(t *testing.T) {
Expand All @@ -24,6 +25,34 @@ func TestCountRWXVolumes(t *testing.T) {
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "zonalvolume",
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
CSI: &v1.CSIPersistentVolumeSource{
Driver: vSphereCSIDdriver,
VolumeHandle: "blahblah",
},
},
NodeAffinity: &v1.VolumeNodeAffinity{
Required: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: "topology.csi.vmware.com/openshift-zone",
Operator: v1.NodeSelectorOperator("In"),
Values: []string{"us-west-1a"},
},
},
},
},
},
},
},
},
},
}

Expand All @@ -34,11 +63,15 @@ func TestCountRWXVolumes(t *testing.T) {
defer cleanup()

// Act
count, err := countRWXPVsFromCluster(ctx)
rwxVolumeCount, zonalPVCount, err := countVolumeTypes(ctx)
if err != nil {
t.Errorf("Unexpected error : %+v", err)
}
if 1 != count {
t.Errorf("expected 1 rwx pv, found %d", count)
if 1 != rwxVolumeCount {
t.Errorf("expected 1 rwx pv, found %d", rwxVolumeCount)
}

if 1 != zonalPVCount {
t.Errorf("expected 1 zonal pv, found %d", zonalPVCount)
}
}
2 changes: 1 addition & 1 deletion pkg/check/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
"CheckFolderPermissions": CheckFolderPermissions,
"CheckDefaultDatastore": CheckDefaultDatastore,
"CheckStorageClasses": CheckStorageClasses,
"CountRWXVolumes": CountRWXVolumes,
"CountVolumeTypes": CountPVTypes,
"CheckAccountPermissions": CheckAccountPermissions,
}
DefaultNodeChecks []NodeCheck = []NodeCheck{
Expand Down

0 comments on commit 7328d21

Please sign in to comment.