Skip to content

Commit 89d8f79

Browse files
inshbha2garomonegrosbhat14
authored
feat: add method to check pvc exists (#167)
* build: go 1.21, godog 0.14.1, api, apimachinery & client-go v0.28.12 (#161) * build: go 1.21, godog 0.14.1, api, apimachinery & client-go v0.28.12 * ci: building examples * ci(examples): check-dirty-repo * test(UT): fixing getIngressWithHostname * test(UT): fixing TestGetInstanceGroupList * refactor: replace 'err == wait.ErrWaitTimeout' with 'wait.Interrupted(err)' * chore: adding missed space Signed-off-by: sbhat14 <[email protected]> * feat: add method to check pvc exists Signed-off-by: sbhat14 <[email protected]> * fix: address review comments Signed-off-by: sbhat14 <[email protected]> * fix: retry Signed-off-by: sbhat14 <[email protected]> * remove unnecessary string formatting Signed-off-by: sbhat14 <[email protected]> --------- Signed-off-by: sbhat14 <[email protected]> Co-authored-by: Alfredo Garo <[email protected]> Co-authored-by: sbhat14 <[email protected]>
1 parent 982eb78 commit 89d8f79

File tree

6 files changed

+122
-14
lines changed

6 files changed

+122
-14
lines changed

docs/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
5050
- `<GK> [the] deployment <any-characters-except-(")> is running in namespace <any-characters-except-(")>` kdt.KubeClientSet.DeploymentIsRunning
5151
- `<GK> [the] data in [the] ConfigMap "<any-characters-except-(")>" in namespace "<any-characters-except-(")>" has key "<any-characters-except-(")>" with value "<any-characters-except-(")>"` kdt.KubeClientSet.ConfigMapDataHasKeyAndValue
5252
- `<GK> [the] persistentvolume <any-characters-except-(")> exists with status (Available|Bound|Released|Failed|Pending)` kdt.KubeClientSet.PersistentVolExists
53+
- `<GK> [the] persistentvolumeclaim <any-characters-except-(")> exists with status (Available|Bound|Released|Failed|Pending) in namespace <any-characters-except-(")>` kdt.KubeClientSet.PersistentVolClaimExists
5354
- `<GK> [the] (clusterrole|clusterrolebinding) with name <any-characters-except-(")> should be found` kdt.KubeClientSet.ClusterRbacIsFound
5455
- `<GK> [the] ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [is] [available] on port <digits> and path <any-characters-except-(")>` kdt.KubeClientSet.IngressAvailable
5556
- `<GK> [I] send <digits> tps to ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [available] on port <digits> and path <any-characters-except-(")> for <digits> (minutes|seconds) expecting up to <digits> error[s]` kdt.KubeClientSet.SendTrafficToIngress

kubedog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (kdt *Test) SetScenario(scenario *godog.ScenarioContext) {
8080
kdt.scenario.Step(`^(?:the )?deployment ([^"]*) is running in namespace ([^"]*)$`, kdt.KubeClientSet.DeploymentIsRunning)
8181
kdt.scenario.Step(`^(?:the )?data in (?:the )?ConfigMap "([^"]*)" in namespace "([^"]*)" has key "([^"]*)" with value "([^"]*)"$`, kdt.KubeClientSet.ConfigMapDataHasKeyAndValue)
8282
kdt.scenario.Step(`^(?:the )?persistentvolume ([^"]*) exists with status (Available|Bound|Released|Failed|Pending)$`, kdt.KubeClientSet.PersistentVolExists)
83+
kdt.scenario.Step(`^(?:the )?persistentvolumeclaim ([^"]*) exists with status (Available|Bound|Released|Failed|Pending) in namespace ([^"]*)$`, kdt.KubeClientSet.PersistentVolClaimExists)
8384
kdt.scenario.Step(`^(?:the )?(clusterrole|clusterrolebinding) with name ([^"]*) should be found$`, kdt.KubeClientSet.ClusterRbacIsFound)
8485
kdt.scenario.Step(`^(?:the )?ingress (\S+) in (?:the )?namespace (\S+) (?:is )?(?:available )?on port (\d+) and path ([^"]*)$`, kdt.KubeClientSet.IngressAvailable)
8586
kdt.scenario.Step(`^(?:I )?send (\d+) tps to ingress (\S+) in (?:the )?namespace (\S+) (?:available )?on port (\d+) and path ([^"]*) for (\d+) (minutes|seconds) expecting up to (\d+) error(?:s)?$`, kdt.KubeClientSet.SendTrafficToIngress)

pkg/kube/kube.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ func (kc *ClientSet) PersistentVolExists(name, expectedPhase string) error {
320320
return structured.PersistentVolExists(kc.KubeInterface, name, expectedPhase)
321321
}
322322

323+
func (kc *ClientSet) PersistentVolClaimExists(name, expectedPhase string, namespace string) error {
324+
return structured.PersistentVolClaimExists(kc.KubeInterface, name, expectedPhase, namespace)
325+
}
326+
323327
func (kc *ClientSet) ClusterRbacIsFound(resourceType, name string) error {
324328
return structured.ClusterRbacIsFound(kc.KubeInterface, resourceType, name)
325329
}

pkg/kube/structured/structured.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http"
2121
"os"
2222
"strconv"
23+
"strings"
2324
"time"
2425

2526
log "github.com/sirupsen/logrus"
@@ -235,6 +236,27 @@ func PersistentVolExists(kubeClientset kubernetes.Interface, name, expectedPhase
235236
return nil
236237
}
237238

239+
func PersistentVolClaimExists(kubeClientset kubernetes.Interface, name, expectedPhase string, namespace string) error {
240+
_, err := util.RetryOnError(
241+
&util.DefaultRetry,
242+
func(err error) bool {
243+
msg := "persistentvolumeclaim had unexpected phase"
244+
return util.IsRetriable(err) || strings.Contains(err.Error(), msg)
245+
},
246+
func() (interface{}, error) {
247+
vol, err := GetPersistentVolumeClaim(kubeClientset, name, namespace)
248+
if err != nil {
249+
return nil, err
250+
}
251+
phase := string(vol.Status.Phase)
252+
if phase != expectedPhase {
253+
return nil, fmt.Errorf("persistentvolumeclaim had unexpected phase %v, expected phase %v", phase, expectedPhase)
254+
}
255+
return nil, nil
256+
})
257+
return err
258+
}
259+
238260
func ValidatePrometheusVolumeClaimTemplatesName(kubeClientset kubernetes.Interface, statefulsetName, namespace, volumeClaimTemplatesName string) error {
239261
// Prometheus StatefulSets deployed, then validate volumeClaimTemplate name.
240262
// Validation required:

pkg/kube/structured/structured_helper.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ func GetPersistentVolume(kubeClientset kubernetes.Interface, name string) (*core
9696
return pvs.(*corev1.PersistentVolume), nil
9797
}
9898

99+
func GetPersistentVolumeClaim(kubeClientset kubernetes.Interface, name string, namespace string) (*corev1.PersistentVolumeClaim, error) {
100+
if err := common.ValidateClientset(kubeClientset); err != nil {
101+
return nil, err
102+
}
103+
104+
pvc, err := util.RetryOnError(&util.DefaultRetry, util.IsRetriable, func() (interface{}, error) {
105+
return kubeClientset.CoreV1().PersistentVolumeClaims(namespace).Get(context.Background(), name, metav1.GetOptions{})
106+
})
107+
if err != nil {
108+
return nil, errors.Wrap(err, "failed to get persistentvolumeclaim")
109+
}
110+
return pvc.(*corev1.PersistentVolumeClaim), nil
111+
}
112+
99113
func GetStatefulSetList(kubeClientset kubernetes.Interface, namespace string) (*appsv1.StatefulSetList, error) {
100114
if err := common.ValidateClientset(kubeClientset); err != nil {
101115
return nil, err

pkg/kube/structured/structured_test.go

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ import (
3636
)
3737

3838
const (
39-
configMapType = "configmap"
40-
deploymentType = "deployment"
41-
serviceType = "service"
42-
hpaType = "horizontalpodautoscaler"
43-
pdbType = "poddisruptionbudget"
44-
saType = "serviceaccount"
45-
clusterRoleType = "clusterrole"
46-
clusterRoleBindingType = "clusterrolebinding"
47-
nodeType = "node"
48-
daemonSetType = "daemonset"
49-
persistentVolumeType = "persistentvolume"
50-
statefulSetType = "statefulset"
51-
secretType = "secret"
52-
ingressType = "ingress"
39+
configMapType = "configmap"
40+
deploymentType = "deployment"
41+
serviceType = "service"
42+
hpaType = "horizontalpodautoscaler"
43+
pdbType = "poddisruptionbudget"
44+
saType = "serviceaccount"
45+
clusterRoleType = "clusterrole"
46+
clusterRoleBindingType = "clusterrolebinding"
47+
nodeType = "node"
48+
daemonSetType = "daemonset"
49+
persistentVolumeType = "persistentvolume"
50+
persistentVolumeClaimType = "persistentVolumeClaim"
51+
statefulSetType = "statefulset"
52+
secretType = "secret"
53+
ingressType = "ingress"
5354
)
5455

5556
func TestNodesWithSelectorShouldBe(t *testing.T) {
@@ -480,6 +481,60 @@ func TestPersistentVolExists(t *testing.T) {
480481
}
481482
}
482483

484+
func TestPersistentVolClaimExists(t *testing.T) {
485+
type args struct {
486+
kubeClientset kubernetes.Interface
487+
name string
488+
namespace string
489+
expectedPhase string
490+
}
491+
// expectedPhase: Available|Bound|Released|Failed|Pending
492+
persistentvolumeClaimName := "persistentvolumeclaim1"
493+
tests := []struct {
494+
name string
495+
args args
496+
wantErr bool
497+
}{
498+
{
499+
name: "pvc found",
500+
args: args{
501+
kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, persistentvolumeClaimName)),
502+
name: persistentvolumeClaimName,
503+
namespace: "",
504+
expectedPhase: "Bound",
505+
},
506+
wantErr: false,
507+
},
508+
{
509+
name: "pvc found with wrong phase",
510+
args: args{
511+
kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, persistentvolumeClaimName)),
512+
name: persistentvolumeClaimName,
513+
namespace: "",
514+
expectedPhase: "Released",
515+
},
516+
wantErr: true,
517+
},
518+
{
519+
name: "pvc not found Test",
520+
args: args{
521+
kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, "testabc")),
522+
name: persistentvolumeClaimName,
523+
namespace: "",
524+
expectedPhase: "",
525+
},
526+
wantErr: true,
527+
},
528+
}
529+
for _, tt := range tests {
530+
t.Run(tt.name, func(t *testing.T) {
531+
if err := PersistentVolClaimExists(tt.args.kubeClientset, tt.args.name, tt.args.expectedPhase, tt.args.namespace); (err != nil) != tt.wantErr {
532+
t.Errorf("PersistentVolClaimExists() error = %v, wantErr %v", err, tt.wantErr)
533+
}
534+
})
535+
}
536+
}
537+
483538
func TestValidatePrometheusVolumeClaimTemplatesName(t *testing.T) {
484539
type args struct {
485540
kubeClientset kubernetes.Interface
@@ -833,6 +888,17 @@ func getResourceWithAll(t *testing.T, resourceType, name, namespace, label strin
833888
Labels: labels,
834889
},
835890
}
891+
case persistentVolumeClaimType:
892+
return &corev1.PersistentVolumeClaim{
893+
ObjectMeta: metav1.ObjectMeta{
894+
Name: name,
895+
Namespace: namespace,
896+
Labels: labels,
897+
},
898+
Status: corev1.PersistentVolumeClaimStatus{
899+
Phase: corev1.ClaimBound,
900+
},
901+
}
836902
case statefulSetType:
837903
return &appsv1.StatefulSet{
838904
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)