main.go 6.3 KB
Newer Older
R
runzexia 已提交
1 2 3
package main

import (
R
runzexia 已提交
4
	"flag"
R
runzexia 已提交
5 6
	"io/ioutil"
	"k8s.io/apimachinery/pkg/api/meta"
R
runzexia 已提交
7
	servicemeshv1alpha2 "kubesphere.io/kubesphere/pkg/apis/servicemesh/v1alpha2"
R
runzexia 已提交
8 9 10 11 12 13 14 15 16 17 18 19
	"kubesphere.io/kubesphere/tools/lib"
	"log"
	"os"
	"path/filepath"

	"github.com/go-openapi/spec"
	s2iv1alpha1 "github.com/kubesphere/s2ioperator/pkg/apis/devops/v1alpha1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/runtime/serializer"
	urlruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/kube-openapi/pkg/common"
R
runzexia 已提交
20 21
	devopsinstall "kubesphere.io/kubesphere/pkg/apis/devops/crdinstall"
	devopsv1alpha1 "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha1"
R
runzexia 已提交
22 23
	networkinstall "kubesphere.io/kubesphere/pkg/apis/network/crdinstall"
	networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
R
runzexia 已提交
24
	servicemeshinstall "kubesphere.io/kubesphere/pkg/apis/servicemesh/crdinstall"
R
runzexia 已提交
25 26 27 28
	tenantinstall "kubesphere.io/kubesphere/pkg/apis/tenant/crdinstall"
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
)

R
runzexia 已提交
29 30 31 32 33 34
var output string

func init() {
	flag.StringVar(&output, "output", "./api/openapi-spec/swagger.json", "--output=./api/openapi-spec/swagger.json")
}

R
runzexia 已提交
35 36 37 38 39 40 41
func main() {

	var (
		Scheme = runtime.NewScheme()
		Codecs = serializer.NewCodecFactory(Scheme)
	)
	// tmp install s2i api group because
R
runzexia 已提交
42 43
	// cannot use Scheme (type *"kubesphere.io/kubesphere/vendor/k8s.io/apimachinery/pkg/runtime".Scheme) as
	// type *"github.com/kubesphere/s2ioperator/vendor/k8s.io/apimachinery/pkg/runtime".Scheme in argument to "github.com/kubesphere/s2ioperator/pkg/apis/devops/install".Install
R
runzexia 已提交
44 45 46
	urlruntime.Must(s2iv1alpha1.AddToScheme(Scheme))
	urlruntime.Must(Scheme.SetVersionPriority(s2iv1alpha1.SchemeGroupVersion))

R
runzexia 已提交
47
	servicemeshinstall.Install(Scheme)
R
runzexia 已提交
48
	tenantinstall.Install(Scheme)
R
runzexia 已提交
49
	networkinstall.Install(Scheme)
R
runzexia 已提交
50
	devopsinstall.Install(Scheme)
R
runzexia 已提交
51 52 53

	mapper := meta.NewDefaultRESTMapper(nil)

R
runzexia 已提交
54 55 56 57 58 59 60 61
	mapper.AddSpecific(servicemeshv1alpha2.SchemeGroupVersion.WithKind(servicemeshv1alpha2.ResourceKindServicePolicy),
		servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourcePluralServicePolicy),
		servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourceSingularServicePolicy), meta.RESTScopeRoot)

	mapper.AddSpecific(servicemeshv1alpha2.SchemeGroupVersion.WithKind(servicemeshv1alpha2.ResourceKindStrategy),
		servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourcePluralStrategy),
		servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourceSingularStrategy), meta.RESTScopeRoot)

R
runzexia 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	mapper.AddSpecific(tenantv1alpha1.SchemeGroupVersion.WithKind(tenantv1alpha1.ResourceKindWorkspace),
		tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha1.ResourcePluralWorkspace),
		tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha1.ResourceSingularWorkspace), meta.RESTScopeRoot)

	mapper.AddSpecific(s2iv1alpha1.SchemeGroupVersion.WithKind(s2iv1alpha1.ResourceKindS2iBuilder),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iBuilder),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourceSingularS2iBuilder), meta.RESTScopeRoot)

	mapper.AddSpecific(s2iv1alpha1.SchemeGroupVersion.WithKind(s2iv1alpha1.ResourceKindS2iBuilderTemplate),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iBuilderTemplate),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourceSingularS2iBuilderTemplate), meta.RESTScopeRoot)

	mapper.AddSpecific(s2iv1alpha1.SchemeGroupVersion.WithKind(s2iv1alpha1.ResourceKindS2iRun),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iRun),
		s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourceSingularS2iRun), meta.RESTScopeRoot)
R
runzexia 已提交
77 78 79
	mapper.AddSpecific(devopsv1alpha1.SchemeGroupVersion.WithKind(devopsv1alpha1.ResourceKindS2iBinary),
		devopsv1alpha1.SchemeGroupVersion.WithResource(devopsv1alpha1.ResourceSingularServicePolicy),
		devopsv1alpha1.SchemeGroupVersion.WithResource(devopsv1alpha1.ResourcePluralServicePolicy), meta.RESTScopeRoot)
R
runzexia 已提交
80

R
runzexia 已提交
81 82 83 84
	mapper.AddSpecific(networkv1alpha1.SchemeGroupVersion.WithKind(networkv1alpha1.ResourceKindWorkspaceNetworkPolicy),
		networkv1alpha1.SchemeGroupVersion.WithResource(networkv1alpha1.ResourcePluralWorkspaceNetworkPolicy),
		networkv1alpha1.SchemeGroupVersion.WithResource(networkv1alpha1.ResourceSingularWorkspaceNetworkPolicy), meta.RESTScopeRoot)

R
runzexia 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	spec, err := lib.RenderOpenAPISpec(lib.Config{
		Scheme: Scheme,
		Codecs: Codecs,
		Info: spec.InfoProps{
			Title:   "KubeSphere Advanced",
			Version: "v2.0.0",
			Contact: &spec.ContactInfo{
				Name:  "KubeSphere",
				URL:   "https://kubesphere.io/",
				Email: "kubesphere@yunify.com",
			},
			License: &spec.License{
				Name: "Apache 2.0",
				URL:  "https://www.apache.org/licenses/LICENSE-2.0.html",
			},
		},
		OpenAPIDefinitions: []common.GetOpenAPIDefinitions{
R
runzexia 已提交
102
			servicemeshv1alpha2.GetOpenAPIDefinitions,
R
runzexia 已提交
103 104
			tenantv1alpha1.GetOpenAPIDefinitions,
			s2iv1alpha1.GetOpenAPIDefinitions,
R
runzexia 已提交
105
			networkv1alpha1.GetOpenAPIDefinitions,
R
runzexia 已提交
106
			devopsv1alpha1.GetOpenAPIDefinitions,
R
runzexia 已提交
107 108
		},
		Resources: []schema.GroupVersionResource{
R
runzexia 已提交
109 110 111 112 113
			//TODO(runzexia) At present, the document generation requires the openapi structure of the go language,
			// but there is no +k8s:openapi-gen=true in the repository of https://github.com/knative/pkg,
			// and the api document cannot be generated temporarily.
			//servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourcePluralStrategy),
			//servicemeshv1alpha2.SchemeGroupVersion.WithResource(servicemeshv1alpha2.ResourcePluralServicePolicy),
R
runzexia 已提交
114 115 116 117
			tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha1.ResourcePluralWorkspace),
			s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iRun),
			s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iBuilderTemplate),
			s2iv1alpha1.SchemeGroupVersion.WithResource(s2iv1alpha1.ResourcePluralS2iBuilder),
R
runzexia 已提交
118
			networkv1alpha1.SchemeGroupVersion.WithResource(networkv1alpha1.ResourcePluralWorkspaceNetworkPolicy),
R
runzexia 已提交
119
			devopsv1alpha1.SchemeGroupVersion.WithResource(devopsv1alpha1.ResourceKindS2iBinary),
R
runzexia 已提交
120 121 122 123 124 125 126
		},
		Mapper: mapper,
	})
	if err != nil {
		log.Fatal(err)
	}

R
runzexia 已提交
127
	err = os.MkdirAll(filepath.Dir(output), 0755)
R
runzexia 已提交
128 129 130
	if err != nil {
		log.Fatal(err)
	}
R
runzexia 已提交
131
	err = ioutil.WriteFile(output, []byte(spec), 0644)
R
runzexia 已提交
132 133 134 135
	if err != nil {
		log.Fatal(err)
	}
}