Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST API using grpc gateway #142

Merged
merged 29 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ required = [
"sigs.k8s.io/controller-runtime/pkg/source",
"sigs.k8s.io/testing_frameworks/integration", # for integration testing
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway",
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger",
"github.com/golang/protobuf/protoc-gen-go",
]

[prune]
Expand Down
10 changes: 10 additions & 0 deletions cmd/manager-rest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:alpine AS build-env
# The GOPATH in the image is /go.
ADD . /go/src/github.com/kubeflow/katib
WORKDIR /go/src/github.com/kubeflow/katib/cmd/manager-rest
RUN go build -o vizier-manager-rest

FROM alpine:3.7
WORKDIR /app
COPY --from=build-env /go/src/github.com/kubeflow/katib/cmd/manager-rest/vizier-manager-rest /app/
ENTRYPOINT ["./vizier-manager-rest"]
43 changes: 43 additions & 0 deletions cmd/manager-rest/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"flag"
"net/http"

"github.com/golang/glog"
"golang.org/x/net/context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"

gw "github.com/kubeflow/katib/pkg/api"
)

var (
vizierCoreEndpoint = flag.String("echo_endpoint", "vizier-core:6789", "vizier-core endpoint")
)

func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
// register handlers for the HTTP endpoints
err := gw.RegisterManagerHandlerFromEndpoint(ctx, mux, *vizierCoreEndpoint, opts)
if err != nil {
return err
}

// proxy server listens on port 80
return http.ListenAndServe(":80", mux)
}

func main() {
flag.Parse()
defer glog.Flush()

if err := run(); err != nil {
glog.Fatal(err)
}
}
77 changes: 77 additions & 0 deletions docs/rest-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## REST API

For each RPC service, we define an equivalent HTTP REST API method using [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway). The mapping between service and REST API method can be found in this [file](https://github.com/kubeflow/katib/blob/master/pkg/api/api.proto). The mapping includes the URL path, query parameters and request body. You can read more details on the supported methods and binding options at this [link](https://cloud.google.com/service-infrastructure/docs/service-management/reference/rpc/google.api#http)

## Examples
Assuming `{HOST}` as the ingress host for vizier-core-rest,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a way of using portforward.

kubectl v1.10~
$ kubectl -n katib port-forward svc/vizier-core-rest 8080:80

kubectl ~v1.9
& kubectl -n katib port-forward $(kubectl -n katib get pod -o=name | grep vizier-core-rest | sed -e "s@pods\/@@") 8080:80

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this.


### Create a new Study

Sample JSON file (study_config.json)

```json
{
"name": "study1",
"owner": "mayankjuneja",
"optimization_type": 2,
"optimization_goal": null,
"parameter_configs": {
"configs": [{
"name": "dropout",
"parameter_type": 1,
"feasible": {
"max": "0.5",
"min": "0.1",
"list": []
}
}, {
"name": "activation",
"parameter_type": 4,
"feasible": {
"max": "0",
"min": "0",
"list": ["tanh", "relu"]
}
}]
},
"objective_value_name": "accuracy"
}
```

Request:

```shell
curl -X POST {HOST}/api/Manager/CreateStudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write explicitly to use above json file.
curl -X POST {HOST}/api/Manager/CreateStudy -d @study_config.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, missed the argument here.

```

Response:

```json
{"study_id": "k350afb1ad0e580e"}
```

### Get List of Studies

Request:
```shell
curl -X GET {HOST}/api/Manager/GetStudyList
```

Response:
```json
{
"study_overviews": [
{
"name": "study1",
"owner": "mayankjuneja",
"id": "k350afb1ad0e580e"
},
{
"name": "study2",
"owner": "mayankjuneja",
"id": "pae3c470c9584cc4"
}
]
}
```

33 changes: 33 additions & 0 deletions manifests/vizier/core-rest/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: vizier-core-rest
namespace: katib
labels:
app: vizier
component: core-rest
spec:
replicas: 1
template:
metadata:
name: vizier-core-rest
labels:
app: vizier
component: core-rest
spec:
serviceAccountName: vizier-core-rest
containers:
- name: vizier-core-rest
image: katib/vizier-core-rest
command:
- './vizier-manager-rest'
ports:
- name: api
containerPort: 80
# resources:
# requests:
# cpu: 500m
# memory: 500M
# limits:
# cpu: 500m
# memory: 500M
16 changes: 16 additions & 0 deletions manifests/vizier/core-rest/ingress.yaml_
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: vizier-core-rest
namespace: katib
annotations:
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: vizier-core-rest.k-cluster.example.net
http:
paths:
- path: /
backend:
serviceName: vizier-core-rest
servicePort: 80
37 changes: 37 additions & 0 deletions manifests/vizier/core-rest/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since core-rest does not need to manage k8s resources, the rbac looks unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, thanks for catching this!

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: vizier-core-rest
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: vizier-core-rest
subjects:
- kind: ServiceAccount
name: vizier-core-rest
namespace: katib
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: vizier-core-rest
rules:
- apiGroups: [""]
resources: ["pods", "nodes", "nodes/*", "pods/log", "pods/status", "services", "persistentvolumes", "persistentvolumes/status","persistentvolumeclaims","persistentvolumeclaims/status"]
verbs: ["*"]
- apiGroups: ["batch"]
resources: ["jobs", "jobs/status"]
verbs: ["*"]
- apiGroups: ["extensions"]
verbs: ["*"]
resources: ["ingresses","ingresses/status","deployments","deployments/status"]
- apiGroups: [""]
verbs: ["*"]
resources: ["services"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: vizier-core-rest
namespace: katib
17 changes: 17 additions & 0 deletions manifests/vizier/core-rest/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v1
kind: Service
metadata:
name: vizier-core-rest
namespace: katib
labels:
app: vizier
component: core-rest
spec:
type: ClusterIP
ports:
- port: 80
protocol: TCP
name: api
selector:
app: vizier
component: core-rest
Loading