Skip to content

Commit

Permalink
fix: add default lenth for slice (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaZhou719 authored Jul 11, 2022
1 parent dc02b06 commit ce01d8e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions apply/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
}

masters := strings.Split(inMasters, ",")
masterHosts := make([]v2.Host, len(masters))
masterHosts := make([]v2.Host, 0, len(masters))
for index, master := range masters {
if index == 0 {
// only master0 should add two roles: master and master0
Expand All @@ -135,7 +135,7 @@ func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
// if inNodes is empty,Split will return a slice of length 1 whose only element is inNodes.
// so we need to filter the empty string to make sure the cluster node ip is valid.
nodes := strings.Split(inNodes, ",")
nodeHosts := make([]v2.Host, len(nodes))
nodeHosts := make([]v2.Host, 0, len(nodes))
for _, node := range nodes {
if node != "" {
nodeHosts = append(nodeHosts, v2.Host{
Expand All @@ -145,7 +145,7 @@ func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
}
}

result := make([]v2.Host, len(masters)+len(nodes))
result := make([]v2.Host, 0, len(masters)+len(nodes))
result = append(result, masterHosts...)
result = append(result, nodeHosts...)

Expand Down
3 changes: 1 addition & 2 deletions pkg/debug/clusterinfo/clusterinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ func GetDNSServiceAll(ctx context.Context, client corev1client.CoreV1Interface)
return domain, serviceClusterIP, endpointsIPs, nil
}

func removeDuplicatesAndEmpty(ss []string) []string {
func removeDuplicatesAndEmpty(ss []string) (res []string) {
if len(ss) == 0 {
return ss
}

res := make([]string, len(ss))
sMap := map[string]bool{}

for _, v := range ss {
Expand Down
2 changes: 1 addition & 1 deletion utils/net/iputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func DisassembleIPList(arg string) []net.IP {
res = append(res, i)
}

resIP := make([]net.IP, len(res))
resIP := make([]net.IP, 0, len(res))
for _, ip := range res {
resIP = append(resIP, net.ParseIP(ip))
}
Expand Down

0 comments on commit ce01d8e

Please sign in to comment.