Skip to content

Commit 4594b9f

Browse files
authored
Merge pull request #1 from kubeflow/master
Generate Kubeflow PyTorchJob SDK (kubeflow#227)
2 parents 22ea5ea + c295893 commit 4594b9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+4911
-8
lines changed

hack/python-sdk/gen-sdk.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2019 The Kubeflow Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
SWAGGER_JAR_URL="http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.6/swagger-codegen-cli-2.4.6.jar"
22+
SWAGGER_CODEGEN_JAR="hack/python-sdk/swagger-codegen-cli.jar"
23+
SWAGGER_CODEGEN_CONF="hack/python-sdk/swagger_config.json"
24+
SWAGGER_CODEGEN_FILE="pkg/apis/pytorch/v1/swagger.json"
25+
SDK_OUTPUT_PATH="sdk/python"
26+
27+
if [ -z "${GOPATH:-}" ]; then
28+
export GOPATH=$(go env GOPATH)
29+
fi
30+
31+
echo "Generating OpenAPI specification ..."
32+
go run vendor/k8s.io/code-generator/cmd/openapi-gen/main.go --input-dirs github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1,github.com/kubeflow/common/job_controller/api/v1 --output-package github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1 --go-header-file hack/boilerplate/boilerplate.go.txt
33+
34+
echo "Generating swagger file ..."
35+
go run hack/python-sdk/main.go 0.1 > ${SWAGGER_CODEGEN_FILE}
36+
37+
echo "Downloading the swagger-codegen JAR package ..."
38+
wget -O ${SWAGGER_CODEGEN_JAR} ${SWAGGER_JAR_URL}
39+
40+
echo "Generating Python SDK for Kubeflow PyTorch-Operator ..."
41+
java -jar ${SWAGGER_CODEGEN_JAR} generate -i ${SWAGGER_CODEGEN_FILE} -l python -o ${SDK_OUTPUT_PATH} -c ${SWAGGER_CODEGEN_CONF}
42+
43+
echo "Kubeflow PyTorch Operator Python SDK is generated successfully to folder ${SDK_OUTPUT_PATH}/."

hack/python-sdk/main.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageName" : "pytorchjob",
3+
"projectName" : "pytorchjob",
4+
"packageVersion": "0.1",
5+
"importMappings": {
6+
"V1Container": "from kubernetes.client import V1Container",
7+
"V1ObjectMeta": "from kubernetes.client import V1ObjectMeta",
8+
"V1ListMeta": "from kubernetes.client import V1ListMeta",
9+
"V1ResourceRequirements": "from kubernetes.client import V1ResourceRequirements",
10+
"V1JobCondition": "from kubernetes.client import V1JobCondition",
11+
"V1PodTemplateSpec": "from kubernetes.client import V1PodTemplateSpec"
12+
}
13+
}

pkg/apis/pytorch/v1/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// +k8s:deepcopy-gen=package,register
1616
// +k8s:defaulter-gen=TypeMeta
17+
// +k8s:openapi-gen=true
1718

1819
// Package v1 is the v1 version of the API.
1920
// +groupName=kubeflow.org

0 commit comments

Comments
 (0)