|
| 1 | +/* |
| 2 | +Copyright 2019 kubeflow.org. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/go-openapi/spec" |
| 26 | + pytorchjob "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1" |
| 27 | + "k8s.io/klog" |
| 28 | + "k8s.io/kube-openapi/pkg/common" |
| 29 | +) |
| 30 | + |
| 31 | +// Generate OpenAPI spec definitions for PyTorchJob Resource |
| 32 | +func main() { |
| 33 | + if len(os.Args) <= 1 { |
| 34 | + klog.Fatal("Supply a version") |
| 35 | + } |
| 36 | + version := os.Args[1] |
| 37 | + if !strings.HasPrefix(version, "v") { |
| 38 | + version = "v" + version |
| 39 | + } |
| 40 | + oAPIDefs := pytorchjob.GetOpenAPIDefinitions(func(name string) spec.Ref { |
| 41 | + return spec.MustCreateRef("#/definitions/" + common.EscapeJsonPointer(swaggify(name))) |
| 42 | + }) |
| 43 | + defs := spec.Definitions{} |
| 44 | + for defName, val := range oAPIDefs { |
| 45 | + defs[swaggify(defName)] = val.Schema |
| 46 | + } |
| 47 | + swagger := spec.Swagger{ |
| 48 | + SwaggerProps: spec.SwaggerProps{ |
| 49 | + Swagger: "2.0", |
| 50 | + Definitions: defs, |
| 51 | + Paths: &spec.Paths{Paths: map[string]spec.PathItem{}}, |
| 52 | + Info: &spec.Info{ |
| 53 | + InfoProps: spec.InfoProps{ |
| 54 | + Title: "pytorch", |
| 55 | + Description: "Python SDK for PyTorch-Operator", |
| 56 | + Version: version, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + jsonBytes, err := json.MarshalIndent(swagger, "", " ") |
| 62 | + if err != nil { |
| 63 | + klog.Fatal(err.Error()) |
| 64 | + } |
| 65 | + fmt.Println(string(jsonBytes)) |
| 66 | +} |
| 67 | + |
| 68 | +func swaggify(name string) string { |
| 69 | + name = strings.Replace(name, "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/", "", -1) |
| 70 | + name = strings.Replace(name, "github.com/kubeflow/common/job_controller/api/", "", -1) |
| 71 | + name = strings.Replace(name, "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/", "", -1) |
| 72 | + name = strings.Replace(name, "k8s.io/api/core/", "", -1) |
| 73 | + name = strings.Replace(name, "k8s.io/apimachinery/pkg/apis/meta/", "", -1) |
| 74 | + name = strings.Replace(name, "k8s.io/kubernetes/pkg/controller/", "", -1) |
| 75 | + name = strings.Replace(name, "k8s.io/client-go/listers/core/", "", -1) |
| 76 | + name = strings.Replace(name, "k8s.io/client-go/util/workqueue", "", -1) |
| 77 | + name = strings.Replace(name, "/", ".", -1) |
| 78 | + return name |
| 79 | +} |
0 commit comments