Skip to content

Commit

Permalink
Add ListAllComputeTemplates endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
daikeshi committed May 12, 2022
1 parent fd2fd2b commit 371ca46
Show file tree
Hide file tree
Showing 8 changed files with 714 additions and 257 deletions.
11 changes: 9 additions & 2 deletions apiserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ KubeRay APIServer provides the gRPC and HTTP API to manage kuberay resources.

### Compute Template

#### Create compute templates
#### Create compute templates in a given namespace

```
POST {{baseUrl}}/apis/v1alpha2/namespaces/<namespace>/compute_templates
```
Expand All @@ -22,7 +23,7 @@ POST {{baseUrl}}/apis/v1alpha2/namespaces/<namespace>/compute_templates
}
```

#### List all compute templates
#### List all compute templates in a given namespace

```
GET {{baseUrl}}/apis/v1alpha2/namespaces/<namespace>/compute_templates
Expand All @@ -43,6 +44,12 @@ GET {{baseUrl}}/apis/v1alpha2/namespaces/<namespace>/compute_templates
}
```

#### List all compute templates in all namespaces

```
GET {{baseUrl}}/apis/v1alpha2/compute_templates
```

#### Get compute template by name

```
Expand Down
32 changes: 21 additions & 11 deletions apiserver/pkg/manager/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,11 @@ func (r *ResourceManager) CreateComputeTemplate(ctx context.Context, runtime *ap
}

func (r *ResourceManager) GetComputeTemplate(ctx context.Context, name string, namespace string) (*v1.ConfigMap, error) {
if len(namespace) == 0 {
return nil, util.NewInvalidInputError("Namespace is empty, failed to get the compute template.")
}

client := r.getKubernetesConfigMapClient(namespace)
return getComputeTemplateByName(ctx, client, name)
}

func (r *ResourceManager) ListComputeTemplates(ctx context.Context, namespace string) ([]*v1.ConfigMap, error) {
if len(namespace) == 0 {
return nil, util.NewInvalidInputError("Namespace is empty, failed to list compute templates.")
}

client := r.getKubernetesConfigMapClient(namespace)
configMapList, err := client.List(ctx, metav1.ListOptions{LabelSelector: "ray.io/config-type=compute-template"})
if err != nil {
Expand All @@ -212,11 +204,29 @@ func (r *ResourceManager) ListComputeTemplates(ctx context.Context, namespace st
return result, nil
}

func (r *ResourceManager) DeleteComputeTemplate(ctx context.Context, name string, namespace string) error {
if len(namespace) == 0 {
return util.NewInvalidInputError("Namespace is empty, failed to delete the compute template.")
func (r *ResourceManager) ListAllComputeTemplates(ctx context.Context) ([]*v1.ConfigMap, error) {
namespaces, err := r.getKubernetesNamespaceClient().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, util.Wrap(err, "Failed to fetch all Kubernetes namespaces")
}

var result []*v1.ConfigMap
for _, namespace := range namespaces.Items {
client := r.getKubernetesConfigMapClient(namespace.Name)
configMapList, err := client.List(ctx, metav1.ListOptions{LabelSelector: "ray.io/config-type=compute-template"})
if err != nil {
return nil, util.Wrap(err, fmt.Sprintf("List compute templates failed in %s", namespace.Name))
}

length := len(configMapList.Items)
for i := 0; i < length; i++ {
result = append(result, &configMapList.Items[i])
}
}
return result, nil
}

func (r *ResourceManager) DeleteComputeTemplate(ctx context.Context, name string, namespace string) error {
client := r.getKubernetesConfigMapClient(namespace)

configMap, err := getComputeTemplateByName(ctx, client, name)
Expand Down
38 changes: 35 additions & 3 deletions apiserver/pkg/server/compute_template_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"fmt"

"github.com/ray-project/kuberay/apiserver/pkg/manager"
"github.com/ray-project/kuberay/apiserver/pkg/model"
Expand Down Expand Up @@ -32,33 +33,64 @@ func (s *ComputeTemplateServer) CreateComputeTemplate(ctx context.Context, reque

runtime, err := s.resourceManager.CreateComputeTemplate(ctx, request.ComputeTemplate)
if err != nil {
return nil, util.Wrap(err, "Create compute template Runtime failed.")
return nil, util.Wrap(err, "Create compute template failed.")
}

return model.FromKubeToAPIComputeTemplate(runtime), nil
}

func (s *ComputeTemplateServer) GetComputeTemplate(ctx context.Context, request *api.GetComputeTemplateRequest) (*api.ComputeTemplate, error) {
if request.Name == "" {
return nil, util.NewInvalidInputError("Compute template name is empty. Please specify a valid value.")
}

if request.Namespace == "" {
return nil, util.NewInvalidInputError("Namespace is empty. Please specify a valid value.")
}

runtime, err := s.resourceManager.GetComputeTemplate(ctx, request.Name, request.Namespace)
if err != nil {
return nil, util.Wrap(err, "Get compute template runtime failed.")
return nil, util.Wrap(err, "Get compute template failed.")
}

return model.FromKubeToAPIComputeTemplate(runtime), nil
}

func (s *ComputeTemplateServer) ListComputeTemplates(ctx context.Context, request *api.ListComputeTemplatesRequest) (*api.ListComputeTemplatesResponse, error) {
if request.Namespace == "" {
return nil, util.NewInvalidInputError("Namespace is empty. Please specify a valid value.")
}

runtimes, err := s.resourceManager.ListComputeTemplates(ctx, request.Namespace)
if err != nil {
return nil, util.Wrap(err, "List compute templates runtime failed.")
return nil, util.Wrap(err, fmt.Sprintf("List compute templates in namespace %s failed.", request.Namespace))
}

return &api.ListComputeTemplatesResponse{
ComputeTemplates: model.FromKubeToAPIComputeTemplates(runtimes),
}, nil
}

func (s *ComputeTemplateServer) ListAllComputeTemplates(ctx context.Context, request *api.ListAllComputeTemplatesRequest) (*api.ListAllComputeTemplatesResponse, error) {
runtimes, err := s.resourceManager.ListAllComputeTemplates(ctx)
if err != nil {
return nil, util.Wrap(err, "List all compute templates from all namespaces failed.")
}

return &api.ListAllComputeTemplatesResponse{
ComputeTemplates: model.FromKubeToAPIComputeTemplates(runtimes),
}, nil
}

func (s *ComputeTemplateServer) DeleteComputeTemplate(ctx context.Context, request *api.DeleteComputeTemplateRequest) (*emptypb.Empty, error) {
if request.Name == "" {
return nil, util.NewInvalidInputError("Compute template name is empty. Please specify a valid value.")
}

if request.Namespace == "" {
return nil, util.NewInvalidInputError("Namespace is empty. Please specify a valid value.")
}

if err := s.resourceManager.DeleteComputeTemplate(ctx, request.Name, request.Namespace); err != nil {
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ service ComputeTemplateService {
};
}

// Finds all compute templates in a given namespace. Supports pagination, and sorting on certain fields.
rpc ListAllComputeTemplates(ListAllComputeTemplatesRequest) returns (ListAllComputeTemplatesResponse) {
option (google.api.http) = {
get: "/apis/v1alpha2/compute_templates"
};
}

// Deletes a compuate template by its name and namespace
rpc DeleteComputeTemplate(DeleteComputeTemplateRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
Expand Down Expand Up @@ -77,6 +84,14 @@ message ListComputeTemplatesResponse {
repeated ComputeTemplate compute_templates = 1;
}

message ListAllComputeTemplatesRequest {
// TODO: support paganation later
}

message ListAllComputeTemplatesResponse {
repeated ComputeTemplate compute_templates = 1;
}

message DeleteComputeTemplateRequest {
// The name of the compute template to be deleted.
string name = 1;
Expand Down Expand Up @@ -157,6 +172,15 @@ message ListImageTemplatesResponse {
repeated ImageTemplate image_templates = 1;
}

message ListAllImageTemplatesRequest {
// TODO: support pagingation later
}

message ListAllImageTemplatesResponse {
// A list of Compute returned.
repeated ImageTemplate image_templates = 1;
}

message DeleteImageTemplateRequest {
// The name of the image template to be deleted.
string name = 1;
Expand Down
Loading

0 comments on commit 371ca46

Please sign in to comment.