forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlist.go
More file actions
347 lines (298 loc) · 10.5 KB
/
list.go
File metadata and controls
347 lines (298 loc) · 10.5 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package workspace
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"slices"
"strconv"
"strings"
"sync"
managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
storagev1 "github.com/loft-sh/api/v4/pkg/apis/storage/v1"
"github.com/sirupsen/logrus"
"github.com/skevetter/devpod/pkg/client/clientimplementation"
"github.com/skevetter/devpod/pkg/config"
daemon "github.com/skevetter/devpod/pkg/daemon/platform"
"github.com/skevetter/devpod/pkg/platform"
providerpkg "github.com/skevetter/devpod/pkg/provider"
"github.com/skevetter/devpod/pkg/types"
"github.com/skevetter/log"
)
const ProjectLabel = "loft.sh/project"
func List(ctx context.Context, devPodConfig *config.Config, skipPro bool, owner platform.OwnerFilter, log log.Logger) ([]*providerpkg.Workspace, error) {
// list local workspaces
localWorkspaces, err := ListLocalWorkspaces(devPodConfig.DefaultContext, skipPro, log)
if err != nil {
return nil, err
}
proWorkspaces := []*providerpkg.Workspace{}
if !skipPro {
// list remote workspaces
proWorkspaceResults, err := listProWorkspaces(ctx, devPodConfig, owner, log)
if err != nil {
return nil, err
}
// extract pure workspace list first
for _, result := range proWorkspaceResults {
proWorkspaces = append(proWorkspaces, result.workspaces...)
}
// Check if every local file based workspace has a remote counterpart
// If not, delete it
// However, we need to differentiate between workspaces that are legitimately not available anymore
// and the ones where we were temporarily not able to reach the host
cleanedLocalWorkspaces := []*providerpkg.Workspace{}
for _, localWorkspace := range localWorkspaces {
if localWorkspace.IsPro() {
if shouldDeleteLocalWorkspace(ctx, localWorkspace, proWorkspaceResults) {
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
Context: devPodConfig.DefaultContext,
WorkspaceID: localWorkspace.ID,
SSHConfigPath: localWorkspace.SSHConfigPath,
SSHConfigIncludePath: localWorkspace.SSHConfigIncludePath,
}, log)
if err != nil {
log.Debugf("failed to delete local workspace %s: %v", localWorkspace.ID, err)
}
continue
}
}
cleanedLocalWorkspaces = append(cleanedLocalWorkspaces, localWorkspaces...)
}
localWorkspaces = cleanedLocalWorkspaces
}
// Set indexed by UID for deduplication
workspaces := map[string]*providerpkg.Workspace{}
// set local workspaces
for _, workspace := range localWorkspaces {
workspaces[workspace.UID] = workspace
}
// merge pro into local with pro taking precedence if UID matches
for _, proWorkspace := range proWorkspaces {
localWorkspace, ok := workspaces[proWorkspace.UID]
if ok {
// we want to use the local workspace IDE configuration
proWorkspace.IDE = localWorkspace.IDE
}
workspaces[proWorkspace.UID] = proWorkspace
}
retWorkspaces := []*providerpkg.Workspace{}
for _, v := range workspaces {
retWorkspaces = append(retWorkspaces, v)
}
return retWorkspaces, nil
}
func ListLocalWorkspaces(contextName string, skipPro bool, log log.Logger) ([]*providerpkg.Workspace, error) {
workspaceDir, err := providerpkg.GetWorkspacesDir(contextName)
if err != nil {
return nil, err
}
entries, err := os.ReadDir(workspaceDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
retWorkspaces := []*providerpkg.Workspace{}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), ".") {
continue
}
workspaceConfig, err := providerpkg.LoadWorkspaceConfig(contextName, entry.Name())
if err != nil {
log.WithFields(logrus.Fields{"workspace": entry.Name(), "error": err}).Warn("could not load workspace")
continue
}
if skipPro && workspaceConfig.IsPro() {
continue
}
retWorkspaces = append(retWorkspaces, workspaceConfig)
}
return retWorkspaces, nil
}
type listProWorkspacesResult struct {
workspaces []*providerpkg.Workspace
err error
}
func listProWorkspaces(ctx context.Context, devPodConfig *config.Config, owner platform.OwnerFilter, log log.Logger) (map[string]listProWorkspacesResult, error) {
results := map[string]listProWorkspacesResult{}
// lock around `results`
var mu sync.Mutex
wg := sync.WaitGroup{}
for provider, providerContextConfig := range devPodConfig.Current().Providers {
if !providerContextConfig.Initialized {
continue
}
providerConfig, err := providerpkg.LoadProviderConfig(devPodConfig.DefaultContext, provider)
if err != nil {
log.WithFields(logrus.Fields{"provider": provider, "error": err}).Warn("load provider config for provider")
continue
}
// only get pro providers
if !providerConfig.IsProxyProvider() && !providerConfig.IsDaemonProvider() {
continue
}
wg.Go(func() {
workspaces, err := listProWorkspacesForProvider(ctx, devPodConfig, provider, providerConfig, owner, log)
mu.Lock()
defer mu.Unlock()
results[provider] = listProWorkspacesResult{
workspaces: workspaces,
err: err,
}
})
}
wg.Wait()
return results, nil
}
func listProWorkspacesForProvider(ctx context.Context, devPodConfig *config.Config, provider string, providerConfig *providerpkg.ProviderConfig, owner platform.OwnerFilter, log log.Logger) ([]*providerpkg.Workspace, error) {
var (
instances []managementv1.DevPodWorkspaceInstance
err error
)
if providerConfig.IsProxyProvider() {
instances, err = listInstancesProxyProvider(ctx, devPodConfig, provider, providerConfig, log)
} else if providerConfig.IsDaemonProvider() {
instances, err = listInstancesDaemonProvider(ctx, provider, owner, log)
} else {
return nil, fmt.Errorf("cannot list pro workspaces with provider %s", provider)
}
if err != nil {
if log.GetLevel() >= logrus.DebugLevel {
log.Warnf("Failed to list pro workspaces for provider %s: %v", provider, err)
}
return nil, err
}
retWorkspaces := []*providerpkg.Workspace{}
for _, instance := range instances {
if instance.GetLabels() == nil {
log.Debugf("no labels for pro workspace \"%s\" found, skipping", instance.GetName())
continue
}
// id
id := instance.GetLabels()[storagev1.DevPodWorkspaceIDLabel]
if id == "" {
log.Debugf("no ID label for pro workspace \"%s\" found, skipping", instance.GetName())
continue
}
// uid
uid := instance.GetLabels()[storagev1.DevPodWorkspaceUIDLabel]
if uid == "" {
log.Debugf("no UID label for pro workspace \"%s\" found, skipping", instance.GetName())
continue
}
// project
projectName := instance.GetLabels()[ProjectLabel]
// source
source := providerpkg.WorkspaceSource{}
if instance.Annotations != nil && instance.Annotations[storagev1.DevPodWorkspaceSourceAnnotation] != "" {
// source to workspace config source
rawSource := instance.Annotations[storagev1.DevPodWorkspaceSourceAnnotation]
s := providerpkg.ParseWorkspaceSource(rawSource)
if s == nil {
log.WithFields(logrus.Fields{"source": rawSource}).Warn("unable to parse workspace source")
} else {
source = *s
}
}
// last used timestamp
var lastUsedTimestamp types.Time
sleepModeConfig := instance.Status.SleepModeConfig
if sleepModeConfig != nil {
lastUsedTimestamp = types.Unix(sleepModeConfig.Status.LastActivity, 0)
} else {
var ts int64
if instance.Annotations != nil {
if val, ok := instance.Annotations["sleepmode.loft.sh/last-activity"]; ok {
var err error
if ts, err = strconv.ParseInt(val, 10, 64); err != nil {
log.Warn("received invalid sleepmode.loft.sh/last-activity from ", instance.GetName())
}
}
}
lastUsedTimestamp = types.Unix(ts, 0)
}
// creation timestamp
creationTimestamp := types.Time{}
if !instance.CreationTimestamp.IsZero() {
creationTimestamp = types.NewTime(instance.CreationTimestamp.Time)
}
workspace := providerpkg.Workspace{
ID: id,
UID: uid,
Context: devPodConfig.DefaultContext,
Source: source,
Provider: providerpkg.WorkspaceProviderConfig{
Name: provider,
},
LastUsedTimestamp: lastUsedTimestamp,
CreationTimestamp: creationTimestamp,
Pro: &providerpkg.ProMetadata{
InstanceName: instance.GetName(),
Project: projectName,
DisplayName: instance.Spec.DisplayName,
},
}
retWorkspaces = append(retWorkspaces, &workspace)
}
return retWorkspaces, nil
}
func shouldDeleteLocalWorkspace(ctx context.Context, localWorkspace *providerpkg.Workspace, proWorkspaceResults map[string]listProWorkspacesResult) bool {
// get the correct result for this local workspace
res, ok := proWorkspaceResults[localWorkspace.Provider.Name]
if !ok {
return false
}
// Don't delete the workspace if we encountered any error fetching the remote workspaces.
// This could potentially be destructive so we err or the side of caution and only allow
// deletion if fetching the remote workspace was successful
if res.err != nil {
return false
}
if localWorkspace.Imported {
// does remote still exist?
if ok := checkInstanceExists(ctx, localWorkspace); ok {
return false
}
}
hasProCounterpart := slices.ContainsFunc(res.workspaces, func(w *providerpkg.Workspace) bool {
return localWorkspace.UID == w.UID
})
return !hasProCounterpart
}
func listInstancesProxyProvider(ctx context.Context, devPodConfig *config.Config, provider string, providerConfig *providerpkg.ProviderConfig, log log.Logger) ([]managementv1.DevPodWorkspaceInstance, error) {
opts := devPodConfig.ProviderOptions(provider)
opts[providerpkg.LOFT_FILTER_BY_OWNER] = config.OptionValue{Value: "true"}
var stdout bytes.Buffer
var stderr bytes.Buffer
if err := clientimplementation.RunCommandWithBinaries(
ctx,
"listWorkspaces",
providerConfig.Exec.Proxy.List.Workspaces,
devPodConfig.DefaultContext,
nil,
nil,
opts,
providerConfig,
nil, nil, &stdout, &stderr, log,
); err != nil {
return nil, fmt.Errorf("failed to list pro workspaces: %s %w", stderr.String(), err)
}
if stdout.Len() == 0 {
return nil, nil
}
instances := []managementv1.DevPodWorkspaceInstance{}
if err := json.Unmarshal(stdout.Bytes(), &instances); err != nil {
return nil, err
}
return instances, nil
}
func listInstancesDaemonProvider(ctx context.Context, provider string, owner platform.OwnerFilter, log log.Logger) ([]managementv1.DevPodWorkspaceInstance, error) {
return daemon.NewLocalClient(provider).ListWorkspaces(ctx, owner)
}
func checkInstanceExists(ctx context.Context, workspace *providerpkg.Workspace) bool {
instance, err := daemon.NewLocalClient(workspace.Provider.Name).GetWorkspace(ctx, workspace.UID)
if err != nil || instance == nil {
return false
}
return true
}