debug

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeDebugPrefix = "node-debugger"
	PodDebugPrefix  = "pod-debugger"

	TypeDebugNode = "node"
	TypeDebugPod  = "pod"

	FSDebugID = "."
)
View Source
const DefaultSealerRegistryURL = "registry.cn-qingdao.aliyuncs.com/sealer-apps/"
View Source
const SealerDebugMotd = `` /* 1495-byte string literal not displayed */

Variables

View Source
var CleanCMD = &cobra.Command{
	Use:   "clean",
	Short: "Clean the debug container od pod",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		cleaner := NewDebugCleaner()
		cleaner.AdminKubeConfigPath = common.KubeAdminConf

		if err := cleaner.CompleteAndVerifyOptions(args); err != nil {
			return err
		}
		if err := cleaner.Run(); err != nil {
			return err
		}

		return nil
	},
}
View Source
var ShowImagesCMD = &cobra.Command{
	Use:   "show-images",
	Short: "List default images",
	Args:  cobra.NoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		manager := NewDebugImagesManager()
		manager.RegistryURL = DefaultSealerRegistryURL

		if err := manager.ShowDefaultImages(); err != nil {
			return err
		}
		return nil
	},
}

Functions

func ContainerNameToRef

func ContainerNameToRef(pod *corev1.Pod) map[string]*corev1.Container

ContainerNameToRef returns the container names in pod.

func GetContainerStatusByName

func GetContainerStatusByName(pod *corev1.Pod, containerName string) *corev1.ContainerStatus

GetContainerStatusByName returns the container status by the containerName.

func GetSize

func GetSize(fd uintptr) *remotecommand.TerminalSize

GetSize returns the current size of the terminal associated with fd.

func IsTerminal

func IsTerminal(i interface{}) bool

IsTerminal returns whether the passed object is a terminal or not

func NewDebugNodeCommand

func NewDebugNodeCommand(options *DebuggerOptions) *cobra.Command

func NewDebugPodCommand

func NewDebugPodCommand(options *DebuggerOptions) *cobra.Command

func SetKubernetesDefaults

func SetKubernetesDefaults(config *rest.Config) error

SetKubernetesDefaults sets default values on the provided client config for accessing the Kubernetes API or returns an error if any of the defaults are impossible or invalid.

func WaitForContainer

func WaitForContainer(ctx context.Context, client corev1client.PodsGetter, namespace, podName, containerName string) (*corev1.Pod, error)

WaitForContainer watches the given pod until the container is running or terminated.

Types

type CleanOptions

type CleanOptions struct {
	PodName       string
	Namespace     string
	ContainerName string
}

CleanOptions holds the the options for an invocation of debug clean.

func NewDebugCleanOptions

func NewDebugCleanOptions() *CleanOptions

type Cleaner

type Cleaner struct {
	*CleanOptions

	AdminKubeConfigPath string

	genericclioptions.IOStreams
	// contains filtered or unexported fields
}

Cleaner cleans the debug containers and pods.

func NewDebugCleaner

func NewDebugCleaner() *Cleaner

func (*Cleaner) CompleteAndVerifyOptions

func (cleaner *Cleaner) CompleteAndVerifyOptions(args []string) error

CompleteAndVerifyOptions completes and verifies DebugCleanOptions.

func (*Cleaner) ExitEphemeralContainer

func (cleaner *Cleaner) ExitEphemeralContainer(config *restclient.Config) error

ExitEphemeralContainer exits the ephemeral containers and the ephemeral container's status will become terminated.

func (*Cleaner) RemovePod

func (cleaner *Cleaner) RemovePod(ctx context.Context, kubeClientCorev1 corev1client.CoreV1Interface) error

RemovePod removes the debug pods.

func (*Cleaner) Run

func (cleaner *Cleaner) Run() error

Run removes debug pods or exits debug containers.

type Connector

type Connector struct {
	NameSpace     string
	ContainerName string
	Command       []string
	Stdin         bool
	TTY           bool
	genericclioptions.IOStreams

	Motd string

	Pod    *corev1.Pod
	Config *restclient.Config
}

Connector holds the options to connect a running container.

func (*Connector) Connect

func (connector *Connector) Connect() error

Connect connects to a running container.

func (*Connector) ContainerToConnect

func (connector *Connector) ContainerToConnect() (*corev1.Container, error)

ContainerToConnect checks if there is a container to attach, and if exists returns the container object to attach.

func (*Connector) DoConnect

func (connector *Connector) DoConnect(method string, url *url.URL, terminalSizeQueue remotecommand.TerminalSizeQueue) error

DoConnect executes attach to a running container with url.

func (*Connector) GetDefaultAttachFunc

func (connector *Connector) GetDefaultAttachFunc(containerToAttach *corev1.Container, sizeQueue remotecommand.TerminalSizeQueue) func() error

GetDefaultAttachFunc returns the default attach function.

func (*Connector) GetDefaultExecFunc

func (connector *Connector) GetDefaultExecFunc(containerToAttach *corev1.Container, sizeQueue remotecommand.TerminalSizeQueue) func() error

GetDefaultExecFunc returns the default exec function.

func (*Connector) SetTTY

func (connector *Connector) SetTTY() TTY

SetTTY handles the stdin and tty with following:

  1. stdin false, tty false --- stdout
  2. stdin false, tty true --- stdout
  3. stdin true, tty false --- stdin、stdout
  4. stdin true, tty true --- stdin、stdout、tty --- t.Raw

then returns a TTY object based on connectOpts.

type Debugger

type Debugger struct {
	*DebuggerOptions
	Motd string

	genericclioptions.IOStreams
	// contains filtered or unexported fields
}

func NewDebugger

func NewDebugger(options *DebuggerOptions) *Debugger

func (*Debugger) CompleteAndVerifyOptions

func (debugger *Debugger) CompleteAndVerifyOptions(cmd *cobra.Command, args []string, imager ImagesManagement) error

CompleteAndVerifyOptions completes and verifies DebugOptions.

func (*Debugger) DebugNode

func (debugger *Debugger) DebugNode(ctx context.Context) (*corev1.Pod, error)

DebugNode can debug a node.

func (*Debugger) DebugPod

func (debugger *Debugger) DebugPod(ctx context.Context) (*corev1.Pod, error)

func (*Debugger) Run

func (debugger *Debugger) Run() (string, error)

Run generates a debug pod/node and attach to it according to command flag.

type DebuggerOptions

type DebuggerOptions struct {
	Type       string // debug pod or node
	TargetName string // pod/node name to be debugged

	Image       string // debug container/pod image name
	Env         []corev1.EnvVar
	Interactive bool     // -i
	TTY         bool     // -t
	Command     []string // after --
	CheckList   []string // check network、volume etc

	DebugContainerName string // debug container name
	Namespace          string // kubernetes namespace
	PullPolicy         string

	AdminKubeConfigPath string

	// Type is container
	TargetContainer string // target container to share the namespace
}

DebuggerOptions holds the options for an invocation of debug.

func NewDebugOptions

func NewDebugOptions() *DebuggerOptions

NewDebugOptions returns a DebugOptions initialized with default values.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler guarantees execution of notifications after a critical section (the function passed to a Run method), even in the presence of process termination. It guarantees exactly once invocation of the provided notify functions.

func Chain

func Chain(handler *Handler, notify ...func()) *Handler

Chain creates a new handler that invokes all notify functions when the critical section exits and then invokes the optional handler's notifications. This allows critical sections to be nested without losing exactly once invocations. Notify functions can invoke any cleanup needed but should not exit (which is the responsibility of the parent handler).

func New

func New(final func(os.Signal), notify ...func()) *Handler

New creates a new handler that guarantees all notify functions are run after the critical section exits (or is interrupted by the OS), then invokes the final handler. If no final handler is specified, the default final is `os.Exit(1)`. A handler can only be used for one critical section.

func (*Handler) Close

func (h *Handler) Close()

Close executes all the notification handlers if they have not yet been executed.

func (*Handler) Run

func (h *Handler) Run(fn func() error) error

Run ensures that any notifications are invoked after the provided fn exits (even if the process is interrupted by an OS termination signal). Notifications are only invoked once per Handler instance, so calling Run more than once will not behave as the user expects.

func (*Handler) Signal

func (h *Handler) Signal(s os.Signal)

Signal is called when an os.Signal is received, and guarantees that all notifications are executed, then the final handler is executed. This function should only be called once per Handler instance.

type ImagesManagement

type ImagesManagement interface {
	ShowDefaultImages() error
	GetDefaultImage() (string, error)
}

type ImagesManager

type ImagesManager struct {
	RegistryURL string

	DefaultImagesMap map[string]string // "RichToolsOnUbuntu": "debug:ubuntu"
	DefaultImageKey  string            // "RichToolsOnUbuntu"

	DefaultImage string // RegistryURL + DefaultImagesMap[DefaultImageName]
}

ImagesManager holds the default images information.

func NewDebugImagesManager

func NewDebugImagesManager() *ImagesManager

func (*ImagesManager) GetDefaultImage

func (manager *ImagesManager) GetDefaultImage() (string, error)

GetDefaultImage return the default image provide by debug.

func (*ImagesManager) ShowDefaultImages

func (manager *ImagesManager) ShowDefaultImages() error

ShowDefaultImages shows default images provided by debug.

type SafeFunc

type SafeFunc func() error

SafeFunc is a function to be invoked by TTY.

type TTY

type TTY struct {
	// In is a reader representing stdin. It is a required field.
	In io.Reader
	// Out is a writer representing stdout. It must be set to support terminal resizing. It is an
	// optional field.
	Out io.Writer
	// Raw is true if the terminal should be set raw.
	Raw bool
	// TryDev indicates the TTY should try to open /dev/tty if the provided input
	// is not a file descriptor.
	TryDev bool
	// Parent is an optional interrupt handler provided to this function - if provided
	// it will be invoked after the terminal state is restored. If it is not provided,
	// a signal received during the TTY will result in os.Exit(0) being invoked.
	Parent *Handler
	// contains filtered or unexported fields
}

TTY helps invoke a function and preserve the state of the terminal, even if the process is terminated during execution. It also provides support for terminal resizing for remote command attachment.

func (TTY) GetSize

func (t TTY) GetSize() *remotecommand.TerminalSize

GetSize returns the current size of the user's terminal. If it isn't a terminal, nil is returned.

func (TTY) IsTerminalIn

func (t TTY) IsTerminalIn() bool

IsTerminalIn returns true if t.In is a terminal. Does not check /dev/tty even if TryDev is set.

func (TTY) IsTerminalOut

func (t TTY) IsTerminalOut() bool

IsTerminalOut returns true if t.Out is a terminal. Does not check /dev/tty even if TryDev is set.

func (*TTY) MonitorSize

func (t *TTY) MonitorSize(initialSizes ...*remotecommand.TerminalSize) remotecommand.TerminalSizeQueue

MonitorSize monitors the terminal's size. It returns a TerminalSizeQueue primed with initialSizes, or nil if there's no TTY present.

func (TTY) Safe

func (t TTY) Safe(fn SafeFunc) error

Safe invokes the provided function and will attempt to ensure that when the function returns (or a termination signal is sent) that the terminal state is reset to the condition it was in prior to the function being invoked. If Raw is true the terminal will be put into raw mode prior to calling the function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL