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

Add ability to create new environments and images to the coder-sdk #100

Merged
merged 1 commit into from
Aug 28, 2020

Conversation

f0ssel
Copy link
Contributor

@f0ssel f0ssel commented Aug 21, 2020

These new public functions will be used for our new load testing harness for the enterprise product

@f0ssel f0ssel changed the title Add ability to create new environments and images Add ability to create new environments and images to the coder-sdk Aug 21, 2020
@f0ssel f0ssel requested review from cmoog and fuskovic August 21, 2020 20:32
Copy link
Contributor

@cmoog cmoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Not that I have a strong opinion, but it'd be nice to settle on a pointer/non-pointer convention for params/return values before we ask users to use this package.

coder-sdk/env.go Outdated
conn, resp, err := websocket.Dial(ctx, u.String(),
&websocket.DialOptions{
HTTPHeader: map[string][]string{
"Cookie": {"session_token=" + c.Token},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just use the Session-Token header instead of emulating cookies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

coder-sdk/env.go Outdated
}

// WatchEnvironmentStats watches the environment update websocket for a given duration.
func (c Client) WatchEnvironmentStats(ctx context.Context, envID string, duration time.Duration) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are clients supposed to use this? It doesn't seem like anything is being done with the stats we're sent over the websocket

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True true. Should return a receive-only channel.

Copy link
Contributor

@coadler coadler Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be much more useful if it returned some sort of EnvStatsWatcher tied to the passed ctx, so users can Read() or Close() themselves. Passing a duration seems hard to use and redundant since callers can use their own context.WithTimeout.

I tend to enjoy structs > bare channels so there's no need for selects, and so the Read function can return an error.

Copy link
Contributor

@fuskovic fuskovic Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably return some data-structure here that implements io.ReadCloser to piggy back on @coadler 's point.

coder-sdk/env.go Outdated
Comment on lines 183 to 187
case <-statsCtx.Done():
return nil
default:
m := stats{}
err = wsjson.Read(ctx, conn, &m)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just pass statsCtx into wsjson.Read instead of using a select?

Copy link
Contributor

@fuskovic fuskovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it cool if we hold off on merging this until Monday?

I'd like to test it against 4792 after I finish making the suggested changes and then I'll be able to provide better feedback on this.

coder-sdk/env.go Outdated
Comment on lines 178 to 180
statsCtx, statsCancel := context.WithTimeout(ctx, duration)
defer statsCancel()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

watching cancelation should happen through the context passed above, not a new context created by the duration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this one @cmoog

I think if he passes statsCtx to the websocket read that gives us two paths to cancellation that could occur at two different times.

I think a cancellation of the parent context would propogate to statsCtx but not the other way around since a time-out is not always indicative of cancellation of the parent ctx.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting. The two paths of cancellation is definitely a valid point. Especially considering you'd want a much shorter timeout for the initial Dial

I'd say that makes me in favor of something similar to what Colin is suggesting where the WebSocket opener just returns some watcher structure that holds additional methods for listening that are bounded by a different context.

coder-sdk/env.go Outdated
}

// WaitForEnvironmentReady watches the environment update websocket and waits for the "done" message type before returning.
func (c Client) WaitForEnvironmentReady(ctx context.Context, envID string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also return a channel that gets EnvUpdates and a higher-level wrapper would wait for "done".

coder-sdk/env.go Outdated
m := stats{}
err = wsjson.Read(ctx, conn, &m)
if err != nil {
return fmt.Errorf("read ws json msg: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xerrors to be consistent.

@coadler
Copy link
Contributor

coadler commented Aug 21, 2020

+1 for the standardization of pointer vs value returns. I lean towards returning pointers personally.

coder-sdk/env.go Outdated

// DeleteEnvironment deletes the environment.
func (c Client) DeleteEnvironment(ctx context.Context, envID string) error {
err := c.requestBody(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick : Is this allocation necessary or can we just return the requestBody invocation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

coder-sdk/env.go Outdated
Comment on lines 178 to 180
statsCtx, statsCancel := context.WithTimeout(ctx, duration)
defer statsCancel()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this one @cmoog

I think if he passes statsCtx to the websocket read that gives us two paths to cancellation that could occur at two different times.

I think a cancellation of the parent context would propogate to statsCtx but not the other way around since a time-out is not always indicative of cancellation of the parent ctx.

coder-sdk/env.go Outdated
}

// WatchEnvironmentStats watches the environment update websocket for a given duration.
func (c Client) WatchEnvironmentStats(ctx context.Context, envID string, duration time.Duration) error {
Copy link
Contributor

@fuskovic fuskovic Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably return some data-structure here that implements io.ReadCloser to piggy back on @coadler 's point.

@f0ssel f0ssel force-pushed the stress-sdk branch 4 times, most recently from df519d9 to 902ce7b Compare August 28, 2020 16:28
@f0ssel
Copy link
Contributor Author

f0ssel commented Aug 28, 2020

Hey @coadler , @cmoog , @fuskovic thanks for the feedback on this. I've cut down my changes to a much simpler set and will move more of the business logic into the enterprise repo to keep this cleaner for everyone while keeping the feedback here in mind.

Copy link
Contributor

@cmoog cmoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for now given that we're not asking users to consume this SDK and this PR is blocking the stress-test work. That said, I'd like us to hide the underlying *websocket.Conn from our stat streaming/buildlog interactions before we expect users to depend on this package.

Copy link
Contributor

@coadler coadler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 what @cmoog said. If we intend for customers to use this, we'll need to write some wrapper around the returned ws conn.

Any specific reason for the departure from returning pointers? The usage seems a bit inconsistent now.

@f0ssel
Copy link
Contributor Author

f0ssel commented Aug 28, 2020

@coadler thanks for the catch, just an oversight, updated to return pointers

@f0ssel
Copy link
Contributor Author

f0ssel commented Aug 28, 2020

@cmoog I agree, I can see us consuming this later similar to how the DialWsep is working currently once the stress testing stuff is in less flux

@f0ssel f0ssel merged commit 0f0160a into master Aug 28, 2020
@f0ssel f0ssel deleted the stress-sdk branch August 28, 2020 19:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants