-
Notifications
You must be signed in to change notification settings - Fork 240
/
health_deployment.go
78 lines (72 loc) · 2.99 KB
/
health_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package health
import (
"fmt"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func getDeploymentHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
gvk := obj.GroupVersionKind()
switch gvk {
case appsv1.SchemeGroupVersion.WithKind(kube.DeploymentKind):
var deployment appsv1.Deployment
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deployment)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured Deployment to typed: %v", err)
}
return getAppsv1DeploymentHealth(&deployment)
default:
return nil, fmt.Errorf("unsupported Deployment GVK: %s", gvk)
}
}
func getAppsv1DeploymentHealth(deployment *appsv1.Deployment) (*HealthStatus, error) {
if deployment.Spec.Paused {
return &HealthStatus{
Status: HealthStatusSuspended,
Message: "Deployment is paused",
}, nil
}
// Borrowed at kubernetes/kubectl/rollout_status.go https://github.com/kubernetes/kubernetes/blob/5232ad4a00ec93942d0b2c6359ee6cd1201b46bc/pkg/kubectl/rollout_status.go#L80
if deployment.Generation <= deployment.Status.ObservedGeneration {
cond := getAppsv1DeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
if cond != nil && cond.Reason == "ProgressDeadlineExceeded" {
return &HealthStatus{
Status: HealthStatusDegraded,
Message: fmt.Sprintf("Deployment %q exceeded its progress deadline", deployment.Name),
}, nil
} else if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d out of %d new replicas have been updated...", deployment.Status.UpdatedReplicas, *deployment.Spec.Replicas),
}, nil
} else if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d old replicas are pending termination...", deployment.Status.Replicas-deployment.Status.UpdatedReplicas),
}, nil
} else if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d of %d updated replicas are available...", deployment.Status.AvailableReplicas, deployment.Status.UpdatedReplicas),
}, nil
}
} else {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: "Waiting for rollout to finish: observed deployment generation less then desired generation",
}, nil
}
return &HealthStatus{
Status: HealthStatusHealthy,
}, nil
}
func getAppsv1DeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}