-
Notifications
You must be signed in to change notification settings - Fork 90
/
deployment.go
56 lines (45 loc) · 1.61 KB
/
deployment.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
package k8sutil
import (
"context"
"time"
"github.com/pkg/errors"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/pointer"
)
func WaitForDeploymentReady(ctx context.Context, clientset kubernetes.Interface, namespace string, deploymentName string, timeout time.Duration) error {
start := time.Now()
for {
d, err := clientset.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing deployment")
}
return nil
}
if d.Status.ObservedGeneration == d.ObjectMeta.Generation && d.Status.ReadyReplicas == *d.Spec.Replicas && d.Status.UnavailableReplicas == 0 {
return nil
}
time.Sleep(time.Second)
if time.Now().Sub(start) > timeout {
return &kotsadmtypes.ErrorTimeout{Message: "timeout waiting for deployment to become ready"}
}
}
}
func ScaleDownDeployment(ctx context.Context, clientset kubernetes.Interface, namespace string, deploymentName string) error {
d, err := clientset.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing deployment")
}
return nil
}
d.Spec.Replicas = pointer.Int32Ptr(0)
_, err = clientset.AppsV1().Deployments(namespace).Update(ctx, d, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update deployment")
}
return nil
}