point to correct backend when switching context

Signed-off-by: Lorena Rangel <lorena.rangel@docker.com>
This commit is contained in:
Lorena Rangel 2021-10-14 17:23:43 +02:00
Родитель 352ba59f25
Коммит 3ff346838a
5 изменённых файлов: 30 добавлений и 27 удалений

Просмотреть файл

@ -35,15 +35,14 @@ import (
func New(ctx context.Context) (*Client, error) {
currentContext := apicontext.Current()
s := store.Instance()
cc, err := s.Get(currentContext)
if err != nil {
return nil, err
}
service := backend.Current()
if service == nil {
return nil, api.ErrNotFound
service, err := backend.Get(cc.Type())
if err != nil {
return nil, err
}
client := NewClient(cc.Type(), service)

Просмотреть файл

@ -20,6 +20,9 @@ var current string
// WithCurrentContext sets the name of the current docker context
func WithCurrentContext(contextName string) {
if contextName == "" {
contextName = "default"
}
current = contextName
}

Просмотреть файл

@ -216,7 +216,12 @@ func main() {
}
ctx = context.WithValue(ctx, config.ContextTypeKey, ctype)
service, err := getBackend(ctype, configDir, opts)
initLocalFn := func() (backend.Service, error) {
return local.GetLocalBackend(configDir, opts)
}
backend.Register(store.DefaultContextType, store.DefaultContextType, initLocalFn, nil)
backend.Register(store.LocalContextType, store.LocalContextType, initLocalFn, nil)
service, err := backend.Get(ctype)
if err != nil {
fatal(err)
}
@ -259,18 +264,6 @@ func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) {
}
}
func getBackend(ctype string, configDir string, opts cliopts.GlobalOpts) (backend.Service, error) {
switch ctype {
case store.DefaultContextType, store.LocalContextType:
return local.GetLocalBackend(configDir, opts)
}
service, err := backend.Get(ctype)
if api.IsNotFoundError(err) {
return service, nil
}
return service, err
}
func handleError(ctx context.Context, err error, ctype string, currentContext string, cc *store.DockerContext, root *cobra.Command) {
// if user canceled request, simply exit without any error message
if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {

Просмотреть файл

@ -101,7 +101,6 @@ func getIncomingContext(ctx context.Context) (string, error) {
// needs: the context store and the api client
func configureContext(ctx context.Context, currentContext string, method string) (context.Context, error) {
configDir := config.Dir()
apicontext.WithCurrentContext(currentContext)
// The contexts service doesn't need the client
@ -111,6 +110,7 @@ func configureContext(ctx context.Context, currentContext string, method string)
return nil, err
}
ctx = context.WithValue(ctx, config.ContextTypeKey, c.ContextType())
ctx = proxy.WithClient(ctx, c)
}

Просмотреть файл

@ -130,6 +130,21 @@ func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Cont
}
func toGrpcContainer(c containers.Container) *containersv1.Container {
var hostConfig *containersv1.HostConfig
if c.HostConfig != nil {
hostConfig = &containersv1.HostConfig{
MemoryReservation: c.HostConfig.MemoryReservation,
MemoryLimit: c.HostConfig.MemoryLimit,
CpuReservation: uint64(c.HostConfig.CPUReservation),
CpuLimit: uint64(c.HostConfig.CPULimit),
RestartPolicy: c.HostConfig.RestartPolicy,
AutoRemove: c.HostConfig.AutoRemove,
}
}
var labels []string
if c.Config != nil {
labels = c.Config.Labels
}
return &containersv1.Container{
Id: c.ID,
Image: c.Image,
@ -140,16 +155,9 @@ func toGrpcContainer(c containers.Container) *containersv1.Container {
Platform: c.Platform,
PidsCurrent: c.PidsCurrent,
PidsLimit: c.PidsLimit,
Labels: c.Config.Labels,
Labels: labels,
Ports: portsToGrpc(c.Ports),
HostConfig: &containersv1.HostConfig{
MemoryReservation: c.HostConfig.MemoryReservation,
MemoryLimit: c.HostConfig.MemoryLimit,
CpuReservation: uint64(c.HostConfig.CPUReservation),
CpuLimit: uint64(c.HostConfig.CPULimit),
RestartPolicy: c.HostConfig.RestartPolicy,
AutoRemove: c.HostConfig.AutoRemove,
},
HostConfig: hostConfig,
Healthcheck: &containersv1.Healthcheck{
Disable: c.Healthcheck.Disable,
Test: c.Healthcheck.Test,