From 1af720eab24facc9bb5bb6a91ed5bc00cab48c07 Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Thu, 21 Mar 2024 18:39:01 +0100 Subject: [PATCH] WIP --- cmd/handlers.go | 1 + cmd/mylab-handler.go | 42 ++++++++++++++++++++++++++----- cmd/public-handler.go | 25 ++++++++++++++++++ cmd/user-handler.go | 12 ++++----- internal/kubernetes/kubernetes.go | 10 ++++++-- 5 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 cmd/public-handler.go diff --git a/cmd/handlers.go b/cmd/handlers.go index 6ff6223..0310b6d 100644 --- a/cmd/handlers.go +++ b/cmd/handlers.go @@ -5,4 +5,5 @@ import "github.com/gin-gonic/gin" func RegisterHandlers(r *gin.Engine) { registerUserHandlers(r) registerMyLabHandlers(r) + registerPublicHandlers(r) } diff --git a/cmd/mylab-handler.go b/cmd/mylab-handler.go index 596fcbd..c832d84 100644 --- a/cmd/mylab-handler.go +++ b/cmd/mylab-handler.go @@ -1,13 +1,18 @@ package cmd import ( + "io" "net/http" "github.com/inseefrlab/onyxia-admin/internal/helm" + "github.com/inseefrlab/onyxia-admin/internal/kubernetes" "github.com/gin-gonic/gin" + eventsv1 "k8s.io/api/events/v1" ) +var namespace = "user-f2wbnp" + type App struct { ID string `json:"id"` Chart string `json:"chart"` @@ -16,14 +21,20 @@ type MyServices struct { Apps []App `apps:"apps"` } -// PingExample godoc -// @Summary ping example +type Quotas struct { + Spec []Quota `spec:"spec"` + Usage []Quota `usage:"usage"` +} + +type Quota struct { +} + +// @Summary List the services installed in a namespace. // @Schemes -// @Description do ping -// @Tags example -// @Accept json +// @Description +// @Tags My lab // @Produce json -// @Success 200 {string} Helloworld +// @Success 200 // @Router /my-lab/services [get] func myServices(c *gin.Context) { myServices := MyServices{} @@ -33,6 +44,25 @@ func myServices(c *gin.Context) { c.JSON(http.StatusOK, myServices) } +func events(c *gin.Context) { + c.Stream(func(w io.Writer) bool { + for event := range kubernetes.GetEvents(namespace).ResultChan() { + item := event.Object.(*eventsv1.Event) + c.SSEvent("message", item) + } + return false + }) +} + +func quotas(c *gin.Context) { + /*quotas := Quotas{} + for { + quotas.Quota = append + }*/ +} + func registerMyLabHandlers(r *gin.Engine) { r.GET("/my-lab/services", myServices) + r.GET("/my-lab/events", events) + r.GET("/my-lab/quota", quotas) } diff --git a/cmd/public-handler.go b/cmd/public-handler.go new file mode 100644 index 0000000..b7a51e2 --- /dev/null +++ b/cmd/public-handler.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// PingExample godoc +// @Summary Get your public IP address. +// @Schemes +// @Description Get the public IP (as seen by this app). +// @Tags public +// @Produce json +// @Success 200 {string} Helloworld +// @Router /public/ip [get] +func ip(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "ip": c.RemoteIP(), + }) +} + +func registerPublicHandlers(r *gin.Engine) { + r.GET("/public/ip", ip) +} diff --git a/cmd/user-handler.go b/cmd/user-handler.go index 3f809ab..c32d0a6 100644 --- a/cmd/user-handler.go +++ b/cmd/user-handler.go @@ -22,15 +22,13 @@ type UserInfo struct { Projects []Project `json:"projects"` } -// PingExample godoc -// @Summary ping example +// @Summary Get user info // @Schemes -// @Description do ping -// @Tags example -// @Accept json +// @Description Get user info and projects +// @Tags user // @Produce json -// @Success 200 {string} Helloworld -// @Router /example/helloworld [get] +// @Success 200 +// @Router /user/info [get] func userInfo(c *gin.Context) { claims, _ := c.Get("claims") var userInfo UserInfo diff --git a/internal/kubernetes/kubernetes.go b/internal/kubernetes/kubernetes.go index 4bb442a..dc2c2f3 100644 --- a/internal/kubernetes/kubernetes.go +++ b/internal/kubernetes/kubernetes.go @@ -7,6 +7,7 @@ import ( "go.uber.org/zap" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -35,8 +36,8 @@ func InitClient() { zap.L().Sugar().Infof("Kubernetes client initialized, %s", version.String()) } -func ListPods() { - pods, err := clientset.CoreV1().Pods("user-f2wbnp").List(context.TODO(), metav1.ListOptions{}) +func ListPods(namespace string) { + pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { zap.L().Sugar().Errorf("Failed retrieving pods %s", err) } else { @@ -45,3 +46,8 @@ func ListPods() { } } } + +func GetEvents(namespace string) watch.Interface { + events, _ := clientset.EventsV1().Events(namespace).Watch(context.TODO(), metav1.ListOptions{}) + return events +}