Skip to content

Commit

Permalink
feat(endpoint): Add update notebook start / stop endpoint
Browse files Browse the repository at this point in the history
Co-authored-by: William Hearn <william.hearn@canada.ca>
Signed-off-by: zachomedia <zachary.seguin@statcan.gc.ca>
  • Loading branch information
zachomedia and sylus committed Feb 16, 2022
1 parent dfa205f commit dcfba4c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion httputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
// APIResponse contains the basic fields of a response from the APIs.
type APIResponse struct {
Success bool `json:"success"`
Log string `json:"log"`
Status int `json:"status"`
Log string `json:"log,omitempty"`
User string `json:"user"`
}

// respond response returns a JSON response to the client.
Expand Down
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func main() {
},
},
}, s.GetNamespaces)).Methods("GET")

router.HandleFunc("/api/namespaces/{namespace}/notebooks", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Expand All @@ -167,6 +168,7 @@ func main() {
},
},
}, s.GetNotebooks)).Methods("GET")

router.HandleFunc("/api/namespaces/{namespace}/notebooks", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Expand All @@ -177,6 +179,18 @@ func main() {
},
},
}, s.NewNotebook)).Headers("Content-Type", "application/json").Methods("POST")

router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "update",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.UpdateNotebook)).Methods("PATCH")

router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Expand Down
67 changes: 67 additions & 0 deletions notebooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"sort"
"strings"
"time"

kubeflowv1 "github.com/StatCan/kubeflow-controller/pkg/apis/kubeflowcontroller/v1"
"github.com/andanhm/go-prettytime"
Expand All @@ -34,6 +35,9 @@ const SharedMemoryVolumePath string = "/dev/shm"
// EnvKfLanguage String.
const EnvKfLanguage string = "KF_LANG"

// StoppedAnnotation String.
const StoppedAnnotation string = "stopped"

type volumetype string

const (
Expand Down Expand Up @@ -96,6 +100,10 @@ type notebooksresponse struct {
Notebooks []notebookresponse `json:"notebooks"`
}

type updatenotebookrequest struct {
Stopped bool `json:"stopped"`
}

//
// EVENT_TYPE_NORMAL = "Normal"
// EVENT_TYPE_WARNING = "Warning"
Expand Down Expand Up @@ -558,3 +566,62 @@ func (s *server) DeleteNotebook(w http.ResponseWriter, r *http.Request) {
Success: true,
})
}

func (s *server) UpdateNotebook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespaceName := vars["namespace"]
notebookName := vars["notebook"]

log.Printf("deleting notebook %q for %q", notebookName, namespaceName)

// Read the incoming notebook
body, err := ioutil.ReadAll(r.Body)
if err != nil {
s.error(w, r, err)
return
}
defer r.Body.Close()

var req updatenotebookrequest
err = json.Unmarshal(body, &req)
if err != nil {
s.error(w, r, err)
return
}

// Read existing notebook
notebook, err := s.listers.notebooks.Notebooks(namespaceName).Get(notebookName)
if err != nil {
s.error(w, r, err)
return
}

update := false
updatedNotebook := notebook.DeepCopy()

// Compare start/stopped state
if _, ok := notebook.Annotations[StoppedAnnotation]; ok != req.Stopped {
update = true

if req.Stopped {
// Set the stopped annotation
updatedNotebook.Annotations[StoppedAnnotation] = time.Now().Format(time.RFC3339)
} else {
// Remove the stopped annotation
delete(updatedNotebook.Annotations, StoppedAnnotation)
}
}

if update {
_, err = s.clientsets.kubeflow.KubeflowV1().Notebooks(namespaceName).Update(r.Context(), updatedNotebook, v1.UpdateOptions{})
if err != nil {
s.error(w, r, err)
return
}
}

s.respond(w, r, APIResponse{
Success: true,
Status: http.StatusOK,
})
}

0 comments on commit dcfba4c

Please sign in to comment.