exec

package module
v0.0.0-...-b4411f2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 16 Imported by: 10

README

kube-exec

CircleCI Go Report Card Documentation

kube-exec is a library similar to os/exec that allows you to run commands in a Kubernetes pod, as if that command was executed locally.

It is inspired from go-dexec by ahmetb, which does the same thing, but for a Docker engine.

The interface of the package is similar to os/exec, and essentially this:

  • creates a new pod in Kubernetes based on a user-specified image
  • waits for the pod to be in Running state
  • attaches to the pod and allows you to stream data to the pod through stdin, and from the pod back to the program through stdout and stderr

How to use it

cfg := kube.Config{
	Kubeconfig: os.Getenv("KUBECONFIG"),
	Image:      "ubuntu",
	Name:       "kube-example",
	Namespace:  "default",
}

cmd := kube.Command(cfg, "/bin/sh", "-c", "sleep 2; echo Running from Kubernetes pod;")
cmd.Stdout = os.Stdout

err := cmd.Run()
if err != nil {
	log.Fatalf("error: %v", err)
}

Here's a list of full examples you can find in this repo:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Path string
	Args []string
	//Env  []string
	Dir string

	Cfg Config

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

Cmd represents the command to execute inside the pod

func Command

func Command(cfg Config, name string, arg ...string) *Cmd

Command returns the Cmd struct to execute the named program with the given arguments.

func (*Cmd) Run

func (cmd *Cmd) Run() error

Run starts the specified command and waits for it to complete.

func (*Cmd) Start

func (cmd *Cmd) Start() error

Start starts the specified command but does not wait for it to complete.

func (*Cmd) StdinPipe

func (cmd *Cmd) StdinPipe() (io.WriteCloser, error)

StdinPipe returns a pipe that will be connected to the command's standard input when the command starts.

Different than os/exec.StdinPipe, returned io.WriteCloser should be closed by user.

func (*Cmd) Wait

func (cmd *Cmd) Wait() error

Wait waits for the command to exit and waits for any copying to stdin or copying from stdout or stderr to complete.

The command must have been started by Start.

type Config

type Config struct {
	Kubeconfig string
	Namespace  string
	Name       string
	Image      string

	Secrets []Secret
}

Config contains all Kubernetes configuration

type Secret

type Secret struct {
	EnvVarName string
	SecretName string
	SecretKey  string
}

Secret represents a Kubernetes secret to pass into the pod as env variable

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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