Skip to content

Commit

Permalink
add restapi
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwei04 committed Aug 28, 2017
1 parent 7c6acbf commit 06f9482
Show file tree
Hide file tree
Showing 28 changed files with 3,958 additions and 650 deletions.
59 changes: 10 additions & 49 deletions cmd/compass/compass.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,35 @@
package main

import (
"context"
"flag"
"fmt"
"net"
"net/http"
"strings"

"github.com/weiwei04/compass/pkg/services/compass"

"go.uber.org/zap"
)

var (
grpcAddr = flag.String("listen", ":8910", "address:port to listen on")
grpcAddr = flag.String("rpcAddr", ":8910", "address:port to listen on")
restAddr = flag.String("httpAddr", ":8911", "address:port to listen on")
//enableTracing = flag.Bool("trace", false, "enable rpc tracing")
tillerAddr = flag.String("tiller", "127.0.0.1:44134", "tiller address, default: :44134")
registryAddr = flag.String("registry", "http://helm-registry:8900", "registry address, default: http://helm-registry:8900")
mock = flag.Bool("mock", false, "enable mock, default false")
)

var count int

func main() {
flag.Parse()
runServer()
}

func runServer() {
zapLogger, _ := zap.NewProduction()
defer zapLogger.Sync()
sugger := zapLogger.Sugar()

config := compass.Config{
TillerAddr: *tillerAddr,
RegistryAddr: *registryAddr,
ListenAt: *grpcAddr,
RPCAddr: *grpcAddr,
RESTAddr: *restAddr,
Mock: *mock,
}
sugger.Infof("tiller[%s] registry[%s] listen[%s]",
config.TillerAddr, config.RegistryAddr, config.ListenAt)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

grpcHandler, err := compass.NewGRPCHandler(ctx, config)
if err != nil {
return
}

restHandler, err := compass.NewRESTHandler(ctx, config)
if err != nil {
return
}

conn, err := net.Listen("tcp", fmt.Sprintf("%s", config.ListenAt))
if err != nil {
panic(err)
}

srv := http.Server{
Addr: config.ListenAt,
Handler: func(grpcHandler compass.GRPCHandler, restHandler compass.RESTHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcHandler.ServeHTTP(w, r)
} else {
restHandler.ServeHTTP(w, r)
}
})
}(grpcHandler, restHandler),
}

srv.Serve(conn)
srv := compass.NewServer(config)
srv.Serve()
}
21 changes: 17 additions & 4 deletions glide.lock

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

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ import:
version: b19bf474d317b857955b12035d2c5acb57ce8b01
- package: golang.org/x/net
version: c8c74377599bd978aee1cf3b9b63a8634051cec2
- package: github.com/stretchr/testify
version: ~1.1.4
48 changes: 48 additions & 0 deletions pkg/api/client/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package client

import (
"fmt"
"github.com/caicloud/helm-registry/pkg/errors"
"net/http"
)

type HTTPCodedError interface {
String() string
Error() string
Code() int
Desc() string
}

type httpCodedError struct {
code int
desc string
}

var _ HTTPCodedError = &httpCodedError{}

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

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

func (e *httpCodedError) Error() string {
return fmt.Sprintf("Code:%q, Desc:%q", e.code, e.desc)
}

func (e *httpCodedError) String() string {
return fmt.Sprintf("Code:%q, Desc:%q", e.code, e.desc)
}

func (e *httpCodedError) Code() int {
return e.code
}

func (e *httpCodedError) Desc() string {
return e.desc
}
26 changes: 1 addition & 25 deletions pkg/api/client/fusion.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package client

type Fusion interface {
Connect() error
Shutdown()
Release() Release
Registry() Registry
}

func NewFusion(registryAddr string, releaseAddr string) Fusion {
return &fusion{
release: NewCompassReleaseClient(releaseAddr),
release: NewReleaseClient(releaseAddr),
registry: NewHelmRegistryClient(registryAddr),
}
}
Expand All @@ -19,28 +17,6 @@ type fusion struct {
registry Registry
}

func (f *fusion) Connect() error {
var err error
defer func() {
if err == nil {
return
}
f.release.Shutdown()
f.registry.Shutdown()
}()
err = f.release.Connect()
if err != nil {
return err
}
err = f.registry.Connect()
return err
}

func (f *fusion) Shutdown() {
f.release.Shutdown()
f.registry.Shutdown()
}

func (f *fusion) Release() Release {
return f.release
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/api/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ type FetchChartResponse struct {
}

type Registry interface {
Connect() error
Shutdown()
// 列取space
ListSpaces(context.Context, *ListSpacesRequest) (*ListSpacesResponse, error)
CreateSpace(context.Context, *CreateSpaceRequest) (*CreateSpaceResponse, error)
Expand Down
38 changes: 17 additions & 21 deletions pkg/api/client/registry_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,22 @@ const (
)

func NewHelmRegistryClient(addr string) Registry {
return &helmRegistry{addr: addr}
client, err := v1.NewClient(addr)
if err != nil {
panic(err)
}
return &helmRegistry{addr: addr, client: client}
}

type helmRegistry struct {
addr string
client *v1.Client
}

func (r *helmRegistry) Connect() error {
var err error
r.client, err = v1.NewClient(r.addr)
return err
}

func (r *helmRegistry) Shutdown() {}

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, err
return nil, errorFromHelmRegistry(err)
}
return &ListSpacesResponse{
Spaces: listResp.Items,
Expand All @@ -50,18 +46,18 @@ 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, err
return nil, errorFromHelmRegistry(err)
}
return &CreateSpaceResponse{
Space: req.Space,
Link: createResp.Link,
}, err
}, nil
}

func (r *helmRegistry) DeleteSpace(ctx context.Context, req *DeleteSpaceRequest) (*DeleteSpaceResponse, error) {
err := r.client.DeleteSpace(req.Space)
if err != nil {
return nil, err
return nil, errorFromHelmRegistry(err)
}
return &DeleteSpaceResponse{}, nil
}
Expand All @@ -70,7 +66,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, err
return nil, errorFromHelmRegistry(err)
}

return &ListChartsResponse{
Expand All @@ -86,7 +82,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, err
return nil, errorFromHelmRegistry(err)
}

return &ListChartVersionsResponse{
Expand All @@ -103,7 +99,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, err
return nil, errorFromHelmRegistry(err)
}

resp := GetChartMetadataResponse{
Expand All @@ -121,7 +117,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, err
return nil, errorFromHelmRegistry(err)
}
values := map[string]interface{}{}
err = json.Unmarshal(raw, &values)
Expand All @@ -135,7 +131,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, err
return nil, errorFromHelmRegistry(err)
}
ch, err := chartutil.LoadArchive(bytes.NewReader(data))
if err != nil {
Expand All @@ -153,7 +149,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, err
return nil, errorFromHelmRegistry(err)
}
return &GetChartReadmeResponse{data}, nil
}
Expand All @@ -179,7 +175,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, err
return nil, errorFromHelmRegistry(err)
}
return &PushChartResponse{
Space: pushResp.Space,
Expand All @@ -192,7 +188,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, err
return nil, errorFromHelmRegistry(err)
}
return &FetchChartResponse{
Data: data,
Expand Down
Loading

0 comments on commit 06f9482

Please sign in to comment.