Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 3ff3468

Browse files
committed
point to correct backend when switching context
Signed-off-by: Lorena Rangel <[email protected]>
1 parent 352ba59 commit 3ff3468

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

api/client/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ import (
3535
func New(ctx context.Context) (*Client, error) {
3636
currentContext := apicontext.Current()
3737
s := store.Instance()
38-
3938
cc, err := s.Get(currentContext)
4039
if err != nil {
4140
return nil, err
4241
}
4342

44-
service := backend.Current()
45-
if service == nil {
46-
return nil, api.ErrNotFound
43+
service, err := backend.Get(cc.Type())
44+
if err != nil {
45+
return nil, err
4746
}
4847

4948
client := NewClient(cc.Type(), service)

api/context/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var current string
2020

2121
// WithCurrentContext sets the name of the current docker context
2222
func WithCurrentContext(contextName string) {
23+
if contextName == "" {
24+
contextName = "default"
25+
}
2326
current = contextName
2427
}
2528

cli/main.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ func main() {
216216
}
217217
ctx = context.WithValue(ctx, config.ContextTypeKey, ctype)
218218

219-
service, err := getBackend(ctype, configDir, opts)
219+
initLocalFn := func() (backend.Service, error) {
220+
return local.GetLocalBackend(configDir, opts)
221+
}
222+
backend.Register(store.DefaultContextType, store.DefaultContextType, initLocalFn, nil)
223+
backend.Register(store.LocalContextType, store.LocalContextType, initLocalFn, nil)
224+
service, err := backend.Get(ctype)
220225
if err != nil {
221226
fatal(err)
222227
}
@@ -259,18 +264,6 @@ func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) {
259264
}
260265
}
261266

262-
func getBackend(ctype string, configDir string, opts cliopts.GlobalOpts) (backend.Service, error) {
263-
switch ctype {
264-
case store.DefaultContextType, store.LocalContextType:
265-
return local.GetLocalBackend(configDir, opts)
266-
}
267-
service, err := backend.Get(ctype)
268-
if api.IsNotFoundError(err) {
269-
return service, nil
270-
}
271-
return service, err
272-
}
273-
274267
func handleError(ctx context.Context, err error, ctype string, currentContext string, cc *store.DockerContext, root *cobra.Command) {
275268
// if user canceled request, simply exit without any error message
276269
if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {

cli/server/interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func getIncomingContext(ctx context.Context) (string, error) {
101101
// needs: the context store and the api client
102102
func configureContext(ctx context.Context, currentContext string, method string) (context.Context, error) {
103103
configDir := config.Dir()
104-
105104
apicontext.WithCurrentContext(currentContext)
106105

107106
// The contexts service doesn't need the client
@@ -111,6 +110,7 @@ func configureContext(ctx context.Context, currentContext string, method string)
111110
return nil, err
112111
}
113112

113+
ctx = context.WithValue(ctx, config.ContextTypeKey, c.ContextType())
114114
ctx = proxy.WithClient(ctx, c)
115115
}
116116

cli/server/proxy/containers.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Cont
130130
}
131131

132132
func toGrpcContainer(c containers.Container) *containersv1.Container {
133+
var hostConfig *containersv1.HostConfig
134+
if c.HostConfig != nil {
135+
hostConfig = &containersv1.HostConfig{
136+
MemoryReservation: c.HostConfig.MemoryReservation,
137+
MemoryLimit: c.HostConfig.MemoryLimit,
138+
CpuReservation: uint64(c.HostConfig.CPUReservation),
139+
CpuLimit: uint64(c.HostConfig.CPULimit),
140+
RestartPolicy: c.HostConfig.RestartPolicy,
141+
AutoRemove: c.HostConfig.AutoRemove,
142+
}
143+
}
144+
var labels []string
145+
if c.Config != nil {
146+
labels = c.Config.Labels
147+
}
133148
return &containersv1.Container{
134149
Id: c.ID,
135150
Image: c.Image,
@@ -140,16 +155,9 @@ func toGrpcContainer(c containers.Container) *containersv1.Container {
140155
Platform: c.Platform,
141156
PidsCurrent: c.PidsCurrent,
142157
PidsLimit: c.PidsLimit,
143-
Labels: c.Config.Labels,
158+
Labels: labels,
144159
Ports: portsToGrpc(c.Ports),
145-
HostConfig: &containersv1.HostConfig{
146-
MemoryReservation: c.HostConfig.MemoryReservation,
147-
MemoryLimit: c.HostConfig.MemoryLimit,
148-
CpuReservation: uint64(c.HostConfig.CPUReservation),
149-
CpuLimit: uint64(c.HostConfig.CPULimit),
150-
RestartPolicy: c.HostConfig.RestartPolicy,
151-
AutoRemove: c.HostConfig.AutoRemove,
152-
},
160+
HostConfig: hostConfig,
153161
Healthcheck: &containersv1.Healthcheck{
154162
Disable: c.Healthcheck.Disable,
155163
Test: c.Healthcheck.Test,

0 commit comments

Comments
 (0)