Skip to content
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

Store workspace last start and stop time in annotation #807

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions api/v1alpha1/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const (
WorkspaceTemplateAnnKeyDeploymentName = "workspace.cosmo-workspace.github.io/deployment"
WorkspaceTemplateAnnKeyServiceName = "workspace.cosmo-workspace.github.io/service"
WorkspaceTemplateAnnKeyServiceMainPort = "workspace.cosmo-workspace.github.io/service-main-port"

WorkspaceAnnKeyLastStoppedAt = "workspace.cosmo-workspace.github.io/last-stopped-at"
WorkspaceAnnKeyLastStartedAt = "workspace.cosmo-workspace.github.io/last-started-at"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions hack/local-run-test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ apply-template: kubectl cosmoctl ## Apply template.
add-user: kubectl cosmoctl ## add user
@echo ====== $@ ======
-cosmoctl user create tom --admin 2> /dev/null
-cosmoctl user create gryffindor-dev --role "gryffindor" 2> /dev/null
-cosmoctl user create gryffindor-admin --role "gryffindor-admin" 2> /dev/null
-cosmoctl user create gryffindor-dev --role "gryffindor" --addon resource-limitter --addon gryffindor-serviceaccount 2> /dev/null
-cosmoctl user create gryffindor-admin --role "gryffindor-admin" --addon resource-limitter --addon gryffindor-serviceaccount 2> /dev/null
-cosmoctl user create slytherin-dev --role "slytherin" 2> /dev/null
-cosmoctl user create slytherin-admin --role "slytherin-admin" 2> /dev/null
-cosmoctl user create grytherin --role "gryffindor,slytherin" 2> /dev/null
-cosmoctl user create grytherin --role "gryffindor,slytherin" --addon resource-limitter --addon gryffindor-serviceaccount 2> /dev/null
-cosmoctl user create ldapuser1 --admin --auth-type ldap 2> /dev/null
-cosmoctl user reset-password tom --password vvv
-cosmoctl user reset-password gryffindor-dev --password xxxxxxxx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by cosmoctl - cosmo v1.0.0-rc2 cosmo-workspace 2023
# Generated by cosmoctl - cosmo v1.0.0-rc4 cosmo-workspace 2023
apiVersion: cosmo-workspace.github.io/v1alpha1
kind: Template
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by cosmoctl - cosmo v1.0.0-rc2 cosmo-workspace 2023
# Generated by cosmoctl - cosmo v1.0.0-rc4 cosmo-workspace 2023
apiVersion: cosmo-workspace.github.io/v1alpha1
kind: Template
metadata:
Expand Down
10 changes: 8 additions & 2 deletions internal/dashboard/__snapshots__/workspace_handler_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,10 @@ SnapShot = """
\"metadata\": {
\"name\": \"ws1\",
\"namespace\": \"cosmo-user-admin-user\",
\"creationTimestamp\": null
\"creationTimestamp\": null,
\"annotations\": {
\"workspace.cosmo-workspace.github.io/last-stopped-at\": \"MASKED\"
}
},
\"spec\": {
\"template\": {
Expand Down Expand Up @@ -643,7 +646,10 @@ SnapShot = """
\"metadata\": {
\"name\": \"ws1\",
\"namespace\": \"cosmo-user-normal-user\",
\"creationTimestamp\": null
\"creationTimestamp\": null,
\"annotations\": {
\"workspace.cosmo-workspace.github.io/last-started-at\": \"MASKED\"
}
},
\"spec\": {
\"template\": {
Expand Down
10 changes: 10 additions & 0 deletions pkg/kosmo/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"sort"
"time"

"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -13,6 +14,7 @@ import (

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/kubeutil"
"github.com/cosmo-workspace/cosmo/pkg/workspace"
)

Expand Down Expand Up @@ -137,6 +139,14 @@ func (c *Client) UpdateWorkspace(ctx context.Context, name, username string, opt
return nil, apierrs.NewBadRequest("no change")
}

if opts.Replicas != nil {
if *opts.Replicas == 0 {
kubeutil.SetAnnotation(ws, cosmov1alpha1.WorkspaceAnnKeyLastStoppedAt, time.Now().Format(time.RFC3339))
} else {
kubeutil.SetAnnotation(ws, cosmov1alpha1.WorkspaceAnnKeyLastStartedAt, time.Now().Format(time.RFC3339))
}
}

if err := c.Update(ctx, ws); err != nil {
log.Error(err, "failed to update workspace", "username", username, "workspace", ws.Name)
return nil, fmt.Errorf("failed to update workspace: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/kubeutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ func GetAnnotation(obj AnnotationHolder, key string) string {
return ann[key]
}

func SetAnnotation(obj AnnotationHolder, key, value string) {
ann := obj.GetAnnotations()
if ann == nil {
ann = make(map[string]string)
}
ann[key] = value
obj.SetAnnotations(ann)
}

type LabelHolder interface {
GetLabels() map[string]string
SetLabels(map[string]string)
Expand Down
9 changes: 9 additions & 0 deletions pkg/snap/object_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ func RemoveDynamicFields(o client.Object) {
ownerRefs[i] = v
}
o.SetOwnerReferences(ownerRefs)

if ann := o.GetAnnotations(); ann != nil {
if _, ok := ann[cosmov1alpha1.WorkspaceAnnKeyLastStartedAt]; ok {
ann[cosmov1alpha1.WorkspaceAnnKeyLastStartedAt] = "MASKED"
}
if _, ok := ann[cosmov1alpha1.WorkspaceAnnKeyLastStoppedAt]; ok {
ann[cosmov1alpha1.WorkspaceAnnKeyLastStoppedAt] = "MASKED"
}
}
}