utils

package
v0.3.14 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExecutableNotFound = exec.ErrNotFound

ErrExecutableNotFound is returned if the executable is not found.

Functions

func AlphaReqToV1Req added in v0.3.0

func AlphaReqToV1Req(
	alphaRequest interface{ Marshal() ([]byte, error) },
	v1Request interface{ Unmarshal(_ []byte) error },
) error

AlphaRequestToV1Request converts cri.v1alpha2 requests to cri.v1 requests

func LimitWriter

func LimitWriter(w io.Writer, n int64) io.Writer

LimitWriter is a copy of the standard library ioutils.LimitReader, applied to the writer interface. LimitWriter returns a Writer that writes to w but stops with EOF after n bytes. The underlying implementation is a *LimitedWriter.

func ParseImageName

func ParseImageName(image string) (string, string, string, error)

ParseImageName parses a docker image string into three parts: repo, tag and digest. If both tag and digest are empty, a default image tag will be returned.

func V1ResponseToAlphaResponse added in v0.3.0

func V1ResponseToAlphaResponse(
	v1Response interface{ Marshal() ([]byte, error) },
	alphaResponse interface{ Unmarshal(_ []byte) error },
) error

V1ResponseToAlphaResponse converts cri.v1 responses to cri.v1alpha2 responses

func WriteCloserWrapper

func WriteCloserWrapper(w io.Writer) io.WriteCloser

WriteCloserWrapper returns a writeCloserWrapper.

Types

type Cache added in v0.3.5

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

Cache stores memoized function call results.

The zero Cache is ready for use.

func (*Cache) ClearByAge added in v0.3.5

func (c *Cache) ClearByAge(d time.Duration)

ClearByAge clears the cache entries which age is longer than d

func (*Cache) Delete added in v0.3.5

func (c *Cache) Delete(key string)

Delete removes the entry from the cache

func (*Cache) Memoize added in v0.3.5

func (c *Cache) Memoize(key string, minTTL time.Duration, fn func() (interface{}, error)) (interface{}, error)

Memoize calls and returns the results of the given function, or returns cached results from a previous call with the given key if the results are fresh enough. Only one call with a given key will be executed at a time. Function calls with a non-nil error are not cached.

type Cmd

type Cmd interface {
	// Run runs the command to the completion.
	Run() error
	// CombinedOutput runs the command and returns its combined standard output
	// and standard error. This follows the pattern of package os/exec.
	CombinedOutput() ([]byte, error)
	// Output runs the command and returns standard output, but not standard err
	Output() ([]byte, error)
	SetDir(dir string)
	SetStdin(in io.Reader)
	SetStdout(out io.Writer)
	SetStderr(out io.Writer)
	SetEnv(env []string)

	// StdoutPipe and StderrPipe for getting the process' Stdout and Stderr as
	// Readers
	StdoutPipe() (io.ReadCloser, error)
	StderrPipe() (io.ReadCloser, error)

	// Start and Wait are for running a process non-blocking
	Start() error
	Wait() error

	// Stops the command by sending SIGTERM. It is not guaranteed the
	// process will stop before this function returns. If the process is not
	// responding, an internal timer function will send a SIGKILL to force
	// terminate after 10 seconds.
	Stop()
}

Cmd is an interface that presents an API that is very similar to Cmd from os/exec. As more functionality is needed, this can grow. Since Cmd is a struct, we will have to replace fields with get/set method pairs.

type CodeExitError

type CodeExitError struct {
	Err  error
	Code int
}

CodeExitError is an implementation of ExitError consisting of an error object and an exit code (the upper bits of os.exec.ExitStatus).

func (CodeExitError) Error

func (e CodeExitError) Error() string

func (CodeExitError) ExitStatus

func (e CodeExitError) ExitStatus() int

ExitStatus is for checking the error code

func (CodeExitError) Exited

func (e CodeExitError) Exited() bool

Exited is to check if the process has finished

func (CodeExitError) String

func (e CodeExitError) String() string

type ExitError

type ExitError interface {
	String() string
	Error() string
	Exited() bool
	ExitStatus() int
}

ExitError is an interface that presents an API similar to os.ProcessState, which is what ExitError from os/exec is. This is designed to make testing a bit easier and probably loses some of the cross-platform properties of the underlying library.

type ExitErrorWrapper

type ExitErrorWrapper struct {
	*exec.ExitError
}

ExitErrorWrapper is an implementation of ExitError in terms of os/exec ExitError. Note: standard exec.ExitError is type *os.ProcessState, which already implements Exited().

func (ExitErrorWrapper) ExitStatus

func (eew ExitErrorWrapper) ExitStatus() int

ExitStatus is part of the ExitError interface.

type Interface

type Interface interface {
	// Command returns a Cmd instance which can be used to run a single command.
	// This follows the pattern of package os/exec.
	Command(cmd string, args ...string) Cmd

	// CommandContext returns a Cmd instance which can be used to run a single command.
	//
	// The provided context is used to kill the process if the context becomes done
	// before the command completes on its own. For example, a timeout can be set in
	// the context.
	CommandContext(ctx context.Context, cmd string, args ...string) Cmd

	// LookPath wraps os/exec.LookPath
	LookPath(file string) (string, error)
}

Interface is an interface that presents a subset of the os/exec API. Use this when you want to inject fakeable/mockable exec behavior.

func New

func New() Interface

New returns a new Interface which will os/exec to run commands.

type LimitedWriter

type LimitedWriter struct {
	W io.Writer // underlying writer
	N int64     // max bytes remaining
}

A LimitedWriter writes to W but limits the amount of data returned to just N bytes. Each call to Write updates N to reflect the new amount remaining. Write returns EOF when N <= 0 or when the underlying W returns EOF.

func (*LimitedWriter) Write

func (l *LimitedWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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