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

Commit 75ef1bc

Browse files
authored
Use site config to use tunnel on workspaces in built-in wsp and site p2p enabled (#343)
1 parent aa6625c commit 75ef1bc

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

coder-sdk/config.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,21 @@ func (c *DefaultClient) SiteConfigExtensionMarketplace(ctx context.Context) (*Co
137137
func (c *DefaultClient) PutSiteConfigExtensionMarketplace(ctx context.Context, req ConfigExtensionMarketplace) error {
138138
return c.requestBody(ctx, http.MethodPut, "/api/private/extensions/config", req, nil)
139139
}
140+
141+
// ConfigWorkspaces is the site configuration for workspace attributes.
142+
type ConfigWorkspaces struct {
143+
GPUVendor string `json:"gpu_vendor,omitempty" valid:"in(nvidia|amd)"`
144+
EnableContainerVMs bool `json:"enable_container_vms,omitempty"`
145+
EnableWorkspacesAsCode bool `json:"enable_workspaces_as_code,omitempty"`
146+
EnableP2P bool `json:"enable_p2p,omitempty"`
147+
}
148+
149+
// SiteConfigWorkspaces fetches the workspace configuration.
150+
func (c *DefaultClient) SiteConfigWorkspaces(ctx context.Context) (*ConfigWorkspaces, error) {
151+
var conf ConfigWorkspaces
152+
// TODO: use the `/api/v0/workspaces/config route once we migrate from using general config
153+
if err := c.requestBody(ctx, http.MethodGet, "/api/private/config", nil, &conf); err != nil {
154+
return nil, err
155+
}
156+
return &conf, nil
157+
}

coder-sdk/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ type Client interface {
6262
// PutSiteConfigExtensionMarketplace sets the extension marketplace configuration.
6363
PutSiteConfigExtensionMarketplace(ctx context.Context, req ConfigExtensionMarketplace) error
6464

65+
// SiteConfigWorkspaces fetches the workspace configuration.
66+
SiteConfigWorkspaces(ctx context.Context) (*ConfigWorkspaces, error)
67+
6568
// DeleteDevURL deletes the specified devurl.
6669
DeleteDevURL(ctx context.Context, envID, urlID string) error
6770

docs/coder_config-ssh.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ coder config-ssh [flags]
1515
```
1616
--filepath string override the default path of your ssh config file (default "~/.ssh/config")
1717
-h, --help help for config-ssh
18-
--p2p (experimental) uses coder tunnel to proxy ssh connection
1918
--remove remove the auto-generated Coder ssh config
2019
```
2120

internal/cmd/configssh.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,21 @@ func configSSHCmd() *cobra.Command {
3535
var (
3636
configpath string
3737
remove = false
38-
p2p = false
3938
)
4039

4140
cmd := &cobra.Command{
4241
Use: "config-ssh",
4342
Short: "Configure SSH to access Coder environments",
4443
Long: "Inject the proper OpenSSH configuration into your local SSH config file.",
45-
RunE: configSSH(&configpath, &remove, &p2p),
44+
RunE: configSSH(&configpath, &remove),
4645
}
4746
cmd.Flags().StringVar(&configpath, "filepath", filepath.Join("~", ".ssh", "config"), "override the default path of your ssh config file")
4847
cmd.Flags().BoolVar(&remove, "remove", false, "remove the auto-generated Coder ssh config")
49-
cmd.Flags().BoolVar(&p2p, "p2p", false, "(experimental) uses coder tunnel to proxy ssh connection")
5048

5149
return cmd
5250
}
5351

54-
func configSSH(configpath *string, remove *bool, p2p *bool) func(cmd *cobra.Command, _ []string) error {
52+
func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []string) error {
5553
return func(cmd *cobra.Command, _ []string) error {
5654
ctx := cmd.Context()
5755
usr, err := user.Current()
@@ -115,7 +113,12 @@ func configSSH(configpath *string, remove *bool, p2p *bool) func(cmd *cobra.Comm
115113
return xerrors.New("SSH is disabled or not available for any environments in your Coder deployment.")
116114
}
117115

118-
newConfig := makeNewConfigs(user.Username, envsWithProviders, privateKeyFilepath, *p2p)
116+
wconf, err := client.SiteConfigWorkspaces(ctx)
117+
if err != nil {
118+
return xerrors.Errorf("getting site workspace config: %w", err)
119+
}
120+
121+
newConfig := makeNewConfigs(user.Username, envsWithProviders, privateKeyFilepath, wconf.EnableP2P)
119122

120123
err = os.MkdirAll(filepath.Dir(*configpath), os.ModePerm)
121124
if err != nil {
@@ -194,15 +197,17 @@ func makeNewConfigs(userName string, envs []coderutil.EnvWithWorkspaceProvider,
194197
clog.LogWarn("invalid access url", clog.Causef("malformed url: %q", env.WorkspaceProvider.EnvproxyAccessURL))
195198
continue
196199
}
197-
newConfig += makeSSHConfig(u.Host, userName, env.Env.Name, privateKeyFilepath, p2p)
200+
201+
useTunnel := env.WorkspaceProvider.BuiltIn && p2p
202+
newConfig += makeSSHConfig(u.Host, userName, env.Env.Name, privateKeyFilepath, useTunnel)
198203
}
199204
newConfig += fmt.Sprintf("\n%s\n", sshEndToken)
200205

201206
return newConfig
202207
}
203208

204-
func makeSSHConfig(host, userName, envName, privateKeyFilepath string, p2p bool) string {
205-
if p2p {
209+
func makeSSHConfig(host, userName, envName, privateKeyFilepath string, tunnel bool) string {
210+
if tunnel {
206211
return fmt.Sprintf(
207212
`Host coder.%s
208213
HostName coder.%s

0 commit comments

Comments
 (0)