Skip to content

Commit

Permalink
Merge pull request #15 from weiwei04/compass-dev
Browse files Browse the repository at this point in the history
check if release already exist
  • Loading branch information
Wei Wei authored Sep 6, 2017
2 parents 31c8698 + 95b78d3 commit 30ab41f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
23 changes: 12 additions & 11 deletions pkg/api/client/registry_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/helm/pkg/proto/hapi/chart"

v1 "github.com/caicloud/helm-registry/pkg/rest/v1"
"github.com/weiwei04/compass/pkg/api/services"
"golang.org/x/net/context"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ type helmRegistry struct {
func (r *helmRegistry) ListSpaces(ctx context.Context, req *ListSpacesRequest) (*ListSpacesResponse, error) {
listResp, err := r.client.ListSpaces(req.Start, req.Limit)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &ListSpacesResponse{
Spaces: listResp.Items,
Expand All @@ -56,7 +57,7 @@ func (r *helmRegistry) ListSpaces(ctx context.Context, req *ListSpacesRequest) (
func (r *helmRegistry) CreateSpace(ctx context.Context, req *CreateSpaceRequest) (*CreateSpaceResponse, error) {
createResp, err := r.client.CreateSpace(req.Space)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &CreateSpaceResponse{
Space: req.Space,
Expand All @@ -67,7 +68,7 @@ func (r *helmRegistry) CreateSpace(ctx context.Context, req *CreateSpaceRequest)
func (r *helmRegistry) DeleteSpace(ctx context.Context, req *DeleteSpaceRequest) (*DeleteSpaceResponse, error) {
err := r.client.DeleteSpace(req.Space)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &DeleteSpaceResponse{}, nil
}
Expand All @@ -76,7 +77,7 @@ func (r *helmRegistry) DeleteSpace(ctx context.Context, req *DeleteSpaceRequest)
func (r *helmRegistry) ListCharts(ctx context.Context, req *ListChartsRequest) (*ListChartsResponse, error) {
listResp, err := r.client.ListCharts(req.Space, req.Start, req.Limit)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}

return &ListChartsResponse{
Expand All @@ -92,7 +93,7 @@ func (r *helmRegistry) ListCharts(ctx context.Context, req *ListChartsRequest) (
func (r *helmRegistry) ListChartVersions(ctx context.Context, req *ListChartVersionsRequest) (*ListChartVersionsResponse, error) {
listResp, err := r.client.ListVersions(req.Space, req.Chart, req.Start, req.Limit)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}

return &ListChartVersionsResponse{
Expand All @@ -109,7 +110,7 @@ func (r *helmRegistry) ListChartVersions(ctx context.Context, req *ListChartVers
func (r *helmRegistry) GetChartMetadata(ctx context.Context, req *GetChartMetadataRequest) (*GetChartMetadataResponse, error) {
tmpResp, err := r.client.FetchVersionMetadata(req.Space, req.Chart, req.Version)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}

resp := GetChartMetadataResponse{
Expand All @@ -127,7 +128,7 @@ func (r *helmRegistry) GetChartMetadata(ctx context.Context, req *GetChartMetada
func (r *helmRegistry) GetChartValues(ctx context.Context, req *GetChartValuesRequest) (*GetChartValuesResponse, error) {
raw, err := r.client.FetchVersionValues(req.Space, req.Chart, req.Version)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
values := map[string]interface{}{}
err = json.Unmarshal(raw, &values)
Expand All @@ -141,7 +142,7 @@ func (r *helmRegistry) GetChartValues(ctx context.Context, req *GetChartValuesRe
func (r *helmRegistry) fetchChartFile(ctx context.Context, space, chart, ver, file string) ([]byte, error) {
data, err := r.client.DownloadVersion(space, chart, ver)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
ch, err := chartutil.LoadArchive(bytes.NewReader(data))
if err != nil {
Expand All @@ -159,7 +160,7 @@ func (r *helmRegistry) fetchChartFile(ctx context.Context, space, chart, ver, fi
func (r *helmRegistry) GetChartReadme(ctx context.Context, req *GetChartReadmeRequest) (*GetChartReadmeResponse, error) {
data, err := r.fetchChartFile(ctx, req.Space, req.Chart, req.Version, readmeFile)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &GetChartReadmeResponse{data}, nil
}
Expand All @@ -185,7 +186,7 @@ func (r *helmRegistry) GetChartRequirements(ctx context.Context, req *GetChartRe
func (r *helmRegistry) PushChart(ctx context.Context, req *PushChartRequest) (*PushChartResponse, error) {
pushResp, err := r.client.UploadChart(req.Space, req.Data)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &PushChartResponse{
Space: pushResp.Space,
Expand All @@ -198,7 +199,7 @@ func (r *helmRegistry) PushChart(ctx context.Context, req *PushChartRequest) (*P
func (r *helmRegistry) FetchChart(ctx context.Context, req *FetchChartRequest) (*FetchChartResponse, error) {
data, err := r.client.DownloadVersion(req.Space, req.Chart, req.Version)
if err != nil {
return nil, errorFromHelmRegistry(err)
return nil, services.ErrorFromHelmRegistry(err)
}
return &FetchChartResponse{
Data: data,
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/client/release_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"encoding/json"
//json "github.com/json-iterator/go"
"github.com/weiwei04/compass/pkg/api/services"
)

// all the ugly things
Expand Down Expand Up @@ -48,7 +49,7 @@ func (c compassClient) doRequestWithoutBody(ctx context.Context, method, url str
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errorFromResponse(resp)
return services.ErrorFromResponse(resp)
}
return json.NewDecoder(resp.Body).Decode(out)
}
Expand All @@ -69,7 +70,7 @@ func (c compassClient) doRequest(ctx context.Context, method, url string, in int
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errorFromResponse(resp)
return services.ErrorFromResponse(resp)
}
return json.NewDecoder(resp.Body).Decode(out)
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/api/client/error.go → pkg/api/services/error.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package client
package services

import (
"fmt"
"net/http"

"github.com/caicloud/helm-registry/pkg/errors"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/status"
)

type HTTPCodedError interface {
Expand All @@ -21,17 +23,24 @@ type httpCodedError struct {

var _ HTTPCodedError = &httpCodedError{}

func errorFromResponse(resp *http.Response) error {
func ErrorFromResponse(resp *http.Response) error {
return &httpCodedError{resp.StatusCode, resp.Status}
}

func errorFromHelmRegistry(err error) error {
func ErrorFromHelmRegistry(err error) error {
if e, ok := err.(*errors.Error); ok {
return &httpCodedError{e.Code, e.Message}
}
return err
}

func ErrorFromGRPC(err error) error {
if s, ok := status.FromError(err); ok {
return &httpCodedError{runtime.HTTPStatusFromCode(s.Code()), s.Message()}
}
return err
}

func (e *httpCodedError) Error() string {
return fmt.Sprintf("Code:%q, Desc:%q", e.code, e.desc)
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/services/compass/compass_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
//"go.uber.org/zap"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
tiller "k8s.io/helm/pkg/proto/hapi/services"
)

Expand Down Expand Up @@ -93,6 +95,14 @@ func (s *compassServer) CreateRelease(ctx context.Context, req *pb.CreateRelease
return &resp, err
}

// check releasename exist
if _, err := s.tiller.GetReleaseStatus(newContext(), &tiller.GetReleaseStatusRequest{
Name: req.Name,
Version: 0,
}); err == nil {
return nil, status.Errorf(codes.AlreadyExists, "release:%s already exist", req.Name)
}

// TODO: improve this
values := req.Values
if values == nil {
Expand Down Expand Up @@ -225,8 +235,6 @@ func (s *compassServer) ListReleases(ctx context.Context, req *pb.ListReleasesRe
}
}
}

return resp, nil
}

func (s *compassServer) GetReleaseStatus(ctx context.Context, req *pb.GetReleaseStatusRequest) (*pb.GetReleaseStatusResponse, error) {
Expand Down

0 comments on commit 30ab41f

Please sign in to comment.