Skip to content

Commit

Permalink
feat: switch currently viewed cluster (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanHoladay authored Nov 6, 2024
1 parent fe41c35 commit 4403aa4
Show file tree
Hide file tree
Showing 29 changed files with 960 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
type: [unit, e2e, e2e-in-cluster, api, local-auth, e2e-reconnect]
type: [unit, e2e, e2e-in-cluster, api, local-auth, e2e-connections]
steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
Expand Down
62 changes: 62 additions & 0 deletions src/pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@ const docTemplate = `{
}
}
},
"/api/v1/cluster": {
"post": {
"description": "Switch the currently viewed cluster",
"consumes": [
"application/json"
],
"tags": [
"cluster"
],
"parameters": [
{
"description": "example body: {",
"name": "request",
"in": "body",
"required": true,
"schema": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/client.ClusterInfo"
}
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v1/clusters": {
"get": {
"description": "get list of clusters with context names from local kubeconfig",
"produces": [
"application/json"
],
"tags": [
"clusters"
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v1/resources/cluster-ops/hpas": {
"get": {
"description": "Get HPAs",
Expand Down Expand Up @@ -3213,6 +3259,22 @@ const docTemplate = `{
}
}
}
},
"definitions": {
"client.ClusterInfo": {
"type": "object",
"properties": {
"context": {
"type": "string"
},
"name": {
"type": "string"
},
"selected": {
"type": "boolean"
}
}
}
}
}`

Expand Down
62 changes: 62 additions & 0 deletions src/pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,52 @@
}
}
},
"/api/v1/cluster": {
"post": {
"description": "Switch the currently viewed cluster",
"consumes": [
"application/json"
],
"tags": [
"cluster"
],
"parameters": [
{
"description": "example body: {",
"name": "request",
"in": "body",
"required": true,
"schema": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/client.ClusterInfo"
}
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v1/clusters": {
"get": {
"description": "get list of clusters with context names from local kubeconfig",
"produces": [
"application/json"
],
"tags": [
"clusters"
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v1/resources/cluster-ops/hpas": {
"get": {
"description": "Get HPAs",
Expand Down Expand Up @@ -3202,5 +3248,21 @@
}
}
}
},
"definitions": {
"client.ClusterInfo": {
"type": "object",
"properties": {
"context": {
"type": "string"
},
"name": {
"type": "string"
},
"selected": {
"type": "boolean"
}
}
}
}
}
39 changes: 39 additions & 0 deletions src/pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
definitions:
client.ClusterInfo:
properties:
context:
type: string
name:
type: string
selected:
type: boolean
type: object
info:
contact: {}
paths:
Expand All @@ -10,6 +20,35 @@ paths:
description: OK
tags:
- auth
/api/v1/cluster:
post:
consumes:
- application/json
description: Switch the currently viewed cluster
parameters:
- description: 'example body: {'
in: body
name: request
required: true
schema:
additionalProperties:
$ref: '#/definitions/client.ClusterInfo'
type: object
responses:
"200":
description: OK
tags:
- cluster
/api/v1/clusters:
get:
description: get list of clusters with context names from local kubeconfig
produces:
- application/json
responses:
"200":
description: OK
tags:
- clusters
/api/v1/resources/cluster-ops/hpas:
get:
consumes:
Expand Down
20 changes: 20 additions & 0 deletions src/pkg/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "github.com/defenseunicorns/uds-runtime/src/pkg/api/docs" //nolint:staticcheck
"github.com/defenseunicorns/uds-runtime/src/pkg/api/resources"
"github.com/defenseunicorns/uds-runtime/src/pkg/api/rest"
"github.com/defenseunicorns/uds-runtime/src/pkg/k8s/client"
"github.com/defenseunicorns/uds-runtime/src/pkg/k8s/session"
)

Expand Down Expand Up @@ -965,3 +966,22 @@ func healthz(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}

// @Description get list of clusters with context names from local kubeconfig
// @Tags clusters
// @Produce json
// @Success 200
// @Router /api/v1/clusters [get]
func getClusters(ks *session.K8sSession) func(w http.ResponseWriter, r *http.Request) {
return client.ServeClusters(ks.InCluster, ks.CurrentCtx)
}

// @Description Switch the currently viewed cluster
// @Tags cluster
// @Param request body map[string]client.ClusterInfo true "example body: {"cluster": {"name": string, "context": string, "selected": bool}}"
// @Accept json
// @Success 200
// @Router /api/v1/cluster [post]
func switchCluster(ks *session.K8sSession) func(w http.ResponseWriter, r *http.Request) {
return ks.Switch()
}
12 changes: 9 additions & 3 deletions src/pkg/api/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var inAirgap bool
// @BasePath /api/v1
// @schemes http https
func Setup(assets *embed.FS) (*chi.Mux, bool, error) {
// Create a k8s session
k8sSession, err := session.CreateK8sSession()
if err != nil {
return nil, false, fmt.Errorf("failed to setup k8s session: %w", err)
Expand All @@ -45,7 +44,6 @@ func Setup(assets *embed.FS) (*chi.Mux, bool, error) {
inCluster := k8sSession.InCluster

if !inCluster {
// Start the cluster monitoring goroutine
go k8sSession.StartClusterMonitoring()
}

Expand All @@ -66,10 +64,12 @@ func Setup(assets *embed.FS) (*chi.Mux, bool, error) {
r.Get("/cluster-check", checkClusterConnection(k8sSession))
r.Route("/api/v1", func(r chi.Router) {
r.Get("/auth", authHandler)
r.Get("/clusters", withLatestSession(k8sSession, getClusters))
r.Post("/cluster", switchCluster(k8sSession))
r.Route("/monitor", func(r chi.Router) {
r.Get("/pepr/", monitor.Pepr)
r.Get("/pepr/{stream}", monitor.Pepr)
r.Get("/cluster-overview", monitor.BindClusterOverviewHandler(k8sSession.Cache))
r.Get("/cluster-overview", withLatestCache(k8sSession, monitor.BindClusterOverviewHandler))
})

r.Route("/resources", func(r chi.Router) {
Expand Down Expand Up @@ -307,3 +307,9 @@ func withLatestCache(k8sSession *session.K8sSession, handler func(cache *resourc
handler(k8sSession.Cache)(w, r)
}
}

func withLatestSession(k8sSession *session.K8sSession, handler func(k8sSession *session.K8sSession) func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handler(k8sSession)(w, r)
}
}
Loading

0 comments on commit 4403aa4

Please sign in to comment.