utils

package
v0.1.56 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const (
	TypeNumber  = "number"
	TypeTime    = "time"
	TypeString  = "string"
	TypeBoolean = "boolean"
)

定义字符串的类型

Variables

This section is empty.

Functions

func AddOrUpdateAnnotations added in v0.1.16

func AddOrUpdateAnnotations(item *unstructured.Unstructured, newAnnotations map[string]string)

AddOrUpdateAnnotations 添加或更新 annotations

func BoolPtr

func BoolPtr(b bool) *bool

func CidrTotalIPs added in v0.1.13

func CidrTotalIPs(cidr string) (int, error)

func CompareMapContains added in v0.1.37

func CompareMapContains[T string | int](small, big map[string]T) bool

CompareMapContains compares two maps and returns true if the small map is contained in the big map small Map中的所有键值都存在于big map中,则返回true

func ConvertUnstructuredToYAML added in v0.0.4

func ConvertUnstructuredToYAML(obj *unstructured.Unstructured) (string, error)

ConvertUnstructuredToYAML 将 Unstructured 对象转换为 YAML 字符串

func DeepCopy

func DeepCopy[T any](src T) (T, error)

DeepCopy 函数接受任何类型的参数并返回其深复制的副本

func DetectType added in v0.1.0

func DetectType(value interface{}) (string, interface{})

DetectType 探测字符串的类型(数字、时间、字符串)

func FNV1 added in v0.1.3

func FNV1(data []byte) uint32

func FNV1_32 added in v0.1.2

func FNV1_32(data []byte) uint32

func FormatResource added in v0.1.10

func FormatResource(q resource.Quantity) string

FormatResource 格式化 resource.Quantity 为人类可读的格式 Example: 示例:内存格式化 q1 := resource.MustParse("8127096Ki") fmt.Println("Formatted memory:", utils.FormatResource(q1))

// 示例:内存格式化(大于 Gi) q2 := resource.MustParse("256Gi") fmt.Println("Formatted memory:", utils.FormatResource(q2))

// 示例:CPU 格式化 q3 := resource.MustParse("500m") // CPU 百分之一核 fmt.Println("Formatted CPU:", q3.String()) // CPU 不需要转换,直接原格式即可

func GetOrSetCache added in v0.1.18

func GetOrSetCache[T any](cache *ristretto.Cache[string, any], cacheKey string, ttl time.Duration, queryFunc func() (T, error)) (T, error)

func Int32Ptr

func Int32Ptr(i int32) *int32

func Int64Ptr

func Int64Ptr(i int64) *int64

func IsTextFile

func IsTextFile(ob []byte) (bool, error)

func MatchNodeSelectorRequirement added in v0.1.37

func MatchNodeSelectorRequirement(nodeLabels map[string]string, req v1.NodeSelectorRequirement) bool

MatchNodeSelectorRequirement checks if a map satisfies the NodeSelectorRequirement.

func NormalizeNewlines added in v0.0.25

func NormalizeNewlines(input string) string

func NormalizeToWindows added in v0.0.25

func NormalizeToWindows(input string) string

func ParseTime added in v0.1.0

func ParseTime(value string) (time.Time, error)

func RandNDigitInt

func RandNDigitInt(n int) int

RandNDigitInt generates a random number with n digits

func RandNLengthString

func RandNLengthString(n int) string

RandNLengthString generates a random string of specified length using the default charset

func RemoveManagedFields added in v0.0.4

func RemoveManagedFields(obj *unstructured.Unstructured)

RemoveManagedFields 删除 unstructured.Unstructured 对象中的 metadata.managedFields 字段

func SanitizeFileName

func SanitizeFileName(filename string) string

SanitizeFileName 去除文件名中的非法字符,并替换为下划线 '_'

func SortByCreationTime added in v0.0.4

func SortByCreationTime(items []unstructured.Unstructured) []unstructured.Unstructured

func StringListToSQLIn added in v0.1.33

func StringListToSQLIn(strList []string) string

StringListToSQLIn 将字符串列表转换为 SQL IN 子句中使用的格式,例如 ('x', 'xx') 例如 ['x', 'xx'] 转换为 ('x', 'xx')

func ToInt

func ToInt(s string) int

func ToInt64

func ToInt64(str string) int64

func ToInt64Slice

func ToInt64Slice(ids string) []int64

func ToIntDefault

func ToIntDefault(s string, i int) int

func ToIntSlice

func ToIntSlice(ids string) []int

ToIntSlice 将逗号分隔的数字字符串转换为 []int 切片

func ToJSON

func ToJSON(v interface{}) string

ToJSON 将任意结构体转换为格式化的 JSON 字符串

func ToUInt

func ToUInt(s string) uint

func TrimQuotes added in v0.1.0

func TrimQuotes(str string) string

func TruncateString

func TruncateString(s string, length int) string

func WaitUntil added in v0.0.22

func WaitUntil(f func() bool, interval time.Duration, timeout time.Duration) bool

WaitUntil 传入一个函数 f,如果 f 返回 true 则停止等待,否则持续检查直到超时 Example: // 定义一个检查函数,模拟每 10 秒满足一次条件

checkCondition := func() bool {
	// 例如条件为当前秒数是 10 的倍数
	return time.Now().Second()%10 == 0
}

// 每 2 秒检查一次,超时设定为 30 秒
interval := 2 * time.Second
timeout := 30 * time.Second

if waitUntil(checkCondition, interval, timeout) {
	fmt.Println("Check succeeded, main process exiting.")
} else {
	fmt.Println("Check failed due to timeout.")
}

Types

type LabelsManager

type LabelsManager struct {
	Labels map[string]string
}

LabelsManager 结构体,包含共享标签

func NewLabelsManager

func NewLabelsManager(labels map[string]string) *LabelsManager

NewLabelsManager 构造函数,初始化并返回一个 LabelsManager

func (*LabelsManager) AddCustomLabel

func (lm *LabelsManager) AddCustomLabel(meta *metav1.ObjectMeta, key, value string)

AddCustomLabel 动态添加用户指定的标签

func (*LabelsManager) AddLabels

func (lm *LabelsManager) AddLabels(meta *metav1.ObjectMeta)

AddLabels 给任意 Kubernetes 资源对象添加共享标签

Jump to

Keyboard shortcuts

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