From bb69de1db3ec9f4a6811c3c781063a9886a65e8b Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Sat, 6 Jun 2020 22:30:04 +0200 Subject: [PATCH] Add context endpoint to set the current context --- cli/cmd/serve.go | 6 +- protos/contexts/v1/contexts.pb.go | 240 +++++++++++++++++++++++++----- protos/contexts/v1/contexts.proto | 13 +- server/contexts.go | 35 ----- server/proxy/contexts.go | 39 +++++ server/proxy/proxy.go | 18 ++- server/server.go | 5 +- 7 files changed, 275 insertions(+), 81 deletions(-) delete mode 100644 server/contexts.go create mode 100644 server/proxy/contexts.go diff --git a/cli/cmd/serve.go b/cli/cmd/serve.go index 0ab492c6f..66d6d9c7a 100644 --- a/cli/cmd/serve.go +++ b/cli/cmd/serve.go @@ -7,6 +7,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + apicontext "github.com/docker/api/context" containersv1 "github.com/docker/api/protos/containers/v1" contextsv1 "github.com/docker/api/protos/contexts/v1" streamsv1 "github.com/docker/api/protos/streams/v1" @@ -45,12 +46,11 @@ func runServe(ctx context.Context, opts serveOpts) error { // nolint errcheck defer listener.Close() - p := proxy.New() - contextsService := server.NewContexts() + p := proxy.New(apicontext.CurrentContext(ctx)) containersv1.RegisterContainersServer(s, p) streamsv1.RegisterStreamingServer(s, p) - contextsv1.RegisterContextsServer(s, contextsService) + contextsv1.RegisterContextsServer(s, p.ContextsProxy()) go func() { <-ctx.Done() diff --git a/protos/contexts/v1/contexts.pb.go b/protos/contexts/v1/contexts.pb.go index 08ba25f67..a5cda1d40 100644 --- a/protos/contexts/v1/contexts.pb.go +++ b/protos/contexts/v1/contexts.pb.go @@ -62,6 +62,7 @@ type Context struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` ContextType string `protobuf:"bytes,2,opt,name=contextType,proto3" json:"contextType,omitempty"` + Current bool `protobuf:"varint,3,opt,name=current,proto3" json:"current,omitempty"` } func (x *Context) Reset() { @@ -110,6 +111,98 @@ func (x *Context) GetContextType() string { return "" } +func (x *Context) GetCurrent() bool { + if x != nil { + return x.Current + } + return false +} + +type SetCurrentRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *SetCurrentRequest) Reset() { + *x = SetCurrentRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetCurrentRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetCurrentRequest) ProtoMessage() {} + +func (x *SetCurrentRequest) ProtoReflect() protoreflect.Message { + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SetCurrentRequest.ProtoReflect.Descriptor instead. +func (*SetCurrentRequest) Descriptor() ([]byte, []int) { + return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{1} +} + +func (x *SetCurrentRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type SetCurrentResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *SetCurrentResponse) Reset() { + *x = SetCurrentResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetCurrentResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetCurrentResponse) ProtoMessage() {} + +func (x *SetCurrentResponse) ProtoReflect() protoreflect.Message { + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SetCurrentResponse.ProtoReflect.Descriptor instead. +func (*SetCurrentResponse) Descriptor() ([]byte, []int) { + return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{2} +} + type ListRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -119,7 +212,7 @@ type ListRequest struct { func (x *ListRequest) Reset() { *x = ListRequest{} if protoimpl.UnsafeEnabled { - mi := &file_protos_contexts_v1_contexts_proto_msgTypes[1] + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -132,7 +225,7 @@ func (x *ListRequest) String() string { func (*ListRequest) ProtoMessage() {} func (x *ListRequest) ProtoReflect() protoreflect.Message { - mi := &file_protos_contexts_v1_contexts_proto_msgTypes[1] + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -145,7 +238,7 @@ func (x *ListRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use ListRequest.ProtoReflect.Descriptor instead. func (*ListRequest) Descriptor() ([]byte, []int) { - return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{1} + return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{3} } type ListResponse struct { @@ -159,7 +252,7 @@ type ListResponse struct { func (x *ListResponse) Reset() { *x = ListResponse{} if protoimpl.UnsafeEnabled { - mi := &file_protos_contexts_v1_contexts_proto_msgTypes[2] + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -172,7 +265,7 @@ func (x *ListResponse) String() string { func (*ListResponse) ProtoMessage() {} func (x *ListResponse) ProtoReflect() protoreflect.Message { - mi := &file_protos_contexts_v1_contexts_proto_msgTypes[2] + mi := &file_protos_contexts_v1_contexts_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -185,7 +278,7 @@ func (x *ListResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use ListResponse.ProtoReflect.Descriptor instead. func (*ListResponse) Descriptor() ([]byte, []int) { - return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{2} + return file_protos_contexts_v1_contexts_proto_rawDescGZIP(), []int{4} } func (x *ListResponse) GetContexts() []*Context { @@ -202,28 +295,41 @@ var file_protos_contexts_v1_contexts_proto_rawDesc = []byte{ 0x73, 0x2f, 0x76, 0x31, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x20, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x65, - 0x78, 0x74, 0x2e, 0x76, 0x31, 0x22, 0x3f, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, + 0x78, 0x74, 0x2e, 0x76, 0x31, 0x22, 0x59, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, - 0x78, 0x74, 0x54, 0x79, 0x70, 0x65, 0x22, 0x0d, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x55, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x45, 0x0a, 0x08, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, - 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, - 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, - 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, 0x76, 0x31, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x65, - 0x78, 0x74, 0x52, 0x08, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x73, 0x32, 0x71, 0x0a, 0x08, - 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x73, 0x12, 0x65, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, - 0x12, 0x2d, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, + 0x78, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, + 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, + 0x22, 0x27, 0x0a, 0x11, 0x53, 0x65, 0x74, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x14, 0x0a, 0x12, 0x53, 0x65, 0x74, + 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x0d, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x55, + 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x45, + 0x0a, 0x08, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x29, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, - 0x2e, 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x2e, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, - 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, - 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x6f, - 0x63, 0x6b, 0x65, 0x72, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2f, - 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x76, 0x31, 0x3b, 0x76, 0x31, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x2e, 0x76, 0x31, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x52, 0x08, 0x63, 0x6f, 0x6e, + 0x74, 0x65, 0x78, 0x74, 0x73, 0x32, 0xea, 0x01, 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, + 0x74, 0x73, 0x12, 0x77, 0x0a, 0x0a, 0x53, 0x65, 0x74, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, + 0x12, 0x33, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, 0x61, 0x70, + 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, + 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x65, 0x74, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x34, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, + 0x65, 0x72, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, + 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x65, 0x74, 0x43, 0x75, 0x72, 0x72, + 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x65, 0x0a, 0x04, 0x4c, + 0x69, 0x73, 0x74, 0x12, 0x2d, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, + 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, + 0x65, 0x78, 0x74, 0x2e, 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x2e, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2e, + 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2e, 0x63, 0x6f, 0x6e, 0x74, 0x65, + 0x78, 0x74, 0x2e, 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x76, 0x31, 0x3b, 0x76, 0x31, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -238,18 +344,22 @@ func file_protos_contexts_v1_contexts_proto_rawDescGZIP() []byte { return file_protos_contexts_v1_contexts_proto_rawDescData } -var file_protos_contexts_v1_contexts_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_protos_contexts_v1_contexts_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_protos_contexts_v1_contexts_proto_goTypes = []interface{}{ - (*Context)(nil), // 0: com.docker.api.protos.context.v1.Context - (*ListRequest)(nil), // 1: com.docker.api.protos.context.v1.ListRequest - (*ListResponse)(nil), // 2: com.docker.api.protos.context.v1.ListResponse + (*Context)(nil), // 0: com.docker.api.protos.context.v1.Context + (*SetCurrentRequest)(nil), // 1: com.docker.api.protos.context.v1.SetCurrentRequest + (*SetCurrentResponse)(nil), // 2: com.docker.api.protos.context.v1.SetCurrentResponse + (*ListRequest)(nil), // 3: com.docker.api.protos.context.v1.ListRequest + (*ListResponse)(nil), // 4: com.docker.api.protos.context.v1.ListResponse } var file_protos_contexts_v1_contexts_proto_depIdxs = []int32{ 0, // 0: com.docker.api.protos.context.v1.ListResponse.contexts:type_name -> com.docker.api.protos.context.v1.Context - 1, // 1: com.docker.api.protos.context.v1.Contexts.List:input_type -> com.docker.api.protos.context.v1.ListRequest - 2, // 2: com.docker.api.protos.context.v1.Contexts.List:output_type -> com.docker.api.protos.context.v1.ListResponse - 2, // [2:3] is the sub-list for method output_type - 1, // [1:2] is the sub-list for method input_type + 1, // 1: com.docker.api.protos.context.v1.Contexts.SetCurrent:input_type -> com.docker.api.protos.context.v1.SetCurrentRequest + 3, // 2: com.docker.api.protos.context.v1.Contexts.List:input_type -> com.docker.api.protos.context.v1.ListRequest + 2, // 3: com.docker.api.protos.context.v1.Contexts.SetCurrent:output_type -> com.docker.api.protos.context.v1.SetCurrentResponse + 4, // 4: com.docker.api.protos.context.v1.Contexts.List:output_type -> com.docker.api.protos.context.v1.ListResponse + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name @@ -274,7 +384,7 @@ func file_protos_contexts_v1_contexts_proto_init() { } } file_protos_contexts_v1_contexts_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ListRequest); i { + switch v := v.(*SetCurrentRequest); i { case 0: return &v.state case 1: @@ -286,6 +396,30 @@ func file_protos_contexts_v1_contexts_proto_init() { } } file_protos_contexts_v1_contexts_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SetCurrentResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_protos_contexts_v1_contexts_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_protos_contexts_v1_contexts_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ListResponse); i { case 0: return &v.state @@ -304,7 +438,7 @@ func file_protos_contexts_v1_contexts_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_protos_contexts_v1_contexts_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 5, NumExtensions: 0, NumServices: 1, }, @@ -330,6 +464,8 @@ const _ = grpc.SupportPackageIsVersion6 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type ContextsClient interface { + // Sets the current request for all calls + SetCurrent(ctx context.Context, in *SetCurrentRequest, opts ...grpc.CallOption) (*SetCurrentResponse, error) // Returns the list of existing contexts List(ctx context.Context, in *ListRequest, opts ...grpc.CallOption) (*ListResponse, error) } @@ -342,6 +478,15 @@ func NewContextsClient(cc grpc.ClientConnInterface) ContextsClient { return &contextsClient{cc} } +func (c *contextsClient) SetCurrent(ctx context.Context, in *SetCurrentRequest, opts ...grpc.CallOption) (*SetCurrentResponse, error) { + out := new(SetCurrentResponse) + err := c.cc.Invoke(ctx, "/com.docker.api.protos.context.v1.Contexts/SetCurrent", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *contextsClient) List(ctx context.Context, in *ListRequest, opts ...grpc.CallOption) (*ListResponse, error) { out := new(ListResponse) err := c.cc.Invoke(ctx, "/com.docker.api.protos.context.v1.Contexts/List", in, out, opts...) @@ -353,6 +498,8 @@ func (c *contextsClient) List(ctx context.Context, in *ListRequest, opts ...grpc // ContextsServer is the server API for Contexts service. type ContextsServer interface { + // Sets the current request for all calls + SetCurrent(context.Context, *SetCurrentRequest) (*SetCurrentResponse, error) // Returns the list of existing contexts List(context.Context, *ListRequest) (*ListResponse, error) } @@ -361,6 +508,9 @@ type ContextsServer interface { type UnimplementedContextsServer struct { } +func (*UnimplementedContextsServer) SetCurrent(context.Context, *SetCurrentRequest) (*SetCurrentResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method SetCurrent not implemented") +} func (*UnimplementedContextsServer) List(context.Context, *ListRequest) (*ListResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method List not implemented") } @@ -369,6 +519,24 @@ func RegisterContextsServer(s *grpc.Server, srv ContextsServer) { s.RegisterService(&_Contexts_serviceDesc, srv) } +func _Contexts_SetCurrent_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetCurrentRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ContextsServer).SetCurrent(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/com.docker.api.protos.context.v1.Contexts/SetCurrent", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ContextsServer).SetCurrent(ctx, req.(*SetCurrentRequest)) + } + return interceptor(ctx, in, info, handler) +} + func _Contexts_List_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(ListRequest) if err := dec(in); err != nil { @@ -391,6 +559,10 @@ var _Contexts_serviceDesc = grpc.ServiceDesc{ ServiceName: "com.docker.api.protos.context.v1.Contexts", HandlerType: (*ContextsServer)(nil), Methods: []grpc.MethodDesc{ + { + MethodName: "SetCurrent", + Handler: _Contexts_SetCurrent_Handler, + }, { MethodName: "List", Handler: _Contexts_List_Handler, diff --git a/protos/contexts/v1/contexts.proto b/protos/contexts/v1/contexts.proto index 74f42b0cf..3a432974d 100644 --- a/protos/contexts/v1/contexts.proto +++ b/protos/contexts/v1/contexts.proto @@ -32,17 +32,26 @@ package com.docker.api.protos.context.v1; option go_package = "github.com/docker/api/protos/context/v1;v1"; service Contexts { + // Sets the current request for all calls + rpc SetCurrent(SetCurrentRequest) returns (SetCurrentResponse); // Returns the list of existing contexts rpc List(ListRequest) returns (ListResponse); } message Context { string name = 1; - string contextType = 2; + string contextType = 2; + bool current = 3; +} + +message SetCurrentRequest { + string name = 1; +} + +message SetCurrentResponse { } message ListRequest { - } message ListResponse { diff --git a/server/contexts.go b/server/contexts.go deleted file mode 100644 index 2c6e88d86..000000000 --- a/server/contexts.go +++ /dev/null @@ -1,35 +0,0 @@ -package server - -import ( - "context" - - "github.com/docker/api/context/store" - contextsv1 "github.com/docker/api/protos/contexts/v1" -) - -type cliServer struct { -} - -// NewContexts returns a contexts server -func NewContexts() contextsv1.ContextsServer { - return &cliServer{} -} - -func (cs *cliServer) List(ctx context.Context, request *contextsv1.ListRequest) (*contextsv1.ListResponse, error) { - s := store.ContextStore(ctx) - contexts, err := s.List() - if err != nil { - return &contextsv1.ListResponse{}, err - } - - result := &contextsv1.ListResponse{} - - for _, c := range contexts { - result.Contexts = append(result.Contexts, &contextsv1.Context{ - Name: c.Name, - ContextType: c.Type, - }) - } - - return result, nil -} diff --git a/server/proxy/contexts.go b/server/proxy/contexts.go new file mode 100644 index 000000000..b6c2e3ed8 --- /dev/null +++ b/server/proxy/contexts.go @@ -0,0 +1,39 @@ +package proxy + +import ( + "context" + + "github.com/docker/api/config" + "github.com/docker/api/context/store" + contextsv1 "github.com/docker/api/protos/contexts/v1" +) + +type contextsProxy struct { +} + +func (cp *contextsProxy) SetCurrent(ctx context.Context, request *contextsv1.SetCurrentRequest) (*contextsv1.SetCurrentResponse, error) { + if err := config.WriteCurrentContext(config.Dir(ctx), request.GetName()); err != nil { + return &contextsv1.SetCurrentResponse{}, err + } + + return &contextsv1.SetCurrentResponse{}, nil +} + +func (cp *contextsProxy) List(ctx context.Context, request *contextsv1.ListRequest) (*contextsv1.ListResponse, error) { + s := store.ContextStore(ctx) + contexts, err := s.List() + if err != nil { + return &contextsv1.ListResponse{}, err + } + + result := &contextsv1.ListResponse{} + + for _, c := range contexts { + result.Contexts = append(result.Contexts, &contextsv1.Context{ + Name: c.Name, + ContextType: c.Type, + }) + } + + return result, nil +} diff --git a/server/proxy/proxy.go b/server/proxy/proxy.go index 809a36a32..ef3f62c27 100644 --- a/server/proxy/proxy.go +++ b/server/proxy/proxy.go @@ -6,6 +6,7 @@ import ( "github.com/docker/api/client" containersv1 "github.com/docker/api/protos/containers/v1" + contextsv1 "github.com/docker/api/protos/contexts/v1" streamsv1 "github.com/docker/api/protos/streams/v1" ) @@ -27,16 +28,25 @@ func Client(ctx context.Context) *client.Client { type Proxy interface { containersv1.ContainersServer streamsv1.StreamingServer + ContextsProxy() contextsv1.ContextsServer } type proxy struct { - mu sync.Mutex - streams map[string]*Stream + currentContext string + mu sync.Mutex + streams map[string]*Stream + contextsProxy *contextsProxy } // New creates a new proxy server -func New() Proxy { +func New(currentContext string) Proxy { return &proxy{ - streams: map[string]*Stream{}, + currentContext: currentContext, + streams: map[string]*Stream{}, + contextsProxy: &contextsProxy{}, } } + +func (p *proxy) ContextsProxy() contextsv1.ContextsServer { + return p.contextsProxy +} diff --git a/server/server.go b/server/server.go index 8a1287b7a..533810fc6 100644 --- a/server/server.go +++ b/server/server.go @@ -40,7 +40,6 @@ import ( "github.com/docker/api/client" apicontext "github.com/docker/api/context" - "github.com/docker/api/context/store" "github.com/docker/api/server/proxy" ) @@ -127,8 +126,8 @@ func getContext(ctx context.Context) string { // configureContext populates the request context with objects the client // needs: the context store and the api client func configureContext(ctx context.Context, currentContext string) (context.Context, error) { - s := store.ContextStore(ctx) - ctx = store.WithContextStore(ctx, s) + // s := store.ContextStore(ctx) + // ctx = store.WithContextStore(ctx, s) if currentContext != "" { ctx = apicontext.WithCurrentContext(ctx, currentContext) }