Skip to content

Commit

Permalink
Merge pull request #838 from jlandowner/20240613-small-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oruharo authored Jun 14, 2024
2 parents ca35b54 + 8b3a17c commit a0e929b
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ endif
##---------------------------------------------------------------------
##@ Test
##---------------------------------------------------------------------
TEST_FILES ?= ./... ./traefik/plugins/cosmo-workspace/cosmoauth/
TEST_FILES ?= ./... ./traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/
COVER_PROFILE ?= cover.out
#TEST_OPTS ?= --ginkgo.focus 'Dashboard server \[User\]' -ginkgo.v -ginkgo.progress -test.v > test.out 2>&1

Expand Down
2 changes: 1 addition & 1 deletion charts/cosmo/templates/dashboard/ingressroute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spec:
routes:
{{- if .Values.dashboard.localRunTest.enabled }}
- kind: Rule
match: Host(`{{ .Values.dashboard.ingressRoute.host }}.{{ .Values.domain }}`) && (Path(`/`) || PathPrefix(`/@`) || PathPrefix(`/src`) || PathPrefix(`/manifest.json`) || PathPrefix(`/node_modules`) || PathPrefix(`/logo`))
match: Host(`{{ .Values.dashboard.ingressRoute.host }}.{{ .Values.domain }}`) && (Path(`/`) || PathPrefix(`/@`) || PathPrefix(`/src`) || PathPrefix(`/manifest.json`) || PathPrefix(`/node_modules`) || PathPrefix(`/logo`)) || PathPrefix(`/favicon.ico`))
priority: 1002
services:
- kind: Service
Expand Down
3 changes: 2 additions & 1 deletion hack/local-run-test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ install-cosmo-local: helm kubectl ## Install cosmo resources.
@echo ====== $@ ======
helm upgrade --install cosmo ../../charts/cosmo \
-n cosmo-system --create-namespace \
--wait \
--set domain="\$(DOMAIN)" \
--set urlbase.host="\$(DEFAULT_URLBASE_HOST)" \
--set controllerManager.image.repository=cosmo.io:5000/cosmo-controller-manager \
Expand Down Expand Up @@ -353,6 +352,8 @@ create-cosmo-resources: apply-template add-user add-workspace cg
run-manager-local: go
@echo ====== $@ ======
@#(cd ../.. && bash hack/download-certs.sh webhook-server-cert && make run QUICK_BUILD=yes)
cd ../.. && rm -f ./cmd/controller-manager/*.tar.gz && make embed-traefik-plugins
(sleep 8 && kubectl rollout restart deploy/traefik -n cosmo-system) &
cd ../.. && bash hack/download-certs.sh webhook-server-cert && \
$(GO) run ./cmd/controller-manager/main.go \
--metrics-bind-address :8085 \
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (o *CreateOption) CreateUserWithDashClient(ctx context.Context) (*dashv1alp
Addons: o.userAddons,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("UserServiceClient.CreateUser", "req", req)
res, err := c.UserServiceClient.CreateUser(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -153,6 +154,8 @@ func (o *CreateOption) CreateUserWithDashClient(ctx context.Context) (*dashv1alp

func (o *CreateOption) CreateUserWithKubeClient(ctx context.Context) (*dashv1alpha1.User, error) {
c := o.KosmoClient

o.Logr.DebugAll().Info("CreateUser", "userName", o.UserName, "displayName", o.DisplayName, "role", o.Roles, "authType", o.AuthType, "addons", apiconv.D2C_UserAddons(o.userAddons))
user, err := c.CreateUser(ctx, o.UserName, o.DisplayName, o.Roles, o.AuthType, apiconv.D2C_UserAddons(o.userAddons))
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (o *DeleteOption) DeleteUserWithDashClient(ctx context.Context, userName st
UserName: userName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("UserServiceClient.DeleteUser", "req", req)
res, err := c.UserServiceClient.DeleteUser(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
19 changes: 6 additions & 13 deletions internal/cmd/user/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,13 @@ func (o *GetOption) ApplyFilters(users []*dashv1alpha1.User) []*dashv1alpha1.Use
}
}

if len(o.UserNames) > 0 {
ts := make([]*dashv1alpha1.User, 0, len(o.UserNames))
UserLoop:
// Or loop
for _, t := range users {
for _, selected := range o.UserNames {
if selected == t.GetName() {
ts = append(ts, t)
continue UserLoop
}
}
}
users = ts
// name filter
for _, userName := range o.UserNames {
users = cli.DoFilter(users, func(u *dashv1alpha1.User) []string {
return []string{u.Name}
}, cli.Filter{Operator: cli.OperatorEqual, Value: userName})
}

return users
}

Expand Down
20 changes: 7 additions & 13 deletions internal/cmd/user/get_addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (o *GetAddonsOption) ListUserAddonsWithDashClient(ctx context.Context, with
WithRaw: ptr.To(withRaw),
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("TemplateServiceClient.GetUserAddonTemplates", "req", req)
res, err := c.TemplateServiceClient.GetUserAddonTemplates(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down Expand Up @@ -147,20 +148,13 @@ func (o *GetAddonsOption) ApplyFilters(tmpls []*dashv1alpha1.Template) []*dashv1
}
}

if len(o.AddonNames) > 0 {
ts := make([]*dashv1alpha1.Template, 0, len(o.AddonNames))
UserLoop:
// Or loop
for _, t := range tmpls {
for _, selected := range o.AddonNames {
if selected == t.GetName() {
ts = append(ts, t)
continue UserLoop
}
}
}
tmpls = ts
// name filter
for _, addonName := range o.AddonNames {
tmpls = cli.DoFilter(tmpls, func(u *dashv1alpha1.Template) []string {
return []string{u.Name}
}, cli.Filter{Operator: cli.OperatorEqual, Value: addonName})
}

return tmpls
}

Expand Down
1 change: 1 addition & 0 deletions internal/cmd/user/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (o *GetEventsOption) GetEventsWithDashClient(ctx context.Context) ([]*dashv
UserName: o.UserName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("UserServiceClient.GetUser", "req", req)
res, err := c.UserServiceClient.GetEvents(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to get events: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/user/update_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (o *UpdateAddonOption) UpdateUserWithDashClient(ctx context.Context) (*dash
UserName: o.UserName,
Addons: o.userAddons,
}
o.Logr.DebugAll().Info("UserServiceClient.UpdateUserAddons", "req", req)
res, err := c.UserServiceClient.UpdateUserAddons(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -148,6 +149,7 @@ func (o *UpdateAddonOption) UpdateUserWithKubeClient(ctx context.Context) (*dash
UserAddons: apiconv.D2C_UserAddons(o.userAddons),
}

o.Logr.DebugAll().Info("UpdateUser", "opts", opts)
user, err := c.UpdateUser(ctx, o.UserName, opts)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/user/update_display_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (o *UpdateDisplayNameOption) UpdateUserDisplayNameWithDashClient(ctx contex
DisplayName: o.DisplayName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("UserServiceClient.UpdateUserDisplayName", "req", req)
res, err := c.UserServiceClient.UpdateUserDisplayName(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -138,6 +139,7 @@ func (o *UpdateDisplayNameOption) UpdateUserDisplayNameWithKubeClient(ctx contex
DisplayName: ptr.To(o.UserName),
}

o.Logr.DebugAll().Info("UpdateUser", "opts", opts)
user, err := c.UpdateUser(ctx, o.UserName, opts)
if err != nil {
return nil, err
Expand All @@ -151,6 +153,7 @@ func (o *UpdateDisplayNameOption) GetUserWithDashClient(ctx context.Context) (*d
req := &dashv1alpha1.GetUserRequest{
UserName: o.UserName,
}
o.Logr.DebugAll().Info("UserServiceClient.GetUser", "req", req)
c := o.CosmoDashClient
res, err := c.UserServiceClient.GetUser(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/user/update_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (o *UpdateRoleOption) UpdateUserRoleWithDashClient(ctx context.Context) (*d
UserName: o.UserName,
Roles: o.Roles,
}
o.Logr.DebugAll().Info("UserServiceClient.UpdateUserRole", "req", req)
c := o.CosmoDashClient
res, err := c.UserServiceClient.UpdateUserRole(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
Expand All @@ -144,6 +145,7 @@ func (o *UpdateRoleOption) UpdateUserRoleWithKubeClient(ctx context.Context) (*d
opts := kosmo.UpdateUserOpts{
UserRoles: apiconv.S2C_UserRoles(o.Roles),
}
o.Logr.DebugAll().Info("UpdateUser", "opts", opts)
user, err := c.UpdateUser(ctx, o.UserName, opts)
if err != nil {
return nil, err
Expand All @@ -158,6 +160,7 @@ func (o *UpdateRoleOption) GetUserWithDashClient(ctx context.Context) (*dashv1al
UserName: o.UserName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("UserServiceClient.GetUser", "req", req)
res, err := c.UserServiceClient.GetUser(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (o *CreateOption) CreateWorkspaceWithDashClient(ctx context.Context) (*dash
Vars: o.vars,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.CreateWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.CreateWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/workspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (o *DeleteOption) DeleteWorkspaceWithDashClient(ctx context.Context, worksp
WsName: workspaceName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.DeleteWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.DeleteWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
19 changes: 6 additions & 13 deletions internal/cmd/workspace/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,13 @@ func (o *GetOption) ApplyFilters(workspaces []*dashv1alpha1.Workspace) []*dashv1
}
}

if len(o.WorkspaceNames) > 0 {
ts := make([]*dashv1alpha1.Workspace, 0, len(o.WorkspaceNames))
WorkspaceLoop:
// Or loop
for _, t := range workspaces {
for _, selected := range o.WorkspaceNames {
if selected == t.GetName() {
ts = append(ts, t)
continue WorkspaceLoop
}
}
}
workspaces = ts
// name filter
for _, wsName := range o.WorkspaceNames {
workspaces = cli.DoFilter(workspaces, func(u *dashv1alpha1.Workspace) []string {
return []string{u.Name}
}, cli.Filter{Operator: cli.OperatorEqual, Value: wsName})
}

return workspaces
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/workspace/get_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (o *GetNetworkOption) GetWorkspaceWithDashClient(ctx context.Context) (*das
UserName: o.UserName,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.GetWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.GetWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -118,6 +119,7 @@ func (o *GetNetworkOption) OutputTable(w io.Writer, workspace *dashv1alpha1.Work

func (o *GetNetworkOption) GetWorkspaceByKubeClient(ctx context.Context) (*dashv1alpha1.Workspace, error) {
c := o.KosmoClient
o.Logr.DebugAll().Info("GetWorkspaceByUserName", "workspaceName", o.WorkspaceName, "userName", o.UserName)
workspace, err := c.GetWorkspaceByUserName(ctx, o.WorkspaceName, o.UserName)
if err != nil {
return nil, err
Expand Down
19 changes: 6 additions & 13 deletions internal/cmd/workspace/get_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,13 @@ func (o *GetTemplatesOption) ApplyFilters(tmpls []*dashv1alpha1.Template) []*das
}
}

if len(o.TemplateNames) > 0 {
ts := make([]*dashv1alpha1.Template, 0, len(o.TemplateNames))
WorkspaceLoop:
// Or loop
for _, t := range tmpls {
for _, selected := range o.TemplateNames {
if selected == t.GetName() {
ts = append(ts, t)
continue WorkspaceLoop
}
}
}
tmpls = ts
// name filter
for _, tmplName := range o.TemplateNames {
tmpls = cli.DoFilter(tmpls, func(u *dashv1alpha1.Template) []string {
return []string{u.Name}
}, cli.Filter{Operator: cli.OperatorEqual, Value: tmplName})
}

return tmpls
}

Expand Down
1 change: 1 addition & 0 deletions internal/cmd/workspace/remove_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (o *RemoveNetworkOption) DeleteNetworkRuleWithDashClient(ctx context.Contex
UserName: o.UserName,
Index: int32(index),
}
o.Logr.DebugAll().Info("WorkspaceServiceClient.DeleteNetworkRule", "req", req)
res, err := c.WorkspaceServiceClient.DeleteNetworkRule(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/workspace/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (o *ResumeOption) ResumeWorkspaceWithDashClient(ctx context.Context, worksp
Replicas: ptr.To(int64(1)),
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.UpdateWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.UpdateWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/workspace/suspend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (o *SuspendOption) SuspendWorkspaceWithDashClient(ctx context.Context, work
Replicas: ptr.To(int64(0)),
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.UpdateWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.UpdateWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return fmt.Errorf("failed to connect dashboard server: %w", err)
Expand Down
9 changes: 6 additions & 3 deletions internal/cmd/workspace/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (o *UpdateOption) UpdateWorkspaceWithDashClient(ctx context.Context) (*dash
Vars: o.vars,
}
c := o.CosmoDashClient
o.Logr.DebugAll().Info("WorkspaceServiceClient.UpdateWorkspace", "req", req)
res, err := c.WorkspaceServiceClient.UpdateWorkspace(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -152,10 +153,12 @@ func (o *UpdateOption) UpdateWorkspaceWithDashClient(ctx context.Context) (*dash
}

func (o *UpdateOption) UpdateWorkspaceWithKubeClient(ctx context.Context) (*dashv1alpha1.Workspace, error) {
c := o.KosmoClient
ws, err := c.UpdateWorkspace(ctx, o.UserName, o.WorkspaceName, kosmo.UpdateWorkspaceOpts{
opts := kosmo.UpdateWorkspaceOpts{
Vars: o.vars,
})
}
c := o.KosmoClient
o.Logr.DebugAll().Info("UpdateWorkspace", "userName", o.UserName, "workspaceName", o.WorkspaceName, "opts", opts)
ws, err := c.UpdateWorkspace(ctx, o.UserName, o.WorkspaceName, opts)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/workspace/upsert_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (o *UpsertNetworkOption) UpsertNetworkRuleWithDashClient(ctx context.Contex
NetworkRule: apiconv.C2D_NetworkRule(o.rule),
Index: int32(index),
}
o.Logr.DebugAll().Info("WorkspaceServiceClient.UpsertNetworkRule", "req", req)
res, err := c.WorkspaceServiceClient.UpsertNetworkRule(ctx, cli.NewRequestWithToken(req, o.CliConfig))
if err != nil {
return nil, fmt.Errorf("failed to connect dashboard server: %w", err)
Expand All @@ -149,6 +150,7 @@ func (o *UpsertNetworkOption) UpsertNetworkRuleByKubeClient(ctx context.Context)
}
index := cosmov1alpha1.GetNetworkRuleIndex(ws.Spec.Network, o.rule)

o.Logr.DebugAll().Info("AddNetworkRule", "workspaceName", o.WorkspaceName, "userName", o.UserName, "rule", o.rule, "index", index)
cr, err := c.AddNetworkRule(ctx, o.WorkspaceName, o.UserName, o.rule, index)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion internal/dashboard/__snapshots__/workspace_handler_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,10 @@ SnapShot = """
]
},
\"status\": {
\"phase\": \"Pending\"
\"phase\": \"Pending\",
\"last_started_at\": {
\"seconds\": 1234567890
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion internal/dashboard/workspace_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
. "github.com/cosmo-workspace/cosmo/pkg/snap"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/utils/ptr"

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
Expand Down Expand Up @@ -232,6 +233,12 @@ var _ = Describe("Dashboard server [Workspace]", func() {

//==================================================================================
Describe("[UpdateWorkspace]", func() {
updateWorkspaceResponseSnap := func(r *dashv1alpha1.UpdateWorkspaceResponse) *dashv1alpha1.UpdateWorkspaceResponse {
if r.Workspace.Status.LastStartedAt != nil {
r.Workspace.Status.LastStartedAt = timestamppb.New(time.Unix(1234567890, 0))
}
return r
}

run_test := func(loginUser string, req *dashv1alpha1.UpdateWorkspaceRequest) {
testUtil.CreateWorkspace("admin-user", "ws1", "template1", map[string]string{})
Expand All @@ -240,7 +247,7 @@ var _ = Describe("Dashboard server [Workspace]", func() {
ctx := context.Background()
res, err := client.UpdateWorkspace(ctx, NewRequestWithSession(req, getSession(loginUser)))
if err == nil {
Ω(res.Msg).To(MatchSnapShot())
Ω(updateWorkspaceResponseSnap(res.Msg)).To(MatchSnapShot())
wsv1Workspace, err := k8sClient.GetWorkspaceByUserName(context.Background(), req.WsName, req.UserName)
Expect(err).NotTo(HaveOccurred())
Ω(ObjectSnapshot(wsv1Workspace)).To(MatchSnapShot())
Expand Down
Loading

0 comments on commit a0e929b

Please sign in to comment.