复制项目
This commit is contained in:
96
pkg/common/discovery/direct/direct_resolver.go
Normal file
96
pkg/common/discovery/direct/direct_resolver.go
Normal file
@@ -0,0 +1,96 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package direct
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"github.com/openimsdk/tools/log"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
const (
|
||||
slashSeparator = "/"
|
||||
// EndpointSepChar is the separator char in endpoints.
|
||||
EndpointSepChar = ','
|
||||
|
||||
subsetSize = 32
|
||||
scheme = "direct"
|
||||
)
|
||||
|
||||
type ResolverDirect struct {
|
||||
}
|
||||
|
||||
func NewResolverDirect() *ResolverDirect {
|
||||
return &ResolverDirect{}
|
||||
}
|
||||
|
||||
func (rd *ResolverDirect) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
|
||||
resolver.Resolver, error) {
|
||||
log.ZDebug(context.Background(), "Build", "target", target)
|
||||
endpoints := strings.FieldsFunc(GetEndpoints(target), func(r rune) bool {
|
||||
return r == EndpointSepChar
|
||||
})
|
||||
endpoints = subset(endpoints, subsetSize)
|
||||
addrs := make([]resolver.Address, 0, len(endpoints))
|
||||
|
||||
for _, val := range endpoints {
|
||||
addrs = append(addrs, resolver.Address{
|
||||
Addr: val,
|
||||
})
|
||||
}
|
||||
if err := cc.UpdateState(resolver.State{
|
||||
Addresses: addrs,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &nopResolver{cc: cc}, nil
|
||||
}
|
||||
func init() {
|
||||
resolver.Register(&ResolverDirect{})
|
||||
}
|
||||
func (rd *ResolverDirect) Scheme() string {
|
||||
return scheme // return your custom scheme name
|
||||
}
|
||||
|
||||
// GetEndpoints returns the endpoints from the given target.
|
||||
func GetEndpoints(target resolver.Target) string {
|
||||
return strings.Trim(target.URL.Path, slashSeparator)
|
||||
}
|
||||
func subset(set []string, sub int) []string {
|
||||
rand.Shuffle(len(set), func(i, j int) {
|
||||
set[i], set[j] = set[j], set[i]
|
||||
})
|
||||
if len(set) <= sub {
|
||||
return set
|
||||
}
|
||||
|
||||
return set[:sub]
|
||||
}
|
||||
|
||||
type nopResolver struct {
|
||||
cc resolver.ClientConn
|
||||
}
|
||||
|
||||
func (n nopResolver) ResolveNow(options resolver.ResolveNowOptions) {
|
||||
|
||||
}
|
||||
|
||||
func (n nopResolver) Close() {
|
||||
|
||||
}
|
||||
174
pkg/common/discovery/direct/directconn.go
Normal file
174
pkg/common/discovery/direct/directconn.go
Normal file
@@ -0,0 +1,174 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package direct
|
||||
|
||||
//import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
//
|
||||
// config2 "git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
|
||||
// "github.com/openimsdk/tools/errs"
|
||||
// "google.golang.org/grpc"
|
||||
// "google.golang.org/grpc/credentials/insecure"
|
||||
//)
|
||||
//
|
||||
//type ServiceAddresses map[string][]int
|
||||
//
|
||||
//func getServiceAddresses(rpcRegisterName *config2.RpcRegisterName,
|
||||
// rpcPort *config2.RpcPort, longConnSvrPort []int) ServiceAddresses {
|
||||
// return ServiceAddresses{
|
||||
// rpcRegisterName.OpenImUserName: rpcPort.OpenImUserPort,
|
||||
// rpcRegisterName.OpenImFriendName: rpcPort.OpenImFriendPort,
|
||||
// rpcRegisterName.OpenImMsgName: rpcPort.OpenImMessagePort,
|
||||
// rpcRegisterName.OpenImMessageGatewayName: longConnSvrPort,
|
||||
// rpcRegisterName.OpenImGroupName: rpcPort.OpenImGroupPort,
|
||||
// rpcRegisterName.OpenImAuthName: rpcPort.OpenImAuthPort,
|
||||
// rpcRegisterName.OpenImPushName: rpcPort.OpenImPushPort,
|
||||
// rpcRegisterName.OpenImConversationName: rpcPort.OpenImConversationPort,
|
||||
// rpcRegisterName.OpenImThirdName: rpcPort.OpenImThirdPort,
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//type ConnDirect struct {
|
||||
// additionalOpts []grpc.DialOption
|
||||
// currentServiceAddress string
|
||||
// conns map[string][]*grpc.ClientConn
|
||||
// resolverDirect *ResolverDirect
|
||||
// config *config2.GlobalConfig
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetClientLocalConns() map[string][]*grpc.ClientConn {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetUserIdHashGatewayHost(ctx context.Context, userId string) (string, error) {
|
||||
// return "", nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) UnRegister() error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) CreateRpcRootNodes(serviceNames []string) error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) RegisterConf2Registry(key string, conf []byte) error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetConfFromRegistry(key string) ([]byte, error) {
|
||||
// return nil, nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) Close() {
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func NewConnDirect(config *config2.GlobalConfig) (*ConnDirect, error) {
|
||||
// return &ConnDirect{
|
||||
// conns: make(map[string][]*grpc.ClientConn),
|
||||
// resolverDirect: NewResolverDirect(),
|
||||
// config: config,
|
||||
// }, nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetConns(ctx context.Context,
|
||||
// serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
||||
//
|
||||
// if conns, exists := cd.conns[serviceName]; exists {
|
||||
// return conns, nil
|
||||
// }
|
||||
// ports := getServiceAddresses(&cd.config.RpcRegisterName,
|
||||
// &cd.config.RpcPort, cd.config.LongConnSvr.OpenImMessageGatewayPort)[serviceName]
|
||||
// var connections []*grpc.ClientConn
|
||||
// for _, port := range ports {
|
||||
// conn, err := cd.dialServiceWithoutResolver(ctx, fmt.Sprintf(cd.config.Rpc.ListenIP+":%d", port), append(cd.additionalOpts, opts...)...)
|
||||
// if err != nil {
|
||||
// return nil, errs.Wrap(fmt.Errorf("connect to port %d failed,serviceName %s, IP %s", port, serviceName, cd.config.Rpc.ListenIP))
|
||||
// }
|
||||
// connections = append(connections, conn)
|
||||
// }
|
||||
//
|
||||
// if len(connections) == 0 {
|
||||
// return nil, errs.New("no connections found for service", "serviceName", serviceName).Wrap()
|
||||
// }
|
||||
// return connections, nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||
// // Get service addresses
|
||||
// addresses := getServiceAddresses(&cd.config.RpcRegisterName,
|
||||
// &cd.config.RpcPort, cd.config.LongConnSvr.OpenImMessageGatewayPort)
|
||||
// address, ok := addresses[serviceName]
|
||||
// if !ok {
|
||||
// return nil, errs.New("unknown service name", "serviceName", serviceName).Wrap()
|
||||
// }
|
||||
// var result string
|
||||
// for _, addr := range address {
|
||||
// if result != "" {
|
||||
// result = result + "," + fmt.Sprintf(cd.config.Rpc.ListenIP+":%d", addr)
|
||||
// } else {
|
||||
// result = fmt.Sprintf(cd.config.Rpc.ListenIP+":%d", addr)
|
||||
// }
|
||||
// }
|
||||
// // Try to dial a new connection
|
||||
// conn, err := cd.dialService(ctx, result, append(cd.additionalOpts, opts...)...)
|
||||
// if err != nil {
|
||||
// return nil, errs.WrapMsg(err, "address", result)
|
||||
// }
|
||||
//
|
||||
// // Store the new connection
|
||||
// cd.conns[serviceName] = append(cd.conns[serviceName], conn)
|
||||
// return conn, nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) GetSelfConnTarget() string {
|
||||
// return cd.currentServiceAddress
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) AddOption(opts ...grpc.DialOption) {
|
||||
// cd.additionalOpts = append(cd.additionalOpts, opts...)
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) CloseConn(conn *grpc.ClientConn) {
|
||||
// if conn != nil {
|
||||
// conn.Close()
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) dialService(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||
// options := append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
// conn, err := grpc.DialContext(ctx, cd.resolverDirect.Scheme()+":///"+address, options...)
|
||||
//
|
||||
// if err != nil {
|
||||
// return nil, errs.WrapMsg(err, "address", address)
|
||||
// }
|
||||
// return conn, nil
|
||||
//}
|
||||
//
|
||||
//func (cd *ConnDirect) dialServiceWithoutResolver(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||
// options := append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
// conn, err := grpc.DialContext(ctx, address, options...)
|
||||
//
|
||||
// if err != nil {
|
||||
// return nil, errs.Wrap(err)
|
||||
// }
|
||||
// return conn, nil
|
||||
//}
|
||||
15
pkg/common/discovery/direct/doc.go
Normal file
15
pkg/common/discovery/direct/doc.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package direct // import "git.imall.cloud/openim/open-im-server-deploy/pkg/common/discovery/direct"
|
||||
55
pkg/common/discovery/discoveryregister.go
Normal file
55
pkg/common/discovery/discoveryregister.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
|
||||
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/discovery/kubernetes"
|
||||
"github.com/openimsdk/tools/discovery"
|
||||
"github.com/openimsdk/tools/discovery/etcd"
|
||||
"github.com/openimsdk/tools/discovery/standalone"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/utils/runtimeenv"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// NewDiscoveryRegister creates a new service discovery and registry client based on the provided environment type.
|
||||
func NewDiscoveryRegister(confDiscovery *config.Discovery, watchNames []string) (discovery.SvcDiscoveryRegistry, error) {
|
||||
if config.Standalone() {
|
||||
return standalone.GetSvcDiscoveryRegistry(), nil
|
||||
}
|
||||
if runtimeenv.RuntimeEnvironment() == config.KUBERNETES {
|
||||
return kubernetes.NewKubernetesConnManager(confDiscovery.Kubernetes.Namespace, watchNames,
|
||||
grpc.WithDefaultCallOptions(
|
||||
grpc.MaxCallSendMsgSize(1024*1024*20),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
switch confDiscovery.Enable {
|
||||
case config.ETCD:
|
||||
return etcd.NewSvcDiscoveryRegistry(
|
||||
confDiscovery.Etcd.RootDirectory,
|
||||
confDiscovery.Etcd.Address,
|
||||
watchNames,
|
||||
etcd.WithDialTimeout(10*time.Second),
|
||||
etcd.WithMaxCallSendMsgSize(20*1024*1024),
|
||||
etcd.WithUsernameAndPassword(confDiscovery.Etcd.Username, confDiscovery.Etcd.Password))
|
||||
default:
|
||||
return nil, errs.New("unsupported discovery type", "type", confDiscovery.Enable).Wrap()
|
||||
}
|
||||
}
|
||||
60
pkg/common/discovery/discoveryregister_test.go
Normal file
60
pkg/common/discovery/discoveryregister_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func setupTestEnvironment() {
|
||||
os.Setenv("ZOOKEEPER_SCHEMA", "openim")
|
||||
os.Setenv("ZOOKEEPER_ADDRESS", "172.28.0.1")
|
||||
os.Setenv("ZOOKEEPER_PORT", "12181")
|
||||
os.Setenv("ZOOKEEPER_USERNAME", "")
|
||||
os.Setenv("ZOOKEEPER_PASSWORD", "")
|
||||
}
|
||||
|
||||
//func TestNewDiscoveryRegister(t *testing.T) {
|
||||
// setupTestEnvironment()
|
||||
// conf := config.NewGlobalConfig()
|
||||
// tests := []struct {
|
||||
// envType string
|
||||
// gatewayName string
|
||||
// expectedError bool
|
||||
// expectedResult bool
|
||||
// }{
|
||||
// {"zookeeper", "MessageGateway", false, true},
|
||||
// {"k8s", "MessageGateway", false, true},
|
||||
// {"direct", "MessageGateway", false, true},
|
||||
// {"invalid", "MessageGateway", true, false},
|
||||
// }
|
||||
//
|
||||
// for _, test := range tests {
|
||||
// conf.Envs.Discovery = test.envType
|
||||
// conf.RpcRegisterName.OpenImMessageGatewayName = test.gatewayName
|
||||
// client, err := NewDiscoveryRegister(conf)
|
||||
//
|
||||
// if test.expectedError {
|
||||
// assert.Error(t, err)
|
||||
// } else {
|
||||
// assert.NoError(t, err)
|
||||
// if test.expectedResult {
|
||||
// assert.Implements(t, (*discovery.SvcDiscoveryRegistry)(nil), client)
|
||||
// } else {
|
||||
// assert.Nil(t, client)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
15
pkg/common/discovery/doc.go
Normal file
15
pkg/common/discovery/doc.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package discovery // import "git.imall.cloud/openim/open-im-server-deploy/pkg/common/discovery"
|
||||
106
pkg/common/discovery/etcd/config_manager.go
Normal file
106
pkg/common/discovery/etcd/config_manager.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ShutDowns []func() error
|
||||
)
|
||||
|
||||
func RegisterShutDown(shutDown ...func() error) {
|
||||
ShutDowns = append(ShutDowns, shutDown...)
|
||||
}
|
||||
|
||||
type ConfigManager struct {
|
||||
client *clientv3.Client
|
||||
watchConfigNames []string
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func BuildKey(s string) string {
|
||||
return ConfigKeyPrefix + s
|
||||
}
|
||||
|
||||
func NewConfigManager(client *clientv3.Client, configNames []string) *ConfigManager {
|
||||
return &ConfigManager{
|
||||
client: client,
|
||||
watchConfigNames: datautil.Batch(func(s string) string { return BuildKey(s) }, append(configNames, RestartKey))}
|
||||
}
|
||||
|
||||
func (c *ConfigManager) Watch(ctx context.Context) {
|
||||
chans := make([]clientv3.WatchChan, 0, len(c.watchConfigNames))
|
||||
for _, name := range c.watchConfigNames {
|
||||
chans = append(chans, c.client.Watch(ctx, name, clientv3.WithPrefix()))
|
||||
}
|
||||
|
||||
doWatch := func(watchChan clientv3.WatchChan) {
|
||||
for watchResp := range watchChan {
|
||||
if watchResp.Err() != nil {
|
||||
log.ZError(ctx, "watch err", errs.Wrap(watchResp.Err()))
|
||||
continue
|
||||
}
|
||||
for _, event := range watchResp.Events {
|
||||
if event.IsModify() {
|
||||
if datautil.Contain(string(event.Kv.Key), c.watchConfigNames...) {
|
||||
c.lock.Lock()
|
||||
err := restartServer(ctx)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "restart server err", err)
|
||||
}
|
||||
c.lock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, ch := range chans {
|
||||
go doWatch(ch)
|
||||
}
|
||||
}
|
||||
|
||||
func restartServer(ctx context.Context) error {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return errs.New("get executable path fail").Wrap()
|
||||
}
|
||||
|
||||
args := os.Args
|
||||
env := os.Environ()
|
||||
|
||||
cmd := exec.Command(exePath, args[1:]...)
|
||||
cmd.Env = env
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
||||
}
|
||||
log.ZInfo(ctx, "shutdown server")
|
||||
for _, f := range ShutDowns {
|
||||
if err = f(); err != nil {
|
||||
log.ZError(ctx, "shutdown fail", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.ZInfo(ctx, "restart server")
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return errs.New("restart server fail").Wrap()
|
||||
}
|
||||
log.ZInfo(ctx, "cmd start over")
|
||||
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
9
pkg/common/discovery/etcd/const.go
Normal file
9
pkg/common/discovery/etcd/const.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package etcd
|
||||
|
||||
const (
|
||||
ConfigKeyPrefix = "/open-im/config/"
|
||||
RestartKey = "restart"
|
||||
EnableConfigCenterKey = "enable-config-center"
|
||||
Enable = "enable"
|
||||
Disable = "disable"
|
||||
)
|
||||
15
pkg/common/discovery/kubernetes/doc.go
Normal file
15
pkg/common/discovery/kubernetes/doc.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package kubernetes // import "git.imall.cloud/openim/open-im-server-deploy/pkg/common/discovery/kubernetes"
|
||||
640
pkg/common/discovery/kubernetes/kubernetes.go
Normal file
640
pkg/common/discovery/kubernetes/kubernetes.go
Normal file
@@ -0,0 +1,640 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/openimsdk/tools/discovery"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
)
|
||||
|
||||
// addrConn 存储连接和地址信息,用于连接复用
|
||||
type addrConn struct {
|
||||
conn *grpc.ClientConn
|
||||
addr string
|
||||
reused bool // 标记是否被复用
|
||||
}
|
||||
|
||||
type KubernetesConnManager struct {
|
||||
clientset *kubernetes.Clientset
|
||||
namespace string
|
||||
dialOptions []grpc.DialOption
|
||||
|
||||
rpcTargets map[string]string
|
||||
selfTarget string
|
||||
|
||||
// watchNames 只监听这些服务的 Endpoints 变化
|
||||
watchNames []string
|
||||
|
||||
mu sync.RWMutex
|
||||
connMap map[string][]*addrConn
|
||||
}
|
||||
|
||||
// NewKubernetesConnManager creates a new connection manager that uses Kubernetes services for service discovery.
|
||||
func NewKubernetesConnManager(namespace string, watchNames []string, options ...grpc.DialOption) (*KubernetesConnManager, error) {
|
||||
ctx := context.Background()
|
||||
log.ZInfo(ctx, "K8s Discovery: Initializing connection manager", "namespace", namespace, "watchNames", watchNames)
|
||||
|
||||
// 获取集群内配置
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to create in-cluster config", err)
|
||||
return nil, fmt.Errorf("failed to create in-cluster config: %v", err)
|
||||
}
|
||||
log.ZDebug(ctx, "K8s Discovery: Successfully created in-cluster config")
|
||||
|
||||
// 创建 K8s API 客户端
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to create clientset", err)
|
||||
return nil, fmt.Errorf("failed to create clientset: %v", err)
|
||||
}
|
||||
log.ZDebug(ctx, "K8s Discovery: Successfully created clientset")
|
||||
|
||||
// 初始化连接管理器
|
||||
k := &KubernetesConnManager{
|
||||
clientset: clientset,
|
||||
namespace: namespace,
|
||||
dialOptions: options,
|
||||
connMap: make(map[string][]*addrConn),
|
||||
rpcTargets: make(map[string]string),
|
||||
watchNames: watchNames,
|
||||
}
|
||||
|
||||
// 启动后台 goroutine 监听 Endpoints 变化
|
||||
log.ZInfo(ctx, "K8s Discovery: Starting Endpoints watcher")
|
||||
go k.watchEndpoints()
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Connection manager initialized successfully")
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// parseServiceName 解析服务名,去掉端口信息
|
||||
// 例如:user-rpc-service:http-10320 -> user-rpc-service
|
||||
func parseServiceName(serviceName string) string {
|
||||
if idx := strings.Index(serviceName, ":"); idx != -1 {
|
||||
return serviceName[:idx]
|
||||
}
|
||||
return serviceName
|
||||
}
|
||||
|
||||
// initializeConns 初始化指定服务的所有 gRPC 连接(支持连接复用)
|
||||
func (k *KubernetesConnManager) initializeConns(serviceName string) error {
|
||||
ctx := context.Background()
|
||||
log.ZInfo(ctx, "K8s Discovery: Starting to initialize connections", "serviceName", serviceName)
|
||||
|
||||
// 步骤 1: 获取 Service 的端口
|
||||
port, err := k.getServicePort(serviceName)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get service port", err, "serviceName", serviceName)
|
||||
return fmt.Errorf("failed to get service port: %w", err)
|
||||
}
|
||||
log.ZDebug(ctx, "K8s Discovery: Got service port", "serviceName", serviceName, "port", port)
|
||||
|
||||
// 步骤 2: 获取旧连接,建立地址到连接的映射(用于复用)
|
||||
k.mu.Lock()
|
||||
oldList := k.connMap[serviceName]
|
||||
addrMap := make(map[string]*addrConn, len(oldList))
|
||||
for _, ac := range oldList {
|
||||
addrMap[ac.addr] = ac
|
||||
ac.reused = false // 重置复用标记
|
||||
}
|
||||
k.mu.Unlock()
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Old connections snapshot", "serviceName", serviceName, "count", len(oldList))
|
||||
|
||||
// 步骤 3: 获取 Service 对应的 Endpoints
|
||||
endpoints, err := k.clientset.CoreV1().Endpoints(k.namespace).Get(
|
||||
ctx,
|
||||
serviceName,
|
||||
metav1.GetOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get endpoints", err, "serviceName", serviceName)
|
||||
return fmt.Errorf("failed to get endpoints for service %s: %w", serviceName, err)
|
||||
}
|
||||
|
||||
// 统计 Endpoints 数量
|
||||
var totalAddresses int
|
||||
for _, subset := range endpoints.Subsets {
|
||||
totalAddresses += len(subset.Addresses)
|
||||
}
|
||||
log.ZDebug(ctx, "K8s Discovery: Found endpoint addresses", "serviceName", serviceName, "count", totalAddresses)
|
||||
|
||||
// 步骤 4: 为每个 Pod IP 创建或复用 gRPC 连接
|
||||
var newList []*addrConn
|
||||
var reusedCount, createdCount int
|
||||
|
||||
for _, subset := range endpoints.Subsets {
|
||||
for _, address := range subset.Addresses {
|
||||
target := fmt.Sprintf("%s:%d", address.IP, port)
|
||||
|
||||
// 检查是否可以复用旧连接
|
||||
if ac, ok := addrMap[target]; ok {
|
||||
// 复用旧连接
|
||||
ac.reused = true
|
||||
newList = append(newList, ac)
|
||||
reusedCount++
|
||||
log.ZDebug(ctx, "K8s Discovery: Reusing existing connection", "serviceName", serviceName, "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
// 创建新连接
|
||||
log.ZDebug(ctx, "K8s Discovery: Creating new connection", "serviceName", serviceName, "target", target)
|
||||
conn, err := grpc.Dial(
|
||||
target,
|
||||
append(k.dialOptions,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: 10 * time.Second,
|
||||
Timeout: 3 * time.Second,
|
||||
PermitWithoutStream: true,
|
||||
}),
|
||||
)...,
|
||||
)
|
||||
if err != nil {
|
||||
log.ZWarn(ctx, "K8s Discovery: Failed to dial endpoint, skipping", err, "serviceName", serviceName, "target", target)
|
||||
// 跳过无法连接的端点,不终止整个初始化
|
||||
continue
|
||||
}
|
||||
|
||||
state := conn.GetState()
|
||||
log.ZDebug(ctx, "K8s Discovery: New connection created", "serviceName", serviceName, "target", target, "state", state.String())
|
||||
|
||||
newList = append(newList, &addrConn{conn: conn, addr: target, reused: false})
|
||||
createdCount++
|
||||
}
|
||||
}
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Connection initialization summary", "serviceName", serviceName,
|
||||
"total", len(newList), "reused", reusedCount, "created", createdCount)
|
||||
|
||||
// 步骤 5: 收集需要关闭的旧连接(未被复用的)
|
||||
var connsToClose []*addrConn
|
||||
for _, ac := range oldList {
|
||||
if !ac.reused {
|
||||
connsToClose = append(connsToClose, ac)
|
||||
}
|
||||
}
|
||||
|
||||
// 步骤 6: 更新连接映射
|
||||
k.mu.Lock()
|
||||
k.connMap[serviceName] = newList
|
||||
k.mu.Unlock()
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Connection map updated", "serviceName", serviceName,
|
||||
"oldCount", len(oldList), "newCount", len(newList), "toClose", len(connsToClose))
|
||||
|
||||
// 步骤 7: 延迟关闭未复用的旧连接
|
||||
if len(connsToClose) > 0 {
|
||||
log.ZInfo(ctx, "K8s Discovery: Scheduling delayed close for unused connections", "serviceName", serviceName, "count", len(connsToClose), "delaySeconds", 5)
|
||||
go func() {
|
||||
time.Sleep(5 * time.Second)
|
||||
log.ZDebug(ctx, "K8s Discovery: Closing unused old connections", "serviceName", serviceName, "count", len(connsToClose))
|
||||
closedCount := 0
|
||||
for _, ac := range connsToClose {
|
||||
if err := ac.conn.Close(); err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to close old connection", err, "serviceName", serviceName, "addr", ac.addr)
|
||||
} else {
|
||||
closedCount++
|
||||
}
|
||||
}
|
||||
log.ZInfo(ctx, "K8s Discovery: Closed unused connections", "serviceName", serviceName, "closed", closedCount, "total", len(connsToClose))
|
||||
}()
|
||||
}
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Connection initialization completed", "serviceName", serviceName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConns returns gRPC client connections for a given Kubernetes service name.
|
||||
func (k *KubernetesConnManager) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]grpc.ClientConnInterface, error) {
|
||||
// 解析服务名,去掉端口信息
|
||||
svcName := parseServiceName(serviceName)
|
||||
log.ZDebug(ctx, "K8s Discovery: GetConns called", "serviceName", serviceName, "parsedName", svcName)
|
||||
|
||||
// 步骤 1: 第一次检查缓存(读锁)
|
||||
k.mu.RLock()
|
||||
conns, exists := k.connMap[svcName]
|
||||
k.mu.RUnlock()
|
||||
|
||||
// 步骤 2: 如果缓存中有连接,检查健康状态
|
||||
if exists && len(conns) > 0 {
|
||||
log.ZDebug(ctx, "K8s Discovery: Found connections in cache, checking health", "serviceName", svcName, "count", len(conns))
|
||||
|
||||
// 检查连接健康状态
|
||||
validConns := k.filterValidConns(ctx, svcName, conns)
|
||||
|
||||
// 如果还有有效连接,更新缓存并返回
|
||||
if len(validConns) > 0 {
|
||||
if len(validConns) < len(conns) {
|
||||
log.ZWarn(ctx, "K8s Discovery: Removed invalid connections", nil, "serviceName", svcName, "removed", len(conns)-len(validConns), "remaining", len(validConns))
|
||||
k.mu.Lock()
|
||||
k.connMap[svcName] = validConns
|
||||
k.mu.Unlock()
|
||||
} else {
|
||||
log.ZDebug(ctx, "K8s Discovery: All connections are healthy", "serviceName", svcName, "count", len(validConns))
|
||||
}
|
||||
// 转换为接口类型
|
||||
result := make([]grpc.ClientConnInterface, len(validConns))
|
||||
for i, ac := range validConns {
|
||||
result[i] = ac.conn
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 如果所有连接都失效,清除缓存并重新初始化
|
||||
log.ZWarn(ctx, "K8s Discovery: All connections are invalid, reinitializing", nil, "serviceName", svcName)
|
||||
k.mu.Lock()
|
||||
delete(k.connMap, svcName)
|
||||
k.mu.Unlock()
|
||||
} else {
|
||||
log.ZDebug(ctx, "K8s Discovery: No connections in cache, initializing", "serviceName", svcName)
|
||||
}
|
||||
|
||||
// 步骤 3: 缓存中没有连接或所有连接都失效,重新初始化
|
||||
k.mu.Lock()
|
||||
conns, exists = k.connMap[svcName]
|
||||
if exists && len(conns) > 0 {
|
||||
log.ZDebug(ctx, "K8s Discovery: Connections were initialized by another goroutine", "serviceName", svcName)
|
||||
k.mu.Unlock()
|
||||
result := make([]grpc.ClientConnInterface, len(conns))
|
||||
for i, ac := range conns {
|
||||
result[i] = ac.conn
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
k.mu.Unlock()
|
||||
|
||||
// 初始化新连接
|
||||
log.ZDebug(ctx, "K8s Discovery: Initializing new connections", "serviceName", svcName)
|
||||
if err := k.initializeConns(svcName); err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to initialize connections", err, "serviceName", svcName)
|
||||
return nil, fmt.Errorf("failed to initialize connections for service %s: %w", svcName, err)
|
||||
}
|
||||
|
||||
// 返回新初始化的连接
|
||||
k.mu.RLock()
|
||||
conns = k.connMap[svcName]
|
||||
k.mu.RUnlock()
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Returning connections", "serviceName", svcName, "count", len(conns))
|
||||
result := make([]grpc.ClientConnInterface, len(conns))
|
||||
for i, ac := range conns {
|
||||
result[i] = ac.conn
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// filterValidConns 过滤出有效的连接
|
||||
func (k *KubernetesConnManager) filterValidConns(ctx context.Context, serviceName string, conns []*addrConn) []*addrConn {
|
||||
validConns := make([]*addrConn, 0, len(conns))
|
||||
invalidStates := make(map[string]int)
|
||||
|
||||
for _, ac := range conns {
|
||||
state := ac.conn.GetState()
|
||||
|
||||
// 只保留 Ready 和 Idle 状态的连接
|
||||
if state == connectivity.Ready || state == connectivity.Idle {
|
||||
validConns = append(validConns, ac)
|
||||
} else {
|
||||
invalidStates[state.String()]++
|
||||
log.ZDebug(ctx, "K8s Discovery: Connection is invalid, closing", "serviceName", serviceName, "addr", ac.addr, "state", state.String())
|
||||
if err := ac.conn.Close(); err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to close invalid connection", err, "serviceName", serviceName, "addr", ac.addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(invalidStates) > 0 {
|
||||
log.ZWarn(ctx, "K8s Discovery: Found invalid connections", nil, "serviceName", serviceName, "invalidStates", invalidStates)
|
||||
}
|
||||
|
||||
return validConns
|
||||
}
|
||||
|
||||
// GetConn returns a single gRPC client connection for a given Kubernetes service name.
|
||||
// 重要:GetConn 使用 DNS,避免连接被强制关闭
|
||||
func (k *KubernetesConnManager) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (grpc.ClientConnInterface, error) {
|
||||
// 解析服务名,去掉端口信息
|
||||
svcName := parseServiceName(serviceName)
|
||||
log.ZDebug(ctx, "K8s Discovery: GetConn called (using DNS)", "serviceName", serviceName, "parsedName", svcName)
|
||||
|
||||
var target string
|
||||
|
||||
// 检查是否有自定义目标
|
||||
if k.rpcTargets[svcName] == "" {
|
||||
// 获取 Service 端口
|
||||
svcPort, err := k.getServicePort(svcName)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get service port", err, "serviceName", svcName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 构建 K8s DNS 名称
|
||||
target = fmt.Sprintf("%s.%s.svc.cluster.local:%d", svcName, k.namespace, svcPort)
|
||||
log.ZDebug(ctx, "K8s Discovery: Using DNS target", "serviceName", svcName, "target", target)
|
||||
} else {
|
||||
target = k.rpcTargets[svcName]
|
||||
log.ZDebug(ctx, "K8s Discovery: Using custom target", "serviceName", svcName, "target", target)
|
||||
}
|
||||
|
||||
// 创建 gRPC 连接
|
||||
log.ZDebug(ctx, "K8s Discovery: Dialing DNS target", "serviceName", svcName, "target", target)
|
||||
conn, err := grpc.DialContext(
|
||||
ctx,
|
||||
target,
|
||||
append([]grpc.DialOption{
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithDefaultCallOptions(
|
||||
grpc.MaxCallRecvMsgSize(1024*1024*10),
|
||||
grpc.MaxCallSendMsgSize(1024*1024*20),
|
||||
),
|
||||
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: 10 * time.Second,
|
||||
Timeout: 3 * time.Second,
|
||||
PermitWithoutStream: true,
|
||||
}),
|
||||
}, k.dialOptions...)...,
|
||||
)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to dial DNS target", err, "serviceName", svcName, "target", target)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: GetConn completed successfully", "serviceName", svcName)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// IsSelfNode checks if the given connection is to the current node
|
||||
func (k *KubernetesConnManager) IsSelfNode(cc grpc.ClientConnInterface) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// watchEndpoints 监听 Endpoints 资源变化
|
||||
func (k *KubernetesConnManager) watchEndpoints() {
|
||||
ctx := context.Background()
|
||||
log.ZInfo(ctx, "K8s Discovery: Starting Endpoints watcher")
|
||||
|
||||
informerFactory := informers.NewSharedInformerFactoryWithOptions(k.clientset, time.Minute*10,
|
||||
informers.WithNamespace(k.namespace))
|
||||
informer := informerFactory.Core().V1().Endpoints().Informer()
|
||||
log.ZDebug(ctx, "K8s Discovery: Endpoints Informer created")
|
||||
|
||||
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: func(obj interface{}) {
|
||||
k.handleEndpointChange(obj)
|
||||
},
|
||||
UpdateFunc: func(oldObj, newObj interface{}) {
|
||||
oldEndpoint := oldObj.(*v1.Endpoints)
|
||||
newEndpoint := newObj.(*v1.Endpoints)
|
||||
|
||||
if k.endpointsChanged(oldEndpoint, newEndpoint) {
|
||||
k.handleEndpointChange(newObj)
|
||||
}
|
||||
},
|
||||
DeleteFunc: func(obj interface{}) {
|
||||
k.handleEndpointChange(obj)
|
||||
},
|
||||
})
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Starting Informer factory")
|
||||
informerFactory.Start(ctx.Done())
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Waiting for Informer cache to sync")
|
||||
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to sync Informer cache", nil)
|
||||
return
|
||||
}
|
||||
log.ZInfo(ctx, "K8s Discovery: Informer cache synced successfully")
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Endpoints watcher is running")
|
||||
<-ctx.Done()
|
||||
log.ZInfo(ctx, "K8s Discovery: Endpoints watcher stopped")
|
||||
}
|
||||
|
||||
// endpointsChanged 检查 Endpoints 是否有实际变化
|
||||
func (k *KubernetesConnManager) endpointsChanged(old, new *v1.Endpoints) bool {
|
||||
oldAddresses := make(map[string]bool)
|
||||
for _, subset := range old.Subsets {
|
||||
for _, address := range subset.Addresses {
|
||||
oldAddresses[address.IP] = true
|
||||
}
|
||||
}
|
||||
|
||||
newAddresses := make(map[string]bool)
|
||||
for _, subset := range new.Subsets {
|
||||
for _, address := range subset.Addresses {
|
||||
newAddresses[address.IP] = true
|
||||
}
|
||||
}
|
||||
|
||||
if len(oldAddresses) != len(newAddresses) {
|
||||
return true
|
||||
}
|
||||
|
||||
for ip := range oldAddresses {
|
||||
if !newAddresses[ip] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// handleEndpointChange 处理 Endpoints 资源变化
|
||||
func (k *KubernetesConnManager) handleEndpointChange(obj interface{}) {
|
||||
ctx := context.Background()
|
||||
|
||||
endpoint, ok := obj.(*v1.Endpoints)
|
||||
if !ok {
|
||||
log.ZError(ctx, "K8s Discovery: Expected *v1.Endpoints", nil, "actualType", fmt.Sprintf("%T", obj))
|
||||
return
|
||||
}
|
||||
|
||||
serviceName := endpoint.Name
|
||||
|
||||
// 只处理 watchNames 中的服务
|
||||
if len(k.watchNames) > 0 && !datautil.Contain(serviceName, k.watchNames...) {
|
||||
log.ZDebug(ctx, "K8s Discovery: Ignoring Endpoints change (not in watchNames)", "serviceName", serviceName)
|
||||
return
|
||||
}
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Handling Endpoints change", "serviceName", serviceName)
|
||||
|
||||
var totalAddresses int
|
||||
for _, subset := range endpoint.Subsets {
|
||||
totalAddresses += len(subset.Addresses)
|
||||
}
|
||||
log.ZDebug(ctx, "K8s Discovery: Endpoint addresses count", "serviceName", serviceName, "count", totalAddresses)
|
||||
|
||||
if err := k.initializeConns(serviceName); err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to initialize connections", err, "serviceName", serviceName)
|
||||
} else {
|
||||
log.ZInfo(ctx, "K8s Discovery: Successfully updated connections", "serviceName", serviceName)
|
||||
}
|
||||
}
|
||||
|
||||
// getServicePort 获取 Service 的 RPC 端口
|
||||
func (k *KubernetesConnManager) getServicePort(serviceName string) (int32, error) {
|
||||
ctx := context.Background()
|
||||
log.ZDebug(ctx, "K8s Discovery: Getting service port", "serviceName", serviceName)
|
||||
|
||||
svc, err := k.clientset.CoreV1().Services(k.namespace).Get(
|
||||
ctx,
|
||||
serviceName,
|
||||
metav1.GetOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get service", err, "serviceName", serviceName, "namespace", k.namespace)
|
||||
return 0, fmt.Errorf("failed to get service %s: %w", serviceName, err)
|
||||
}
|
||||
|
||||
if len(svc.Spec.Ports) == 0 {
|
||||
log.ZError(ctx, "K8s Discovery: Service has no ports defined", nil, "serviceName", serviceName)
|
||||
return 0, fmt.Errorf("service %s has no ports defined", serviceName)
|
||||
}
|
||||
|
||||
var svcPort int32
|
||||
for _, port := range svc.Spec.Ports {
|
||||
if port.Port != 10001 {
|
||||
svcPort = port.Port
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if svcPort == 0 {
|
||||
log.ZError(ctx, "K8s Discovery: Service has no RPC port", nil, "serviceName", serviceName)
|
||||
return 0, fmt.Errorf("service %s has no RPC port (all ports are 10001)", serviceName)
|
||||
}
|
||||
|
||||
log.ZDebug(ctx, "K8s Discovery: Got service port", "serviceName", serviceName, "port", svcPort)
|
||||
return svcPort, nil
|
||||
}
|
||||
|
||||
// Close 关闭所有连接
|
||||
func (k *KubernetesConnManager) Close() {
|
||||
ctx := context.Background()
|
||||
log.ZInfo(ctx, "K8s Discovery: Closing all connections")
|
||||
|
||||
k.mu.Lock()
|
||||
defer k.mu.Unlock()
|
||||
|
||||
totalConns := 0
|
||||
for serviceName, conns := range k.connMap {
|
||||
log.ZDebug(ctx, "K8s Discovery: Closing connections for service", "serviceName", serviceName, "count", len(conns))
|
||||
for _, ac := range conns {
|
||||
if err := ac.conn.Close(); err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to close connection", err, "serviceName", serviceName, "addr", ac.addr)
|
||||
}
|
||||
}
|
||||
totalConns += len(conns)
|
||||
}
|
||||
|
||||
log.ZInfo(ctx, "K8s Discovery: Closed all connections", "totalCount", totalConns)
|
||||
k.connMap = make(map[string][]*addrConn)
|
||||
}
|
||||
|
||||
// GetSelfConnTarget returns the connection target for the current service.
|
||||
func (k *KubernetesConnManager) GetSelfConnTarget() string {
|
||||
ctx := context.Background()
|
||||
|
||||
if k.selfTarget == "" {
|
||||
hostName := os.Getenv("HOSTNAME")
|
||||
log.ZDebug(ctx, "K8s Discovery: Getting self connection target", "hostname", hostName)
|
||||
|
||||
pod, err := k.clientset.CoreV1().Pods(k.namespace).Get(ctx, hostName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get pod", err, "hostname", hostName)
|
||||
}
|
||||
|
||||
for pod.Status.PodIP == "" {
|
||||
log.ZDebug(ctx, "K8s Discovery: Waiting for pod IP to be assigned", "hostname", hostName)
|
||||
pod, err = k.clientset.CoreV1().Pods(k.namespace).Get(context.TODO(), hostName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ZError(ctx, "K8s Discovery: Failed to get pod", err)
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
|
||||
var selfPort int32
|
||||
for _, port := range pod.Spec.Containers[0].Ports {
|
||||
if port.ContainerPort != 10001 {
|
||||
selfPort = port.ContainerPort
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
k.selfTarget = fmt.Sprintf("%s:%d", pod.Status.PodIP, selfPort)
|
||||
log.ZInfo(ctx, "K8s Discovery: Self connection target", "target", k.selfTarget)
|
||||
}
|
||||
|
||||
return k.selfTarget
|
||||
}
|
||||
|
||||
// AddOption appends gRPC dial options to the existing options.
|
||||
func (k *KubernetesConnManager) AddOption(opts ...grpc.DialOption) {
|
||||
k.mu.Lock()
|
||||
defer k.mu.Unlock()
|
||||
k.dialOptions = append(k.dialOptions, opts...)
|
||||
log.ZDebug(context.Background(), "K8s Discovery: Added dial options", "count", len(opts))
|
||||
}
|
||||
|
||||
// CloseConn closes a given gRPC client connection.
|
||||
func (k *KubernetesConnManager) CloseConn(conn *grpc.ClientConn) {
|
||||
log.ZDebug(context.Background(), "K8s Discovery: Closing single connection")
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) Register(ctx context.Context, serviceName, host string, port int, opts ...grpc.DialOption) error {
|
||||
// K8s 环境下不需要注册,返回 nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) UnRegister() error {
|
||||
// K8s 环境下不需要注销,返回 nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) GetUserIdHashGatewayHost(ctx context.Context, userId string) (string, error) {
|
||||
// K8s 环境下不支持,返回空
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// KeyValue interface methods - K8s环境下不支持,返回 discovery.ErrNotSupported
|
||||
// 这样调用方可以通过 errors.Is(err, discovery.ErrNotSupported) 来判断并忽略
|
||||
|
||||
func (k *KubernetesConnManager) SetKey(ctx context.Context, key string, value []byte) error {
|
||||
return discovery.ErrNotSupported
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) SetWithLease(ctx context.Context, key string, val []byte, ttl int64) error {
|
||||
return discovery.ErrNotSupported
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) GetKey(ctx context.Context, key string) ([]byte, error) {
|
||||
return nil, discovery.ErrNotSupported
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) GetKeyWithPrefix(ctx context.Context, key string) ([][]byte, error) {
|
||||
return nil, discovery.ErrNotSupported
|
||||
}
|
||||
|
||||
func (k *KubernetesConnManager) WatchKey(ctx context.Context, key string, fn discovery.WatchKeyHandler) error {
|
||||
return discovery.ErrNotSupported
|
||||
}
|
||||
Reference in New Issue
Block a user