-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (43 loc) · 1.47 KB
/
utils.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 trigger
import (
"context"
"fmt"
"io/ioutil"
"time"
"github.com/ghodss/yaml"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
appsv1clientset "k8s.io/client-go/kubernetes/typed/apps/v1"
watchtools "k8s.io/client-go/tools/watch"
)
func readDeploymentFixture(path string) (*appsv1.Deployment, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
deployment := new(appsv1.Deployment)
err = yaml.Unmarshal(data, deployment)
if err != nil {
return nil, err
}
return deployment, err
}
func waitForDeploymentModification(appsClient appsv1clientset.AppsV1Interface, objMeta metav1.ObjectMeta, timeout time.Duration, condition func(deployment *appsv1.Deployment) (bool, error)) (*appsv1.Deployment, error) {
watcher, err := appsClient.Deployments(objMeta.Namespace).Watch(metav1.SingleObject(objMeta))
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
event, err := watchtools.UntilWithoutRetry(ctx, watcher, func(event watch.Event) (bool, error) {
if event.Type != watch.Modified && (objMeta.ResourceVersion == "" && event.Type != watch.Added) {
return true, fmt.Errorf("different kind of event appeared while waiting for Deployment modification: event: %#v", event)
}
return condition(event.Object.(*appsv1.Deployment))
})
if err != nil {
return nil, err
}
return event.Object.(*appsv1.Deployment), nil
}