/
deploy.go
98 lines (75 loc) · 2.43 KB
/
deploy.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package helm
import (
"fmt"
"strconv"
"time"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/covexo/devspace/pkg/devspace/kubectl"
"github.com/covexo/devspace/pkg/util/log"
)
// WaitForReleasePodToGetReady waits for the release pod to get ready
func WaitForReleasePodToGetReady(client *kubernetes.Clientset, releaseName, releaseNamespace string, releaseRevision int) (*k8sv1.Pod, error) {
for true {
time.Sleep(4 * time.Second)
podList, err := client.Core().Pods(releaseNamespace).List(metav1.ListOptions{
LabelSelector: "release=" + releaseName,
})
if err != nil {
log.Panicf("Unable to list devspace pods: %s", err.Error())
}
if len(podList.Items) > 0 {
highestRevision := 0
var selectedPod *k8sv1.Pod
for i, pod := range podList.Items {
if kubectl.GetPodStatus(&pod) == "Terminating" {
continue
}
podRevision, podHasRevision := pod.Annotations["revision"]
hasHigherRevision := (i == 0)
if !hasHigherRevision && podHasRevision {
podRevisionInt, _ := strconv.Atoi(podRevision)
if podRevisionInt > highestRevision {
hasHigherRevision = true
}
}
if hasHigherRevision {
selectedPod = &pod
highestRevision, _ = strconv.Atoi(podRevision)
}
}
if selectedPod != nil {
_, hasRevision := selectedPod.Annotations["revision"]
if !hasRevision || highestRevision == releaseRevision {
if !hasRevision {
log.Warn("Found pod without revision. Use annotation 'revision' for your pods to avoid this warning.")
}
err = waitForPodReady(client, selectedPod, 2*60*time.Second, 5*time.Second)
if err != nil {
return nil, fmt.Errorf("Error during waiting for pod: %s", err.Error())
}
return selectedPod, nil
}
log.Info("Waiting for release upgrade to complete.")
}
} else {
log.Info("Waiting for release to be deployed.")
}
}
return nil, nil
}
func waitForPodReady(kubectl *kubernetes.Clientset, pod *k8sv1.Pod, maxWaitTime time.Duration, checkInterval time.Duration) error {
for maxWaitTime > 0 {
pod, err := kubectl.Core().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{})
if err != nil {
return err
}
if len(pod.Status.ContainerStatuses) > 0 && pod.Status.ContainerStatuses[0].Ready {
return nil
}
time.Sleep(checkInterval)
maxWaitTime = maxWaitTime - checkInterval
}
return fmt.Errorf("Max wait time expired")
}