Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Mar 21, 2024
1 parent 6a3df93 commit 1af720e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "github.com/gin-gonic/gin"
func RegisterHandlers(r *gin.Engine) {
registerUserHandlers(r)
registerMyLabHandlers(r)
registerPublicHandlers(r)
}
42 changes: 36 additions & 6 deletions cmd/mylab-handler.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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{}
Expand All @@ -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)
}
25 changes: 25 additions & 0 deletions cmd/public-handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 5 additions & 7 deletions cmd/user-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions internal/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -45,3 +46,8 @@ func ListPods() {
}
}
}

func GetEvents(namespace string) watch.Interface {
events, _ := clientset.EventsV1().Events(namespace).Watch(context.TODO(), metav1.ListOptions{})
return events
}

0 comments on commit 1af720e

Please sign in to comment.