diff --git a/cmd/kk/app/options/web.go b/cmd/kk/app/options/web.go index 66790b6a..463fb00f 100644 --- a/cmd/kk/app/options/web.go +++ b/cmd/kk/app/options/web.go @@ -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 } diff --git a/cmd/kk/app/web.go b/cmd/kk/app/web.go index 3c4dddbb..91f1113f 100644 --- a/cmd/kk/app/web.go +++ b/cmd/kk/app/web.go @@ -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()) }, } diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index b079e0bc..e2ddce57 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -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, } } diff --git a/pkg/manager/web_manager.go b/pkg/manager/web_manager.go index eccc1386..043fb86e 100644 --- a/pkg/manager/web_manager.go +++ b/pkg/manager/web_manager.go @@ -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())) diff --git a/pkg/web/openapi.go b/pkg/web/openapi.go index 3a77c2ad..9fd81c8e 100644 --- a/pkg/web/openapi.go +++ b/pkg/web/openapi.go @@ -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 { diff --git a/pkg/web/schema.go b/pkg/web/schema.go new file mode 100644 index 00000000..55f3e5ff --- /dev/null +++ b/pkg/web/schema.go @@ -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 +}