From cfe7e0c73483038d9e9242ae95da1a7c466f2891 Mon Sep 17 00:00:00 2001 From: Xiao Deshi Date: Wed, 10 Mar 2021 15:18:13 +0800 Subject: [PATCH] remove duplicated func GetAddresses refactor tunnel.go and controller.go, remove duplicated lines. Signed-off-by: Xiao Deshi --- pkg/agent/tunnel/tunnel.go | 28 ++++------------------------ pkg/apiaddresses/controller.go | 25 ++----------------------- pkg/util/api.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 47 deletions(-) create mode 100644 pkg/util/api.go diff --git a/pkg/agent/tunnel/tunnel.go b/pkg/agent/tunnel/tunnel.go index 28e740662d..489a21c653 100644 --- a/pkg/agent/tunnel/tunnel.go +++ b/pkg/agent/tunnel/tunnel.go @@ -6,13 +6,13 @@ import ( "fmt" "net" "reflect" - "strconv" "sync" "time" "github.com/gorilla/websocket" "github.com/rancher/k3s/pkg/agent/proxy" "github.com/rancher/k3s/pkg/daemons/config" + "github.com/rancher/k3s/pkg/util" "github.com/rancher/k3s/pkg/version" "github.com/rancher/remotedialer" "github.com/sirupsen/logrus" @@ -32,26 +32,6 @@ var ( } ) -func getAddresses(endpoint *v1.Endpoints) []string { - serverAddresses := []string{} - if endpoint == nil { - return serverAddresses - } - for _, subset := range endpoint.Subsets { - var port string - if len(subset.Ports) > 0 { - port = strconv.Itoa(int(subset.Ports[0].Port)) - } - if port == "" { - port = "443" - } - for _, address := range subset.Addresses { - serverAddresses = append(serverAddresses, net.JoinHostPort(address.IP, port)) - } - } - return serverAddresses -} - func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error { restConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigK3sController) if err != nil { @@ -75,9 +55,9 @@ func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error { endpoint, _ := client.CoreV1().Endpoints("default").Get(ctx, "kubernetes", metav1.GetOptions{}) if endpoint != nil { - addresses := getAddresses(endpoint) + addresses := util.GetAddresses(endpoint) if len(addresses) > 0 { - proxy.Update(getAddresses(endpoint)) + proxy.Update(util.GetAddresses(endpoint)) } } @@ -119,7 +99,7 @@ func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error { continue watching } - newAddresses := getAddresses(endpoint) + newAddresses := util.GetAddresses(endpoint) if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) { continue watching } diff --git a/pkg/apiaddresses/controller.go b/pkg/apiaddresses/controller.go index 122e067aa6..38c0b8e12a 100644 --- a/pkg/apiaddresses/controller.go +++ b/pkg/apiaddresses/controller.go @@ -4,11 +4,10 @@ import ( "bytes" "context" "encoding/json" - "net" - "strconv" "github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/etcd" + "github.com/rancher/k3s/pkg/util" "github.com/rancher/k3s/pkg/version" controllerv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1" etcdv3 "go.etcd.io/etcd/clientv3" @@ -54,7 +53,7 @@ func (h *handler) sync(key string, endpoint *v1.Endpoints) (*v1.Endpoints, error } w := &bytes.Buffer{} - if err := json.NewEncoder(w).Encode(getAddresses(endpoint)); err != nil { + if err := json.NewEncoder(w).Encode(util.GetAddresses(endpoint)); err != nil { return nil, err } @@ -65,23 +64,3 @@ func (h *handler) sync(key string, endpoint *v1.Endpoints) (*v1.Endpoints, error return endpoint, nil } - -func getAddresses(endpoint *v1.Endpoints) []string { - serverAddresses := []string{} - if endpoint == nil { - return serverAddresses - } - for _, subset := range endpoint.Subsets { - var port string - if len(subset.Ports) > 0 { - port = strconv.Itoa(int(subset.Ports[0].Port)) - } - if port == "" { - port = "443" - } - for _, address := range subset.Addresses { - serverAddresses = append(serverAddresses, net.JoinHostPort(address.IP, port)) - } - } - return serverAddresses -} diff --git a/pkg/util/api.go b/pkg/util/api.go new file mode 100644 index 0000000000..49d4a5d021 --- /dev/null +++ b/pkg/util/api.go @@ -0,0 +1,28 @@ +package util + +import ( + "net" + "strconv" + + v1 "k8s.io/api/core/v1" +) + +func GetAddresses(endpoint *v1.Endpoints) []string { + serverAddresses := []string{} + if endpoint == nil { + return serverAddresses + } + for _, subset := range endpoint.Subsets { + var port string + if len(subset.Ports) > 0 { + port = strconv.Itoa(int(subset.Ports[0].Port)) + } + if port == "" { + port = "443" + } + for _, address := range subset.Addresses { + serverAddresses = append(serverAddresses, net.JoinHostPort(address.IP, port)) + } + } + return serverAddresses +}