-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathworkload.go
61 lines (53 loc) · 1.69 KB
/
workload.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
52
53
54
55
56
57
58
59
60
61
package test
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
type Deployment struct {
Name string
ExpectedScale *int32
}
func WithWorkloadReady(ctx *Context, name string, namespace string) error {
waitErr := withDeploymentReady(ctx, name, namespace)
if apierrors.IsNotFound(waitErr) {
waitErr = withStatefulSetReady(ctx, name, namespace)
}
if waitErr != nil {
return fmt.Errorf("deployment %s in namespace %s not ready in time: %w", name, namespace, waitErr)
}
return nil
}
func withDeploymentReady(ctx *Context, name string, namespace string) error {
var deployment *appsv1.Deployment
waitErr := wait.PollUntilContextTimeout(context.Background(), Interval, Timeout, true, func(_ context.Context) (bool, error) {
var err error
deployment, err = ctx.Clients.Kube.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return false, err
}
if deployment.Status.ReadyReplicas < *deployment.Spec.Replicas {
return false, nil
}
return true, nil
})
return waitErr
}
func withStatefulSetReady(ctx *Context, name string, namespace string) error {
var ss *appsv1.StatefulSet
waitErr := wait.PollUntilContextTimeout(context.Background(), Interval, Timeout, true, func(_ context.Context) (bool, error) {
var err error
ss, err = ctx.Clients.Kube.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return false, err
}
if ss.Status.ReadyReplicas < *ss.Spec.Replicas {
return false, nil
}
return true, nil
})
return waitErr
}