Răsfoiți Sursa

Merge pull request #182 from rumpl/feat-context-server

Feat context server
Djordje Lukic 5 ani în urmă
părinte
comite
574a378886

+ 2 - 2
cli/cmd/context/use.go

@@ -33,7 +33,7 @@ import (
 
 	"github.com/spf13/cobra"
 
-	cliconfig "github.com/docker/api/cli/config"
+	"github.com/docker/api/config"
 	"github.com/docker/api/context/store"
 )
 
@@ -56,7 +56,7 @@ func runUse(ctx context.Context, name string) error {
 			return err
 		}
 	}
-	if err := cliconfig.WriteCurrentContext(cliconfig.Dir(ctx), name); err != nil {
+	if err := config.WriteCurrentContext(config.Dir(ctx), name); err != nil {
 		return err
 	}
 	fmt.Println(name)

+ 3 - 3
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()

+ 3 - 10
cli/config/flags.go

@@ -32,15 +32,8 @@ import (
 	"path/filepath"
 
 	"github.com/spf13/pflag"
-)
 
-const (
-	// ConfigFileName is the name of config file
-	ConfigFileName = "config.json"
-	// ConfigFileDir is the default folder where the config file is stored
-	ConfigFileDir = ".docker"
-	// ConfigFlagName is the name of the config flag
-	ConfigFlagName = "config"
+	"github.com/docker/api/config"
 )
 
 // ConfigFlags are the global CLI flags
@@ -51,7 +44,7 @@ type ConfigFlags struct {
 
 // AddConfigFlags adds persistent (global) flags
 func (c *ConfigFlags) AddConfigFlags(flags *pflag.FlagSet) {
-	flags.StringVar(&c.Config, ConfigFlagName, confDir(), "Location of the client config files `DIRECTORY`")
+	flags.StringVar(&c.Config, config.ConfigFlagName, confDir(), "Location of the client config files `DIRECTORY`")
 }
 
 func confDir() string {
@@ -60,5 +53,5 @@ func confDir() string {
 		return env
 	}
 	home, _ := os.UserHomeDir()
-	return filepath.Join(home, ConfigFileDir)
+	return filepath.Join(home, config.ConfigFileDir)
 }

+ 5 - 7
cli/main.go

@@ -38,10 +38,6 @@ import (
 	"syscall"
 	"time"
 
-	"github.com/docker/api/cli/cmd/login"
-
-	"github.com/docker/api/cli/dockerclassic"
-
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -54,9 +50,11 @@ import (
 	"github.com/docker/api/cli/cmd"
 	"github.com/docker/api/cli/cmd/compose"
 	contextcmd "github.com/docker/api/cli/cmd/context"
+	"github.com/docker/api/cli/cmd/login"
 	"github.com/docker/api/cli/cmd/run"
-	cliconfig "github.com/docker/api/cli/config"
+	"github.com/docker/api/cli/dockerclassic"
 	cliopts "github.com/docker/api/cli/options"
+	"github.com/docker/api/config"
 	apicontext "github.com/docker/api/context"
 	"github.com/docker/api/context/store"
 )
@@ -151,7 +149,7 @@ func main() {
 		fatal(errors.New("config path cannot be empty"))
 	}
 	configDir := opts.Config
-	ctx = cliconfig.WithDir(ctx, configDir)
+	ctx = config.WithDir(ctx, configDir)
 
 	currentContext, err := determineCurrentContext(opts.Context, configDir)
 	if err != nil {
@@ -206,7 +204,7 @@ func newSigContext() (context.Context, func()) {
 func determineCurrentContext(flag string, configDir string) (string, error) {
 	res := flag
 	if res == "" {
-		config, err := cliconfig.LoadFile(configDir)
+		config, err := config.LoadFile(configDir)
 		if err != nil {
 			return "", err
 		}

+ 1 - 1
cli/main_test.go

@@ -40,7 +40,7 @@ import (
 
 	"github.com/stretchr/testify/require"
 
-	"github.com/docker/api/cli/config"
+	"github.com/docker/api/config"
 )
 
 var contextSetConfig = []byte(`{

+ 0 - 0
cli/config/config.go → config/config.go


+ 0 - 0
cli/config/config_test.go → config/config_test.go


+ 9 - 0
cli/config/keys.go → config/keys.go

@@ -27,6 +27,15 @@
 
 package config
 
+const (
+	// ConfigFileName is the name of config file
+	ConfigFileName = "config.json"
+	// ConfigFileDir is the default folder where the config file is stored
+	ConfigFileDir = ".docker"
+	// ConfigFlagName is the name of the config flag
+	ConfigFlagName = "config"
+)
+
 const (
 	// currentContextKey is the key used in the Docker config file to set the
 	// default context

+ 206 - 34
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, 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, 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, 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,

+ 11 - 2
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 ListRequest {
+message SetCurrentRequest {
+	string name = 1;
+}
 
+message SetCurrentResponse {
+}
+
+message ListRequest {
 }
 
 message ListResponse {

+ 0 - 35
server/contexts.go

@@ -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
-}

+ 8 - 2
server/proxy/containers.go

@@ -6,6 +6,7 @@ import (
 
 	"github.com/docker/api/containers"
 	containersv1 "github.com/docker/api/protos/containers/v1"
+	"github.com/docker/api/server/proxy/streams"
 )
 
 func portsToGrpc(ports []containers.Port) []*containersv1.Port {
@@ -97,7 +98,10 @@ func (p *proxy) Exec(ctx context.Context, request *containersv1.ExecRequest) (*c
 		return &containersv1.ExecResponse{}, errors.New("unknown stream id")
 	}
 
-	err := Client(ctx).ContainerService().Exec(ctx, request.GetId(), request.GetCommand(), &reader{stream}, &writer{stream})
+	io := &streams.IO{
+		Stream: stream,
+	}
+	err := Client(ctx).ContainerService().Exec(ctx, request.GetId(), request.GetCommand(), io, io)
 
 	return &containersv1.ExecResponse{}, err
 }
@@ -108,6 +112,8 @@ func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Cont
 
 	return c.ContainerService().Logs(ctx, request.GetContainerId(), containers.LogsRequest{
 		Follow: request.Follow,
-		Writer: &logStream{stream},
+		Writer: &streams.Log{
+			Stream: stream,
+		},
 	})
 }

+ 39 - 0
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
+}

+ 0 - 25
server/proxy/logstream.go

@@ -1,25 +0,0 @@
-package proxy
-
-import (
-	"io"
-
-	"google.golang.org/grpc"
-
-	containersv1 "github.com/docker/api/protos/containers/v1"
-)
-
-type logStream struct {
-	stream grpc.ServerStream
-}
-
-func newStreamWriter(stream grpc.ServerStream) io.Writer {
-	return &logStream{
-		stream: stream,
-	}
-}
-
-func (w *logStream) Write(p []byte) (n int, err error) {
-	return len(p), w.stream.SendMsg(&containersv1.LogsResponse{
-		Value: p,
-	})
-}

+ 15 - 4
server/proxy/proxy.go

@@ -6,7 +6,9 @@ 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"
+	"github.com/docker/api/server/proxy/streams"
 )
 
 type clientKey struct{}
@@ -27,16 +29,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]*streams.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]*streams.Stream{},
+		contextsProxy:  &contextsProxy{},
 	}
 }
+
+func (p *proxy) ContextsProxy() contextsv1.ContextsServer {
+	return p.contextsProxy
+}

+ 3 - 73
server/proxy/streams.go

@@ -1,41 +1,14 @@
 package proxy
 
 import (
-	"sync"
-
 	"github.com/containerd/containerd/log"
-	"github.com/golang/protobuf/ptypes"
 	"github.com/google/uuid"
-	"google.golang.org/grpc"
 	"google.golang.org/grpc/metadata"
 
 	streamsv1 "github.com/docker/api/protos/streams/v1"
+	"github.com/docker/api/server/proxy/streams"
 )
 
-// Stream is a bidirectional stream for container IO
-type Stream struct {
-	streamsv1.Streaming_NewStreamServer
-
-	errm    sync.Mutex
-	errChan chan<- error
-}
-
-// CloseWithError sends the result of an action to the errChan or nil
-// if no erros
-func (s *Stream) CloseWithError(err error) error {
-	s.errm.Lock()
-	defer s.errm.Unlock()
-
-	if s.errChan != nil {
-		if err != nil {
-			s.errChan <- err
-		}
-		close(s.errChan)
-		s.errChan = nil
-	}
-	return nil
-}
-
 func (p *proxy) NewStream(stream streamsv1.Streaming_NewStreamServer) error {
 	var (
 		ctx = stream.Context()
@@ -53,9 +26,9 @@ func (p *proxy) NewStream(stream streamsv1.Streaming_NewStreamServer) error {
 	errc := make(chan error)
 
 	p.mu.Lock()
-	p.streams[id] = &Stream{
+	p.streams[id] = &streams.Stream{
 		Streaming_NewStreamServer: stream,
-		errChan:                   errc,
+		ErrChan:                   errc,
 	}
 	p.mu.Unlock()
 
@@ -73,46 +46,3 @@ func (p *proxy) NewStream(stream streamsv1.Streaming_NewStreamServer) error {
 		return ctx.Err()
 	}
 }
-
-// io.Reader that forwards everything to the stream
-type reader struct {
-	stream *Stream
-}
-
-func (r reader) Read(p []byte) (int, error) {
-	a, err := r.stream.Recv()
-	if err != nil {
-		return 0, err
-	}
-
-	var m streamsv1.BytesMessage
-	err = ptypes.UnmarshalAny(a, &m)
-	if err != nil {
-		return 0, err
-	}
-
-	return copy(p, m.Value), nil
-}
-
-// io.Writer that writes
-type writer struct {
-	stream grpc.ServerStream
-}
-
-func (w *writer) Write(p []byte) (n int, err error) {
-	if len(p) == 0 {
-		return 0, nil
-	}
-
-	message := streamsv1.BytesMessage{
-		Type:  streamsv1.IOStream_STDOUT,
-		Value: p,
-	}
-
-	m, err := ptypes.MarshalAny(&message)
-	if err != nil {
-		return 0, err
-	}
-
-	return len(message.Value), w.stream.SendMsg(m)
-}

+ 45 - 0
server/proxy/streams/io.go

@@ -0,0 +1,45 @@
+package streams
+
+import (
+	"github.com/golang/protobuf/ptypes"
+
+	streamsv1 "github.com/docker/api/protos/streams/v1"
+)
+
+// IO implements an io.ReadWriter that forwards everything to the stream
+type IO struct {
+	Stream *Stream
+}
+
+func (io *IO) Read(p []byte) (int, error) {
+	a, err := io.Stream.Recv()
+	if err != nil {
+		return 0, err
+	}
+
+	var m streamsv1.BytesMessage
+	err = ptypes.UnmarshalAny(a, &m)
+	if err != nil {
+		return 0, err
+	}
+
+	return copy(p, m.Value), nil
+}
+
+func (io *IO) Write(p []byte) (n int, err error) {
+	if len(p) == 0 {
+		return 0, nil
+	}
+
+	message := streamsv1.BytesMessage{
+		Type:  streamsv1.IOStream_STDOUT,
+		Value: p,
+	}
+
+	m, err := ptypes.MarshalAny(&message)
+	if err != nil {
+		return 0, err
+	}
+
+	return len(message.Value), io.Stream.SendMsg(m)
+}

+ 26 - 0
server/proxy/streams/logs.go

@@ -0,0 +1,26 @@
+package streams
+
+import (
+	"io"
+
+	"google.golang.org/grpc"
+
+	containersv1 "github.com/docker/api/protos/containers/v1"
+)
+
+// Log implements an io.Writer that proxies logs over a gRPC stream
+type Log struct {
+	Stream grpc.ServerStream
+}
+
+func newStreamWriter(stream grpc.ServerStream) io.Writer {
+	return &Log{
+		Stream: stream,
+	}
+}
+
+func (w *Log) Write(p []byte) (n int, err error) {
+	return len(p), w.Stream.SendMsg(&containersv1.LogsResponse{
+		Value: p,
+	})
+}

+ 1 - 1
server/proxy/logstream_test.go → server/proxy/streams/logs_test.go

@@ -1,4 +1,4 @@
-package proxy
+package streams
 
 import (
 	"context"

+ 31 - 0
server/proxy/streams/stream.go

@@ -0,0 +1,31 @@
+package streams
+
+import (
+	"sync"
+
+	streamsv1 "github.com/docker/api/protos/streams/v1"
+)
+
+// Stream is a bidirectional stream for container IO
+type Stream struct {
+	streamsv1.Streaming_NewStreamServer
+
+	errm    sync.Mutex
+	ErrChan chan<- error
+}
+
+// CloseWithError sends the result of an action to the errChan or nil
+// if no erros
+func (s *Stream) CloseWithError(err error) error {
+	s.errm.Lock()
+	defer s.errm.Unlock()
+
+	if s.ErrChan != nil {
+		if err != nil {
+			s.ErrChan <- err
+		}
+		close(s.ErrChan)
+		s.ErrChan = nil
+	}
+	return nil
+}

+ 9 - 5
server/proxy/streams_test.go → server/proxy/streams/stream_test.go

@@ -1,4 +1,4 @@
-package proxy
+package streams
 
 import (
 	"context"
@@ -54,7 +54,7 @@ func (bs *byteStream) RecvMsg(m interface{}) error {
 	return nil
 }
 
-func getReader(t *testing.T, in []byte, errResult error) reader {
+func getReader(t *testing.T, in []byte, errResult error) IO {
 	message := streamsv1.BytesMessage{
 		Type:  streamsv1.IOStream_STDOUT,
 		Value: in,
@@ -62,8 +62,8 @@ func getReader(t *testing.T, in []byte, errResult error) reader {
 	m, err := ptypes.MarshalAny(&message)
 	require.Nil(t, err)
 
-	return reader{
-		stream: &Stream{
+	return IO{
+		Stream: &Stream{
 			Streaming_NewStreamServer: &byteStream{
 				recvResult: m,
 				recvErr:    errResult,
@@ -109,7 +109,11 @@ func TestStreamWriter(t *testing.T) {
 	expected := getAny(t, in)
 
 	bs := byteStream{}
-	w := writer{stream: &bs}
+	w := IO{
+		Stream: &Stream{
+			Streaming_NewStreamServer: &bs,
+		},
+	}
 
 	n, err := w.Write(in)
 	assert.Nil(t, err)

+ 0 - 3
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,6 @@ 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)
 	if currentContext != "" {
 		ctx = apicontext.WithCurrentContext(ctx, currentContext)
 	}