Skip to content

Commit 4804269

Browse files
committed
Merge branch 'colin/pg-coordinate' into replica
2 parents 1dcf0d0 + 5c43d63 commit 4804269

File tree

76 files changed

+1846
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1846
-391
lines changed

.github/workflows/dogfood.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212
workflow_dispatch:
1313

1414
jobs:
15-
deploy:
15+
deploy_image:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: Get branch name
@@ -47,3 +47,27 @@ jobs:
4747
tags: "codercom/oss-dogfood:${{ steps.docker-tag-name.outputs.tag }},codercom/oss-dogfood:latest"
4848
cache-from: type=registry,ref=codercom/oss-dogfood:latest
4949
cache-to: type=inline
50+
deploy_template:
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@v3
55+
- name: Get short commit SHA
56+
id: vars
57+
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
58+
- name: "Install latest Coder"
59+
run: |
60+
curl -L https://coder.com/install.sh | sh
61+
# env:
62+
# VERSION: 0.x
63+
- name: "Push template"
64+
run: |
65+
coder templates push $CODER_TEMPLATE_NAME --directory $CODER_TEMPLATE_DIR --yes --name=$CODER_TEMPLATE_VERSION
66+
env:
67+
# Consumed by Coder CLI
68+
CODER_URL: https://dev.coder.com
69+
CODER_SESSION_TOKEN: ${{ secrets.CODER_SESSION_TOKEN }}
70+
# Template source & details
71+
CODER_TEMPLATE_NAME: ${{ secrets.CODER_TEMPLATE_NAME }}
72+
CODER_TEMPLATE_VERSION: ${{ steps.vars.outputs.sha_short }}
73+
CODER_TEMPLATE_DIR: ./dogfood

agent/agent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func setupAgent(t *testing.T, metadata codersdk.WorkspaceAgentMetadata, ptyTimeo
560560
if metadata.DERPMap == nil {
561561
metadata.DERPMap = tailnettest.RunDERPAndSTUN(t)
562562
}
563-
coordinator := tailnet.NewCoordinator()
563+
coordinator := tailnet.NewMemoryCoordinator()
564564
agentID := uuid.New()
565565
statsCh := make(chan *codersdk.AgentStats)
566566
closer := agent.New(agent.Options{

cli/login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ func login() *cobra.Command {
248248
return nil
249249
},
250250
}
251-
cliflag.StringVarP(cmd.Flags(), &email, "email", "e", "CODER_EMAIL", "", "Specifies an email address to authenticate with.")
252-
cliflag.StringVarP(cmd.Flags(), &username, "username", "u", "CODER_USERNAME", "", "Specifies a username to authenticate with.")
253-
cliflag.StringVarP(cmd.Flags(), &password, "password", "p", "CODER_PASSWORD", "", "Specifies a password to authenticate with.")
251+
cliflag.StringVarP(cmd.Flags(), &email, "first-user-email", "", "CODER_FIRST_USER_EMAIL", "", "Specifies an email address to use if creating the first user for the deployment.")
252+
cliflag.StringVarP(cmd.Flags(), &username, "first-user-username", "", "CODER_FIRST_USER_USERNAME", "", "Specifies a username to use if creating the first user for the deployment.")
253+
cliflag.StringVarP(cmd.Flags(), &password, "first-user-password", "", "CODER_FIRST_USER_PASSWORD", "", "Specifies a password to use if creating the first user for the deployment.")
254254
return cmd
255255
}
256256

cli/login_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestLogin(t *testing.T) {
7474
// accurately detect Windows ptys when they are not attached to a process:
7575
// https://github.com/mattn/go-isatty/issues/59
7676
doneChan := make(chan struct{})
77-
root, _ := clitest.New(t, "login", client.URL.String(), "--username", "testuser", "--email", "user@coder.com", "--password", "password")
77+
root, _ := clitest.New(t, "login", client.URL.String(), "--first-user-username", "testuser", "--first-user-email", "user@coder.com", "--first-user-password", "password")
7878
pty := ptytest.New(t)
7979
root.SetIn(pty.Input())
8080
root.SetOut(pty.Output())

cli/tokens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func createToken() *cobra.Command {
6767
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(res.Key)))
6868
cmd.Println()
6969
cmd.Println(cliui.Styles.Wrap.Render(
70-
fmt.Sprintf("You can use this token by setting the --%s CLI flag, the %s environment variable, or the %q HTTP header.", varToken, envSessionToken, codersdk.SessionTokenKey),
70+
fmt.Sprintf("You can use this token by setting the --%s CLI flag, the %s environment variable, or the %q HTTP header.", varToken, envSessionToken, codersdk.SessionCustomHeader),
7171
))
7272

7373
return nil

coderd/coderd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Options struct {
7676
TracerProvider trace.TracerProvider
7777
AutoImportTemplates []AutoImportTemplate
7878

79-
TailnetCoordinator *tailnet.Coordinator
79+
TailnetCoordinator tailnet.Coordinator
8080
DERPMap *tailcfg.DERPMap
8181

8282
MetricsCacheRefreshInterval time.Duration
@@ -119,7 +119,7 @@ func New(options *Options) *API {
119119
options.PrometheusRegistry = prometheus.NewRegistry()
120120
}
121121
if options.TailnetCoordinator == nil {
122-
options.TailnetCoordinator = tailnet.NewCoordinator()
122+
options.TailnetCoordinator = tailnet.NewMemoryCoordinator()
123123
}
124124
if options.Auditor == nil {
125125
options.Auditor = audit.NewNop()
@@ -159,6 +159,7 @@ func New(options *Options) *API {
159159
api.Auditor.Store(&options.Auditor)
160160
api.WorkspaceQuotaEnforcer.Store(&options.WorkspaceQuotaEnforcer)
161161
api.workspaceAgentCache = wsconncache.New(api.dialWorkspaceAgentTailnet, 0)
162+
api.TailnetCoordinator.Store(&options.TailnetCoordinator)
162163
api.derpServer = derp.NewServer(key.NewNode(), tailnet.Logger(options.Logger))
163164
oauthConfigs := &httpmw.OAuth2Configs{
164165
Github: options.GithubOAuth2Config,
@@ -531,6 +532,7 @@ type API struct {
531532
Auditor atomic.Pointer[audit.Auditor]
532533
WorkspaceClientCoordinateOverride atomic.Pointer[func(rw http.ResponseWriter) bool]
533534
WorkspaceQuotaEnforcer atomic.Pointer[workspacequota.Enforcer]
535+
TailnetCoordinator atomic.Pointer[tailnet.Coordinator]
534536
HTTPAuth *HTTPAuthorizer
535537

536538
// APIHandler serves "/api/v2"

coderd/database/databasefake/databasefake.go

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ func (q *fakeQuerier) GetWorkspaces(ctx context.Context, arg database.GetWorkspa
554554
return workspaces, err
555555
}
556556

557-
func (q *fakeQuerier) GetAuthorizedWorkspaces(_ context.Context, arg database.GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]database.Workspace, error) {
557+
//nolint:gocyclo
558+
func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.GetWorkspacesParams, authorizedFilter rbac.AuthorizeFilter) ([]database.Workspace, error) {
558559
q.mutex.RLock()
559560
defer q.mutex.RUnlock()
560561

@@ -563,25 +564,118 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(_ context.Context, arg database.Ge
563564
if arg.OwnerID != uuid.Nil && workspace.OwnerID != arg.OwnerID {
564565
continue
565566
}
567+
566568
if arg.OwnerUsername != "" {
567-
owner, err := q.GetUserByID(context.Background(), workspace.OwnerID)
569+
owner, err := q.GetUserByID(ctx, workspace.OwnerID)
568570
if err == nil && !strings.EqualFold(arg.OwnerUsername, owner.Username) {
569571
continue
570572
}
571573
}
574+
572575
if arg.TemplateName != "" {
573-
template, err := q.GetTemplateByID(context.Background(), workspace.TemplateID)
576+
template, err := q.GetTemplateByID(ctx, workspace.TemplateID)
574577
if err == nil && !strings.EqualFold(arg.TemplateName, template.Name) {
575578
continue
576579
}
577580
}
581+
578582
if !arg.Deleted && workspace.Deleted {
579583
continue
580584
}
581585

582586
if arg.Name != "" && !strings.Contains(strings.ToLower(workspace.Name), strings.ToLower(arg.Name)) {
583587
continue
584588
}
589+
590+
if arg.Status != "" {
591+
build, err := q.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
592+
if err != nil {
593+
return nil, xerrors.Errorf("get latest build: %w", err)
594+
}
595+
596+
job, err := q.GetProvisionerJobByID(ctx, build.JobID)
597+
if err != nil {
598+
return nil, xerrors.Errorf("get provisioner job: %w", err)
599+
}
600+
601+
switch arg.Status {
602+
case "pending":
603+
if !job.StartedAt.Valid {
604+
continue
605+
}
606+
607+
case "starting":
608+
if !job.StartedAt.Valid &&
609+
!job.CanceledAt.Valid &&
610+
job.CompletedAt.Valid &&
611+
time.Since(job.UpdatedAt) > 30*time.Second ||
612+
build.Transition != database.WorkspaceTransitionStart {
613+
continue
614+
}
615+
616+
case "running":
617+
if !job.CompletedAt.Valid &&
618+
job.CanceledAt.Valid &&
619+
job.Error.Valid ||
620+
build.Transition != database.WorkspaceTransitionStart {
621+
continue
622+
}
623+
624+
case "stopping":
625+
if !job.StartedAt.Valid &&
626+
!job.CanceledAt.Valid &&
627+
job.CompletedAt.Valid &&
628+
time.Since(job.UpdatedAt) > 30*time.Second ||
629+
build.Transition != database.WorkspaceTransitionStop {
630+
continue
631+
}
632+
633+
case "stopped":
634+
if !job.CompletedAt.Valid &&
635+
job.CanceledAt.Valid &&
636+
job.Error.Valid ||
637+
build.Transition != database.WorkspaceTransitionStop {
638+
continue
639+
}
640+
641+
case "failed":
642+
if (!job.CanceledAt.Valid && !job.Error.Valid) ||
643+
(!job.CompletedAt.Valid && !job.Error.Valid) {
644+
continue
645+
}
646+
647+
case "canceling":
648+
if !job.CanceledAt.Valid && job.CompletedAt.Valid {
649+
continue
650+
}
651+
652+
case "canceled":
653+
if !job.CanceledAt.Valid && !job.CompletedAt.Valid {
654+
continue
655+
}
656+
657+
case "deleted":
658+
if !job.StartedAt.Valid &&
659+
job.CanceledAt.Valid &&
660+
!job.CompletedAt.Valid &&
661+
time.Since(job.UpdatedAt) > 30*time.Second ||
662+
build.Transition != database.WorkspaceTransitionDelete {
663+
continue
664+
}
665+
666+
case "deleting":
667+
if !job.CompletedAt.Valid &&
668+
job.CanceledAt.Valid &&
669+
job.Error.Valid &&
670+
build.Transition != database.WorkspaceTransitionDelete {
671+
continue
672+
}
673+
674+
default:
675+
return nil, xerrors.Errorf("unknown workspace status in filter: %q", arg.Status)
676+
}
677+
}
678+
585679
if len(arg.TemplateIds) > 0 {
586680
match := false
587681
for _, id := range arg.TemplateIds {
@@ -772,7 +866,7 @@ func (q *fakeQuerier) GetLatestWorkspaceBuildByWorkspaceID(_ context.Context, wo
772866
var row database.WorkspaceBuild
773867
var buildNum int32 = -1
774868
for _, workspaceBuild := range q.workspaceBuilds {
775-
if workspaceBuild.WorkspaceID.String() == workspaceID.String() && workspaceBuild.BuildNumber > buildNum {
869+
if workspaceBuild.WorkspaceID == workspaceID && workspaceBuild.BuildNumber > buildNum {
776870
row = workspaceBuild
777871
buildNum = workspaceBuild.BuildNumber
778872
}
@@ -817,7 +911,7 @@ func (q *fakeQuerier) GetLatestWorkspaceBuildsByWorkspaceIDs(_ context.Context,
817911
buildNumbers := make(map[uuid.UUID]int32)
818912
for _, workspaceBuild := range q.workspaceBuilds {
819913
for _, id := range ids {
820-
if id.String() == workspaceBuild.WorkspaceID.String() && workspaceBuild.BuildNumber > buildNumbers[id] {
914+
if id == workspaceBuild.WorkspaceID && workspaceBuild.BuildNumber > buildNumbers[id] {
821915
builds[id] = workspaceBuild
822916
buildNumbers[id] = workspaceBuild.BuildNumber
823917
}

coderd/database/modelqueries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
168168
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s AND %s", getWorkspaces, authorizedFilter.SQLString(rbac.NoACLConfig()))
169169
rows, err := q.db.QueryContext(ctx, query,
170170
arg.Deleted,
171+
arg.Status,
171172
arg.OwnerID,
172173
arg.OwnerUsername,
173174
arg.TemplateName,

coderd/database/pubsub_memory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ func (m *memoryPubsub) Publish(event string, message []byte) error {
4747
return nil
4848
}
4949
for _, listener := range listeners {
50-
listener(context.Background(), message)
50+
go listener(context.Background(), message)
5151
}
52+
5253
return nil
5354
}
5455

0 commit comments

Comments
 (0)