Skip to content

Commit

Permalink
feature: bcs-k8s-watch supports to sync dato to storage in external n…
Browse files Browse the repository at this point in the history
…etwork. issue TencentBlueKing#377
  • Loading branch information
bryanhe-bupt committed Feb 25, 2020
1 parent 4dd3bc1 commit 072ae4d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bcs-k8s/bcs-k8s-watch/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ func RunAsLeader(stopChan <-chan struct{}, config *options.WatchConfig, clusterI
bcsTLSConfig := config.BCS.TLS

glog.Info("getting storage service now...")
storageService, storageServiceZKRD, err := bcs.GetStorageService(zkHosts, bcsTLSConfig, config.BCS.CustomStorageEndpoints)
storageService, storageServiceZKRD, err := bcs.GetStorageService(zkHosts, bcsTLSConfig, config.BCS.CustomStorageEndpoints, config.BCS.IsExternal)
if err != nil {
panic(err)
}
glog.Info("get storage service done")

glog.Info("getting netservice now...")
netservice, netserviceZKRD, err := bcs.GetNetService(netServiceZKHosts, bcsTLSConfig, config.BCS.CustomNetServiceEndpoints)
netservice, netserviceZKRD, err := bcs.GetNetService(netServiceZKHosts, bcsTLSConfig, config.BCS.CustomNetServiceEndpoints, false)
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions bcs-k8s/bcs-k8s-watch/app/bcs/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func GetClusterID(zkHosts string, hostIP string, bcsTLSConfig options.TLS) (stri
}

// GetStorageService returns storage InnerService object for discovery.
func GetStorageService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints []string) (*InnerService, *RegisterDiscover.RegDiscover, error) {
func GetStorageService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints []string, isExternal bool) (*InnerService, *RegisterDiscover.RegDiscover, error) {
discovery := RegisterDiscover.NewRegDiscoverEx(zkHosts, 5*time.Second)
if err := discovery.Start(); err != nil {
return nil, nil, fmt.Errorf("get storage service from ZK failed, %+v", err)
Expand All @@ -151,14 +151,14 @@ func GetStorageService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints
return nil, nil, fmt.Errorf("discover storage service failed, %+v", err)
}

storageService := NewInnerService(types.BCS_MODULE_STORAGE, eventChan, customEndpoints)
storageService := NewInnerService(types.BCS_MODULE_STORAGE, eventChan, customEndpoints, isExternal)
go storageService.Watch(bcsTLSConfig)

return storageService, discovery, nil
}

// GetNetService returns netservice InnerService object for discovery.
func GetNetService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints []string) (*InnerService, *RegisterDiscover.RegDiscover, error) {
func GetNetService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints []string, isExternal bool) (*InnerService, *RegisterDiscover.RegDiscover, error) {
discovery := RegisterDiscover.NewRegDiscoverEx(zkHosts, 5*time.Second)
if err := discovery.Start(); err != nil {
return nil, nil, fmt.Errorf("get netservice from ZK failed, %+v", err)
Expand All @@ -174,7 +174,7 @@ func GetNetService(zkHosts string, bcsTLSConfig options.TLS, customEndpoints []s
return nil, nil, fmt.Errorf("discover netservice failed, %+v", err)
}

netService := NewInnerService(types.BCS_MODULE_NETSERVICE, eventChan, customEndpoints)
netService := NewInnerService(types.BCS_MODULE_NETSERVICE, eventChan, customEndpoints, isExternal)
go netService.Watch(bcsTLSConfig)

return netService, discovery, nil
Expand Down
12 changes: 10 additions & 2 deletions bcs-k8s/bcs-k8s-watch/app/bcs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ type InnerService struct {
eventChan <-chan *RegisterDiscover.DiscoverEvent
servers map[string]*HTTPClientConfig
customEndpoints []string
isExternal bool
}

// NewInnerService creates a new serviceName InnerService instance for discovery.
func NewInnerService(serviceName string, eventChan <-chan *RegisterDiscover.DiscoverEvent,
customEndpoints []string) *InnerService {
customEndpoints []string, isExternal bool) *InnerService {

svc := &InnerService{
name: serviceName,
eventChan: eventChan,
servers: make(map[string]*HTTPClientConfig),
customEndpoints: customEndpoints,
isExternal: isExternal,
}

return svc
Expand Down Expand Up @@ -148,7 +150,13 @@ func (s *InnerService) update(servers []string, bcsTLSConfig options.TLS) {
continue
}

address := fmt.Sprintf("%s://%s:%d", serverInfo.Scheme, serverInfo.IP, serverInfo.Port)
var address string
if s.isExternal {
address = fmt.Sprintf("%s://%s:%d", serverInfo.Scheme, serverInfo.ExternalIp, serverInfo.ExternalPort)
} else {
address = fmt.Sprintf("%s://%s:%d", serverInfo.Scheme, serverInfo.IP, serverInfo.Port)
}

currentServers[address] = ""

if _, exists := s.servers[address]; !exists {
Expand Down
3 changes: 3 additions & 0 deletions bcs-k8s/bcs-k8s-watch/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type BCSConfig struct {

// CustomNetServiceEndpoints is custom target netservice endpoints.
CustomNetServiceEndpoints []string `json:"custom-netservice-endpoints"`

// whether the k8s cluster and bcs-k8s-watch is in external network
IsExternal bool `json:"is-external"`
}

type K8sConfig struct {
Expand Down

0 comments on commit 072ae4d

Please sign in to comment.