Skip to content

Commit

Permalink
Merge branch 'cosmo-workspace:main' into robust-reconcililation2
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner authored Aug 30, 2023
2 parents ff93bc1 + c987b81 commit ae37582
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 7 deletions.
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 @@ -97,4 +97,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"
}
}
}

0 comments on commit ae37582

Please sign in to comment.