feat: add rest routes to take the control of flink jobs
This commit is contained in:
28
internal/rest/base.go
Normal file
28
internal/rest/base.go
Normal 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")))
|
||||
}
|
||||
81
internal/rest/controller/crd.go
Normal file
81
internal/rest/controller/crd.go
Normal 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
55
internal/rest/router.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user