Skip to content

Commit

Permalink
Merge pull request #29 from VinceCui/master
Browse files Browse the repository at this point in the history
  • Loading branch information
fanux authored May 25, 2022
2 parents 7605c65 + ae1a50e commit d8f3f86
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/care.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
// and all subcommands, e.g.:
// careCmd.PersistentFlags().String("foo", "", "A help for foo")
careCmd.Flags().BoolVar(&care.LVS.RunOnce, "run-once", false, "is run once mode")
careCmd.Flags().StringVar(&care.LVS.VirtualServer, "vs", "", "virturl server like 10.54.0.2:6443")
careCmd.Flags().StringVar(&care.LVS.VirtualServer, "vs", "", "virtual server like 10.54.0.2:6443")
careCmd.Flags().StringSliceVar(&care.LVS.RealServer, "rs", []string{}, "real server like 192.168.0.2:6443")
careCmd.Flags().StringVar(&care.LVS.Logger, "logger", "INFO", "logger level: DEBG/INFO")
careCmd.Flags().BoolVar(&care.LVS.Clean, "clean", false, "before run clean ipvs rules")
Expand Down
3 changes: 1 addition & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type EndPoint struct {
}

func (ep EndPoint) String() string {
port := strconv.Itoa(int(ep.Port))
return ep.IP + ":" + port
return net.JoinHostPort(ep.IP, strconv.Itoa(int(ep.Port)))
}

//Lvser is
Expand Down
17 changes: 9 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import (
"net"
"net/http"
"strconv"
"strings"
)

//SplitServer is
func SplitServer(server string) (string, uint16) {
s := strings.Split(server, ":")
if len(s) != 2 {
glog.Warning("SplitServer error: len(s) is not two.")
glog.V(8).Infof("server %s", server)

ip, port, err := net.SplitHostPort(server)
if err != nil {
glog.Errorf("SplitServer error: %v.", err)
return "", 0
}
glog.V(8).Infof("SplitServer debug: IP: %s, Port: %s", s[0], s[1])
p, err := strconv.Atoi(s[1])
glog.V(8).Infof("SplitServer debug: IP: %s, Port: %s", ip, port)
p, err := strconv.Atoi(port)
if err != nil {
glog.Warningf("SplitServer error: %v", err)
return "", 0
}
return s[0], uint16(p)
return ip, uint16(p)
}

//IsHTTPAPIHealth is check http error
Expand All @@ -33,7 +34,7 @@ func IsHTTPAPIHealth(ip, port, path, schem string) bool {
url := fmt.Sprintf("%s://%s:%s%s", schem, ip, port, path)
resp, err := http.Get(url)
if err != nil {
glog.V(8).Infof("IsHTTPAPIHealth error: ", err)
glog.V(8).Infof("IsHTTPAPIHealth error: %v", err)
return false
}
defer resp.Body.Close()
Expand Down
49 changes: 49 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// alibaba-inc.com Inc.
// Copyright (c) 2004-2022 All Rights Reserved.
//
// @Author : huaiyou.cyz
// @Time : 2022/5/25 4:08 PM
// @File : utils_test.go
//

package utils

import "testing"

func TestSplitServer(t *testing.T) {
type args struct {
server string
}
tests := []struct {
name string
args args
want string
want1 uint16
}{
{
args: args{
server: "[1408:4003:10bb:6a01:83b9:6360:c66d:ed3e]:6443",
},
want: "1408:4003:10bb:6a01:83b9:6360:c66d:ed3e",
want1: 6443,
},
{
args: args{
server: "1.1.1.1:6443",
},
want: "1.1.1.1",
want1: 6443,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := SplitServer(tt.args.server)
if got != tt.want {
t.Errorf("SplitServer() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("SplitServer() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

0 comments on commit d8f3f86

Please sign in to comment.