From 41bb584a6d586a6f864985256d6d08e6f33100bf Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Tue, 24 Oct 2023 18:40:23 +0800 Subject: [PATCH] update swagger info for SwaggerUI (#1009) * fix: config argoCD and fluxCD by kubesphere.yaml * filter out ApiVersions that not start with /kapis * update swagger info --- cmd/apiserver/app/options/options.go | 4 ++-- cmd/controller/app/options/options.go | 2 +- docs/swagger.md | 5 ++++- pkg/config/gitops.go | 12 ++++++------ pkg/kapis/doc/registry.go | 22 ++++++++++++++++++++-- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cmd/apiserver/app/options/options.go b/cmd/apiserver/app/options/options.go index b0fbd8cc..1e55e606 100644 --- a/cmd/apiserver/app/options/options.go +++ b/cmd/apiserver/app/options/options.go @@ -68,8 +68,8 @@ func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) { s.JenkinsOptions.AddFlags(fss.FlagSet("devops"), s.JenkinsOptions) s.SonarQubeOptions.AddFlags(fss.FlagSet("sonarqube"), s.SonarQubeOptions) s.S3Options.AddFlags(fss.FlagSet("s3"), s.S3Options) - s.ArgoCDOption.AddFlags(fss.FlagSet("argocd")) - s.FluxCDOption.AddFlags(fss.FlagSet("fluxcd")) + s.ArgoCDOption.AddFlags(fss.FlagSet("argocd"), s.ArgoCDOption) + s.FluxCDOption.AddFlags(fss.FlagSet("fluxcd"), s.FluxCDOption) fs = fss.FlagSet("klog") local := flag.NewFlagSet("klog", flag.ExitOnError) diff --git a/cmd/controller/app/options/options.go b/cmd/controller/app/options/options.go index 70ceb6f1..2847522c 100644 --- a/cmd/controller/app/options/options.go +++ b/cmd/controller/app/options/options.go @@ -81,7 +81,7 @@ func (s *DevOpsControllerManagerOptions) Flags() cliflag.NamedFlagSets { s.KubernetesOptions.AddFlags(fss.FlagSet("kubernetes"), s.KubernetesOptions) s.JenkinsOptions.AddFlags(fss.FlagSet("devops"), s.JenkinsOptions) s.FeatureOptions.AddFlags(fss.FlagSet("feature"), s.FeatureOptions) - s.ArgoCDOption.AddFlags(fss.FlagSet("argocd")) + s.ArgoCDOption.AddFlags(fss.FlagSet("argocd"), s.ArgoCDOption) fs := fss.FlagSet("leaderelection") s.bindLeaderElectionFlags(s.LeaderElection, fs) diff --git a/docs/swagger.md b/docs/swagger.md index 0eaeb3cd..ff98015c 100644 --- a/docs/swagger.md +++ b/docs/swagger.md @@ -7,4 +7,7 @@ make swagger-ui Then, start the APIServer and explore all API documentation via the Swagger UI: . -* The URL pattern is like `http://ip:port/apidocs/?url=http://ip:port/apidocs.json` \ No newline at end of file +* The URL pattern is like `http://ip:port/apidocs/?url=http://ip:port/apidocs.json` + +--- +In kubesphere enabled DevOps, you could update service type of devops-apiserver to NodePort, and then via the Swagger UI: `http://ip:NodePort/apidocs/?url=http://ip:NodePort/apidocs.json`. diff --git a/pkg/config/gitops.go b/pkg/config/gitops.go index ee9cdff7..2cc65d63 100644 --- a/pkg/config/gitops.go +++ b/pkg/config/gitops.go @@ -23,15 +23,15 @@ import ( // ArgoCDOption as the ArgoCD integration configuration type ArgoCDOption struct { - Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty" description:"enabled FluxCD"` + Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty" description:"enabled ArgoCD"` Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty" description:"Which namespace the ArgoCD located"` } // AddFlags adds the flags which related to argocd -func (o *ArgoCDOption) AddFlags(fs *pflag.FlagSet) { - fs.BoolVar(&o.Enabled, "argocd-enabled", false, "Enable ArgoCD APIs") +func (o *ArgoCDOption) AddFlags(fs *pflag.FlagSet, parentOptions *ArgoCDOption) { + fs.BoolVar(&o.Enabled, "argocd-enabled", parentOptions.Enabled, "Enable ArgoCD APIs") // see also https://argo-cd.readthedocs.io/en/stable/getting_started/ - fs.StringVarP(&o.Namespace, "argocd-namespace", o.Namespace, "argocd", "Which namespace the ArgoCD located") + fs.StringVar(&o.Namespace, "argocd-namespace", parentOptions.Namespace, "Which namespace the ArgoCD located") } // FluxCDOption as the FluxCD integration configuration @@ -40,8 +40,8 @@ type FluxCDOption struct { } // AddFlags adds the flags which related to fluxcd -func (o *FluxCDOption) AddFlags(fs *pflag.FlagSet) { - fs.BoolVar(&o.Enabled, "fluxcd-enabled", false, "Enable FluxCD APIs") +func (o *FluxCDOption) AddFlags(fs *pflag.FlagSet, parentOptions *FluxCDOption) { + fs.BoolVar(&o.Enabled, "fluxcd-enabled", parentOptions.Enabled, "Enable FluxCD APIs") } // GetGitOpsEngine return gitops engine type diff --git a/pkg/kapis/doc/registry.go b/pkg/kapis/doc/registry.go index a2b64165..390d492b 100644 --- a/pkg/kapis/doc/registry.go +++ b/pkg/kapis/doc/registry.go @@ -19,14 +19,32 @@ package doc import ( "github.com/emicklei/go-restful" swagger "github.com/emicklei/go-restful-swagger12" + "kubesphere.io/devops/pkg/apiserver/runtime" + "strings" ) // AddSwaggerService adds the Swagger service func AddSwaggerService(wss []*restful.WebService, c *restful.Container) { + var wssWithSchema []*restful.WebService + for _, service := range wss { + if strings.HasPrefix(service.RootPath(), runtime.ApiRootPath) { + wssWithSchema = append(wssWithSchema, service) + } + } + config := swagger.Config{ - WebServices: wss, + WebServices: wssWithSchema, ApiPath: "/apidocs.json", SwaggerPath: "/apidocs/", - SwaggerFilePath: "bin/swagger-ui/dist"} + SwaggerFilePath: "bin/swagger-ui/dist", + Info: swagger.Info{ + Title: "KubeSphere DevOps", + Description: "KubeSphere DevOps OpenAPI", + TermsOfServiceUrl: "https://kubesphere.io/", + Contact: "kubesphere@yunify.com", + License: "Apache 2.0", + LicenseUrl: "https://www.apache.org/licenses/LICENSE-2.0.html", + }, + } swagger.RegisterSwaggerService(config, c) }