remove duplicated func GetAddresses

refactor tunnel.go and controller.go, remove duplicated lines.

Signed-off-by: Xiao Deshi <xiaods@gmail.com>
This commit is contained in:
Xiao Deshi 2021-03-10 15:18:13 +08:00 committed by Brad Davidson
parent 93b18b343a
commit cfe7e0c734
3 changed files with 34 additions and 47 deletions

View File

@ -6,13 +6,13 @@ import (
"fmt" "fmt"
"net" "net"
"reflect" "reflect"
"strconv"
"sync" "sync"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/rancher/k3s/pkg/agent/proxy" "github.com/rancher/k3s/pkg/agent/proxy"
"github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/k3s/pkg/util"
"github.com/rancher/k3s/pkg/version" "github.com/rancher/k3s/pkg/version"
"github.com/rancher/remotedialer" "github.com/rancher/remotedialer"
"github.com/sirupsen/logrus" "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 { func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error {
restConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigK3sController) restConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigK3sController)
if err != nil { 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{}) endpoint, _ := client.CoreV1().Endpoints("default").Get(ctx, "kubernetes", metav1.GetOptions{})
if endpoint != nil { if endpoint != nil {
addresses := getAddresses(endpoint) addresses := util.GetAddresses(endpoint)
if len(addresses) > 0 { 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 continue watching
} }
newAddresses := getAddresses(endpoint) newAddresses := util.GetAddresses(endpoint)
if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) { if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) {
continue watching continue watching
} }

View File

@ -4,11 +4,10 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"net"
"strconv"
"github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/k3s/pkg/etcd" "github.com/rancher/k3s/pkg/etcd"
"github.com/rancher/k3s/pkg/util"
"github.com/rancher/k3s/pkg/version" "github.com/rancher/k3s/pkg/version"
controllerv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1" controllerv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
etcdv3 "go.etcd.io/etcd/clientv3" 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{} 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 return nil, err
} }
@ -65,23 +64,3 @@ func (h *handler) sync(key string, endpoint *v1.Endpoints) (*v1.Endpoints, error
return endpoint, nil 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
}

28
pkg/util/api.go Normal file
View File

@ -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
}