Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bns debug start/stop #66

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions cmd/component_debug/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package component_debug

import (
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/k8s/bridge"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/debug_component/action"
"bunnyshell.com/cli/pkg/debug_component/action/down"
"github.com/spf13/cobra"
)

func init() {
options := config.GetOptions()
settings := config.GetSettings()

resourceLoader := bridge.NewResourceLoader()
downOptions := down.NewOptions(resourceLoader)

command := &cobra.Command{
Use: "stop",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: lib.OnlyStylish,

RunE: func(cmd *cobra.Command, args []string) error {
if err := resourceLoader.Load(settings.Profile); err != nil {
return err
}

downParameters, err := downOptions.ToParameters()
if err != nil {
return err
}

downAction := action.NewDown(*resourceLoader.Environment)

return downAction.Run(downParameters)
},
}

flags := command.Flags()

flags.AddFlag(options.Organization.GetFlag("organization"))
flags.AddFlag(options.Project.GetFlag("project"))
flags.AddFlag(options.Environment.GetFlag("environment"))
flags.AddFlag(options.ServiceComponent.GetFlag("component"))

downOptions.UpdateFlagSet(command, flags)

mainCmd.AddCommand(command)
}
21 changes: 21 additions & 0 deletions cmd/component_debug/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package component_debug

import (
"bunnyshell.com/cli/pkg/config"
"github.com/spf13/cobra"
)

var mainCmd = &cobra.Command{
Use: "debug",
Aliases: []string{"debug"},

Short: "Debug Component",
}

func init() {
config.MainManager.CommandWithAPI(mainCmd)
}

func GetMainCommand() *cobra.Command {
return mainCmd
}
19 changes: 19 additions & 0 deletions cmd/component_debug/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package component_debug

import (
"github.com/spf13/pflag"
)

type SSHOptions struct {
Shell string

NoTTY bool
NoBanner bool
}

func (o *SSHOptions) UpdateFlagSet(flags *pflag.FlagSet) {
flags.StringVar(&o.Shell, "shell", o.Shell, "Shell to use")

flags.BoolVar(&o.NoTTY, "no-tty", o.NoTTY, "Do not allocate a TTY")
flags.BoolVar(&o.NoBanner, "no-banner", o.NoBanner, "Do not show environment banner before ssh")
}
111 changes: 111 additions & 0 deletions cmd/component_debug/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package component_debug

import (
"fmt"

"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/k8s/bridge"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/debug_component/action"
upAction "bunnyshell.com/cli/pkg/debug_component/action/up"
"github.com/spf13/cobra"
)

func init() {
options := config.GetOptions()
settings := config.GetSettings()

sshOptions := SSHOptions{
Shell: "/bin/sh",
}

resourceLoader := bridge.NewResourceLoader()
upOptions := upAction.NewOptions(resourceLoader)

command := &cobra.Command{
Use: "start",

ValidArgsFunction: cobra.NoFileCompletions,

PreRunE: func(cmd *cobra.Command, args []string) error {
if err := upOptions.Validate(); err != nil {
return err
}

return lib.OnlyStylish(cmd, args)
},

RunE: func(cmd *cobra.Command, args []string) error {
upOptions.SetCommand(args)

if err := resourceLoader.Load(settings.Profile); err != nil {
return err
}

upParameters, err := upOptions.ToParameters()
if err != nil {
return err
}

upAction := action.NewUp(*resourceLoader.Environment)

if err = upAction.Run(upParameters); err != nil {
return err
}

selectedContainerName, err := upAction.GetSelectedContainerName()
if err != nil {
return err
}

if err = startSSH(*resourceLoader.Component.Id, selectedContainerName, sshOptions, cmd, args); err != nil {
return fmt.Errorf("debug SSH exited with: %s", err)
}

return upAction.Close()
},
}

flags := command.Flags()

flags.AddFlag(options.Organization.GetFlag("organization"))
flags.AddFlag(options.Project.GetFlag("project"))
flags.AddFlag(options.Environment.GetFlag("environment"))
flags.AddFlag(options.ServiceComponent.GetFlag("component"))

upOptions.UpdateFlagSet(command, flags)

sshOptions.UpdateFlagSet(flags)

mainCmd.AddCommand(command)
}

func startSSH(componentId string, containerName string, sshOptions SSHOptions, cmd *cobra.Command, args []string) error {
proxyArgs := []string{
"components", "ssh",
"--id", componentId,
}

proxyArgs = append(proxyArgs, "--container", containerName)

if sshOptions.Shell != "" {
proxyArgs = append(proxyArgs, "--shell", sshOptions.Shell)
}

if sshOptions.NoBanner {
proxyArgs = append(proxyArgs, "--no-banner")
}

if sshOptions.NoTTY {
proxyArgs = append(proxyArgs, "--no-tty")
}

root := cmd.Root()
root.SetArgs(append(proxyArgs, args...))

if err := root.Execute(); err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions cmd/utils/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bunnyshell.com/cli/cmd/git"
"bunnyshell.com/cli/cmd/component_debug"
"bunnyshell.com/cli/cmd/remote_development"
"github.com/spf13/cobra"
)
Expand All @@ -11,6 +12,7 @@ var mainCmd = &cobra.Command{}
func init() {
mainCmd.AddCommand(git.GetMainCommand())
mainCmd.AddCommand(remote_development.GetMainCommand())
mainCmd.AddCommand(component_debug.GetMainCommand())
}

func GetMainCommand() *cobra.Command {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.2
replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16

require (
bunnyshell.com/dev v0.6.0
bunnyshell.com/dev v0.7.0
bunnyshell.com/sdk v0.20.0
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/MakeNowJust/heredoc v1.0.0
Expand All @@ -19,7 +19,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/thediveo/enumflag/v2 v2.0.5
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
Expand Down Expand Up @@ -90,6 +89,7 @@ require (
go.starlark.net v0.0.0-20240520160348-046347dcd104 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bunnyshell.com/dev v0.6.0 h1:JywvLrzxYKgnxegPByh+ET7CwFme6JVeJpWzRuqQTWc=
bunnyshell.com/dev v0.6.0/go.mod h1:+Xk46UXX9AW0nHrFMdO/IwpUPfALrck1/qI+LIXsDmE=
bunnyshell.com/dev v0.7.0 h1:O66E0uTWSjx5ARI4Gyb19hmk9NX4uhFoyEP/nm/8kHY=
bunnyshell.com/dev v0.7.0/go.mod h1:+Xk46UXX9AW0nHrFMdO/IwpUPfALrck1/qI+LIXsDmE=
bunnyshell.com/sdk v0.20.0 h1:xcoDn0x1JqexMy5vYGqTBK4DGdY3juc+5DU7vb1yFeA=
bunnyshell.com/sdk v0.20.0/go.mod h1:RfgfUzZ4WHZGCkToUfu2/hoQS6XsQc8IdPTVAlpS138=
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/event/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func applyOptions(request sdk.ApiEventListRequest, options *ListOptions) sdk.Api
}

if options.Status != "" {
request.Status(options.Status)
request = request.Status(options.Status)
}

return request
Expand Down
46 changes: 46 additions & 0 deletions pkg/debug_component/action/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package action

import (
"errors"
"fmt"

"bunnyshell.com/cli/pkg/remote_development/workspace"
"bunnyshell.com/dev/pkg/debug"
"bunnyshell.com/sdk"
)

var ErrResourceKindNotSupported = errors.New("resource kind not supported")

type Action struct {
workspace *workspace.Workspace
}

func NewAction(
environment sdk.EnvironmentItem,
) *Action {
return &Action{
workspace: workspace.NewWorkspace(environment.GetId()),
}
}

func (action *Action) GetDebugCmp(resource sdk.ComponentResourceItem) (*debug.DebugComponent, error) {
kubeConfigFile, err := action.workspace.DownloadKubeConfig()
if err != nil {
return nil, err
}

debugCmp := debug.NewDebugComponent().
WithKubernetesClient(kubeConfigFile).
WithNamespaceName(resource.GetNamespace())

switch kind := resource.GetKind(); kind {
case "Deployment":
return debugCmp.WithDeploymentName(resource.GetName()), nil
case "StatefulSet":
return debugCmp.WithStatefulSetName(resource.GetName()), nil
case "DaemonSet":
return debugCmp.WithDaemonSetName(resource.GetName()), nil
default:
return nil, fmt.Errorf("%w: %s", ErrResourceKindNotSupported, kind)
}
}
30 changes: 30 additions & 0 deletions pkg/debug_component/action/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package action

import (
"bunnyshell.com/sdk"
)

type DownParameters struct {
Resource sdk.ComponentResourceItem
}

type Down struct {
Action
}

func NewDown(
environment sdk.EnvironmentItem,
) *Down {
return &Down{
Action: *NewAction(environment),
}
}

func (down *Down) Run(parameters *DownParameters) error {
debugCmp, err := down.Action.GetDebugCmp(parameters.Resource)
if err != nil {
return err
}

return debugCmp.Down()
}
13 changes: 13 additions & 0 deletions pkg/debug_component/action/down/down.cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package down

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func (down *Options) UpdateFlagSet(
command *cobra.Command,
flags *pflag.FlagSet,
) {
flags.StringVarP(&down.resourcePath, "resource", "s", down.resourcePath, "The cluster resource to use (namespace/kind/name format).")
}
Loading
Loading