forked from openshift/origin
/
readiness.go
202 lines (167 loc) · 6.91 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package controller
import (
"strconv"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/apis/batch"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/extensions"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
oapps "github.com/openshift/api/apps"
"github.com/openshift/api/build"
"github.com/openshift/api/route"
"github.com/openshift/origin/pkg/api/legacy"
appsapi "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.(*appsapi.DeploymentConfig)
var progressing, available *appsapi.DeploymentCondition
for i, condition := range dc.Status.Conditions {
switch condition.Type {
case appsapi.DeploymentProgressing:
progressing = &dc.Status.Conditions[i]
case appsapi.DeploymentAvailable:
available = &dc.Status.Conditions[i]
}
}
ready := dc.Status.ObservedGeneration == dc.Generation &&
progressing != nil &&
progressing.Status == kapi.ConditionTrue &&
progressing.Reason == appsapi.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){
build.Kind("Build"): checkBuildReadiness,
apps.Kind("Deployment"): checkDeploymentReadiness,
extensions.Kind("Deployment"): checkDeploymentReadiness,
oapps.Kind("DeploymentConfig"): checkDeploymentConfigReadiness,
batch.Kind("Job"): checkJobReadiness,
apps.Kind("StatefulSet"): checkStatefulSetReadiness,
route.Kind("Route"): checkRouteReadiness,
legacy.Kind("Build"): checkBuildReadiness,
legacy.Kind("DeploymentConfig"): checkDeploymentConfigReadiness,
legacy.Kind("Route"): checkRouteReadiness,
}
// CanCheckReadiness indicates whether a readiness check exists for a GK.
func CanCheckReadiness(ref corev1.ObjectReference) bool {
switch ref.GroupVersionKind().GroupKind() {
case build.Kind("BuildConfig"), schema.GroupKind{Group: "", 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 corev1.ObjectReference, obj *unstructured.Unstructured) (bool, bool, error) {
castObj, err := legacyscheme.Scheme.New(ref.GroupVersionKind())
if err != nil {
return false, false, err
}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, castObj); err != nil {
return false, false, err
}
internalObj, err := legacyscheme.Scheme.New(ref.GroupVersionKind().GroupKind().WithVersion(runtime.APIVersionInternal))
if err != nil {
return false, false, err
}
if err := legacyscheme.Scheme.Convert(castObj, internalObj, nil); err != nil {
return false, false, err
}
switch ref.GroupVersionKind().GroupKind() {
case build.Kind("BuildConfig"), schema.GroupKind{Group: "", Kind: "BuildConfig"}:
return checkBuildConfigReadiness(oc, internalObj)
}
return readinessCheckers[ref.GroupVersionKind().GroupKind()](internalObj)
}