MCPcopy Create free account
hub / github.com/OpenAtomFoundation/SmartIDE / AddLabels

Function AddLabels

cli/pkg/k8s/helper.go:30–70  ·  view source on GitHub ↗

添加label

(kind interface{}, labels map[string]string)

Source from the content-addressed store, hash-verified

28
29// 添加label
30func AddLabels(kind interface{}, labels map[string]string) interface{} {
31 origin := reflect.ValueOf(kind)
32 if origin.Kind() == reflect.Ptr {
33 origin = origin.Elem()
34 }
35
36 // 返回的对象
37 result := reflect.New(origin.Type()).Elem() // 实例
38 result.Set(origin) // 赋值
39
40 // deployment的时候需要给templte 中的labels 进行赋值
41 if origin.FieldByName("Kind").String() == "Deployment" {
42 originLabels := origin.FieldByName("Spec").FieldByName("Template").FieldByName("Labels").
43 Interface().(map[string]string)
44 currentLabels := make(map[string]string)
45 if originLabels != nil {
46 copier.Copy(&currentLabels, originLabels)
47 }
48 for key, value := range labels {
49 relValue := filterSpecialCharacters4LabelValue(value)
50 currentLabels[key] = relValue
51 }
52 result.FieldByName("Spec").FieldByName("Template").FieldByName("Labels").
53 Set(reflect.ValueOf(currentLabels))
54 }
55
56 // 原类型中的labels赋值
57 objMeta := origin.FieldByName("ObjectMeta").Interface().(v1.ObjectMeta)
58 if objMeta.Labels != nil {
59 currentLabels := make(map[string]string)
60 copier.Copy(&currentLabels, objMeta.Labels)
61 objMeta.Labels = currentLabels
62 }
63 for key, value := range labels {
64 relValue := filterSpecialCharacters4LabelValue(value)
65 v1.SetMetaDataLabel(&objMeta, key, relValue)
66 }
67 result.FieldByName("ObjectMeta").Set(reflect.ValueOf(objMeta))
68
69 return result.Interface()
70}
71
72func filterSpecialCharacters4LabelValue(content string) string {
73 if len(content) >= 63 {

Callers

nothing calls this directly

Calls 3

CopyMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected