Skip to content

Commit 08537ae

Browse files
committed
Refactor last activity to be consistent acrtoss the controllers
1 parent 1ef3f80 commit 08537ae

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

components/ws-manager-mk2/controllers/timeout_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
corev1 "k8s.io/api/core/v1"
1313
apierrors "k8s.io/apimachinery/pkg/api/errors"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1514
"k8s.io/apimachinery/pkg/types"
1615
"k8s.io/client-go/tools/record"
1716
"k8s.io/client-go/util/retry"
@@ -21,6 +20,7 @@ import (
2120
"sigs.k8s.io/controller-runtime/pkg/log"
2221

2322
"github.com/gitpod-io/gitpod/common-go/util"
23+
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/activity"
2424
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
2525
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
2626
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
@@ -155,7 +155,7 @@ func (r *TimeoutReconciler) isWorkspaceTimedOut(ws *workspacev1.Workspace) (reas
155155
}
156156

157157
start := ws.ObjectMeta.CreationTimestamp.Time
158-
lastActivity := ws.Status.LastActivity
158+
lastActivity := activity.Last(ws)
159159
isClosed := ws.IsConditionTrue(workspacev1.WorkspaceConditionClosed)
160160

161161
switch phase {
@@ -187,8 +187,7 @@ func (r *TimeoutReconciler) isWorkspaceTimedOut(ws *workspacev1.Workspace) (reas
187187
activity := activityNone
188188
if ws.IsHeadless() {
189189
timeout = timeouts.HeadlessWorkspace
190-
nt := metav1.NewTime(start)
191-
lastActivity = &nt
190+
lastActivity = &start
192191
activity = activityRunningHeadless
193192
} else if lastActivity == nil {
194193
// The workspace is up and running, but the user has never produced any activity
@@ -202,13 +201,13 @@ func (r *TimeoutReconciler) isWorkspaceTimedOut(ws *workspacev1.Workspace) (reas
202201
return ""
203202
}
204203
}
205-
return decide(lastActivity.Time, afterClosed, activityClosed)
204+
return decide(*lastActivity, afterClosed, activityClosed)
206205
}()
207206
if reason != "" {
208207
return reason
209208
}
210209
}
211-
return decide(lastActivity.Time, timeout, activity)
210+
return decide(*lastActivity, timeout, activity)
212211

213212
case workspacev1.WorkspacePhaseStopping:
214213
if isWorkspaceBeingDeleted(ws) && !ws.IsConditionTrue(workspacev1.WorkspaceConditionBackupComplete) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package activity
6+
7+
import (
8+
"time"
9+
10+
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
11+
)
12+
13+
func Last(ws *workspacev1.Workspace) *time.Time {
14+
lastActivity := ws.Status.LastActivity
15+
if lastActivity != nil {
16+
return &lastActivity.Time
17+
}
18+
19+
return nil
20+
}

components/ws-manager-mk2/service/manager.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/gitpod-io/gitpod/common-go/tracing"
4141
"github.com/gitpod-io/gitpod/common-go/util"
4242
csapi "github.com/gitpod-io/gitpod/content-service/api"
43+
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/activity"
4344
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
4445
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
4546
"github.com/gitpod-io/gitpod/ws-manager/api/config"
@@ -464,7 +465,7 @@ func (wsm *WorkspaceManagerServer) DescribeWorkspace(ctx context.Context, req *w
464465
Status: wsm.extractWorkspaceStatus(&ws),
465466
}
466467

467-
lastActivity := getLastActivity(&ws)
468+
lastActivity := activity.Last(&ws)
468469
if lastActivity != nil {
469470
result.LastActivity = lastActivity.UTC().Format(time.RFC3339Nano)
470471
}
@@ -1485,7 +1486,7 @@ func (wav *workspaceActivityVec) getWorkspaceActivityCounts() (active, notActive
14851486
continue
14861487
}
14871488

1488-
hasActivity := getLastActivity(&ws) != nil
1489+
hasActivity := activity.Last(&ws) != nil
14891490
if hasActivity {
14901491
active++
14911492
} else {
@@ -1495,21 +1496,3 @@ func (wav *workspaceActivityVec) getWorkspaceActivityCounts() (active, notActive
14951496

14961497
return
14971498
}
1498-
1499-
func getLastActivity(ws *workspacev1.Workspace) *time.Time {
1500-
lastActivity := ws.Status.LastActivity
1501-
if lastActivity != nil {
1502-
return &lastActivity.Time
1503-
}
1504-
1505-
// In case we don't have a record of the workspace's last activity, check for the FirstUserActivity condition
1506-
// to see if the lastActivity got lost on a manager restart.
1507-
if ws.IsConditionTrue(workspacev1.WorkspaceConditionFirstUserActivity) {
1508-
now := time.Now().UTC()
1509-
lastActivityStatus := metav1.NewTime(now)
1510-
return &lastActivityStatus.Time
1511-
}
1512-
1513-
// If the FirstUserActivity condition isn't present we know that the workspace has never had user activity.
1514-
return nil
1515-
}

0 commit comments

Comments
 (0)