util

package
v1.12.1-RC1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2020 License: Apache-2.0 Imports: 17 Imported by: 46

Documentation

Overview

CheckForIscsi is blocking call for checking status of istgt in cstor-istgt container.

Package util provides functions based on k8s.io/apimachinery/pkg/apis/meta/v1/unstructured They are copied here to make them exported.

TODO Check if it makes sense to import the entire unstructured package of k8s.io/apimachinery/pkg/apis/meta/v1/unstructured versus. copying

TODO Move to maya/pkg/unstructured/v1alpha1 as helpers.go

Index

Constants

View Source
const (
	IstgtConfPath        = "/usr/local/etc/istgt/istgt.conf"
	IstgtStatusCmd       = "STATUS"
	IstgtRefreshCmd      = "REFRESH"
	IstgtReplicaCmd      = "REPLICA"
	IstgtExecuteQuietCmd = "-q"
	ReplicaStatus        = "Replica status"
	WaitTimeForIscsi     = 3 * time.Second
	// IstgtResizeCmd holds the command to trigger resize
	IstgtResizeCmd = "RESIZE"
	IstgtDRFCmd    = "DRF"
)
View Source
const (
	IstgtUctlUnxpath = "/var/run/istgt_ctl_sock"
	EndOfLine        = "\r\n"
	IstgtHeader      = "iSCSI Target Controller version"
)

IstgtUctlUnxpath is the storage path for the UNIX domain socket from istgt

Variables

View Source
var (
	// ErrMAPIADDRNotSet is the new error to display this error if MAPI_ADDR is not set.
	ErrMAPIADDRNotSet = errors.New("MAPI_ADDR environment variable not set")
	// ErrInternalServerError is the new error to raise if an error occurs while rendering the service
	ErrInternalServerError = errors.New("Internal Server Error")
	// ErrServerUnavailable is the new error to raise if the server is not available
	ErrServerUnavailable = errors.New("Server Unavailable")
	// ErrServerNotReachable is the new error to raise if the server is not reachable
	ErrServerNotReachable = errors.New("Server Not Reachable")
	// ErrPageNotFound is the new error to raise if the page is not found
	ErrPageNotFound = errors.New("Page Not Found")
)

Functions

func CheckErr

func CheckErr(err error, handleErr func(string))

CheckErr to handle command errors

func CheckFalsy

func CheckFalsy(falsy string) bool

CheckFalsy checks for non-truthiness of the passed argument.

func CheckForIscsi

func CheckForIscsi(UnixSockVar UnixSock)

func CheckTruthy

func CheckTruthy(truth string) bool

CheckTruthy checks for truthiness of the passed argument.

func ContainKeys

func ContainKeys(mapOfObjs map[string]interface{}, keys []string) bool

ContainKeys returns true if all the provided keys are present in the provided map

func ContainsKey

func ContainsKey(mapOfObjs map[string]interface{}, key string) bool

ContainsKey returns true if the provided key is present in the provided map

func ContainsString

func ContainsString(stringarr []string, element string) bool

ContainsString returns true if the provided element is present in the provided array

func DeleteNestedField

func DeleteNestedField(obj map[string]interface{}, fields ...string)

DeleteNestedField deletes a nested field from the provided map

func Fatal

func Fatal(msg string)

Fatal prints the message (if provided) and then exits. If V(2) or greater, klog.Fatal is invoked for extended information.

func FormatKV

func FormatKV(in []string) string

FormatKV takes a set of strings and formats them into properly aligned k = v pairs using the columnize library.

func FormatList

func FormatList(in []string) string

FormatList takes a set of strings and formats them into properly aligned output, replacing any blank fields with a placeholder for awk-ability.

func FormatListWithSpaces

func FormatListWithSpaces(in []string) string

FormatListWithSpaces takes a set of strings and formats them into properly aligned output. It should be used sparingly since it doesn't replace empty values and hence not awk/sed friendly

func FormatTime

func FormatTime(t time.Time) string

FormatTime formats the time to string based on RFC822

func FormatTimeDifference

func FormatTimeDifference(first, second time.Time, d time.Duration) string

FormatTimeDifference takes two times and determines their duration difference truncating to a passed unit. E.g. formatTimeDifference(first=1m22s33ms, second=1m28s55ms, time.Second) -> 6s

func FormatUnixNanoTime

func FormatUnixNanoTime(nano int64) string

FormatUnixNanoTime is a helper for formatting time for output.

func GetMapOfStrings

func GetMapOfStrings(obj map[string]interface{}, field string) map[string]string

GetMapOfStrings gets the direct value from the passed obj & the field path The value returned should be expected of the form map[string]string

func GetNestedArray

func GetNestedArray(obj map[string]interface{}, fields ...string) []interface{}

GetNestedArray returns an nested array from the provided map

func GetNestedField

func GetNestedField(obj map[string]interface{}, fields ...string) interface{}

GetNestedField returns a nested field from the provided map

func GetNestedFieldInto

func GetNestedFieldInto(out interface{}, obj map[string]interface{}, fields ...string) error

GetNestedFieldInto converts a nested field to requested type from the provided map

func GetNestedInt64

func GetNestedInt64(obj map[string]interface{}, fields ...string) int64

GetNestedInt64 returns an nested int64 from the provided map

func GetNestedInt64Pointer

func GetNestedInt64Pointer(obj map[string]interface{}, fields ...string) *int64

GetNestedInt64Pointer returns a nested int64 pointer from the provided map

func GetNestedMap

func GetNestedMap(obj map[string]interface{}, fields ...string) map[string]string

GetNestedMap returns a nested map from the provided map

func GetNestedSlice

func GetNestedSlice(obj map[string]interface{}, fields ...string) []string

GetNestedSlice returns a nested slice from the provided map

func GetNestedString

func GetNestedString(obj map[string]interface{}, fields ...string) string

GetNestedString returns a nested string from the provided map

func IsChangeInLists

func IsChangeInLists(listA, listB []string) bool

IsChangeInLists returns true if there is any difference in listA and listB

func IsCurrentLessThanNewVersion

func IsCurrentLessThanNewVersion(old, new string) bool

IsCurrentLessThanNewVersion compares current and new version and returns true if currentversion is less `<` then new version (return true in case of equal version) TODO use version lib to properly handle versions https://github.com/hashicorp/go-version

func IsResponseEOD

func IsResponseEOD(resp []string, cmd string) bool

IsResponseEOD will detect if the data coming from UNIX pipe is completely received

func IsUniqueList

func IsUniqueList(list []string) bool

IsUniqueList returns true if values in list are not repeated else return false

func ListDiff

func ListDiff(listA []string, listB []string) []string

ListDiff returns list of string which are in listA but not in listB

func ListIntersection

func ListIntersection(listA []string, listB []string) []string

ListIntersection returns list of string which are in listA and listB

func MergeMapOfObjects

func MergeMapOfObjects(dest map[string]interface{}, src map[string]interface{}) bool

MergeMapOfObjects will merge the map from src to dest. It will override existing keys of the destination

func MergeMapOfStrings

func MergeMapOfStrings(dest map[string]string, src map[string]string) bool

MergeMapOfStrings will merge the map from src to dest

func MergeMaps

func MergeMaps(maps ...map[string]interface{}) map[string]interface{}

MergeMaps merges maps and returns the resulting map. map priority increases with order i.e. MergeMaps(m1,m2) will result in a map with overriding values from m2

func Reader

func Reader(r io.Reader, cmd string) []string

Reader reads the response from unix domain socket

func RemoveItemFromSlice

func RemoveItemFromSlice(slice []string, str string) []string

RemoveItemFromSlice removes the string passed as argument from the slice

func RemoveString

func RemoveString(slice []string, s string) (result []string)

RemoveString removes all occurrences of a string from slice and returns a new updated slice

func SetNestedField

func SetNestedField(obj map[string]interface{}, value interface{}, fields ...string)

SetNestedField sets a nested field into the provided map

func SetNestedMap

func SetNestedMap(obj map[string]interface{}, value map[string]string, fields ...string)

SetNestedMap sets a nested map from the provided map

func SetNestedSlice

func SetNestedSlice(obj map[string]interface{}, value []string, fields ...string)

SetNestedSlice sets a nested slice from the provided map

func StrToInt32

func StrToInt32(val string) *int32

StrToInt32 converts a string type to corresponding *int32 type

NOTE:

This swallows the error if any

func StringToInt32

func StringToInt32(val string) (*int32, error)

StringToInt32 converts a string type to corresponding *int32 type

func Writer

func Writer(w io.Writer, msg string) error

Writer writes a command to unix domain socket

Types

type FileOperator

type FileOperator interface {
	Write(filename string, data []byte, perm os.FileMode) error
	Updatefile(fileName, updateVal, searchString string, perm os.FileMode) error
	GetLineDetails(filename, searchString string) (int, string, error)
	UpdateOrAppendMultipleLines(fileName string, keyUpdateValue map[string]string, perm os.FileMode) error
}

FileOperator operates on files

type LineLimitReader

type LineLimitReader struct {
	io.ReadCloser
	// contains filtered or unexported fields
}

LineLimitReader wraps another reader and provides `tail -n` like behavior. LineLimitReader buffers up to the searchLimit and returns `-n` number of lines. After those lines have been returned, LineLimitReader streams the underlying ReadCloser

func NewLineLimitReader

func NewLineLimitReader(r io.ReadCloser, lines, searchLimit int, timeLimit time.Duration) *LineLimitReader

NewLineLimitReader takes the ReadCloser to wrap, the number of lines to find searching backwards in the first searchLimit bytes. timeLimit can optionally be specified by passing a non-zero duration. When set, the search for the last n lines is aborted if no data has been read in the duration. This can be used to flush what is had if no extra data is being received. When used, the underlying reader must not block forever and must periodically unblock even when no data has been read.

func (*LineLimitReader) Read

func (l *LineLimitReader) Read(p []byte) (n int, err error)

type RealFileOperator

type RealFileOperator struct{}

RealFileOperator is used for writing the actual files without mocking

func (RealFileOperator) GetLineDetails

func (r RealFileOperator) GetLineDetails(filename, searchString string) (int, string, error)

GetLineDetails return the line number and line content of matched string in file

func (RealFileOperator) UpdateOrAppendMultipleLines

func (r RealFileOperator) UpdateOrAppendMultipleLines(fileName string,
	keyUpdateValue map[string]string, perm os.FileMode) error

UpdateOrAppendMultipleLines will update or append multiple lines based on the given string

func (RealFileOperator) Updatefile

func (r RealFileOperator) Updatefile(fileName, updatedVal, searchString string, perm os.FileMode) error

Updatefile updates the line number with the given string

func (RealFileOperator) Write

func (r RealFileOperator) Write(filename string, data []byte, perm os.FileMode) error

the real file operator for the actual program,

type RealRunner

type RealRunner struct{}

RealRunner is the real runner for the program that actually execs the command.

func (RealRunner) RunCombinedOutput

func (r RealRunner) RunCombinedOutput(command string, args ...string) ([]byte, error)

RunCombinedOutput runs the command and returns its combined standard output and standard error.

func (RealRunner) RunCommandWithLog

func (r RealRunner) RunCommandWithLog(command string, args ...string) ([]byte, error)

RunCommandWithLog triggers command passed as arguments and it also does the following things before command completion 1. Logs the stdout of command to stdout(standard output) 2. Logs stderr of the command to standard error

func (RealRunner) RunCommandWithTimeoutContext

func (r RealRunner) RunCommandWithTimeoutContext(timeout time.Duration, command string, args ...string) ([]byte, error)

RunCommandWithTimeoutContext executes command provides and returns stdout error. If command does not returns within given timout interval command will be killed and return "Context time exceeded"

func (RealRunner) RunStdoutPipe

func (r RealRunner) RunStdoutPipe(command string, args ...string) ([]byte, error)

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

type RealUnixSock

type RealUnixSock struct{}

RealUnixSock is used for sending data through real unix domain sockets

func (RealUnixSock) SendCommand

func (r RealUnixSock) SendCommand(cmd string) ([]string, error)

SendCommand for the real unix sock for the actual program,

type Runner

type Runner interface {
	RunCombinedOutput(string, ...string) ([]byte, error)
	RunStdoutPipe(string, ...string) ([]byte, error)
	RunCommandWithTimeoutContext(time.Duration, string, ...string) ([]byte, error)
	RunCommandWithLog(string, ...string) ([]byte, error)
}

Runner interface implements various methods of running binaries which can be modified for unit testing.

type TestFileOperator

type TestFileOperator struct{}

TestFileOperator is used as a dummy FileOperator

func (TestFileOperator) GetLineDetails

func (r TestFileOperator) GetLineDetails(filename, searchString string) (int, string, error)

GetLineDetails is to mock operation for FileOperator interface

func (TestFileOperator) UpdateOrAppendMultipleLines

func (r TestFileOperator) UpdateOrAppendMultipleLines(
	fileName string,
	keyUpdateValue map[string]string,
	perm os.FileMode) error

UpdateOrAppendMultipleLines is to mock operation for FileOperator interface

func (TestFileOperator) Updatefile

func (r TestFileOperator) Updatefile(fileName, updateStorageVal, searchString string, perm os.FileMode) error

Updatefile is to mock Updatefile operation for FileOperator interface

func (TestFileOperator) Write

func (r TestFileOperator) Write(filename string, data []byte, perm os.FileMode) error

Write is to mock write operation for FileOperator interface

type TestRunner

type TestRunner struct{}

TestRunner is used as a dummy Runner

func (TestRunner) RunCombinedOutput

func (r TestRunner) RunCombinedOutput(command string, args ...string) ([]byte, error)

RunCombinedOutput is to mock Real runner exec.

func (TestRunner) RunCommandWithLog

func (r TestRunner) RunCommandWithLog(command string, args ...string) ([]byte, error)

RunCommandWithLog is to mock real runner exec with stdoutpipe.

func (TestRunner) RunCommandWithTimeoutContext

func (r TestRunner) RunCommandWithTimeoutContext(timeout time.Duration, command string, args ...string) ([]byte, error)

RunCommandWithTimeoutContext is to mock Real runner exec.

func (TestRunner) RunStdoutPipe

func (r TestRunner) RunStdoutPipe(command string, args ...string) ([]byte, error)

RunStdoutPipe is to mock real runner exec with stdoutpipe.

type TestUnixSock

type TestUnixSock struct{}

TestUnixSock is used as a dummy UnixSock

func (TestUnixSock) SendCommand

func (r TestUnixSock) SendCommand(cmd string) ([]string, error)

SendCommand for the real unix sock for the actual program,

type UnixSock

type UnixSock interface {
	SendCommand(cmd string) ([]string, error)
}

UnixSock operates on unix domain sockets

Directories

Path Synopsis
http

Jump to

Keyboard shortcuts

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