9
9
"encoding/binary"
10
10
"errors"
11
11
"fmt"
12
+ "math/big"
12
13
"net"
13
14
"strconv"
14
15
"strings"
@@ -31,6 +32,7 @@ import (
31
32
32
33
const (
33
34
wcpSystemResource = "vmware-system-shared-t1"
35
+ base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
34
36
)
35
37
36
38
var (
@@ -39,44 +41,53 @@ var (
39
41
40
42
var log = & logger .Log
41
43
44
+ func truncateLabelHash (data string ) string {
45
+ return Sha1 (data )[:common .HashLength ]
46
+ }
42
47
func NormalizeLabels (matchLabels * map [string ]string ) * map [string ]string {
43
48
newLabels := make (map [string ]string )
44
49
for k , v := range * matchLabels {
45
- newLabels [NormalizeLabelKey (k )] = NormalizeName (v )
50
+ newLabels [NormalizeLabelKey (k , truncateLabelHash )] = NormalizeName (v , truncateLabelHash )
46
51
}
47
52
return & newLabels
48
53
}
49
54
50
- func NormalizeLabelKey (key string ) string {
55
+ func NormalizeLabelKey (key string , shaFn func ( data string ) string ) string {
51
56
if len (key ) <= common .MaxTagScopeLength {
52
57
return key
53
58
}
54
59
splitted := strings .Split (key , "/" )
55
60
key = splitted [len (splitted )- 1 ]
56
- return normalizeNameByLimit (key , "" , common .MaxTagScopeLength )
61
+ return normalizeNameByLimit (key , "" , common .MaxTagScopeLength , shaFn )
57
62
}
58
63
59
- func NormalizeName (name string ) string {
60
- return normalizeNameByLimit (name , "" , common .MaxTagValueLength )
64
+ func NormalizeName (name string , shaFn func ( data string ) string ) string {
65
+ return normalizeNameByLimit (name , "" , common .MaxTagValueLength , shaFn )
61
66
}
62
67
63
- func normalizeNameByLimit (name string , suffix string , limit int ) string {
68
+ func normalizeNameByLimit (name string , suffix string , limit int , hashFn func ( data string ) string ) string {
64
69
newName := connectStrings (common .ConnectorUnderline , name , suffix )
65
70
if len (newName ) <= limit {
66
71
return newName
67
72
}
68
73
69
- var hashString string
74
+ hashedTarget := name
70
75
if len (suffix ) > 0 {
71
- hashString = Sha1 (suffix )
76
+ hashedTarget = suffix
77
+ }
78
+
79
+ var hashString string
80
+ if hashFn != nil {
81
+ hashString = hashFn (hashedTarget )
72
82
} else {
73
- hashString = Sha1 ( name )
83
+ hashString = Sha1WithBase62 ( hashedTarget )[: 6 ]
74
84
}
75
- nameLength := limit - common .HashLength - 1
85
+
86
+ nameLength := limit - len (hashString ) - 1
76
87
if len (name ) < nameLength {
77
88
nameLength = len (name )
78
89
}
79
- return strings .Join ([]string {name [:nameLength ], hashString [: common . HashLength ] }, common .ConnectorUnderline )
90
+ return strings .Join ([]string {name [:nameLength ], hashString }, common .ConnectorUnderline )
80
91
}
81
92
82
93
func NormalizeId (name string ) string {
@@ -94,10 +105,36 @@ func NormalizeId(name string) string {
94
105
}
95
106
96
107
func Sha1 (data string ) string {
108
+ sum := getSha1Bytes (data )
109
+ return fmt .Sprintf ("%x" , sum )
110
+ }
111
+
112
+ func getSha1Bytes (data string ) []byte {
97
113
h := sha1 .New () // #nosec G401: not used for security purposes
98
114
h .Write ([]byte (data ))
99
115
sum := h .Sum (nil )
100
- return fmt .Sprintf ("%x" , sum )
116
+ return sum
117
+ }
118
+
119
+ // Sha1WithBase62 uses the chars in `base62Chars` to present the hash result on the input data. We now use Sha1 as
120
+ // the hash algorithm.
121
+ func Sha1WithBase62 (data string ) string {
122
+ sum := getSha1Bytes (data )
123
+ value := new (big.Int ).SetBytes (sum [:])
124
+ base := big .NewInt (int64 (len (base62Chars )))
125
+ var result []byte
126
+ for value .Cmp (big .NewInt (0 )) > 0 {
127
+ mod := new (big.Int ).Mod (value , base )
128
+ result = append (result , base62Chars [mod .Int64 ()])
129
+ value .Div (value , base )
130
+ }
131
+
132
+ // Reverse the result because the encoding process generates characters in reverse order
133
+ for i , j := 0 , len (result )- 1 ; i < j ; i , j = i + 1 , j - 1 {
134
+ result [i ], result [j ] = result [j ], result [i ]
135
+ }
136
+
137
+ return string (result )
101
138
}
102
139
103
140
func RemoveDuplicateStr (strSlice []string ) []string {
@@ -343,7 +380,7 @@ func UpdateK8sResourceAnnotation(client client.Client, ctx context.Context, k8sO
343
380
// this function is used on the resources with VPC scenario, and the provided obj is the K8s CR which is
344
381
// used to generate the NSX resource.
345
382
func GenerateIDByObject (obj metav1.Object ) string {
346
- return normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), common .MaxIdLength )
383
+ return normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), common .MaxIdLength , nil )
347
384
}
348
385
349
386
// GenerateIDByObjectByLimit generate string id for NSX resource using the provided Object's name and uid,
@@ -352,13 +389,13 @@ func GenerateIDByObjectByLimit(obj metav1.Object, limit int) string {
352
389
if limit == 0 {
353
390
limit = common .MaxIdLength
354
391
}
355
- return normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), limit )
392
+ return normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), limit , nil )
356
393
}
357
394
358
395
func GenerateIDByObjectWithSuffix (obj metav1.Object , suffix string ) string {
359
396
limit := common .MaxIdLength
360
397
limit -= len (suffix ) + 1
361
- return connectStrings (common .ConnectorUnderline , normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), limit ), suffix )
398
+ return connectStrings (common .ConnectorUnderline , normalizeNameByLimit (obj .GetName (), string (obj .GetUID ()), limit , nil ), suffix )
362
399
}
363
400
364
401
// GenerateID generate id for NSX resource, some resources has complex index, so set it type to string
@@ -391,7 +428,7 @@ func GenerateTruncName(limit int, resName string, prefix, suffix, project, clust
391
428
}
392
429
oldName := generateDisplayName (common .ConnectorUnderline , resName , "" , "" , project , cluster )
393
430
if len (oldName ) > adjustedLimit {
394
- newName := normalizeNameByLimit (oldName , "" , adjustedLimit )
431
+ newName := normalizeNameByLimit (oldName , "" , adjustedLimit , nil )
395
432
return generateDisplayName (common .ConnectorUnderline , newName , prefix , suffix , "" , "" )
396
433
}
397
434
return generateDisplayName (common .ConnectorUnderline , resName , prefix , suffix , project , cluster )
0 commit comments