feat: add schema service (#2593)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-05-27 14:41:14 +08:00 committed by GitHub
parent 9c87926929
commit 38944a5d2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 13 deletions

View File

@ -19,7 +19,7 @@ type KubeKeyWebOptions struct {
Port int // Port specifies the port number for the web server
Workdir string // Workdir specifies the base directory for KubeKey
JSONSchema string
SchemaPath string
}
// NewKubeKeyWebOptions creates and returns a new KubeKeyWebOptions instance with default values
@ -45,7 +45,7 @@ func (o *KubeKeyWebOptions) Flags() cliflag.NamedFlagSets {
wfs := fss.FlagSet("web flags")
wfs.IntVar(&o.Port, "port", o.Port, fmt.Sprintf("the server port of kubekey web default is: %d", o.Port))
wfs.StringVar(&o.Workdir, "workdir", o.Workdir, "the base Dir for kubekey. Default current dir. ")
wfs.StringVar(&o.JSONSchema, "json-schema", o.JSONSchema, "the json schema to render web ui.")
wfs.StringVar(&o.SchemaPath, "schema-path", o.SchemaPath, "the json schema dir path to render web ui.")
return fss
}

View File

@ -13,6 +13,9 @@ import (
"github.com/kubesphere/kubekey/v4/pkg/proxy"
)
// newWebCommand creates a new cobra command for starting the KubeKey web server
// It initializes the web server with the provided configuration options and starts
// the HTTP server with web UI interface
func newWebCommand() *cobra.Command {
o := options.NewKubeKeyWebOptions()
@ -20,22 +23,27 @@ func newWebCommand() *cobra.Command {
Use: "web",
Short: "start a http server with web UI.",
RunE: func(cmd *cobra.Command, args []string) error {
// Initialize REST config for Kubernetes client
restconfig := &rest.Config{}
if err := proxy.RestConfig(filepath.Join(o.Workdir, _const.RuntimeDir), restconfig); err != nil {
return err
}
// Create Kubernetes client with the REST config
client, err := ctrlclient.New(restconfig, ctrlclient.Options{
Scheme: _const.Scheme,
})
if err != nil {
return err
}
// Initialize and run the web manager with provided options
return manager.NewWebManager(manager.WebManagerOptions{
Workdir: o.Workdir,
Port: o.Port,
Client: client,
Config: restconfig,
Workdir: o.Workdir,
Port: o.Port,
SchemaPath: o.SchemaPath,
Client: client,
Config: restconfig,
}).Run(cmd.Context())
},
}

View File

@ -61,8 +61,9 @@ func NewControllerManager(o *options.ControllerManagerServerOptions) Manager {
// WebManagerOptions contains the configuration options for creating a new web manager
type WebManagerOptions struct {
Workdir string
Port int
Workdir string
Port int
SchemaPath string
ctrlclient.Client
*rest.Config
}
@ -70,9 +71,10 @@ type WebManagerOptions struct {
// NewWebManager creates and returns a new web manager instance with the provided options
func NewWebManager(o WebManagerOptions) Manager {
return &webManager{
workdir: o.Workdir,
port: o.Port,
Client: o.Client,
Config: o.Config,
workdir: o.Workdir,
port: o.Port,
schemaPath: o.SchemaPath,
Client: o.Client,
Config: o.Config,
}
}

View File

@ -22,6 +22,8 @@ type webManager struct {
port int
workdir string
schemaPath string
ctrlclient.Client
*rest.Config
}
@ -34,6 +36,7 @@ func (m webManager) Run(ctx context.Context) error {
logStackOnRecover(panicReason, httpWriter)
})
container.Add(web.NewWebService(ctx, m.workdir, m.Client, m.Config)).
Add(web.NewSchemaService(m.schemaPath)).
// openapi
Add(web.NewSwaggerUIService()).
Add(web.NewAPIService(container.RegisteredWebServices()))

View File

@ -19,7 +19,6 @@ import (
func NewSwaggerUIService() *restful.WebService {
ws := new(restful.WebService)
ws.Path("/swagger-ui")
ws.Produces(restful.MIME_JSON)
subFS, err := fs.Sub(config.Swagger, "swagger-ui")
if err != nil {

21
pkg/web/schema.go Normal file
View File

@ -0,0 +1,21 @@
package web
import (
"net/http"
"github.com/emicklei/go-restful/v3"
)
// NewSchemaService creates a new WebService that serves schema files from the specified root path.
// It sets up a route that handles GET requests to /schema/{subpath} and serves files from the rootPath directory.
// The {subpath:*} parameter allows for matching any path under /schema/.
func NewSchemaService(rootPath string) *restful.WebService {
ws := new(restful.WebService)
ws.Path("/schema")
ws.Route(ws.GET("/{subpath:*}").To(func(req *restful.Request, resp *restful.Response) {
http.StripPrefix("/schema/", http.FileServer(http.Dir(rootPath))).ServeHTTP(resp.ResponseWriter, req.Request)
}))
return ws
}