-
Notifications
You must be signed in to change notification settings - Fork 553
Expand file tree
/
Copy pathdelete.go
More file actions
102 lines (87 loc) · 2.85 KB
/
delete.go
File metadata and controls
102 lines (87 loc) · 2.85 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
package daemonclient
import (
"context"
"fmt"
"time"
clientpkg "github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/client/clientimplementation"
"github.com/loft-sh/devpod/pkg/platform"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
func (c *client) Delete(ctx context.Context, opt clientpkg.DeleteOptions) error {
c.m.Lock()
defer c.m.Unlock()
baseClient, err := c.initPlatformClient(ctx)
if err != nil {
return err
}
workspace, err := platform.FindInstance(ctx, baseClient, c.workspace.UID)
if err != nil {
return err
} else if workspace == nil {
// delete the workspace folder
err = clientimplementation.DeleteWorkspaceFolder(c.workspace.Context, c.workspace.ID, c.workspace.SSHConfigPath, c.workspace.SSHConfigIncludePath, c.log)
if err != nil {
return err
}
return nil
}
managementClient, err := baseClient.Management()
if err != nil {
return err
}
var gracePeriod *time.Duration
if opt.GracePeriod != "" {
duration, err := time.ParseDuration(opt.GracePeriod)
if err == nil {
gracePeriod = &duration
}
}
// kill the command after the grace period
if gracePeriod != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, *gracePeriod)
defer cancel()
}
// delete the workspace
err = managementClient.Loft().ManagementV1().DevPodWorkspaceInstances(workspace.Namespace).Delete(ctx, workspace.Name, metav1.DeleteOptions{})
if err != nil {
if !opt.Force {
return fmt.Errorf("delete workspace: %w", err)
}
if !kerrors.IsNotFound(err) {
c.log.Errorf("Error deleting workspace: %v", err)
}
}
// delete the workspace folder
err = clientimplementation.DeleteWorkspaceFolder(c.workspace.Context, c.workspace.ID, c.workspace.SSHConfigPath, c.workspace.SSHConfigIncludePath, c.log)
if err != nil {
return err
}
// calculate wait timeout
waitTimeout := time.Minute
if gracePeriod != nil {
waitTimeout = *gracePeriod
}
// wait until the workspace is deleted
c.log.Debugf("Waiting for workspace to get deleted...")
err = wait.PollUntilContextTimeout(ctx, time.Second, waitTimeout, false, func(ctx context.Context) (done bool, err error) {
workspaceInstance, err := managementClient.Loft().ManagementV1().DevPodWorkspaceInstances(workspace.Namespace).Get(ctx, workspace.Name, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return true, nil
} else if err != nil {
return false, fmt.Errorf("error getting workspace: %w", err)
} else if workspaceInstance.ObjectMeta.DeletionTimestamp == nil {
// this can occur if the workspace is already deleted and was recreated
return true, nil
}
c.log.Debugf("Workspace is not deleted yet, waiting again...")
return false, nil
})
if err != nil {
return fmt.Errorf("error waiting for workspace to get deleted: %w", err)
}
return nil
}