Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit a58a2e7

Browse files
author
Faris Huskovic
authored
feat: implement cordon/uncordon workspace provider (#302)
* feat: implement cordon/uncordon workspace provider
1 parent 60ac4a4 commit a58a2e7

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Makefile for Coder CLI
22

3-
.PHONY: clean build build/macos build/windows build/linux fmt lint gendocs test/go
3+
.PHONY: clean build build/macos build/windows build/linux fmt lint gendocs test/go dev
44

55
PROJECT_ROOT := $(shell git rev-parse --show-toplevel)
66
MAKE_ROOT := $(shell pwd)
@@ -42,3 +42,10 @@ test/coverage:
4242
$$(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
4343

4444
goveralls -coverprofile=coverage -service=github
45+
46+
dev: build/linux
47+
@echo "removing project root binary if exists"
48+
-rm ./coder
49+
@echo "untarring..."
50+
@tar -xzf ./ci/bin/coder-cli-linux-amd64.tar.gz
51+
@echo "new dev binary ready"

coder

12.6 MB
Binary file not shown.

coder-sdk/interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,11 @@ type Client interface {
225225

226226
// BaseURL returns the BaseURL configured for this Client.
227227
BaseURL() url.URL
228+
229+
// CordonWorkspaceProvider prevents the provider from having any more workspaces placed on it.
230+
CordonWorkspaceProvider(ctx context.Context, id, reason string) error
231+
232+
// UnCordonWorkspaceProvider changes an existing cordoned providers status to 'Ready';
233+
// allowing it to continue creating new workspaces and provisioning resources for them.
234+
UnCordonWorkspaceProvider(ctx context.Context, id string) error
228235
}

coder-sdk/workspace_providers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,28 @@ func (c *DefaultClient) CreateWorkspaceProvider(ctx context.Context, req CreateW
9898
func (c *DefaultClient) DeleteWorkspaceProviderByID(ctx context.Context, id string) error {
9999
return c.requestBody(ctx, http.MethodDelete, "/api/private/resource-pools/"+id, nil, nil)
100100
}
101+
102+
// CordoneWorkspaceProviderReq defines the request parameters for creating a new workspace provider entity.
103+
type CordoneWorkspaceProviderReq struct {
104+
Reason string `json:"reason"`
105+
}
106+
107+
// CordonWorkspaceProvider prevents the provider from having any more workspaces placed on it.
108+
func (c *DefaultClient) CordonWorkspaceProvider(ctx context.Context, id, reason string) error {
109+
req := CordoneWorkspaceProviderReq{Reason: reason}
110+
err := c.requestBody(ctx, http.MethodPost, "/api/private/resource-pools/"+id+"/cordon", req, nil)
111+
if err != nil {
112+
return err
113+
}
114+
return nil
115+
}
116+
117+
// UnCordonWorkspaceProvider changes an existing cordoned providers status to 'Ready';
118+
// allowing it to continue creating new workspaces and provisioning resources for them.
119+
func (c *DefaultClient) UnCordonWorkspaceProvider(ctx context.Context, id string) error {
120+
err := c.requestBody(ctx, http.MethodPost, "/api/private/resource-pools/"+id+"/uncordon", nil, nil)
121+
if err != nil {
122+
return err
123+
}
124+
return nil
125+
}

internal/cmd/providers.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/url"
66

7+
"cdr.dev/coder-cli/internal/coderutil"
78
"cdr.dev/coder-cli/internal/x/xcobra"
89

910
"github.com/spf13/cobra"
@@ -26,6 +27,8 @@ func providersCmd() *cobra.Command {
2627
createProviderCmd(),
2728
listProviderCmd(),
2829
deleteProviderCmd(),
30+
cordonProviderCmd(),
31+
unCordonProviderCmd(),
2932
)
3033
return cmd
3134
}
@@ -215,3 +218,69 @@ coder providers rm my-workspace-provider`,
215218
}
216219
return cmd
217220
}
221+
222+
func cordonProviderCmd() *cobra.Command {
223+
var reason string
224+
225+
cmd := &cobra.Command{
226+
Use: "cordon [workspace_provider_name]",
227+
Args: xcobra.ExactArgs(1),
228+
Short: "cordon a workspace provider.",
229+
Long: "Prevent an existing Coder workspace provider from supporting any additional workspaces.",
230+
Example: `# cordon an existing workspace provider by name
231+
coder providers cordon my-workspace-provider --reason "limit cloud clost"`,
232+
RunE: func(cmd *cobra.Command, args []string) error {
233+
ctx := cmd.Context()
234+
client, err := newClient(ctx)
235+
if err != nil {
236+
return err
237+
}
238+
239+
wpName := args[0]
240+
provider, err := coderutil.ProviderByName(ctx, client, wpName)
241+
if err != nil {
242+
return err
243+
}
244+
245+
if err := client.CordonWorkspaceProvider(ctx, provider.ID, reason); err != nil {
246+
return err
247+
}
248+
clog.LogSuccess(fmt.Sprintf("provider %q successfully cordoned - you can no longer create workspaces on this provider without uncordoning first", wpName))
249+
return nil
250+
},
251+
}
252+
cmd.Flags().StringVar(&reason, "reason", "", "reason for cordoning the provider")
253+
_ = cmd.MarkFlagRequired("reason")
254+
return cmd
255+
}
256+
257+
func unCordonProviderCmd() *cobra.Command {
258+
cmd := &cobra.Command{
259+
Use: "uncordon [workspace_provider_name]",
260+
Args: xcobra.ExactArgs(1),
261+
Short: "uncordon a workspace provider.",
262+
Long: "Set a currently cordoned provider as ready; enabling it to continue provisioning resources for new workspaces.",
263+
Example: `# uncordon an existing workspace provider by name
264+
coder providers uncordon my-workspace-provider`,
265+
RunE: func(cmd *cobra.Command, args []string) error {
266+
ctx := cmd.Context()
267+
client, err := newClient(ctx)
268+
if err != nil {
269+
return err
270+
}
271+
272+
wpName := args[0]
273+
provider, err := coderutil.ProviderByName(ctx, client, wpName)
274+
if err != nil {
275+
return err
276+
}
277+
278+
if err := client.UnCordonWorkspaceProvider(ctx, provider.ID); err != nil {
279+
return err
280+
}
281+
clog.LogSuccess(fmt.Sprintf("provider %q successfully uncordoned - you can now create workspaces on this provider", wpName))
282+
return nil
283+
},
284+
}
285+
return cmd
286+
}

0 commit comments

Comments
 (0)