Fix openapi schema (#2013)
* fix openapi spec fix openapi schema bug * fix api schema bug
This commit is contained in:
@@ -24,13 +24,18 @@ import (
|
||||
"fmt"
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/emicklei/go-restful-openapi"
|
||||
"github.com/go-openapi/loads"
|
||||
"github.com/go-openapi/spec"
|
||||
"github.com/go-openapi/strfmt"
|
||||
"github.com/go-openapi/validate"
|
||||
"github.com/pkg/errors"
|
||||
"io/ioutil"
|
||||
urlruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/klog"
|
||||
authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
fakeksClient "kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
devopsv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/devops/v1alpha2"
|
||||
iamv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/iam/v1alpha2"
|
||||
loggingv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/logging/v1alpha2"
|
||||
@@ -42,12 +47,13 @@ import (
|
||||
metricsv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/servicemesh/metrics/v1alpha2"
|
||||
tenantv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/tenant/v1alpha2"
|
||||
terminalv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/terminal/v1alpha2"
|
||||
"kubesphere.io/kubesphere/pkg/models/iam/am"
|
||||
"kubesphere.io/kubesphere/pkg/models/iam/im"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/devops/fake"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/mysql"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
|
||||
fakes3 "kubesphere.io/kubesphere/pkg/simple/client/s3/fake"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
|
||||
"kubesphere.io/kubesphere/pkg/version"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var output string
|
||||
@@ -58,27 +64,63 @@ func init() {
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
generateSwaggerJson()
|
||||
swaggerSpec := generateSwaggerJson()
|
||||
|
||||
err := validateSpec(swaggerSpec)
|
||||
if err != nil {
|
||||
klog.Warningf("Swagger specification has errors")
|
||||
}
|
||||
}
|
||||
|
||||
func generateSwaggerJson() {
|
||||
func validateSpec(apiSpec []byte) error {
|
||||
|
||||
swaggerDoc, err := loads.Analyzed(apiSpec, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempts to report about all errors
|
||||
validate.SetContinueOnErrors(true)
|
||||
|
||||
v := validate.NewSpecValidator(swaggerDoc.Schema(), strfmt.Default)
|
||||
result, _ := v.Validate(swaggerDoc)
|
||||
|
||||
if result.HasWarnings() {
|
||||
log.Printf("See warnings below:\n")
|
||||
for _, desc := range result.Warnings {
|
||||
log.Printf("- WARNING: %s\n", desc.Error())
|
||||
}
|
||||
|
||||
}
|
||||
if result.HasErrors() {
|
||||
str := fmt.Sprintf("The swagger spec is invalid against swagger specification %s.\nSee errors below:\n", swaggerDoc.Version())
|
||||
for _, desc := range result.Errors {
|
||||
str += fmt.Sprintf("- %s\n", desc.Error())
|
||||
}
|
||||
log.Println(str)
|
||||
return errors.New(str)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateSwaggerJson() []byte {
|
||||
|
||||
container := runtime.Container
|
||||
devopsv1alpha2Service := runtime.NewWebService(devopsv1alpha2.GroupVersion)
|
||||
urlruntime.Must(devopsv1alpha2.AddPipelineToWebService(devopsv1alpha2Service, &fake.Devops{}, &mysql.Database{}))
|
||||
urlruntime.Must(devopsv1alpha2.AddS2IToWebService(devopsv1alpha2Service, fakeksClient.NewSimpleClientset(), externalversions.NewSharedInformerFactory(
|
||||
fakeksClient.NewSimpleClientset(), time.Duration(0)), fakes3.NewFakeS3()))
|
||||
urlruntime.Must(devopsv1alpha2.AddSonarToWebService(devopsv1alpha2Service, &fake.Devops{}, &mysql.Database{}, sonarqube.NewSonar(nil)))
|
||||
container.Add(devopsv1alpha2Service)
|
||||
urlruntime.Must(iamv1alpha2.AddToContainer(container, nil, nil, nil, nil, nil))
|
||||
urlruntime.Must(loggingv1alpha2.AddToContainer(container, nil, nil))
|
||||
urlruntime.Must(monitoringv1alpha3.AddToContainer(container, nil, nil))
|
||||
urlruntime.Must(openpitrixv1.AddToContainer(container, nil, nil))
|
||||
urlruntime.Must(operationsv1alpha2.AddToContainer(container, nil))
|
||||
urlruntime.Must(resourcesv1alpha2.AddToContainer(container, nil, nil))
|
||||
urlruntime.Must(resourcesv1alpha3.AddToContainer(container, nil))
|
||||
urlruntime.Must(tenantv1alpha2.AddToContainer(container, nil, nil, nil))
|
||||
urlruntime.Must(terminalv1alpha2.AddToContainer(container, nil, nil))
|
||||
clientsets := k8s.NewNullClient()
|
||||
|
||||
informerFactory := informers.NewNullInformerFactory()
|
||||
|
||||
urlruntime.Must(devopsv1alpha2.AddToContainer(container, informerFactory.KubeSphereSharedInformerFactory(), &fake.Devops{}, nil, clientsets.KubeSphere(), fakes3.NewFakeS3()))
|
||||
urlruntime.Must(iamv1alpha2.AddToContainer(container, im.NewOperator(clientsets.KubeSphere(), informerFactory.KubeSphereSharedInformerFactory()), am.NewAMOperator(clientsets.KubeSphere(), informerFactory.KubeSphereSharedInformerFactory()), authoptions.NewAuthenticateOptions()))
|
||||
urlruntime.Must(loggingv1alpha2.AddToContainer(container, clientsets, nil))
|
||||
urlruntime.Must(monitoringv1alpha3.AddToContainer(container, clientsets.Kubernetes(), nil))
|
||||
urlruntime.Must(openpitrixv1.AddToContainer(container, informerFactory, nil))
|
||||
urlruntime.Must(operationsv1alpha2.AddToContainer(container, clientsets.Kubernetes()))
|
||||
urlruntime.Must(resourcesv1alpha2.AddToContainer(container, clientsets.Kubernetes(), informerFactory))
|
||||
urlruntime.Must(resourcesv1alpha3.AddToContainer(container, informerFactory))
|
||||
urlruntime.Must(tenantv1alpha2.AddToContainer(container, clientsets, informerFactory))
|
||||
urlruntime.Must(terminalv1alpha2.AddToContainer(container, clientsets.Kubernetes(), nil))
|
||||
urlruntime.Must(metricsv1alpha2.AddToContainer(container))
|
||||
|
||||
config := restfulspec.Config{
|
||||
@@ -125,7 +167,7 @@ func generateSwaggerJson() {
|
||||
},
|
||||
{
|
||||
Name: "Logging",
|
||||
Tags: []string{constants.LogQueryTag, constants.FluentBitSetting},
|
||||
Tags: []string{constants.LogQueryTag},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -135,6 +177,8 @@ func generateSwaggerJson() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("successfully written to %s", output)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func enrichSwaggerObject(swo *spec.Swagger) {
|
||||
@@ -151,7 +195,7 @@ func enrichSwaggerObject(swo *spec.Swagger) {
|
||||
Name: "Apache",
|
||||
URL: "http://www.apache.org/licenses/",
|
||||
},
|
||||
Version: "2.0.2",
|
||||
Version: version.Version,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user