forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utlis.go
79 lines (73 loc) · 1.33 KB
/
utlis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package k8s
import (
"strings"
)
func getHostnameAndIp(node []string) ([]string, []string) {
var resHost, resIp []string
if len(node) == 0 {
return node, node
}
for _, n := range node {
if !IsIpv4(n) {
resHost = append(resHost, n)
} else {
resIp = append(resIp, n)
}
}
return resHost, resIp
}
func IsIpv4(ip string) bool {
//matched, _ := regexp.MatchString("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}", ip)
arr := strings.Split(ip, ".")
if len(arr) != 4 {
return false
}
for _, v := range arr {
if v == "" {
return false
}
if len(v) > 1 && v[0] == '0' {
return false
}
num := 0
for _, c := range v {
if c >= '0' && c <= '9' {
num = num*10 + int(c-'0')
} else {
return false
}
}
if num > 255 {
return false
}
}
return true
}
// remove is remove b in a []string
func remove(a []string, b string) []string {
if len(a) == 0 {
return a
}
for i, v := range a {
if v == b {
a = append(a[:i], a[i+1:]...)
return remove(a, b)
}
}
return a
}
// removeRep is Deduplication []string
func removeRep(a []string) []string {
if len(a) == 0 {
return a
}
res := make([]string, 0, len(a))
tmp := map[string]struct{}{}
for _, v := range a {
if _, ok := tmp[v]; !ok {
tmp[v] = struct{}{}
res = append(res, v)
}
}
return res
}