From 89ef4a5ad60682ed3620ca367f3c95bc27024448 Mon Sep 17 00:00:00 2001 From: Diego Garcia Date: Mon, 7 Aug 2017 16:31:55 -0300 Subject: [PATCH] a bunch of refactors on app list implementation --- cmd/client/cmd/app.go | 17 ++--- pkg/protobuf/app/app.pb.go | 128 ++++++++++++++++---------------- pkg/protobuf/app/app.proto | 6 +- pkg/server/app/app.go | 25 +++---- pkg/server/app/app_test.go | 37 +++++---- pkg/server/app/fake.go | 27 +++---- pkg/server/app/fake_test.go | 10 ++- pkg/server/app/handlers.go | 10 +-- pkg/server/app/handlers_test.go | 12 --- pkg/server/app/models.go | 47 ++++-------- pkg/server/app/models_test.go | 52 ++++--------- pkg/server/k8s/client.go | 21 +++--- pkg/server/k8s/errors.go | 2 +- pkg/server/k8s/k8s.go | 7 +- pkg/server/k8s/k8s_test.go | 2 +- 15 files changed, 179 insertions(+), 224 deletions(-) diff --git a/cmd/client/cmd/app.go b/cmd/client/cmd/app.go index 993723f7e..f61b1c973 100644 --- a/cmd/client/cmd/app.go +++ b/cmd/client/cmd/app.go @@ -161,7 +161,6 @@ var appListCmd = &cobra.Command{ } func appList(cmd *cobra.Command, args []string) { - conn, err := connection.New(cfgFile) if err != nil { client.PrintErrorAndExit("Error connecting to server: %v", err) @@ -171,12 +170,11 @@ func appList(cmd *cobra.Command, args []string) { cli := appb.NewAppClient(conn) resp, err := cli.List(context.Background(), &appb.Empty{}) if err != nil { - fmt.Fprintln(os.Stderr, client.GetErrorMsg(err)) - return + client.PrintErrorAndExit(client.GetErrorMsg(err)) } if len(resp.Apps) == 0 { - fmt.Println("You do not have to any app") + fmt.Println("You don't have any app") return } // rendering app list in a table view @@ -186,11 +184,12 @@ func appList(cmd *cobra.Command, args []string) { table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetRowSeparator("-") table.SetAutoWrapText(false) - for _, t := range resp.Apps { - if t.Urls == "" { - t.Urls = "n/a" + for _, a := range resp.Apps { + urls := strings.Join(a.Urls, ",") + if urls == "" { + urls = "n/a" } - r := []string{t.Team, t.App, t.Urls} + r := []string{a.Team, a.Name, urls} table.Append(r) } table.Render() @@ -455,8 +454,6 @@ func init() { // App logs appLogsCmd.Flags().Int64("lines", 10, "number of lines") appLogsCmd.Flags().Bool("follow", false, "follow logs") - // App list - appListCmd.Flags().Bool("show-apps", false, "Show team's app(s)") } func appLogs(cmd *cobra.Command, args []string) { diff --git a/pkg/protobuf/app/app.pb.go b/pkg/protobuf/app/app.pb.go index dbb8f78ea..6ee14cfdf 100644 --- a/pkg/protobuf/app/app.pb.go +++ b/pkg/protobuf/app/app.pb.go @@ -1,11 +1,12 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// source: app.proto +// Code generated by protoc-gen-go. +// source: pkg/protobuf/app/app.proto +// DO NOT EDIT! /* Package app is a generated protocol buffer package. It is generated from these files: - app.proto + pkg/protobuf/app/app.proto It has these top-level messages: CreateRequest @@ -189,9 +190,9 @@ func (m *ListResponse) GetApps() []*ListResponse_App { } type ListResponse_App struct { - Team string `protobuf:"bytes,1,opt,name=team" json:"team,omitempty"` - App string `protobuf:"bytes,2,opt,name=app" json:"app,omitempty"` - Urls string `protobuf:"bytes,3,opt,name=urls" json:"urls,omitempty"` + Team string `protobuf:"bytes,1,opt,name=team" json:"team,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` + Urls []string `protobuf:"bytes,3,rep,name=urls" json:"urls,omitempty"` } func (m *ListResponse_App) Reset() { *m = ListResponse_App{} } @@ -206,18 +207,18 @@ func (m *ListResponse_App) GetTeam() string { return "" } -func (m *ListResponse_App) GetApp() string { +func (m *ListResponse_App) GetName() string { if m != nil { - return m.App + return m.Name } return "" } -func (m *ListResponse_App) GetUrls() string { +func (m *ListResponse_App) GetUrls() []string { if m != nil { return m.Urls } - return "" + return nil } type LogsRequest struct { @@ -878,61 +879,62 @@ var _App_serviceDesc = grpc.ServiceDesc{ ServerStreams: true, }, }, - Metadata: "app.proto", + Metadata: "pkg/protobuf/app/app.proto", } -func init() { proto.RegisterFile("app.proto", fileDescriptor0) } +func init() { proto.RegisterFile("pkg/protobuf/app/app.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 789 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0xdd, 0x6a, 0x3b, 0x45, - 0x14, 0x67, 0xbb, 0x9b, 0x4d, 0x72, 0x92, 0xda, 0x3a, 0xd4, 0xb2, 0x5d, 0x7b, 0xd1, 0x2e, 0x14, - 0x22, 0xad, 0x31, 0xb6, 0x05, 0x41, 0x2f, 0x34, 0x48, 0x04, 0x21, 0x60, 0x9d, 0xb6, 0xde, 0x86, - 0x31, 0x99, 0xd6, 0xc5, 0xcd, 0xce, 0x74, 0x67, 0x36, 0x36, 0x05, 0x5f, 0x40, 0x9f, 0xc4, 0x07, - 0xf3, 0x09, 0xc4, 0x7b, 0x99, 0x8f, 0xdd, 0x6c, 0xd2, 0x24, 0x52, 0xc1, 0xff, 0x55, 0xce, 0x39, - 0xf3, 0x3b, 0x1f, 0xf3, 0x9b, 0x73, 0xce, 0x06, 0x9a, 0x84, 0xf3, 0x2e, 0xcf, 0x98, 0x64, 0xc8, - 0x25, 0x9c, 0x47, 0x7f, 0x78, 0xb0, 0xfb, 0x75, 0x46, 0x89, 0xa4, 0x98, 0x3e, 0xe5, 0x54, 0x48, - 0x84, 0xc0, 0x4b, 0xc9, 0x94, 0x06, 0xce, 0x89, 0xd3, 0x69, 0x62, 0x2d, 0x2b, 0x9b, 0xa4, 0x64, - 0x1a, 0xec, 0x18, 0x9b, 0x92, 0xd1, 0x29, 0xb4, 0x79, 0xc6, 0xc6, 0x54, 0x88, 0x91, 0x9c, 0x73, - 0x1a, 0xb8, 0xfa, 0xac, 0x65, 0x6d, 0x77, 0x73, 0x4e, 0xd1, 0xa7, 0xe0, 0x27, 0xf1, 0x34, 0x96, - 0x22, 0xf0, 0x4e, 0x9c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x0b, 0xed, 0x9b, 0x85, 0xbb, 0xd8, 0x8a, 0xe1, 0xfb, 0x15, 0x8b, 0x79, - 0xaf, 0x9e, 0x83, 0xce, 0xc1, 0x53, 0x2f, 0x68, 0xe1, 0x95, 0x7d, 0x66, 0xe1, 0x4b, 0xdb, 0xab, - 0x03, 0xbe, 0xe1, 0xca, 0x56, 0xb1, 0x44, 0xdc, 0x52, 0x15, 0x17, 0xd0, 0x28, 0x28, 0x40, 0x07, - 0xda, 0xbe, 0xc2, 0xc8, 0x12, 0xfa, 0x0c, 0x3c, 0xf5, 0x65, 0x40, 0x15, 0x5b, 0x51, 0x6d, 0xe5, - 0x83, 0xf1, 0xa3, 0xaf, 0xff, 0x99, 0x5c, 0xfd, 0x13, 0x00, 0x00, 0xff, 0xff, 0x35, 0x92, 0xfc, - 0x1d, 0xa6, 0x08, 0x00, 0x00, + // 803 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0xdd, 0x8e, 0xdb, 0x44, + 0x14, 0x96, 0x63, 0xc7, 0x49, 0x4e, 0x52, 0x5a, 0x46, 0x4b, 0xe5, 0x9a, 0x5e, 0xa4, 0x96, 0x2a, + 0x05, 0xb5, 0x64, 0x43, 0x5a, 0x09, 0x09, 0x6e, 0x88, 0x50, 0x90, 0x90, 0x22, 0x51, 0x66, 0x77, + 0xb9, 0x8d, 0xa6, 0xc9, 0x24, 0x58, 0x75, 0x3c, 0x53, 0xcf, 0x38, 0x6c, 0x10, 0xbc, 0x00, 0x3c, + 0x09, 0x0f, 0xc6, 0x13, 0x20, 0xee, 0xd1, 0xfc, 0xd8, 0xb1, 0xb3, 0x49, 0x50, 0x91, 0xe8, 0x45, + 0xe4, 0x33, 0x67, 0xbe, 0xf3, 0x33, 0xdf, 0x9c, 0x73, 0x26, 0x10, 0xf2, 0x37, 0xeb, 0x4b, 0x9e, + 0x31, 0xc9, 0x5e, 0xe7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 0x9d, 0xdc, 0xfd, 0xaf, 0x43, 0x2e, 0xfa, 0xdd, 0x81, 0x7b, + 0x57, 0x54, 0x4e, 0xd3, 0xed, 0xb9, 0x11, 0xf0, 0xb2, 0xd2, 0x2f, 0xd5, 0x3e, 0xab, 0x59, 0x1e, + 0x36, 0xcc, 0xbb, 0x57, 0x5a, 0xf4, 0x15, 0xdc, 0xbf, 0x49, 0xc5, 0xbf, 0xa6, 0xf3, 0xe8, 0x20, + 0x9d, 0x4e, 0x19, 0x33, 0x6a, 0x41, 0x73, 0xba, 0xe1, 0x72, 0x37, 0xfe, 0xad, 0x61, 0x86, 0xe3, + 0x00, 0x7c, 0xf3, 0x9c, 0x20, 0x74, 0xf7, 0x6d, 0x09, 0x41, 0xeb, 0xb4, 0x05, 0xfa, 0x14, 0x3c, + 0x35, 0xb9, 0xd0, 0x03, 0x33, 0x72, 0xf7, 0x53, 0x31, 0xfc, 0xb0, 0xa2, 0x31, 0xf7, 0x35, 0x72, + 0xd0, 0x33, 0xf0, 0xd4, 0x0d, 0x5a, 0x78, 0x65, 0x9e, 0x59, 0x78, 0x6d, 0x7a, 0x0d, 0xc0, 0x37, + 0x5c, 0xd9, 0x2c, 0x6a, 0xc4, 0xd5, 0xb2, 0x78, 0x0e, 0xed, 0x82, 0x02, 0x74, 0xa1, 0xf5, 0x07, + 0x8c, 0xd4, 0xd0, 0x4f, 0xc1, 0x53, 0x6f, 0x03, 0xaa, 0xe8, 0x8a, 0x6c, 0x2b, 0x4f, 0xc6, 0x6b, + 0x5f, 0xff, 0x37, 0x79, 0xf1, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfc, 0x16, 0x9f, 0xb7, 0xb9, + 0x08, 0x00, 0x00, } diff --git a/pkg/protobuf/app/app.proto b/pkg/protobuf/app/app.proto index cef704bba..9b5c92696 100644 --- a/pkg/protobuf/app/app.proto +++ b/pkg/protobuf/app/app.proto @@ -36,11 +36,11 @@ message CreateRequest { } message ListResponse { - + message App { string team = 1; - string app = 2; - string urls = 3; + string name = 2; + repeated string urls = 3; } repeated App apps = 1; diff --git a/pkg/server/app/app.go b/pkg/server/app/app.go index 45ef5ec6c..ca853c7cc 100644 --- a/pkg/server/app/app.go +++ b/pkg/server/app/app.go @@ -26,7 +26,7 @@ type Operations interface { HasPermission(user *storage.User, appName string) bool SetEnv(user *storage.User, appName string, evs []*EnvVar) error UnsetEnv(user *storage.User, appName string, evs []string) error - List(user *storage.User) ([]*List, error) + List(user *storage.User) ([]*AppListItem, error) } type K8sOperations interface { @@ -48,7 +48,7 @@ type K8sOperations interface { DeleteDeployEnvVars(namespace, name string, evNames []string) error CreateOrUpdateDeployEnvVars(namespace, name string, evs []*EnvVar) error DeleteNamespace(namespace string) error - ListNamespaceByLabel(label string) ([]string, error) + NamespaceListByLabel(label, value string) ([]string, error) } type AppOperations struct { @@ -337,31 +337,30 @@ func checkForProtectedEnvVars(evsNames []string) error { return nil } -func (ops *AppOperations) List(user *storage.User) ([]*List, error) { +func (ops *AppOperations) List(user *storage.User) ([]*AppListItem, error) { teams, err := ops.tops.ListByUser(user.Email) if err != nil { return nil, err } - result := make([]*List, 0) + items := make([]*AppListItem, 0) for _, team := range teams { - appNames, err := ops.kops.ListNamespaceByLabel(team.Name) + apps, err := ops.kops.NamespaceListByLabel(TeresaTeamLabel, team.Name) if err != nil { return nil, err } - for _, app := range appNames { - appAdd, err := ops.kops.AddressList(app) + for _, a := range apps { + addrs, err := ops.kops.AddressList(a) if err != nil { return nil, err } - list := &List{ + items = append(items, &AppListItem{ Team: team.Name, - Addresses: appAdd, - Name: string(app), - } - result = append(result, list) + Name: a, + Addresses: addrs, + }) } } - return result, nil + return items, nil } func NewOperations(tops team.Operations, kops K8sOperations, st st.Storage) Operations { diff --git a/pkg/server/app/app_test.go b/pkg/server/app/app_test.go index 2f5ae2903..c9d633c63 100644 --- a/pkg/server/app/app_test.go +++ b/pkg/server/app/app_test.go @@ -20,8 +20,7 @@ import ( ) type fakeK8sOperations struct { - Namespaces map[string]struct{} - ErrNotFound error + Namespaces map[string]struct{} } type errK8sOperations struct { @@ -130,8 +129,12 @@ func (f *fakeK8sOperations) DeleteNamespace(namespace string) error { return nil } -func (*fakeK8sOperations) ListNamespaceByLabel(label string) ([]string, error) { - return nil, nil +func (f *fakeK8sOperations) NamespaceListByLabel(label, value string) ([]string, error) { + ns := make([]string, 0) + for s, _ := range f.Namespaces { + ns = append(ns, s) + } + return ns, nil } func (e *errK8sOperations) CreateNamespace(app *App, user string) error { @@ -207,7 +210,7 @@ func (e *errK8sOperations) DeleteNamespace(namespace string) error { return e.DeleteNamespaceErr } -func (e *errK8sOperations) ListNamespaceByLabel(label string) ([]string, error) { +func (e *errK8sOperations) NamespaceListByLabel(label, value string) ([]string, error) { return nil, e.Err } @@ -529,27 +532,35 @@ func TestAppOperationsInfoErrNotFound(t *testing.T) { func TestAppOperationsList(t *testing.T) { tops := team.NewFakeOperations() - ops := NewOperations(tops, &fakeK8sOperations{}, nil) + appName := "teresa" teamName := "luizalabs" + user := &storage.User{Email: "teresa@luizalabs.com"} - app := &App{Name: "teresa", Team: teamName} - tops.(*team.FakeOperations).Storage[app.Name] = &storage.Team{ + fk8s := &fakeK8sOperations{Namespaces: map[string]struct{}{appName: struct{}{}}} + + ops := NewOperations(tops, fk8s, nil) + tops.(*team.FakeOperations).Storage[appName] = &storage.Team{ Name: teamName, Users: []storage.User{*user}, } - list, err := ops.List(user) + apps, err := ops.List(user) if err != nil { - t.Fatal("error getting app list: ", err) + t.Fatal("error getting app list:", err) } - for _, a := range list { + if len(apps) == 0 { + t.Fatal("expected at least one app") + } + for _, a := range apps { + if a.Name != appName { + t.Errorf("expected %s, got %s", appName, a.Name) + } if a.Team != teamName { t.Errorf("expected %s, got %s", teamName, a.Team) } - if len(a.Addresses) != 1 { // see fakeK8sOperations.AddressList - t.Errorf("expected 2, got %d", len(a.Addresses)) + t.Errorf("expected 1 address, got %d", len(a.Addresses)) } } } diff --git a/pkg/server/app/fake.go b/pkg/server/app/fake.go index 2c541d0bc..d7f077b66 100644 --- a/pkg/server/app/fake.go +++ b/pkg/server/app/fake.go @@ -79,22 +79,19 @@ func (f *FakeOperations) Info(user *storage.User, appName string) (*Info, error) return &Info{}, nil } -func (f *FakeOperations) List(user *storage.User) ([]*List, error) { - f.mutex.Lock() - defer f.mutex.Unlock() - - if !hasPerm(user.Email) { - return nil, teresa_errors.New(auth.ErrPermissionDenied, fmt.Errorf("error")) +func (f *FakeOperations) List(user *storage.User) ([]*AppListItem, error) { + f.mutex.RLock() + defer f.mutex.RUnlock() + + items := make([]*AppListItem, 0) + for k, v := range f.Storage { + items = append(items, &AppListItem{ + Team: v.Team, + Name: k, + Addresses: []*Address{&Address{Hostname: "localhost"}}, + }) } - - return []*List { - &List{ - Team: "teresa", - Name: "test", - Addresses: []*Address{&Address{Hostname: "localhost"}, - }, - }, - }, nil + return items, nil } func (f *FakeOperations) TeamName(appName string) (string, error) { diff --git a/pkg/server/app/fake_test.go b/pkg/server/app/fake_test.go index f4173d00e..9253ebb80 100644 --- a/pkg/server/app/fake_test.go +++ b/pkg/server/app/fake_test.go @@ -168,13 +168,17 @@ func TestFakeOperationsList(t *testing.T) { app := &App{Name: "teresa"} fake.(*FakeOperations).Storage[app.Name] = app - list, err := fake.List(user) + apps, err := fake.List(user) if err != nil { t.Fatal("error getting app info: ", err) } - if list == nil { - t.Fatal("expected a valid info, got nil") + if len(apps) != 1 { + t.Fatal("expected a valid app, got nil") + } + + if apps[0].Name != app.Name { + t.Errorf("expected test, got %s", apps[0].Name) } } diff --git a/pkg/server/app/handlers.go b/pkg/server/app/handlers.go index 4b1745bf9..4d2646111 100644 --- a/pkg/server/app/handlers.go +++ b/pkg/server/app/handlers.go @@ -3,8 +3,6 @@ package app import ( "time" - log "github.com/Sirupsen/logrus" - context "golang.org/x/net/context" "google.golang.org/grpc" @@ -12,7 +10,6 @@ import ( "github.com/luizalabs/teresa-api/models/storage" "github.com/luizalabs/teresa-api/pkg/goutil" appb "github.com/luizalabs/teresa-api/pkg/protobuf/app" - "github.com/luizalabs/teresa-api/pkg/server/teresa_errors" ) const ( @@ -98,13 +95,12 @@ func (s *Service) UnsetEnv(ctx context.Context, req *appb.UnsetEnvRequest) (*app func (s *Service) List(ctx context.Context, _ *appb.Empty) (*appb.ListResponse, error) { user := ctx.Value("user").(*storage.User) - list, err := s.ops.List(user) + apps, err := s.ops.List(user) if err != nil { - log.Errorf("app list failed: %v", err) - return nil, teresa_errors.Get(err) + return nil, err } - return newListResponse(list), nil + return newListResponse(apps), nil } func (s *Service) RegisterService(grpcServer *grpc.Server) { diff --git a/pkg/server/app/handlers_test.go b/pkg/server/app/handlers_test.go index 54e7a9e92..5b295576f 100644 --- a/pkg/server/app/handlers_test.go +++ b/pkg/server/app/handlers_test.go @@ -173,18 +173,6 @@ func TestListSuccess(t *testing.T) { } } -/* -func TestListAppNotFound(t *testing.T) { - s := NewService(NewFakeOperations()) - user := &storage.User{Email: "gopher@luizalabs.com"} - ctx := context.WithValue(context.Background(), "user", user) - - if _, err := s.List(ctx, &appb.Empty{}); teresa_errors.Get(err) != nil { - t.Errorf("expected ErrNotFound, got %v", err) - } -} -*/ - func TestSetEnvSuccess(t *testing.T) { fake := NewFakeOperations() name := "teresa" diff --git a/pkg/server/app/models.go b/pkg/server/app/models.go index 563920b8d..8032bce72 100644 --- a/pkg/server/app/models.go +++ b/pkg/server/app/models.go @@ -1,10 +1,6 @@ package app -import ( - appb "github.com/luizalabs/teresa-api/pkg/protobuf/app" - - "strings" -) +import appb "github.com/luizalabs/teresa-api/pkg/protobuf/app" const ( ProcessTypeWeb = "web" @@ -63,14 +59,10 @@ type Info struct { Limits *Limits } -type List struct { +type AppListItem struct { Team string - Addresses []*Address Name string -} - -type AppList struct { - AppList string + Addresses []*Address } func newSliceLrq(s []*appb.CreateRequest_Limits_LimitRangeQuantity) []*LimitRangeQuantity { @@ -250,32 +242,23 @@ func unsetEnvVars(app *App, evs []string) { } } -func newListResponse(list []*List) *appb.ListResponse { - if list == nil { +func newListResponse(items []*AppListItem) *appb.ListResponse { + if items == nil { return nil } - appNames := []*appb.ListResponse_App{} - - for _, item := range list { - if item == nil { - continue - } - var tmp []string - for _, elt := range item.Addresses { - tmp = append(tmp, elt.Hostname) + apps := make([]*appb.ListResponse_App, 0) + for _, item := range items { + addresses := make([]string, 0) + for _, addr := range item.Addresses { + addresses = append(addresses, addr.Hostname) } - addrs := strings.Join(tmp, ",") - - appName := &appb.ListResponse_App{ - Urls: addrs, - App: item.Name, + apps = append(apps, &appb.ListResponse_App{ + Urls: addresses, + Name: item.Name, Team: item.Team, - } - appNames = append(appNames, appName) + }) } - return &appb.ListResponse{ - Apps: appNames, - } + return &appb.ListResponse{Apps: apps} } diff --git a/pkg/server/app/models_test.go b/pkg/server/app/models_test.go index 36bfffec7..bd9e2e504 100644 --- a/pkg/server/app/models_test.go +++ b/pkg/server/app/models_test.go @@ -2,18 +2,11 @@ package app import ( "reflect" - "strings" "testing" appb "github.com/luizalabs/teresa-api/pkg/protobuf/app" ) -type ListTest struct { - team string - url string - app string -} - // Shamelessly adapted from the standard library func deepEqual(x, y interface{}) bool { if x == nil || y == nil { @@ -162,41 +155,28 @@ func TestNewInfoResponse(t *testing.T) { } func TestNewListResponse(t *testing.T) { - lists := make([]*List, 0) - list := &List{ + items := []*AppListItem{{ Team: "luizalabs", Addresses: []*Address{{Hostname: "host1"}}, Name: "teste", - } - lists = append(lists, list) - items := []*appb.ListResponse_App{} - - for _, item := range lists { - if item == nil { - continue - } - var tmp []string - for _, elt := range item.Addresses { - tmp = append(tmp, elt.Hostname) - } - addrs := strings.Join(tmp, ",") + }} - appName := &appb.ListResponse_App{ - Urls: addrs, - App: item.Name, - Team: item.Team, - } - items = append(items, appName) + resp := newListResponse(items) + if len(items) != len(resp.Apps) { + t.Fatal("expected %d items, got %d", len(items), len(resp.Apps)) } - - items2 := &appb.ListResponse{ - Apps: items, + itemExpected := items[0] + itemActual := resp.Apps[0] + expectedUrl := itemExpected.Addresses[0].Hostname + actualUrl := itemActual.Urls[0] + if expectedUrl != actualUrl { + t.Errorf("expected %s, got %s", expectedUrl, actualUrl) } - - resp := newListResponse(lists) - - if !deepEqual(resp, items2) { - t.Errorf("expected %v, got %v", resp, items2) + if itemExpected.Name != itemActual.Name { + t.Errorf("expected %s, got %s", itemExpected.Name, itemActual.Name) + } + if itemExpected.Team != itemActual.Team { + t.Errorf("expected %s, got %s", itemExpected.Team, itemActual.Team) } } diff --git a/pkg/server/k8s/client.go b/pkg/server/k8s/client.go index d9394df62..5a305ac97 100644 --- a/pkg/server/k8s/client.go +++ b/pkg/server/k8s/client.go @@ -103,10 +103,10 @@ func newNs(a *app.App, user string) *k8sv1.Namespace { ObjectMeta: k8sv1.ObjectMeta{ Name: a.Name, Labels: map[string]string{ - "teresa.io/team": a.Team, + app.TeresaTeamLabel: a.Team, }, Annotations: map[string]string{ - "teresa.io/last-user": user, + app.TeresaLastUser: user, }, }, } @@ -118,7 +118,7 @@ func addAppToNs(a *app.App, ns *k8sv1.Namespace) error { return err } - ns.Annotations["teresa.io/app"] = string(b) + ns.Annotations[app.TeresaAnnotation] = string(b) return nil } @@ -502,18 +502,17 @@ func (k *k8sClient) DeleteNamespace(namespace string) error { return errors.Wrap(err, "delete ns failed") } -func (k k8sClient) ListNamespaceByLabel(label string) ([]string, error) { - ls := fmt.Sprintf("teresa.io/team=%s", label) - appls, err := k.kc.CoreV1().Namespaces().List(k8sv1.ListOptions{LabelSelector: ls}) +func (k k8sClient) NamespaceListByLabel(label, value string) ([]string, error) { + labelSelector := fmt.Sprintf("%s=%s", label, value) + nl, err := k.kc.CoreV1().Namespaces().List(k8sv1.ListOptions{LabelSelector: labelSelector}) if err != nil { return nil, err } - apps := make([]string, 0) - for _, appl := range appls.Items { - app := appl.ObjectMeta.Name - apps = append(apps, string(app)) + namespaces := make([]string, 0) + for _, item := range nl.Items { + namespaces = append(namespaces, item.ObjectMeta.Name) } - return apps, nil + return namespaces, nil } func newInClusterK8sClient(conf *Config) (Client, error) { diff --git a/pkg/server/k8s/errors.go b/pkg/server/k8s/errors.go index 0aabab223..9faed5ccf 100644 --- a/pkg/server/k8s/errors.go +++ b/pkg/server/k8s/errors.go @@ -5,7 +5,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - + k8serrors "k8s.io/client-go/pkg/api/errors" ) diff --git a/pkg/server/k8s/k8s.go b/pkg/server/k8s/k8s.go index d7c576749..973c629f2 100644 --- a/pkg/server/k8s/k8s.go +++ b/pkg/server/k8s/k8s.go @@ -1,7 +1,6 @@ package k8s import ( - "github.com/luizalabs/teresa-api/pkg/server/app" "github.com/luizalabs/teresa-api/pkg/server/deploy" "github.com/luizalabs/teresa-api/pkg/server/healthcheck" @@ -15,8 +14,8 @@ var validServiceTypes = map[api.ServiceType]bool{ } type Config struct { - ConfigFile string `split_words:"true"` - DefaultServiceType string `split_words:"true" default:"LoadBalancer"` + ConfigFile string `split_words:"true"` + DefaultServiceType string `split_words:"true" default:"LoadBalancer"` } type Client interface { @@ -41,4 +40,4 @@ func New(conf *Config) (Client, error) { return newInClusterK8sClient(conf) } return newOutOfClusterK8sClient(conf) -} \ No newline at end of file +} diff --git a/pkg/server/k8s/k8s_test.go b/pkg/server/k8s/k8s_test.go index 2adb05102..76b8c84c2 100644 --- a/pkg/server/k8s/k8s_test.go +++ b/pkg/server/k8s/k8s_test.go @@ -19,4 +19,4 @@ func TestValidateConfig(t *testing.T) { t.Errorf("expected %v, got %v", tc.expectedError, err) } } -} \ No newline at end of file +}