Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
comments
  • Loading branch information
deansheather committed Nov 27, 2023
commit 3c8b0a884386047c3b714554e40d8bb7d3d63c8c
93 changes: 26 additions & 67 deletions agent/proto/convert.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package proto

import (
"strings"
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"
"google.golang.org/protobuf/types/known/durationpb"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/codersdk"
)

Expand Down Expand Up @@ -99,53 +100,27 @@ func SDKAgentScriptFromProto(protoScript *WorkspaceAgentScript) (codersdk.Worksp
}, nil
}

func DBAppsToProto(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) []*WorkspaceApp {
func DBAppsToProto(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) ([]*WorkspaceApp, error) {
ret := make([]*WorkspaceApp, len(dbApps))
for i, dbApp := range dbApps {
ret[i] = DBAppToProto(dbApp, agent, ownerName, workspace)
}
return ret
}

func DBAppToProto(dbApp database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) *WorkspaceApp {
var subdomainName string
if dbApp.Subdomain && agent.Name != "" && ownerName != "" && workspace.Name != "" {
appSlug := dbApp.Slug
if appSlug == "" {
appSlug = dbApp.DisplayName
var err error
ret[i], err = DBAppToProto(dbApp, agent, ownerName, workspace)
if err != nil {
return nil, xerrors.Errorf("parse app %v (%q): %w", i, dbApp.Slug, err)
}
subdomainName = httpapi.ApplicationURL{
// We never generate URLs with a prefix. We only allow prefixes
// when parsing URLs from the hostname. Users that want this
// feature can write out their own URLs.
Prefix: "",
AppSlugOrPort: appSlug,
AgentName: agent.Name,
WorkspaceName: workspace.Name,
Username: ownerName,
}.String()
}
return ret, nil
}

sharingLevel := WorkspaceApp_SHARING_LEVEL_UNSPECIFIED
switch dbApp.SharingLevel {
case database.AppSharingLevelOwner:
sharingLevel = WorkspaceApp_OWNER
case database.AppSharingLevelAuthenticated:
sharingLevel = WorkspaceApp_AUTHENTICATED
case database.AppSharingLevelPublic:
sharingLevel = WorkspaceApp_PUBLIC
func DBAppToProto(dbApp database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) (*WorkspaceApp, error) {
sharingLevelRaw, ok := WorkspaceApp_SharingLevel_value[strings.ToUpper(string(dbApp.SharingLevel))]
if !ok {
return nil, xerrors.Errorf("unknown app sharing level: %q", dbApp.SharingLevel)
}

health := WorkspaceApp_HEALTH_UNSPECIFIED
switch dbApp.Health {
case database.WorkspaceAppHealthDisabled:
health = WorkspaceApp_DISABLED
case database.WorkspaceAppHealthInitializing:
health = WorkspaceApp_INITIALIZING
case database.WorkspaceAppHealthHealthy:
health = WorkspaceApp_HEALTHY
case database.WorkspaceAppHealthUnhealthy:
health = WorkspaceApp_UNHEALTHY
healthRaw, ok := WorkspaceApp_Health_value[strings.ToUpper(string(dbApp.Health))]
if !ok {
return nil, xerrors.Errorf("unknown app health: %q", dbApp.SharingLevel)
}

return &WorkspaceApp{
Expand All @@ -157,15 +132,15 @@ func DBAppToProto(dbApp database.WorkspaceApp, agent database.WorkspaceAgent, ow
Command: dbApp.Command.String,
Icon: dbApp.Icon,
Subdomain: dbApp.Subdomain,
SubdomainName: subdomainName,
SharingLevel: sharingLevel,
SubdomainName: db2sdk.AppSubdomain(dbApp, agent.Name, workspace.Name, ownerName),
SharingLevel: WorkspaceApp_SharingLevel(sharingLevelRaw),
Healthcheck: &WorkspaceApp_Healthcheck{
Url: dbApp.HealthcheckUrl,
Interval: durationpb.New(time.Duration(dbApp.HealthcheckInterval) * time.Second),
Threshold: dbApp.HealthcheckThreshold,
},
Health: health,
}
Health: WorkspaceApp_Health(healthRaw),
}, nil
}

func SDKAppsFromProto(protoApps []*WorkspaceApp) ([]codersdk.WorkspaceApp, error) {
Expand All @@ -186,30 +161,14 @@ func SDKAppFromProto(protoApp *WorkspaceApp) (codersdk.WorkspaceApp, error) {
return codersdk.WorkspaceApp{}, xerrors.Errorf("parse id: %w", err)
}

var sharingLevel codersdk.WorkspaceAppSharingLevel
switch protoApp.SharingLevel {
case WorkspaceApp_OWNER:
sharingLevel = codersdk.WorkspaceAppSharingLevelOwner
case WorkspaceApp_AUTHENTICATED:
sharingLevel = codersdk.WorkspaceAppSharingLevelAuthenticated
case WorkspaceApp_PUBLIC:
sharingLevel = codersdk.WorkspaceAppSharingLevelPublic
default:
return codersdk.WorkspaceApp{}, xerrors.Errorf("unknown sharing level: %v", protoApp.SharingLevel)
var sharingLevel codersdk.WorkspaceAppSharingLevel = codersdk.WorkspaceAppSharingLevel(strings.ToLower(protoApp.SharingLevel.String()))
if _, ok := codersdk.MapWorkspaceAppSharingLevels[sharingLevel]; !ok {
return codersdk.WorkspaceApp{}, xerrors.Errorf("unknown app sharing level: %v (%q)", protoApp.SharingLevel, protoApp.SharingLevel.String())
}

var health codersdk.WorkspaceAppHealth
switch protoApp.Health {
case WorkspaceApp_DISABLED:
health = codersdk.WorkspaceAppHealthDisabled
case WorkspaceApp_INITIALIZING:
health = codersdk.WorkspaceAppHealthInitializing
case WorkspaceApp_HEALTHY:
health = codersdk.WorkspaceAppHealthHealthy
case WorkspaceApp_UNHEALTHY:
health = codersdk.WorkspaceAppHealthUnhealthy
default:
return codersdk.WorkspaceApp{}, xerrors.Errorf("unknown health: %v", protoApp.Health)
var health codersdk.WorkspaceAppHealth = codersdk.WorkspaceAppHealth(strings.ToLower(protoApp.Health.String()))
if _, ok := codersdk.MapWorkspaceAppHealths[health]; !ok {
return codersdk.WorkspaceApp{}, xerrors.Errorf("unknown app health: %v (%q)", protoApp.Health, protoApp.Health.String())
}

return codersdk.WorkspaceApp{
Expand Down
7 changes: 6 additions & 1 deletion coderd/agentapi/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
}
}

apps, err := agentproto.DBAppsToProto(dbApps, workspaceAgent, owner.Username, workspace)
if err != nil {
return nil, xerrors.Errorf("converting workspace apps: %w", err)
}

return &agentproto.Manifest{
AgentId: workspaceAgent.ID[:],
GitAuthConfigs: gitAuthConfigs,
Expand All @@ -141,7 +146,7 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest

DerpMap: tailnetproto.DERPMapToProto(a.DerpMapFn()),
Scripts: agentproto.DBAgentScriptsToProto(scripts),
Apps: agentproto.DBAppsToProto(dbApps, workspaceAgent, owner.Username, workspace),
Apps: apps,
Metadata: agentproto.DBAgentMetadataToProtoDescription(metadata),
}, nil
}
47 changes: 47 additions & 0 deletions coderd/database/db2sdk/db2sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"tailscale.com/tailcfg"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/parameter"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
Expand Down Expand Up @@ -326,3 +327,49 @@ func WorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordinator,

return workspaceAgent, nil
}

func AppSubdomain(dbApp database.WorkspaceApp, agentName, workspaceName, ownerName string) string {
if !dbApp.Subdomain || agentName == "" || ownerName == "" || workspaceName == "" {
return ""
}

appSlug := dbApp.Slug
if appSlug == "" {
appSlug = dbApp.DisplayName
}
return httpapi.ApplicationURL{
// We never generate URLs with a prefix. We only allow prefixes when
// parsing URLs from the hostname. Users that want this feature can
// write out their own URLs.
Prefix: "",
AppSlugOrPort: appSlug,
AgentName: agentName,
WorkspaceName: workspaceName,
Username: ownerName,
}.String()
}

func Apps(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) []codersdk.WorkspaceApp {
apps := make([]codersdk.WorkspaceApp, 0)
for _, dbApp := range dbApps {
apps = append(apps, codersdk.WorkspaceApp{
ID: dbApp.ID,
URL: dbApp.Url.String,
External: dbApp.External,
Slug: dbApp.Slug,
DisplayName: dbApp.DisplayName,
Command: dbApp.Command.String,
Icon: dbApp.Icon,
Subdomain: dbApp.Subdomain,
SubdomainName: AppSubdomain(dbApp, agent.Name, workspace.Name, ownerName),
SharingLevel: codersdk.WorkspaceAppSharingLevel(dbApp.SharingLevel),
Healthcheck: codersdk.Healthcheck{
URL: dbApp.HealthcheckUrl,
Interval: dbApp.HealthcheckInterval,
Threshold: dbApp.HealthcheckThreshold,
},
Health: codersdk.WorkspaceAppHealth(dbApp.Health),
})
}
return apps
}
47 changes: 2 additions & 45 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) {
}

apiAgent, err := db2sdk.WorkspaceAgent(
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps, workspaceAgent, owner.Username, workspace), convertScripts(scripts), convertLogSources(logSources), api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, db2sdk.Apps(dbApps, workspaceAgent, owner.Username, workspace), convertScripts(scripts), convertLogSources(logSources), api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down Expand Up @@ -1665,50 +1665,7 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
// convertProvisionedApps converts applications that are in the middle of provisioning process.
// It means that they may not have an agent or workspace assigned (dry-run job).
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
return convertApps(dbApps, database.WorkspaceAgent{}, "", database.Workspace{})
}

func convertApps(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) []codersdk.WorkspaceApp {
apps := make([]codersdk.WorkspaceApp, 0)
for _, dbApp := range dbApps {
var subdomainName string
if dbApp.Subdomain && agent.Name != "" && ownerName != "" && workspace.Name != "" {
appSlug := dbApp.Slug
if appSlug == "" {
appSlug = dbApp.DisplayName
}
subdomainName = httpapi.ApplicationURL{
// We never generate URLs with a prefix. We only allow prefixes
// when parsing URLs from the hostname. Users that want this
// feature can write out their own URLs.
Prefix: "",
AppSlugOrPort: appSlug,
AgentName: agent.Name,
WorkspaceName: workspace.Name,
Username: ownerName,
}.String()
}

apps = append(apps, codersdk.WorkspaceApp{
ID: dbApp.ID,
URL: dbApp.Url.String,
External: dbApp.External,
Slug: dbApp.Slug,
DisplayName: dbApp.DisplayName,
Command: dbApp.Command.String,
Icon: dbApp.Icon,
Subdomain: dbApp.Subdomain,
SubdomainName: subdomainName,
SharingLevel: codersdk.WorkspaceAppSharingLevel(dbApp.SharingLevel),
Healthcheck: codersdk.Healthcheck{
URL: dbApp.HealthcheckUrl,
Interval: dbApp.HealthcheckInterval,
Threshold: dbApp.HealthcheckThreshold,
},
Health: codersdk.WorkspaceAppHealth(dbApp.Health),
})
}
return apps
return db2sdk.Apps(dbApps, database.WorkspaceAgent{}, "", database.Workspace{})
}

func convertLogSources(dbLogSources []database.WorkspaceAgentLogSource) []codersdk.WorkspaceAgentLogSource {
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ func (api *API) convertWorkspaceBuild(
scripts := scriptsByAgentID[agent.ID]
logSources := logSourcesByAgentID[agent.ID]
apiAgent, err := db2sdk.WorkspaceAgent(
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(apps, agent, ownerName, workspace), convertScripts(scripts), convertLogSources(logSources), api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, db2sdk.Apps(apps, agent, ownerName, workspace), convertScripts(scripts), convertLogSources(logSources), api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions codersdk/workspaceapps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const (
WorkspaceAppHealthUnhealthy WorkspaceAppHealth = "unhealthy"
)

var MapWorkspaceAppHealths = map[WorkspaceAppHealth]struct{}{
WorkspaceAppHealthDisabled: {},
WorkspaceAppHealthInitializing: {},
WorkspaceAppHealthHealthy: {},
}

type WorkspaceAppSharingLevel string

const (
Expand All @@ -21,6 +27,12 @@ const (
WorkspaceAppSharingLevelPublic WorkspaceAppSharingLevel = "public"
)

var MapWorkspaceAppSharingLevels = map[WorkspaceAppSharingLevel]struct{}{
WorkspaceAppSharingLevelOwner: {},
WorkspaceAppSharingLevelAuthenticated: {},
WorkspaceAppSharingLevelPublic: {},
}

type WorkspaceApp struct {
ID uuid.UUID `json:"id" format:"uuid"`
// URL is the address being proxied to inside the workspace.
Expand Down
Loading