Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

feat: add username to 'coder ws ls'#486

Merged
sreya merged 3 commits intomainfromjon/wslsusername
Jul 20, 2022
Merged
Changes from1 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
NextNext commit
feat: add username to 'coder ws ls'
  • Loading branch information
@sreya
sreya committedJul 19, 2022
commitba0d33c03fd0dc670cc35c894acadc7903114e9a
81 changes: 64 additions & 17 deletionsinternal/coderutil/workspace.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -79,14 +79,15 @@ func DefaultWorkspaceProvider(ctx context.Context, c coder.Client) (*coder.Kuber
// WorkspaceTable defines an Workspace-like structure with associated entities composed in a human
// readable form.
type WorkspaceTable struct {
Name string `table:"Name"`
Image string `table:"Image"`
CPU float32 `table:"vCPU"`
MemoryGB float32 `table:"MemoryGB"`
DiskGB int `table:"DiskGB"`
Status string `table:"Status"`
Provider string `table:"Provider"`
CVM bool `table:"CVM"`
Name string `table:"Name" json:"name"`
Image string `table:"Image" json:"image"`
CPU float32 `table:"vCPU" json:"cpu"`
MemoryGB float32 `table:"MemoryGB" json:"memory_gb"`
DiskGB int `table:"DiskGB" json:"disk_gb"`
Status string `table:"Status" json:"status"`
Provider string `table:"Provider" json:"provider"`
CVM bool `table:"CVM" json:"cvm"`
Username string `table:"Username" json:"username"`
}

// WorkspacesHumanTable performs the composition of each Workspace with its associated ProviderName and ImageRepo.
Expand All@@ -96,6 +97,11 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
return nil, err
}

userMap, err := MakeUserMap(ctx, client, workspaces)
if err != nil {
return nil, err
}

pooledWorkspaces := make([]WorkspaceTable, 0, len(workspaces))
providers, err := client.WorkspaceProviders(ctx)
if err != nil {
Expand All@@ -105,25 +111,66 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
for _, p := range providers.Kubernetes {
providerMap[p.ID] = p
}
for _,e := range workspaces {
workspaceProvider, ok := providerMap[e.ResourcePoolID]
for _,ws := range workspaces {
workspaceProvider, ok := providerMap[ws.ResourcePoolID]
if !ok {
return nil, xerrors.Errorf("fetch workspace workspace provider: %w", coder.ErrNotFound)
}
pooledWorkspaces = append(pooledWorkspaces, WorkspaceTable{
Name:e.Name,
Image: fmt.Sprintf("%s:%s", imageMap[e.ImageID].Repository,e.ImageTag),
CPU:e.CPUCores,
MemoryGB:e.MemoryGB,
DiskGB:e.DiskGB,
Status: string(e.LatestStat.ContainerStatus),
Name:ws.Name,
Image: fmt.Sprintf("%s:%s", imageMap[ws.ImageID].Repository,ws.ImageTag),
CPU:ws.CPUCores,
MemoryGB:ws.MemoryGB,
DiskGB:ws.DiskGB,
Status: string(ws.LatestStat.ContainerStatus),
Provider: workspaceProvider.Name,
CVM: e.UseContainerVM,
CVM: ws.UseContainerVM,
Username: userMap[ws.UserID].Username,
})
}
return pooledWorkspaces, nil
}

func MakeUserMap(ctx context.Context, client coder.Client, workspaces []coder.Workspace) (map[string]*coder.User, error) {
var (
mu sync.Mutex
egroup = clog.LoggedErrGroup()
)

userMap := map[string]*coder.User{}

// Iterate over all the workspaces to get a list of unique User IDs.
for _, ws := range workspaces {
userMap[ws.UserID] = nil
}

fetchIds := make([]string, 0, len(userMap))
for id, _ := range userMap {
fetchIds = append(fetchIds, id)
}

for _, id := range fetchIds {
id := id
egroup.Go(func() error {
user, err := client.UserByID(ctx, id)
if err != nil {
return xerrors.Errorf("get user by id: %w", err)
}
mu.Lock()
defer mu.Unlock()

userMap[id] = user
return nil
})
}

if err := egroup.Wait(); err != nil {
return nil, xerrors.Errorf("fetch all workspace users: %w", err)
}

return userMap, nil
}

// MakeImageMap fetches all image entities specified in the slice of workspaces, then places them into an ID map.
func MakeImageMap(ctx context.Context, client coder.Client, workspaces []coder.Workspace) (map[string]*coder.Image, error) {
var (
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp