-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathattach.go
More file actions
121 lines (102 loc) · 3.75 KB
/
attach.go
File metadata and controls
121 lines (102 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cmd
import (
"context"
"github.com/loft-sh/devspace/cmd/flags"
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
"github.com/loft-sh/devspace/pkg/devspace/hook"
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
"github.com/loft-sh/devspace/pkg/devspace/plugin"
"github.com/loft-sh/devspace/pkg/devspace/services/attach"
"github.com/loft-sh/devspace/pkg/devspace/services/targetselector"
"github.com/loft-sh/devspace/pkg/util/factory"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// AttachCmd is a struct that defines a command call for "enter"
type AttachCmd struct {
*flags.GlobalFlags
LabelSelector string
ImageSelector string
Container string
Pod string
Pick bool
}
// NewAttachCmd creates a new attach command
func NewAttachCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &AttachCmd{GlobalFlags: globalFlags}
attachCmd := &cobra.Command{
Use: "attach",
Short: "Attaches to a container",
Long: `
#######################################################
################# devspace attach #####################
#######################################################
Attaches to a running container
devspace attach
devspace attach --pick # Select pod to enter
devspace attach -c my-container
devspace attach -n my-namespace
#######################################################`,
RunE: func(cobraCmd *cobra.Command, args []string) error {
plugin.SetPluginCommand(cobraCmd, args)
return cmd.Run(f, cobraCmd, args)
},
}
attachCmd.Flags().StringVarP(&cmd.Container, "container", "c", "", "Container name within pod where to execute command")
attachCmd.Flags().StringVar(&cmd.Pod, "pod", "", "Pod to open a shell to")
attachCmd.Flags().StringVar(&cmd.ImageSelector, "image-selector", "", "The image to search a pod for (e.g. nginx, nginx:latest, ${runtime.images.app}, nginx:${runtime.images.app.tag})")
attachCmd.Flags().StringVarP(&cmd.LabelSelector, "label-selector", "l", "", "Comma separated key=value selector list (e.g. release=test)")
attachCmd.Flags().BoolVar(&cmd.Pick, "pick", true, "Select a pod")
return attachCmd
}
// Run executes the command logic
func (cmd *AttachCmd) Run(f factory.Factory, cobraCmd *cobra.Command, args []string) error {
// Set config root
log := f.GetLog()
configOptions := cmd.ToConfigOptions()
configLoader, err := f.NewConfigLoader(cmd.ConfigPath)
if err != nil {
return err
}
configExists, err := configLoader.SetDevSpaceRoot(log)
if err != nil {
return err
}
// Get kubectl client
client, err := f.NewKubeClientFromContext(cmd.KubeContext, cmd.Namespace)
if err != nil {
return errors.Wrap(err, "new kube client")
}
// Load generated config if possible
if configExists {
localCache, err := configLoader.LoadLocalCache()
if err != nil {
return err
}
// If the current kube context or namespace is different from old,
// show warnings and reset kube client if necessary
client, err = kubectl.CheckKubeContext(client, localCache, cmd.NoWarn, cmd.SwitchContext, false, log)
if err != nil {
return err
}
}
// create the context
ctx := devspacecontext.NewContext(context.Background(), nil, log).WithKubeClient(client)
// Execute plugin hook
err = hook.ExecuteHooks(ctx, nil, "attach")
if err != nil {
return err
}
// get image selector if specified
imageSelector, err := getImageSelector(ctx, configLoader, configOptions, cmd.ImageSelector)
if err != nil {
return err
}
// Build params
options := targetselector.NewOptionsFromFlags(cmd.Container, cmd.LabelSelector, imageSelector, cmd.Namespace, cmd.Pod).
WithPick(cmd.Pick).
WithWait(false).
WithQuestion("Which pod do you want to attach to?")
// Start attach
return attach.StartAttachFromCMD(ctx, targetselector.NewTargetSelector(options))
}