Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Generate Kubeflow PyTorchJob SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchihe committed Dec 10, 2019
1 parent 5522f91 commit df796a2
Show file tree
Hide file tree
Showing 47 changed files with 4,514 additions and 0 deletions.
43 changes: 43 additions & 0 deletions hack/python-sdk/gen-sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

SWAGGER_JAR_URL="http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.6/swagger-codegen-cli-2.4.6.jar"
SWAGGER_CODEGEN_JAR="hack/python-sdk/swagger-codegen-cli.jar"
SWAGGER_CODEGEN_CONF="hack/python-sdk/swagger_config.json"
SWAGGER_CODEGEN_FILE="pkg/apis/pytorch/v1/swagger.json"
SDK_OUTPUT_PATH="sdk/python"

if [ -z "${GOPATH:-}" ]; then
export GOPATH=$(go env GOPATH)
fi

echo "Generating OpenAPI specification ..."
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

echo "Generating swagger file ..."
go run hack/python-sdk/main.go 0.1 > ${SWAGGER_CODEGEN_FILE}

echo "Downloading the swagger-codegen JAR package ..."
wget -O ${SWAGGER_CODEGEN_JAR} ${SWAGGER_JAR_URL}

echo "Generating Python SDK for Kubeflow PyTorch-Operator ..."
java -jar ${SWAGGER_CODEGEN_JAR} generate -i ${SWAGGER_CODEGEN_FILE} -l python -o ${SDK_OUTPUT_PATH} -c ${SWAGGER_CODEGEN_CONF}

echo "Kubeflow PyTorch Operator Python SDK is generated successfully to folder ${SDK_OUTPUT_PATH}/."
79 changes: 79 additions & 0 deletions hack/python-sdk/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2019 kubeflow.org.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/go-openapi/spec"
pytorchjob "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1"
"k8s.io/klog"
"k8s.io/kube-openapi/pkg/common"
)

// Generate OpenAPI spec definitions for PyTorchJob Resource
func main() {
if len(os.Args) <= 1 {
klog.Fatal("Supply a version")
}
version := os.Args[1]
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
oAPIDefs := pytorchjob.GetOpenAPIDefinitions(func(name string) spec.Ref {
return spec.MustCreateRef("#/definitions/" + common.EscapeJsonPointer(swaggify(name)))
})
defs := spec.Definitions{}
for defName, val := range oAPIDefs {
defs[swaggify(defName)] = val.Schema
}
swagger := spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Definitions: defs,
Paths: &spec.Paths{Paths: map[string]spec.PathItem{}},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "pytorch",
Description: "Python SDK for PyTorch-Operator",
Version: version,
},
},
},
}
jsonBytes, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
klog.Fatal(err.Error())
}
fmt.Println(string(jsonBytes))
}

func swaggify(name string) string {
name = strings.Replace(name, "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/", "", -1)
name = strings.Replace(name, "github.com/kubeflow/common/job_controller/api/", "", -1)
name = strings.Replace(name, "github.com/kubernetes-sigs/kube-batch/pkg/client/clientset/", "", -1)
name = strings.Replace(name, "k8s.io/api/core/", "", -1)
name = strings.Replace(name, "k8s.io/apimachinery/pkg/apis/meta/", "", -1)
name = strings.Replace(name, "k8s.io/kubernetes/pkg/controller/", "", -1)
name = strings.Replace(name, "k8s.io/client-go/listers/core/", "", -1)
name = strings.Replace(name, "k8s.io/client-go/util/workqueue", "", -1)
name = strings.Replace(name, "/", ".", -1)
return name
}
13 changes: 13 additions & 0 deletions hack/python-sdk/swagger_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"packageName" : "pytorchjob",
"projectName" : "pytorchjob",
"packageVersion": "0.1",
"importMappings": {
"V1Container": "from kubernetes.client import V1Container",
"V1ObjectMeta": "from kubernetes.client import V1ObjectMeta",
"V1ListMeta": "from kubernetes.client import V1ListMeta",
"V1ResourceRequirements": "from kubernetes.client import V1ResourceRequirements",
"V1JobCondition": "from kubernetes.client import V1JobCondition",
"V1PodTemplateSpec": "from kubernetes.client import V1PodTemplateSpec"
}
}
1 change: 1 addition & 0 deletions pkg/apis/pytorch/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// +k8s:deepcopy-gen=package,register
// +k8s:defaulter-gen=TypeMeta
// +k8s:openapi-gen=true

// Package v1 is the v1 version of the API.
// +groupName=kubeflow.org
Expand Down
Loading

0 comments on commit df796a2

Please sign in to comment.