Skip to content

Commit

Permalink
refactor delve server logic, apply small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
runz0rd committed Aug 3, 2022
1 parent 93ae4ae commit 2033ec0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
7 changes: 3 additions & 4 deletions cmd/actions/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&flagPod, "pod", "p", "", "pod name (default most recent one)")
rootCmd.PersistentFlags().StringVarP(&flagContainer, "container", "c", "", "container name (default deployment name)")
patchCmd.Flags().StringVarP(&flagImage, "image", "i", "", "image to be used for patching (default deployment image)")
patchCmd.Flags().StringVarP(&flagRepo, "repo", "r", "", "repository to be used for pushing patched image (default none)")
patchCmd.Flags().StringArrayVarP(&flagMounts, "mount", "m", []string{}, "host path to be mounted (default none)")
patchCmd.Flags().BoolVar(&flagRollback, "rollback", false, "rollback deployment to a previous state")
delveCmd.Flags().StringVar(&flagSourcePath, "source", "", ".go file source path (default cwd)")
delveCmd.Flags().Var(flagArgs, "args", "go file args")
delveCmd.Flags().Var(flagListen, "listen", "delve host:port to listen on")
delveCmd.Flags().BoolVar(&flagVscode, "vscode", false, "launch a debug configuration in vscode")
delveCmd.Flags().BoolVar(&flagJSONLog, "json-log", false, "log as json")
delveCmd.Flags().BoolVar(&flagContinue, "continue", false, "start delve server execution without wiating for client connection")
rootCmd.AddCommand(versionCmd, patchCmd, shellCmd, delveCmd)
}

Expand All @@ -33,7 +33,6 @@ var (
flagNamespace string
flagPod string
flagContainer string
flagRepo string
flagImage string
flagMounts []string
flagSourcePath string
Expand Down Expand Up @@ -80,7 +79,7 @@ var (
if err != nil {
return err
}
return grapple.Patch(flagRepo, flagImage, flagTag, flagContainer, mounts)
return grapple.Patch(flagImage, flagTag, flagContainer, mounts)
},
}
shellCmd = &cobra.Command{
Expand All @@ -96,7 +95,7 @@ var (
Short: "start a headless delve debug server for .go input on a patched deployment",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return grapple.Delve(flagPod, flagContainer, flagSourcePath, flagArgs.items, flagListen.Host, flagListen.Port, flagVscode)
return grapple.Delve(flagPod, flagContainer, flagSourcePath, flagArgs.items, flagListen.Host, flagListen.Port, flagVscode, flagContinue)
},
}
)
Expand Down
5 changes: 0 additions & 5 deletions delve/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func (kdc KubeDelveClient) ValidateState() error {
if state.Exited {
// Exited indicates whether the debugged process has exited.
return fmt.Errorf("delve debugged process has exited")
} else if !state.Running {
// Running is true if the process is running and no other information can be collected.
// theres a case when you are in a breakpoint on a zombie process
// dlv will not handle that gracefully
return fmt.Errorf("delve debugged process is not running (zombie/breakpoint)")
} else {
// were good
return nil
Expand Down
19 changes: 12 additions & 7 deletions delve/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewKubeDelveServer(l *logrus.Entry, namespace, host string, port int) *Kube
}

func (kds *KubeDelveServer) StartNoWait(ctx context.Context, pod, container string,
binDest string, binArgs []string) {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.buildCommand(binDest, binArgs))
binDest string, binArgs []string, doContinue bool) {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.getRunCmd(binDest, binArgs, doContinue))
cmd.PostStart(
func(p *os.Process) error {
kds.process = p
Expand All @@ -43,8 +43,8 @@ func (kds *KubeDelveServer) StartNoWait(ctx context.Context, pod, container stri
}

func (kds *KubeDelveServer) Start(ctx context.Context, pod, container string,
binDest string, binArgs []string) error {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.buildCommand(binDest, binArgs))
binDest string, binArgs []string, doContinue bool) error {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.getRunCmd(binDest, binArgs, doContinue))
// execute command to run dlv on container
out, err := cmd.PostStart(
func(p *os.Process) error {
Expand All @@ -54,11 +54,16 @@ func (kds *KubeDelveServer) Start(ctx context.Context, pod, container string,
return errors.WithMessage(err, out)
}

func (kds KubeDelveServer) buildCommand(binDest string, binArgs []string) []string {
// doContinue will start the execution without waiting for a client connection
func (kds KubeDelveServer) getRunCmd(binDest string, binArgs []string, doContinue bool) []string {
cmd := []string{
"dlv", "exec", binDest, "--headless",
fmt.Sprintf("--listen=:%v", kds.port), "--accept-multiclient", "--continue",
"dlv", "exec", binDest, "--headless", "--accept-multiclient",
fmt.Sprintf("--listen=:%v", kds.port),
}
if doContinue {
cmd = append(cmd, "--continue")
}
// cmd = append(cmd, "--log")
if len(binArgs) > 0 {
cmd = append(cmd, "--")
cmd = append(cmd, binArgs...)
Expand Down
2 changes: 1 addition & 1 deletion exec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *Cmd) run(cmd *goexec.Cmd) (string, error) {
c.stdoutWriters = append(c.stdoutWriters, combinedBuf)
c.stderrWriters = append(c.stderrWriters, combinedBuf)
if c.l != nil {
c.l.Tracef("executing %q", cmd.String())
c.l.Tracef("executing %q in %q", cmd.String(), cmd.Dir)
c.stdoutWriters = append(c.stdoutWriters, c.l.WriterLevel(logrus.TraceLevel))
c.stderrWriters = append(c.stderrWriters, c.l.WriterLevel(logrus.WarnLevel))
}
Expand Down
4 changes: 2 additions & 2 deletions exec/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ func NewGoCommand() *GoCmd {
return &GoCmd{*NewCommand("go")}
}

func (c GoCmd) Build(workDir, output string, inputs []string, flags ...string) *Cmd {
return c.Args("build", "-o", output).Cwd(workDir).Args(flags...).Args(inputs...)
func (c GoCmd) Build(output string, inputs []string, flags ...string) *Cmd {
return c.Args("build", "-o", output).Args(flags...).Args(inputs...)
}
5 changes: 3 additions & 2 deletions the-hook/deployment-patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ spec:
image: {{ .Image }}
command:
args:
livenessProbe:
readinessProbe:
livenessProbe: ~
readinessProbe: ~
startupProbe: ~
volumeMounts:
- name: patch-configmap
mountPath: {{ .ConfigMapMount }}
Expand Down

0 comments on commit 2033ec0

Please sign in to comment.