-
Notifications
You must be signed in to change notification settings - Fork 48
/
status.go
289 lines (257 loc) · 10.7 KB
/
status.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Copyright (c) 2019-2022 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package controllers
import (
"context"
"fmt"
"net/url"
"sort"
"time"
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeclock "k8s.io/apimachinery/pkg/util/clock"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/controllers/workspace/metrics"
"github.com/devfile/devworkspace-operator/pkg/conditions"
)
const (
// devworkspacePhaseTerminating represents a DevWorkspace that has been deleted but is waiting on a finalizer.
// TODO: Should be moved to devfile/api side.
devworkspacePhaseTerminating dw.DevWorkspacePhase = "Terminating"
// devworkspacePhaseFailing represents a DevWorkspace that has encountered an unrecoverable error and is in
// the process of stopping.
devworkspacePhaseFailing dw.DevWorkspacePhase = "Failing"
// warningPresentInfoMessage is the info message printed
warningPresentInfoMessage string = "[warnings present]"
)
type currentStatus struct {
workspaceConditions
// Current workspace phase
phase dw.DevWorkspacePhase
}
// clock is used to set status condition timestamps.
// This variable makes it easier to test conditions.
var clock kubeclock.Clock = &kubeclock.RealClock{}
// updateWorkspaceStatus updates the current workspace's status field with conditions and phase from the passed in status.
// Parameters for result and error are returned unmodified, unless error is nil and another error is encountered while
// updating the status.
func (r *DevWorkspaceReconciler) updateWorkspaceStatus(workspace *common.DevWorkspaceWithConfig, logger logr.Logger, status *currentStatus, reconcileResult reconcile.Result, reconcileError error) (reconcile.Result, error) {
syncConditions(&workspace.Status, status)
oldPhase := workspace.Status.Phase
workspace.Status.Phase = status.phase
infoMessage := getInfoMessage(workspace, status)
if warn := conditions.GetConditionByType(workspace.Status.Conditions, conditions.DevWorkspaceWarning); warn != nil && warn.Status == corev1.ConditionTrue {
infoMessage = fmt.Sprintf("%s %s", warningPresentInfoMessage, infoMessage)
}
if workspace.Status.Message != infoMessage {
workspace.Status.Message = infoMessage
}
err := r.Status().Update(context.TODO(), workspace.DevWorkspace)
if err != nil {
if k8sErrors.IsConflict(err) {
logger.Info("Failed to update workspace status due to conflict; retrying")
} else {
logger.Info(fmt.Sprintf("Error updating workspace status: %s", err))
if reconcileError == nil {
reconcileError = err
}
}
} else {
updateMetricsForPhase(workspace, oldPhase, status.phase, logger)
}
return reconcileResult, reconcileError
}
func syncConditions(workspaceStatus *dw.DevWorkspaceStatus, currentStatus *currentStatus) {
currTransitionTime := metav1.Time{Time: clock.Now()}
// Set of conditions already set on the workspace
existingConditions := map[dw.DevWorkspaceConditionType]bool{}
for idx, workspaceCondition := range workspaceStatus.Conditions {
existingConditions[workspaceCondition.Type] = true
currCondition, ok := currentStatus.conditions[workspaceCondition.Type]
if !ok {
// Didn't observe this condition this time; set status to unknown
if workspaceCondition.Status != corev1.ConditionUnknown {
workspaceStatus.Conditions[idx].LastTransitionTime = currTransitionTime
workspaceStatus.Conditions[idx].Status = corev1.ConditionUnknown
workspaceStatus.Conditions[idx].Message = ""
workspaceStatus.Conditions[idx].Reason = ""
}
continue
}
// Update condition if needed
if workspaceCondition.Status != currCondition.Status || workspaceCondition.Message != currCondition.Message || workspaceCondition.Reason != currCondition.Reason {
workspaceStatus.Conditions[idx].LastTransitionTime = currTransitionTime
workspaceStatus.Conditions[idx].Status = currCondition.Status
workspaceStatus.Conditions[idx].Message = currCondition.Message
workspaceStatus.Conditions[idx].Reason = currCondition.Reason
}
}
// Check for conditions we need to add
for condType, cond := range currentStatus.conditions {
if existingConditions[condType] {
// Condition is already present and was updated (if necessary) above
continue
}
workspaceStatus.Conditions = append(workspaceStatus.Conditions, dw.DevWorkspaceCondition{
LastTransitionTime: currTransitionTime,
Type: condType,
Status: cond.Status,
Message: cond.Message,
Reason: cond.Reason,
})
}
// Sort conditions to avoid unnecessary updates
sort.SliceStable(workspaceStatus.Conditions, func(i, j int) bool {
return getConditionIndexInOrder(workspaceStatus.Conditions[i].Type) < getConditionIndexInOrder(workspaceStatus.Conditions[j].Type)
})
}
func syncWorkspaceMainURL(workspace *common.DevWorkspaceWithConfig, exposedEndpoints map[string]v1alpha1.ExposedEndpointList, clusterAPI sync.ClusterAPI) (ok bool, err error) {
mainUrl := getMainUrl(exposedEndpoints)
if workspace.Status.MainUrl == mainUrl {
return true, nil
}
workspace.Status.MainUrl = mainUrl
err = clusterAPI.Client.Status().Update(context.TODO(), workspace.DevWorkspace)
return false, err
}
func checkServerStatus(workspace *common.DevWorkspaceWithConfig) (ok bool, err error) {
mainUrl := workspace.Status.MainUrl
if mainUrl == "" {
// Support DevWorkspaces that do not specify an mainUrl
return true, nil
}
healthz, err := url.Parse(mainUrl)
if err != nil {
return false, err
}
healthz.Path = healthz.Path + "healthz"
resp, err := healthCheckHttpClient.Get(healthz.String())
if err != nil {
return false, err
}
if (resp.StatusCode / 100) == 4 {
// Assume endpoint is unimplemented and/or * is covered with authentication.
return true, nil
}
ok = (resp.StatusCode / 100) == 2
return ok, nil
}
func getMainUrl(exposedEndpoints map[string]v1alpha1.ExposedEndpointList) string {
for _, endpoints := range exposedEndpoints {
for _, endpoint := range endpoints {
if endpoint.Attributes.GetString(string(v1alpha1.TypeEndpointAttribute), nil) == string(v1alpha1.MainEndpointType) {
return endpoint.Url
}
}
}
return ""
}
func getInfoMessage(workspace *common.DevWorkspaceWithConfig, status *currentStatus) string {
// Check for errors and failure
if cond, ok := status.conditions[dw.DevWorkspaceError]; ok {
return cond.Message
}
if cond, ok := status.conditions[dw.DevWorkspaceFailedStart]; ok {
return cond.Message
}
switch workspace.Status.Phase {
case dw.DevWorkspaceStatusRunning:
if workspace.Status.MainUrl == "" {
return "Workspace is running"
}
return workspace.Status.MainUrl
case dw.DevWorkspaceStatusStopped, dw.DevWorkspaceStatusStopping:
return string(workspace.Status.Phase)
}
latestCondition := status.getFirstFalse()
if latestCondition != nil {
return latestCondition.Message
}
latestTrueCondition := status.getLastTrue()
if latestTrueCondition != nil {
return latestTrueCondition.Message
}
// No conditions are set but workspace is not running; unclear what value should be set.
return ""
}
// updateMetricsForPhase increments DevWorkspace startup metrics based on phase transitions in a DevWorkspace. It avoids
// incrementing the underlying metrics where possible (e.g. reconciling an already running workspace) by only incrementing
// counters when the new phase is different from the current on in the DevWorkspace.
func updateMetricsForPhase(workspace *common.DevWorkspaceWithConfig, oldPhase, newPhase dw.DevWorkspacePhase, logger logr.Logger) {
if oldPhase == newPhase {
return
}
switch newPhase {
case dw.DevWorkspaceStatusRunning:
metrics.WorkspaceRunning(workspace, logger)
case dw.DevWorkspaceStatusFailed:
metrics.WorkspaceFailed(workspace, logger)
}
}
// checkForStartTimeout checks if the provided workspace has not progressed for longer than the configured
// startup timeout. This is determined by checking to see if the last condition transition time is more
// than [timeout] duration ago. Workspaces that are not in the "Starting" phase cannot timeout. Returns
// an error with message when timeout is reached.
func checkForStartTimeout(workspace *common.DevWorkspaceWithConfig) error {
if workspace.Status.Phase != dw.DevWorkspaceStatusStarting {
return nil
}
timeout, err := time.ParseDuration(workspace.Config.Workspace.ProgressTimeout)
if err != nil {
return fmt.Errorf("invalid duration specified for timeout: %w", err)
}
currTime := clock.Now()
lastUpdateTime := time.Time{}
for _, condition := range workspace.Status.Conditions {
if condition.LastTransitionTime.Time.After(lastUpdateTime) {
lastUpdateTime = condition.LastTransitionTime.Time
}
}
if !lastUpdateTime.IsZero() && lastUpdateTime.Add(timeout).Before(currTime) {
return fmt.Errorf("devworkspace failed to progress past phase '%s' for longer than timeout (%s)",
workspace.Status.Phase, workspace.Config.Workspace.ProgressTimeout)
}
return nil
}
// checkForFailingTimeout checks that the current workspace has not been in the "Failing" state for longer than the
// configured progress timeout. If the workspace is not in the Failing state or does not have a DevWorkspaceFailed
// condition set, returns false. Otherwise, returns true if the workspace has timed out. Returns an error if
// timeout is configured with an unparsable duration.
func checkForFailingTimeout(workspace *common.DevWorkspaceWithConfig) (isTimedOut bool, err error) {
if workspace.Status.Phase != devworkspacePhaseFailing {
return false, nil
}
timeout, err := time.ParseDuration(workspace.Config.Workspace.ProgressTimeout)
if err != nil {
return false, fmt.Errorf("invalid duration specified for timeout: %w", err)
}
currTime := clock.Now()
failedTime := time.Time{}
for _, condition := range workspace.Status.Conditions {
if condition.Type == dw.DevWorkspaceFailedStart {
failedTime = condition.LastTransitionTime.Time
}
}
if !failedTime.IsZero() && failedTime.Add(timeout).Before(currTime) {
return true, nil
}
return false, nil
}