utils

package
v0.0.0-...-8680cc4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Second = time.Second
	Minute = Second * 60
	Hour   = Minute * 60
	Day    = Hour * 24
	Week   = Day * 7
	Month  = Day * 30
	Year   = Day * 365

	TimeLayout = time.RFC3339Nano

	// RFC3339NanoFixed is our own version of RFC339Nano because we want one
	// that pads the nano seconds part with zeros to ensure
	// the timestamps are aligned in the logs.
	RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
)

Common durations that is . There are some definitions for units of Day and larger .

Variables

This section is empty.

Functions

func CombineErrors

func CombineErrors(errs []error, formatErrMsg FormatErrMsgFunc) error

CombineErrors is a function which used by Inspect to merge multiple errors into one error.

func Contains

func Contains(input []interface{}, value interface{}) (bool, error)

Contains check if a interface in a interface slice.

func ConvertKVStrToMapWithNoErr

func ConvertKVStrToMapWithNoErr(values []string) map[string]string

ConvertKVStrToMapWithNoErr converts input strings and converts them all in a map, When there is invalid input, the dealing procedure ignores the error and log a warning message.

func ConvertKVStringsToMap

func ConvertKVStringsToMap(values []string) (map[string]string, error)

ConvertKVStringsToMap converts ["key=value"] into {"key":"value"}

func ConvertStrToKV

func ConvertStrToKV(input string) (string, string, error)

ConvertStrToKV converts an string into key and value string without returning an error. For example, for input "a=b", it should return "a", "b".

func DeDuplicate

func DeDuplicate(input []string) []string

DeDuplicate make a slice with no duplicated elements.

func ExtractIPAndPortFromAddresses

func ExtractIPAndPortFromAddresses(addresses []string) (string, string)

ExtractIPAndPortFromAddresses extract first valid ip and port from addresses.

func FormatSize

func FormatSize(size int64) string

FormatSize format image size to B/KB/MB/GB

func FormatTimeInterval

func FormatTimeInterval(sec, nano int64) (formattedTime string, err error)

FormatTimeInterval is used to show the time interval from input time to now.

func GetTLSCommonName

func GetTLSCommonName(ctx context.Context) string

GetTLSCommonName fetch common name from context.

func GetTLSIssuer

func GetTLSIssuer(ctx context.Context) string

GetTLSIssuer fetch issuer name from context.

func GetUnixTimestamp

func GetUnixTimestamp(value string, base time.Time) (string, error)

GetUnixTimestamp will parse the value into time and get the nano-timestamp in string.

NOTE: if the value is not relative time, GetUnixTimestamp will use RFC3339 format to parse the value.

func If

func If(cond bool, v1, v2 interface{}) interface{}

If implements ternary operator. if cond is true return v1, or return v2 instead.

func IsFileExist

func IsFileExist(file string) bool

IsFileExist checks if file is exits on host.

func IsMountpoint

func IsMountpoint(dir string) bool

IsMountpoint is used to check the directory is mountpoint or not.

func IsProcessAlive

func IsProcessAlive(pid int) bool

IsProcessAlive returns true if process with a given pid is running.

func KillProcess

func KillProcess(pid int)

KillProcess force-stops a process.

func MakeFSVolume

func MakeFSVolume(fscmd []string, devicePath string, timeout time.Duration) error

MakeFSVolume is used to make file system on device with format type and options.

func MatchLabelSelector

func MatchLabelSelector(selector, labels map[string]string) bool

MatchLabelSelector returns true if labels cover selector.

func Merge

func Merge(src, dest interface{}) error

Merge merge object from src to dest, dest object should be pointer, only accept struct type, notice: src will overwrite dest's data

func MergeMap

func MergeMap(m1 map[string]interface{}, m2 map[string]interface{}) (map[string]interface{}, error)

MergeMap merges the m2 into m1, if it has the same keys, m2 will overwrite m1.

func MountVolume

func MountVolume(mountCmd []string, devicePath, mountPath string, timeout time.Duration) error

MountVolume is used to mount device to directory with options.

func NewPidfile

func NewPidfile(path string) error

NewPidfile checks if pidfile exist, and saves daemon pid.

func ParseTimestamp

func ParseTimestamp(value string, defaultSec int64) (int64, int64, error)

ParseTimestamp returns seconds and nanoseconds.

  1. If the value is empty, it will return default second, the second arg.
  2. If the incoming nanosecond portion is longer or shorter than 9 digits, it will be converted into 9 digits nanoseconds.

func RandString

func RandString(n int, prefix, suffix string) string

RandString is a thread/goroutine safe solution to generate a random string of a fixed length

func ResolveHomeDir

func ResolveHomeDir(path string) (string, error)

ResolveHomeDir resolve a target path from home dir, home dir must not be a relative path, must not be a file, create directory if not exist, returns the target directory if directory is symlink.

func SetOOMScore

func SetOOMScore(pid, score int) error

SetOOMScore sets process's oom_score value The higher the value of oom_score of any process, the higher is its likelihood of getting killed by the OOM Killer in an out-of-memory situation.

func SetTLSCommonName

func SetTLSCommonName(ctx context.Context, cn string) context.Context

SetTLSCommonName set common name of tls to context.

func SetTLSIssuer

func SetTLSIssuer(ctx context.Context, issuer string) context.Context

SetTLSIssuer set issuer name of tls to context.

func StringDefault

func StringDefault(s string, val string) string

StringDefault return default value if s is empty, otherwise return s.

func StringInSlice

func StringInSlice(input []string, str string) bool

StringInSlice checks if a string in the slice.

func StringSliceDelete

func StringSliceDelete(in []string, del string) []string

StringSliceDelete deletes the `del` string in string slice.

func StringSliceEqual

func StringSliceEqual(s1, s2 []string) bool

StringSliceEqual compares two string slice, ignore the order. If all items in the two string slice are equal, this function will return true even though there may have duplicate elements in the slice, otherwise reture false.

func ToStringMap

func ToStringMap(in map[string]interface{}) map[string]string

ToStringMap changes the map[string]interface{} to map[string]string, If the interface is not string, it will be ignore.

func TruncateID

func TruncateID(id string) string

TruncateID is used to transfer image ID from digest to short ID.

Types

type FormatErrMsgFunc

type FormatErrMsgFunc func(idx int, err error) (string, error)

FormatErrMsgFunc is a function which used by CombineErrors to format error message

type TLSKey

type TLSKey string

TLSKey is the key related to tls.

const (
	// PouchTLSIssuer is the key of tls issue stored in context.
	PouchTLSIssuer TLSKey = "pouch.server.tls.issuer"
	// PouchTLSCommonName is the key of tls common name stored in context.
	PouchTLSCommonName TLSKey = "pouch.server.tls.cn"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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