-
Notifications
You must be signed in to change notification settings - Fork 13
/
poddisruptionbudget.go
51 lines (41 loc) · 1.59 KB
/
poddisruptionbudget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package globalhelper
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
policyv1 "k8s.io/api/policy/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
. "github.com/onsi/gomega"
)
// IsPodDisruptionBudgetCreated checks if a pod disruption budget is created.
func IsPodDisruptionBudgetCreated(pdbName string, namespace string) (bool, error) {
pdb, err := GetAPIClient().PolicyV1Interface.PodDisruptionBudgets(namespace).Get(context.TODO(), pdbName, metav1.GetOptions{})
if err != nil {
return false, err
}
return len(pdb.UID) != 0, nil
}
// CreatPodDisruptionBudget creates Pod Disruption Budget and wait until pdb is created.
func CreatePodDisruptionBudget(pdb *policyv1.PodDisruptionBudget, timeout time.Duration) error {
poddisruptionbudget, err := GetAPIClient().PolicyV1Interface.PodDisruptionBudgets(pdb.Namespace).Create(
context.TODO(),
pdb,
metav1.CreateOptions{})
if k8serrors.IsAlreadyExists(err) {
glog.V(5).Info(fmt.Sprintf("Pod Disruption Budget %s already exists", pdb.Name))
} else if err != nil {
return fmt.Errorf("failed to create Pod Disruption Budget %q (ns %s): %w", pdb.Name, pdb.Namespace, err)
}
Eventually(func() bool {
status, err := IsPodDisruptionBudgetCreated(poddisruptionbudget.Name, poddisruptionbudget.Namespace)
if err != nil {
glog.V(5).Info(fmt.Sprintf(
"Pod Disruption Budget %s is not ready, retry in 5 seconds", poddisruptionbudget.Name))
return false
}
return status
}, timeout, retryInterval*time.Second).Should(Equal(true), "Pod Disruption Budget is not ready")
return nil
}