forked from openshift/origin
/
readiness.go
180 lines (146 loc) · 6.24 KB
/
readiness.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package controller
import (
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/apis/batch"
"k8s.io/kubernetes/pkg/apis/extensions"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
buildutil "github.com/openshift/origin/pkg/build/util"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
)
// checkBuildReadiness determins if a Build is ready, failed or neither.
func checkBuildReadiness(obj runtime.Object) (bool, bool, error) {
b := obj.(*buildapi.Build)
ready := buildutil.IsTerminalPhase(b.Status.Phase) &&
b.Status.Phase == buildapi.BuildPhaseComplete
failed := buildutil.IsTerminalPhase(b.Status.Phase) &&
b.Status.Phase != buildapi.BuildPhaseComplete
return ready, failed, nil
}
// checkBuildConfigReadiness determins if a BuildConfig is ready, failed or
// neither. TODO: this should be reported on the BuildConfig object itself.
func checkBuildConfigReadiness(oc buildclient.Interface, obj runtime.Object) (bool, bool, error) {
bc := obj.(*buildapi.BuildConfig)
builds, err := oc.Build().Builds(bc.Namespace).List(metav1.ListOptions{LabelSelector: buildutil.BuildConfigSelector(bc.Name).String()})
if err != nil {
return false, false, err
}
for _, build := range builds.Items {
if build.Annotations[buildapi.BuildNumberAnnotation] == strconv.FormatInt(bc.Status.LastVersion, 10) {
return checkBuildReadiness(&build)
}
}
return false, false, nil
}
// checkDeploymentReadiness determins if a Deployment is ready, failed or
// neither.
func checkDeploymentReadiness(obj runtime.Object) (bool, bool, error) {
d := obj.(*extensions.Deployment)
var progressing, available *extensions.DeploymentCondition
for i, condition := range d.Status.Conditions {
switch condition.Type {
case extensions.DeploymentProgressing:
progressing = &d.Status.Conditions[i]
case extensions.DeploymentAvailable:
available = &d.Status.Conditions[i]
}
}
ready := d.Status.ObservedGeneration == d.Generation &&
progressing != nil &&
progressing.Status == kapi.ConditionTrue &&
progressing.Reason == deploymentutil.NewRSAvailableReason &&
available != nil &&
available.Status == kapi.ConditionTrue
failed := d.Status.ObservedGeneration == d.Generation &&
progressing != nil &&
progressing.Status == kapi.ConditionFalse
return ready, failed, nil
}
// checkDeploymentConfigReadiness determins if a DeploymentConfig is ready,
// failed or neither.
func checkDeploymentConfigReadiness(obj runtime.Object) (bool, bool, error) {
dc := obj.(*deployapi.DeploymentConfig)
var progressing, available *deployapi.DeploymentCondition
for i, condition := range dc.Status.Conditions {
switch condition.Type {
case deployapi.DeploymentProgressing:
progressing = &dc.Status.Conditions[i]
case deployapi.DeploymentAvailable:
available = &dc.Status.Conditions[i]
}
}
ready := dc.Status.ObservedGeneration == dc.Generation &&
progressing != nil &&
progressing.Status == kapi.ConditionTrue &&
progressing.Reason == deployapi.NewRcAvailableReason &&
available != nil &&
available.Status == kapi.ConditionTrue
failed := dc.Status.ObservedGeneration == dc.Generation &&
progressing != nil &&
progressing.Status == kapi.ConditionFalse
return ready, failed, nil
}
// checkJobReadiness determins if a Job is ready, failed or neither.
func checkJobReadiness(obj runtime.Object) (bool, bool, error) {
job := obj.(*batch.Job)
ready := job.Status.CompletionTime != nil
failed := job.Status.Failed > 0
return ready, failed, nil
}
// checkStatefulSetReadiness determins if a StatefulSet is ready, failed or
// neither.
func checkStatefulSetReadiness(obj runtime.Object) (bool, bool, error) {
ss := obj.(*apps.StatefulSet)
ready := ss.Status.ObservedGeneration != nil &&
*ss.Status.ObservedGeneration == ss.Generation &&
ss.Status.ReadyReplicas == ss.Spec.Replicas
failed := false
return ready, failed, nil
}
// checkRouteReadiness checks if host field was prepopulated already.
func checkRouteReadiness(obj runtime.Object) (bool, bool, error) {
route := obj.(*routeapi.Route)
ready := route.Spec.Host != ""
failed := false
return ready, failed, nil
}
// readinessCheckers maps GroupKinds to the appropriate function. Note that in
// some cases more than one GK maps to the same function.
var readinessCheckers = map[schema.GroupKind]func(runtime.Object) (bool, bool, error){
buildapi.LegacyKind("Build"): checkBuildReadiness,
buildapi.Kind("Build"): checkBuildReadiness,
apps.Kind("Deployment"): checkDeploymentReadiness,
extensions.Kind("Deployment"): checkDeploymentReadiness,
deployapi.LegacyKind("DeploymentConfig"): checkDeploymentConfigReadiness,
deployapi.Kind("DeploymentConfig"): checkDeploymentConfigReadiness,
batch.Kind("Job"): checkJobReadiness,
apps.Kind("StatefulSet"): checkStatefulSetReadiness,
routeapi.Kind("Route"): checkRouteReadiness,
routeapi.LegacyKind("Route"): checkRouteReadiness,
}
// CanCheckReadiness indicates whether a readiness check exists for a GK.
func CanCheckReadiness(ref kapi.ObjectReference) bool {
if ref.GroupVersionKind().GroupKind() == buildapi.LegacyKind("BuildConfig") ||
ref.GroupVersionKind().GroupKind() == buildapi.Kind("BuildConfig") {
return true
}
_, found := readinessCheckers[ref.GroupVersionKind().GroupKind()]
return found
}
// CheckReadiness runs the readiness check on a given object. TODO: remove
// "oc client.Interface" and error once BuildConfigs can report on the status of
// their latest build.
func CheckReadiness(oc buildclient.Interface, ref kapi.ObjectReference, obj runtime.Object) (bool, bool, error) {
if ref.GroupVersionKind().GroupKind() == buildapi.LegacyKind("BuildConfig") ||
ref.GroupVersionKind().GroupKind() == buildapi.Kind("BuildConfig") {
return checkBuildConfigReadiness(oc, obj)
}
return readinessCheckers[ref.GroupVersionKind().GroupKind()](obj)
}