Skip to content

feat: fetch hostname suffix from API #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

spikecurtis
Copy link
Collaborator

@spikecurtis spikecurtis commented May 15, 2025

Fixes #49

Adds support to query the hostname suffix from Coder server, and then propagates any changes to the agent view models.

Copy link
Collaborator Author

spikecurtis commented May 15, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@spikecurtis spikecurtis requested a review from deansheather May 15, 2025 10:34
@spikecurtis spikecurtis marked this pull request as ready for review May 15, 2025 10:34
private string _domainSuffix = DefaultSuffix;
private bool _dirty = false;
private bool _getInProgress = false;
private CredentialModel _credentialModel = new() { State = CredentialState.Invalid };
Copy link
Member

Choose a reason for hiding this comment

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

We should just fetch this on-demand from the CredentialManager (since it's cheap) rather than storing it here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried this, but it complicates things considerably in terms of race conditions because you can't synchronize changes to GetCachedCredentials with the other items we track here like _dirty, so its hard to guarantee the correct behavior. Writing updates to this while holding the lock ends up being much simpler.

using var _ = _lock.Lock();
_logger.LogDebug("credentials updated with state {state}", credentials.State);
_credentialModel = credentials;
if (credentials.State != CredentialState.Valid) return;
Copy link
Member

Choose a reason for hiding this comment

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

Should you set the stored domain back to the default in this case?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it would be better to just always create the task if the credentials changed (i.e. remove this check here) and do the check once in the task.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think we should set the stored domain back to the default. The most common scenario for getting invalid creds is probably a logout or expired token --- so it's not particularly likely that the Default is more correct.

In any case, once we sign back in we'll get the correct value.


In terms of avoiding the check and always creating the task, seems like a lot of churn (locking, creating tasks, dropping logs) to avoid one conditional.

}

using var l = await _lock.LockAsync(CancellationToken.None);
if ((_dirty || prev.IsFaulted) && _credentialModel.State == CredentialState.Valid)
Copy link
Member

Choose a reason for hiding this comment

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

_dirty seems to only be true here if the credentials became invalid between when the event was handled and the task started. So the only way for this to trigger would be if credentials became valid, then became invalid, then became valid again, which seems very unlikely to actually happen. The user would need to sign in or start the app, then sign out, then enter their credentials and sign back in (with API auth check latency included).

You could probably also accomplish the same thing without needing a bool by throwing in the credential state guard at the top of Refresh(), and only relying on prev.IsFaulted here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The idea of _dirty is that while we are making the network call a new, valid credential for a different deployment could come in. That is, just because we had a valid credential when we started our work, and have a valid credential at the end, it doesn't mean that it was the same credential. If that happens then we refresh to ensure we have the correct value from the new credential.

}
}

private async Task Refresh()
Copy link
Member

Choose a reason for hiding this comment

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

This should probably just take the credentials model as an argument.

Copy link
Collaborator Author

@spikecurtis spikecurtis May 16, 2025

Choose a reason for hiding this comment

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

We want to clear the _dirty flag while grabbing the most up to date credential (holding the lock), then any later valid credentials that come in will re-set the _dirty flag. That doesn't work if we pass the credential as an argument because we can't guarantee it's the "latest" if we're not holding the lock.

@spikecurtis spikecurtis changed the base branch from spike/49-dynamic-hostname-suffix to graphite-base/103 May 16, 2025 05:54
@spikecurtis spikecurtis requested a review from deansheather May 16, 2025 07:10
@spikecurtis spikecurtis force-pushed the spike/49-fetch-suffix branch from 3afa871 to 427a8f6 Compare May 16, 2025 07:11
@spikecurtis spikecurtis changed the base branch from graphite-base/103 to main May 16, 2025 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorporate dynamic workspace suffix into UI
2 participants