feat: add rest routes to take the control of flink jobs

This commit is contained in:
2025-01-09 00:22:53 +03:30
parent 4dd82c6380
commit 0df874b222
14 changed files with 1019 additions and 12 deletions

28
internal/rest/base.go Normal file
View File

@@ -0,0 +1,28 @@
package rest
import (
"fmt"
"log"
"github.com/danielgtaylor/huma/v2"
humaFiber "github.com/danielgtaylor/huma/v2/adapters/humafiber"
"github.com/gofiber/fiber/v2"
)
func Init() {
app := fiber.New()
config := huma.DefaultConfig("Go API", "1.0.0")
config.Servers = []*huma.Server{{}}
config.Components.SecuritySchemes = map[string]*huma.SecurityScheme{
"auth": {
Type: "http",
Scheme: "bearer",
BearerFormat: "JWT",
},
}
api := humaFiber.New(app, config)
initRouter(api)
log.Fatal(app.Listen(fmt.Sprintf(":%s", "3000")))
}

View File

@@ -0,0 +1,81 @@
package controller
import (
"context"
"flink-kube-operator/internal/crd"
"flink-kube-operator/internal/crd/v1alpha1"
"flink-kube-operator/internal/manager"
"k8s.io/apimachinery/pkg/types"
)
type GetJobsReq struct {
}
type GetJobsResp struct {
Body []v1alpha1.FlinkJob
}
func GetJobs(ctx context.Context, req *GetJobsReq) (*GetJobsResp, error) {
jobs := []v1alpha1.FlinkJob{}
for _, key := range crd.GetAllJobKeys() {
job := crd.GetJob(key)
job.ManagedFields = nil
jobs = append(jobs, job)
}
return &GetJobsResp{Body: jobs}, nil
}
type StopJobReq struct {
JobUId string `path:"uid"`
}
type StopJobRespBody struct {
Success bool `json:"success"`
}
type StopJobResp struct {
Body StopJobRespBody
}
func StopJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := manager.GetManager()
job := mgr.GetJob(types.UID(req.JobUId))
err := job.Stop()
if err != nil {
return nil, err
}
return &StopJobResp{Body: StopJobRespBody{
Success: true,
}}, nil
}
func StartJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := manager.GetManager()
job := mgr.GetJob(types.UID(req.JobUId))
err := job.Run(true)
if err != nil {
return nil, err
}
return &StopJobResp{Body: StopJobRespBody{
Success: true,
}}, nil
}
func RemoveJobJar(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := manager.GetManager()
job := mgr.GetJob(types.UID(req.JobUId))
job.RemoveJar()
return &StopJobResp{Body: StopJobRespBody{
Success: true,
}}, nil
}
func PauseJob(ctx context.Context, req *StopJobReq) (*StopJobResp, error) {
mgr := manager.GetManager()
job := mgr.GetJob(types.UID(req.JobUId))
job.Pause()
return &StopJobResp{Body: StopJobRespBody{
Success: true,
}}, nil
}

55
internal/rest/router.go Normal file
View File

@@ -0,0 +1,55 @@
package rest
import (
"flink-kube-operator/internal/rest/controller"
"net/http"
"github.com/danielgtaylor/huma/v2"
)
func initRouter(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "get-jobs",
Method: http.MethodGet,
Path: "/jobs",
Summary: "Get Jobs",
Description: "Get Flink Jobs",
Tags: []string{"Job"},
}, controller.GetJobs)
huma.Register(api, huma.Operation{
OperationID: "stop-job",
Method: http.MethodPost,
Path: "/jobs/{uid}/stop",
Summary: "Stop Job",
Description: "Stop Flink Job",
Tags: []string{"Job"},
}, controller.StopJob)
huma.Register(api, huma.Operation{
OperationID: "start-job",
Method: http.MethodPost,
Path: "/jobs/{uid}/start",
Summary: "Start Job",
Description: "Start Flink Job",
Tags: []string{"Job"},
}, controller.StartJob)
huma.Register(api, huma.Operation{
OperationID: "remove-jar",
Method: http.MethodPost,
Path: "/jobs/{uid}/remove-jar",
Summary: "Remove Job Jar",
Description: "Remove Flink Job Jar",
Tags: []string{"Job"},
}, controller.RemoveJobJar)
huma.Register(api, huma.Operation{
OperationID: "pause-job",
Method: http.MethodPost,
Path: "/jobs/{uid}/pause",
Summary: "Pause Job",
Description: "Pause Flink Job",
Tags: []string{"Job"},
}, controller.PauseJob)
}