/
handlers.go
75 lines (60 loc) · 2.08 KB
/
handlers.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
package watcher
import (
"context"
"github.com/onspaceship/agent/pkg/config"
"github.com/apex/log"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (w *Watcher) onUpdate(_, obj interface{}) {
deployment := obj.(*appsv1.Deployment)
if deliveryId, ok := deployment.Annotations[config.DeliveryIdAnnotation]; !ok {
log.WithField("deployment", deployment.Name).Info("No delivery ID found")
} else {
// Don't process the wrong deployment revision
if deployment.Annotations[config.KubernetesRevisionAnnotation] != deployment.Annotations[config.DeliveryRevisionAnnotation] {
return
}
status, err := w.getDeploymentStatus(deployment)
if err != nil {
log.WithError(err).Error("Problem getting deployment status")
return
}
log.
WithField("deployment", deployment.Name).
WithField("delivery", deliveryId).
WithField("status", status).
Info("Updating delivery status with Core")
w.Core.DeliveryUpdate(deliveryId, status)
}
}
func (w *Watcher) getDeploymentStatus(deployment *appsv1.Deployment) (string, error) {
if deployment.Status.Replicas != deployment.Status.AvailableReplicas {
return "deploying", nil
}
replicaSets, err := w.getReplicaSetsForDeployment(deployment)
currentRev := deployment.Annotations[config.KubernetesRevisionAnnotation]
for _, replicaSet := range replicaSets.Items {
if replicaSet.Annotations[config.KubernetesRevisionAnnotation] == currentRev {
if replicaSet.Status.Replicas != *deployment.Spec.Replicas || replicaSet.Status.AvailableReplicas < replicaSet.Status.Replicas {
return "deploying", nil
} else {
return "complete", nil
}
}
}
return "error", err
}
func (w *Watcher) getReplicaSetsForDeployment(deployment *appsv1.Deployment) (*appsv1.ReplicaSetList, error) {
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
rsList, err := w.Client.AppsV1().ReplicaSets(deployment.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
return nil, err
}
return rsList, nil
}