util

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: GPL-3.0 Imports: 28 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// EnvKubeConfig environment variable for kube config file
	EnvKubeConfig = "KUBECONFIG"

	// KubernetesToolkit name of this tool
	KubernetesToolkit = "kt"
	// ComponentConnect connect command
	ComponentConnect = "connect"
	// ComponentExchange exchange command
	ComponentExchange = "exchange"
	// ComponentMesh mesh command
	ComponentMesh = "mesh"
	// ComponentPreview preview command
	ComponentPreview = "preview"
	// ComponentForward forward command
	ComponentForward = "forward"

	// ImageKtShadow default shadow image
	ImageKtShadow = "registry.cn-hangzhou.aliyuncs.com/rdc-incubator/kt-connect-shadow"
	// ImageKtRouter default router image
	ImageKtRouter = "registry.cn-hangzhou.aliyuncs.com/rdc-incubator/kt-connect-router"
	// ImageKtNavigator default navigator image
	ImageKtNavigator = "registry.cn-hangzhou.aliyuncs.com/rdc-incubator/kt-connect-navigator"

	// ConnectModeShuttle sshuttle mode
	ConnectModeShuttle = "sshuttle"
	// ConnectModeTun2Socks tun2socks mode
	ConnectModeTun2Socks = "tun2socks"
	// ExchangeModeScale scale mode
	ExchangeModeScale = "scale"
	// ExchangeModeEphemeral ephemeral mode
	ExchangeModeEphemeral = "ephemeral"
	// ExchangeModeSelector selector mode
	ExchangeModeSelector = "selector"
	// MeshModeAuto auto mode
	MeshModeAuto = "auto"
	// MeshModeManual manual mode
	MeshModeManual = "manual"
	// DnsModeLocalDns local dns mode
	DnsModeLocalDns = "localDNS"
	// DnsModePodDns pod dns mode
	DnsModePodDns = "podDNS"
	// DnsModeHosts hosts dns mode
	DnsModeHosts = "hosts"
	// DnsOrderCluster proxy to cluster dns
	DnsOrderCluster = "cluster"
	// DnsOrderUpstream proxy to upstream dns
	DnsOrderUpstream = "upstream"

	// ControlBy label used for mark shadow pod
	ControlBy = "control-by"
	// KtTarget label used for service selecting shadow or route pod
	KtTarget = "kt-target"
	// KtRole label used for mark kt pod role
	KtRole = "kt-role"
	// KtConfig annotation used for clean up context
	KtConfig = "kt-config"
	// KtUser annotation used for record independent username
	KtUser = "kt-user"
	// KtSelector annotation used for record service origin selector
	KtSelector = "kt-selector"
	// KtRefCount annotation used for count of shared pod / service
	KtRefCount = "kt-ref-count"
	// KtLastHeartBeat annotation used for timestamp of last heart beat
	KtLastHeartBeat = "kt-last-heart-beat"
	// KtLock annotation used for avoid auto mesh conflict
	KtLock = "kt-lock"

	// PostfixRsaKey postfix of local private key name
	PostfixRsaKey = ".key"
	// RouterBin path to router executable
	RouterBin = "/usr/sbin/router"
	// SshBitSize ssh bit size
	SshBitSize = 2048
	// SshAuthKey auth key name
	SshAuthKey = "authorized"
	// SshAuthPrivateKey ssh private key
	SshAuthPrivateKey = "privateKey"
	// DefaultNamespace default namespace
	DefaultNamespace = "default"
	// KtExchangeContainer name of exchange ephemeral container
	KtExchangeContainer = "kt-exchange"
	// DefaultContainer default container name
	DefaultContainer = "standalone"
	// StuntmanServiceSuffix suffix of stuntman service name
	StuntmanServiceSuffix = "-kt-stuntman"
	// RouterPodSuffix suffix of router pod name
	RouterPodSuffix = "-kt-router"
	// ExchangePodInfix exchange pod name
	ExchangePodInfix = "-kt-exchange-"
	// MeshPodInfix mesh pod and mesh service name
	MeshPodInfix = "-kt-mesh-"
	// RectifierPodPrefix rectifier pod name
	RectifierPodPrefix = "kt-rectifier-"
	// RoleConnectShadow shadow role
	RoleConnectShadow = "shadow-connect"
	// RoleExchangeShadow shadow role
	RoleExchangeShadow = "shadow-exchange"
	// RoleMeshShadow shadow role
	RoleMeshShadow = "shadow-mesh"
	// RolePreviewShadow shadow role
	RolePreviewShadow = "shadow-preview"
	// RoleRouter router role
	RoleRouter = "router"
	// SortByName birdseye sort
	SortByName = "name"
	// SortByStatus birdseye sort
	SortByStatus = "status"
	// TunNameWin tun device name in windows
	TunNameWin = "KtConnectTunnel"
	// TunNameLinux tun device name in linux
	TunNameLinux = "kt0"
	// TunNameMac tun device name in MacOS
	TunNameMac = "utun"
	// AlternativeDnsPort alternative port for local dns
	AlternativeDnsPort = 10053

	// ResourceHeartBeatIntervalMinus interval of resource heart beat
	ResourceHeartBeatIntervalMinus = 2
	// PortForwardHeartBeatIntervalSec interval of port-forward heart beat
	PortForwardHeartBeatIntervalSec = 60
)
View Source
const (
	ResolvConf      = "/etc/resolv.conf"
	FieldNameserver = "nameserver"
	FieldDomain     = "domain"
	FieldSearch     = "search"

	HostsFilePath = "/etc/hosts"
	Eol           = "\n"
)
View Source
const IpAddrPattern = "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"

Variables

View Source
var (
	KtHome       = fmt.Sprintf("%s/.kt", UserHome)
	KtKeyDir     = fmt.Sprintf("%s/key", KtHome)
	KtPidDir     = fmt.Sprintf("%s/pid", KtHome)
	KtLockDir    = fmt.Sprintf("%s/lock", KtHome)
	KtProfileDir = fmt.Sprintf("%s/profile", KtHome)
	KtConfigFile = fmt.Sprintf("%s/config", KtHome)
)
View Source
var BackgroundLogger = io.Discard
View Source
var TimeDifference int64 = 0

TimeDifference seconds between remote and local time

View Source
var (
	UserHome = os.Getenv("HOME")
)

Functions

func Append added in v0.2.0

func Append(base string, inc string) string

Append Add segment to a comma separated string

func ArrayDelete added in v0.3.2

func ArrayDelete(arr []string, item string) []string

func ArrayEquals added in v0.3.2

func ArrayEquals(src, target []string) bool

func BackgroundRun

func BackgroundRun(cmd *exec.Cmd, name string, res chan error) error

BackgroundRun run cmd in background with context

func CanRun added in v0.2.4

func CanRun(cmd *exec.Cmd) bool

CanRun check whether a command can execute successful

func Capitalize added in v0.3.5

func Capitalize(word string) string

Capitalize convert dash separated string to capitalized string

func CleanBackgroundLogs added in v0.3.5

func CleanBackgroundLogs()

CleanBackgroundLogs delete expired log files

func CleanRsaKeys added in v0.1.0

func CleanRsaKeys()

CleanRsaKeys ...

func Contains added in v0.1.0

func Contains(container any, target any) bool

Contains check whether target exist in container, the type of container can be an array, slice or map

func CreateDirIfNotExist

func CreateDirIfNotExist(dir string) error

CreateDirIfNotExist create dir

func DashSeparated added in v0.3.5

func DashSeparated(word string) string

DashSeparated convert capitalized string to dash separated string

func ExtractErrorMessage added in v0.2.3

func ExtractErrorMessage(msg string) string

ExtractErrorMessage extract error from log

func ExtractHostIp added in v0.3.3

func ExtractHostIp(url string) string

ExtractHostIp Get host ip address from url

func FindBrokenLocalPort added in v0.3.0

func FindBrokenLocalPort(exposePorts string) string

FindBrokenLocalPort Check if all ports has process listening to Return empty string if all ports are listened, otherwise return the first broken port

func FindInvalidRemotePort added in v0.3.0

func FindInvalidRemotePort(exposePorts string, svcPorts map[int]string) string

FindInvalidRemotePort Check if all ports exist in provide service

func FixFileOwner added in v0.1.1

func FixFileOwner(path string) (err error)

FixFileOwner set owner to original user when run with sudo

func FormattedTime added in v0.3.4

func FormattedTime() string

FormattedTime get timestamp to print

func GetAdminUserName added in v0.3.0

func GetAdminUserName() string

func GetDaemonRunning added in v0.1.0

func GetDaemonRunning(componentName string) int

GetDaemonRunning fetch daemon pid if exist

func GetLocalUserName added in v0.2.0

func GetLocalUserName() string

GetLocalUserName get current username

func GetRandomTcpPort added in v0.3.1

func GetRandomTcpPort() int

GetRandomTcpPort get pod random ssh port

func GetTime added in v0.3.3

func GetTime() int64

GetTime get time with rectification

func GetTimestamp added in v0.1.1

func GetTimestamp() string

GetTimestamp get current timestamp

func IsCmd added in v0.1.1

func IsCmd() bool

IsCmd check running in windows cmd shell

func IsLinux added in v0.1.0

func IsLinux() bool

IsLinux check runtime is linux

func IsMacos added in v0.3.5

func IsMacos() bool

IsMacos check runtime is macos

func IsProcessExist added in v0.1.0

func IsProcessExist(pid int) bool

IsProcessExist check whether specified process still running

func IsRunAsAdmin added in v0.3.0

func IsRunAsAdmin() bool

func IsValidIp added in v0.3.6

func IsValidIp(ip string) bool

IsValidIp check if specified ip address valid

func IsWindows added in v0.0.12

func IsWindows() bool

IsWindows check runtime is windows

func MapContains added in v0.2.3

func MapContains(subset, fullset map[string]string) bool

func MapEquals added in v0.2.5

func MapEquals(src, target map[string]string) bool

func MapPut added in v0.2.3

func MapPut(m map[string]string, k, v string) map[string]string

func MergeMap added in v0.2.4

func MergeMap(m1, m2 map[string]string) map[string]string

func ParsePortMapping added in v0.2.3

func ParsePortMapping(exposePort string) (int, int, error)

ParsePortMapping parse <port> or <localPort>:<removePort> parameter

func ParseTimestamp added in v0.3.0

func ParseTimestamp(timestamp string) int64

ParseTimestamp parse timestamp to unix time

func PrepareLogger added in v0.3.4

func PrepareLogger(enable bool)

func PrivateKeyPath added in v0.0.12

func PrivateKeyPath(name string) string

PrivateKeyPath ...

func RandomPort added in v0.2.3

func RandomPort() int

RandomPort Generate random number [1024, 65535)

func RandomSeconds added in v0.2.5

func RandomSeconds(min, max int) time.Duration

RandomSeconds Generate random duration number in second

func RandomString

func RandomString(n int) string

RandomString Generate random string

func RemoveColor added in v0.2.3

func RemoveColor(msg string) string

RemoveColor remove shell color character in text

func RunAndWait added in v0.2.4

func RunAndWait(cmd *exec.Cmd) (string, string, error)

RunAndWait run cmd

func String2Map added in v0.0.12

func String2Map(str string) map[string]string

String2Map Convert parameter string to real map "k1=v1,k2=v2" -> {"k1":"v1","k2","v2"}

func UnCapitalize added in v0.3.5

func UnCapitalize(word string) string

UnCapitalize convert dash separated string to capitalized string TODO: 0.4 - remove this method, use DashSeparated() instead

func WritePidFile added in v0.0.12

func WritePidFile(componentName string, ch chan os.Signal) error

WritePidFile write pid to file

func WritePrivateKey added in v0.1.0

func WritePrivateKey(privateKeyPath string, data []byte) error

WritePrivateKey write ssh private key to privateKeyPath

Types

type FileWriter added in v0.3.4

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

func (FileWriter) Write added in v0.3.4

func (f FileWriter) Write(p []byte) (n int, err error)

Write log text

type InterpretableReader added in v0.3.1

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

func NewInterpretableReader added in v0.3.1

func NewInterpretableReader(r io.Reader) InterpretableReader

func (InterpretableReader) Cancel added in v0.3.1

func (r InterpretableReader) Cancel()

func (InterpretableReader) Read added in v0.3.1

func (r InterpretableReader) Read(p []byte) (n int, err error)

type SSHGenerator added in v0.1.0

type SSHGenerator struct {
	PrivateKey, PublicKey []byte
	PrivateKeyPath        string
}

SSHGenerator ssh key pair generator

func Generate added in v0.1.0

func Generate(privateKeyPath string) (*SSHGenerator, error)

Generate generate SSHGenerator

func NewSSHGenerator added in v0.1.0

func NewSSHGenerator(privateKey string, publicKey string, privateKeyPath string) *SSHGenerator

NewSSHGenerator create ssh generator

Jump to

Keyboard shortcuts

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