feat: support eureka registry (#464)

Signed-off-by: charlie <qianglin98@qq.com>
This commit is contained in:
Qianglin Li
2023-08-14 11:26:39 +08:00
committed by GitHub
parent c49c8f1ec2
commit f8f8b41fa2
12 changed files with 865 additions and 0 deletions

View File

@@ -0,0 +1,204 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 client
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/avast/retry-go/v4"
"github.com/hudl/fargo"
"istio.io/pkg/log"
)
var httpClient = http.DefaultClient
type EurekaHttpClient interface {
GetApplications() (*Applications, error)
GetApplication(name string) (*fargo.Application, error)
ScheduleAppUpdates(name string, stop <-chan struct{}) <-chan fargo.AppUpdate
GetDelta() (*Applications, error)
}
func NewEurekaHttpClient(config EurekaHttpConfig) EurekaHttpClient {
return &eurekaHttpClient{config}
}
type EurekaHttpConfig struct {
BaseUrl string
ConnectTimeoutSeconds int // default 30
PollInterval int //default 30
Retries int // default 3
RetryDelayTime int // default 100ms
EnableDelta bool
}
func NewDefaultConfig() EurekaHttpConfig {
return EurekaHttpConfig{
ConnectTimeoutSeconds: 10,
PollInterval: 30,
EnableDelta: true,
Retries: 3,
RetryDelayTime: 100,
}
}
type eurekaHttpClient struct {
EurekaHttpConfig
}
func (e *eurekaHttpClient) GetApplications() (*Applications, error) {
return e.getApplications("/apps")
}
func (e *eurekaHttpClient) GetApplication(name string) (*fargo.Application, error) {
return e.getApplication("/apps/" + name)
}
func (e *eurekaHttpClient) ScheduleAppUpdates(name string, stop <-chan struct{}) <-chan fargo.AppUpdate {
updates := make(chan fargo.AppUpdate)
consume := func(app *fargo.Application, err error) {
// Drop attempted sends when the consumer hasn't received the last buffered update
select {
case updates <- fargo.AppUpdate{App: app, Err: err}:
default:
}
}
go func() {
ticker := time.NewTicker(time.Duration(e.PollInterval) * time.Second)
defer ticker.Stop()
for {
select {
case <-stop:
close(updates)
return
case <-ticker.C:
consume(e.GetApplication(name))
}
}
}()
return updates
}
func (e *eurekaHttpClient) GetDelta() (*Applications, error) {
if !e.EnableDelta {
return nil, fmt.Errorf("failed to get DeltaAppliation, enableDelta is false")
}
return e.getApplications("/apps/delta")
}
func (c *eurekaHttpClient) getApplications(path string) (*Applications, error) {
res, code, err := c.request(path)
if err != nil {
log.Errorf("Failed to get applications, err: %v", err)
return nil, err
}
if code != 200 {
log.Warnf("Failed to get applications, http code : %v", code)
}
var rj fargo.GetAppsResponseJson
if err = json.Unmarshal(res, &rj); err != nil {
log.Errorf("Failed to unmarshal response body to fargo.GetAppResponseJosn, error: %v", err)
return nil, err
}
apps := map[string]*fargo.Application{}
for idx := range rj.Response.Applications {
app := rj.Response.Applications[idx]
apps[app.Name] = app
}
for name, app := range apps {
log.Debugf("Parsing metadata for app %v", name)
if err := app.ParseAllMetadata(); err != nil {
return nil, err
}
}
return &Applications{
Apps: apps,
HashCode: rj.Response.AppsHashcode,
VersionDelta: rj.Response.VersionsDelta,
}, nil
}
func (c *eurekaHttpClient) getApplication(path string) (*fargo.Application, error) {
res, code, err := c.request(path)
if err != nil {
log.Errorf("Failed to get application, err: %v", err)
return nil, err
}
if code != 200 {
log.Warnf("Failed to get application, http code : %v", code)
}
var rj fargo.GetAppResponseJson
if err = json.Unmarshal(res, &rj); err != nil {
log.Errorf("Failed to unmarshal response body to fargo.GetAppResponseJson, error: %v", err)
return nil, err
}
return &rj.Application, nil
}
func (c *eurekaHttpClient) request(urlPath string) ([]byte, int, error) {
req, err := http.NewRequest("GET", c.getUrl(urlPath), nil)
if err != nil {
log.Errorf("Failed to new a Request, error: %v", err.Error())
return nil, -1, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
retryConfig := []retry.Option{
retry.Attempts(uint(c.Retries)),
retry.Delay(time.Duration(c.RetryDelayTime)),
}
resp := &http.Response{}
err = retry.Do(func() error {
resp, err = httpClient.Do(req)
return err
}, retryConfig...)
if err != nil {
log.Errorf("Failed to get response from eureka-server, error : %v", err)
return nil, -1, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Errorf("Failed to read request body, error : %v", err)
return nil, -1, err
}
log.Infof("Get eureka response from url=%v", req.URL)
return body, resp.StatusCode, nil
}
func (c *eurekaHttpClient) getUrl(path string) string {
return "http://" + c.BaseUrl + "/eureka" + path
}

View File

@@ -0,0 +1,80 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 client
import (
"github.com/hudl/fargo"
"istio.io/pkg/log"
)
type Handler func(application *fargo.Application) error
/*
* Plan can be used to get the latest information of a service
*
* (service B) ┌──────┐
* ┌────────────►│Plan B│
* │ Timer └──────┘
* │
* ┌───────────────┐ │
* │ eureka-server ├────┤
* └───────────────┘ │
* │
* │
* │(service A) ┌──────┐
* └────────────►│Plan A│
* Timer └──────┘
*/
type Plan struct {
client EurekaHttpClient
stop chan struct{}
handler Handler
}
func NewPlan(client EurekaHttpClient, serviceName string, handler Handler) *Plan {
p := &Plan{
client: client,
stop: make(chan struct{}),
handler: handler,
}
ch := client.ScheduleAppUpdates(serviceName, p.stop)
go p.watch(ch)
return p
}
func (p *Plan) Stop() {
defer close(p.stop)
p.stop <- struct{}{}
}
func (p *Plan) watch(ch <-chan fargo.AppUpdate) {
for {
select {
case <-p.stop:
log.Info("stop eureka plan")
return
case updateItem := <-ch:
if updateItem.Err != nil {
log.Error("get eureka application failed, error : %v", updateItem.Err)
continue
}
if err := p.handler(updateItem.App); err != nil {
log.Error("handle eureka application failed, error : %v", err)
}
}
}
}

View File

@@ -0,0 +1,23 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 client
import "github.com/hudl/fargo"
type Applications struct {
Apps map[string]*fargo.Application
HashCode string
VersionDelta int
}

304
registry/eureka/watcher.go Normal file
View File

@@ -0,0 +1,304 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 eureka
import (
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/hudl/fargo"
"istio.io/api/networking/v1alpha3"
versionedclient "istio.io/client-go/pkg/clientset/versioned"
"istio.io/pkg/log"
ctrl "sigs.k8s.io/controller-runtime"
apiv1 "github.com/alibaba/higress/api/networking/v1"
"github.com/alibaba/higress/pkg/common"
provider "github.com/alibaba/higress/registry"
. "github.com/alibaba/higress/registry/eureka/client"
"github.com/alibaba/higress/registry/memory"
)
const (
DefaultFullRefreshIntervalLimit = time.Second * 30
suffix = "eureka"
)
type watcher struct {
provider.BaseWatcher
apiv1.RegistryConfig
WatchingServices map[string]*Plan `json:"watching_services"`
RegistryType provider.ServiceRegistryType `json:"registry_type"`
Status provider.WatcherStatus `json:"status"`
cache memory.Cache
mutex *sync.Mutex
stop chan struct{}
istioClient *versionedclient.Clientset
isStop bool
updateCacheWhenEmpty bool
eurekaClient EurekaHttpClient
fullRefreshIntervalLimit time.Duration
deltaRefreshIntervalLimit time.Duration
}
type WatcherOption func(w *watcher)
func NewWatcher(cache memory.Cache, opts ...WatcherOption) (provider.Watcher, error) {
w := &watcher{
WatchingServices: make(map[string]*Plan),
RegistryType: provider.Eureka,
Status: provider.UnHealthy,
cache: cache,
mutex: &sync.Mutex{},
stop: make(chan struct{}),
}
config, err := ctrl.GetConfig()
if err != nil {
return nil, err
}
ic, err := versionedclient.NewForConfig(config)
if err != nil {
log.Errorf("can not new istio client, err:%v", err)
return nil, err
}
w.istioClient = ic
w.fullRefreshIntervalLimit = DefaultFullRefreshIntervalLimit
for _, opt := range opts {
opt(w)
}
cfg := NewDefaultConfig()
cfg.BaseUrl = net.JoinHostPort(w.Domain, strconv.FormatUint(uint64(w.Port), 10))
w.eurekaClient = NewEurekaHttpClient(cfg)
return w, nil
}
func WithEurekaFullRefreshInterval(refreshInterval int64) WatcherOption {
return func(w *watcher) {
if refreshInterval < int64(DefaultFullRefreshIntervalLimit) {
refreshInterval = int64(DefaultFullRefreshIntervalLimit)
}
w.fullRefreshIntervalLimit = time.Duration(refreshInterval)
}
}
func WithType(t string) WatcherOption {
return func(w *watcher) {
w.Type = t
}
}
func WithName(name string) WatcherOption {
return func(w *watcher) {
w.Name = name
}
}
func WithDomain(domain string) WatcherOption {
return func(w *watcher) {
w.Domain = domain
}
}
func WithPort(port uint32) WatcherOption {
return func(w *watcher) {
w.Port = port
}
}
func WithUpdateCacheWhenEmpty(enable bool) WatcherOption {
return func(w *watcher) {
w.updateCacheWhenEmpty = enable
}
}
func (w *watcher) Run() {
ticker := time.NewTicker(w.fullRefreshIntervalLimit)
defer ticker.Stop()
w.Status = provider.ProbeWatcherStatus(w.Domain, strconv.FormatUint(uint64(w.Port), 10))
w.doFullRefresh()
w.Ready(true)
for {
select {
case <-ticker.C:
w.doFullRefresh()
case <-w.stop:
log.Info("eureka watcher(%v) is stopping ...", w.Name)
return
}
}
}
func (w *watcher) Stop() {
w.mutex.Lock()
defer w.mutex.Unlock()
for serviceName := range w.WatchingServices {
if err := w.unsubscribe(serviceName); err != nil {
log.Errorf("Failed to unsubscribe service : %v", serviceName)
continue
}
w.cache.DeleteServiceEntryWrapper(makeHost(serviceName))
}
w.UpdateService()
}
func (w *watcher) IsHealthy() bool {
return w.Status == provider.Healthy
}
func (w *watcher) GetRegistryType() string {
return w.RegistryType.String()
}
// doFullRefresh todo(lql): it's better to support deltaRefresh
func (w *watcher) doFullRefresh() {
w.mutex.Lock()
defer w.mutex.Unlock()
applications, err := w.eurekaClient.GetApplications()
if err != nil {
log.Errorf("Failed to full fetch eureka services, error : %v", err)
return
}
fetchedServices := applications.Apps
for serviceName := range w.WatchingServices {
if _, ok := fetchedServices[serviceName]; !ok {
if err = w.unsubscribe(serviceName); err != nil {
log.Errorf("Failed to unsubscribe service %v, error : %v", serviceName, err)
continue
}
}
}
for serviceName := range fetchedServices {
if _, ok := w.WatchingServices[serviceName]; !ok {
if err = w.subscribe(fetchedServices[serviceName]); err != nil {
log.Errorf("Failed to subscribe service %v, error : %v", serviceName, err)
continue
}
}
}
}
func (w *watcher) subscribe(service *fargo.Application) error {
if service == nil {
return fmt.Errorf("service is nil")
}
callback := func(service *fargo.Application) error {
defer w.UpdateService()
if len(service.Instances) != 0 {
se, err := generateServiceEntry(service)
if err != nil {
return err
}
w.cache.UpdateServiceEntryWrapper(makeHost(service.Name), &memory.ServiceEntryWrapper{
ServiceName: service.Name,
ServiceEntry: se,
Suffix: suffix,
RegistryType: w.Type,
})
return nil
}
if w.updateCacheWhenEmpty {
w.cache.DeleteServiceEntryWrapper(makeHost(service.Name))
}
return nil
}
if err := callback(service); err != nil {
log.Errorf("failed to subscribe service %v, error: %v", service.Name, err)
return err
}
w.WatchingServices[service.Name] = NewPlan(w.eurekaClient, service.Name, callback)
return nil
}
func (w *watcher) unsubscribe(serviceName string) error {
w.WatchingServices[serviceName].Stop()
delete(w.WatchingServices, serviceName)
w.UpdateService()
return nil
}
func makeHost(serviceName string) string {
return serviceName + common.DotSeparator + suffix
}
func convertMap(m map[string]interface{}) map[string]string {
result := make(map[string]string)
for k, v := range m {
if value, ok := v.(string); ok {
result[k] = value
}
}
return result
}
func generateServiceEntry(app *fargo.Application) (*v1alpha3.ServiceEntry, error) {
portList := make([]*v1alpha3.Port, 0)
endpoints := make([]*v1alpha3.WorkloadEntry, 0)
for _, instance := range app.Instances {
protocol := common.HTTP
if val, _ := instance.Metadata.GetString("protocol"); val != "" {
if protocol = common.ParseProtocol(val); protocol == common.Unsupported {
return nil, fmt.Errorf("unsupported protocol %v", val)
}
}
port := &v1alpha3.Port{
Name: protocol.String(),
Number: uint32(instance.Port),
Protocol: protocol.String(),
}
if len(portList) == 0 {
portList = append(portList, port)
}
endpoint := v1alpha3.WorkloadEntry{
Address: instance.IPAddr,
Ports: map[string]uint32{port.Protocol: port.Number},
Labels: convertMap(instance.Metadata.GetMap()),
}
endpoints = append(endpoints, &endpoint)
}
se := &v1alpha3.ServiceEntry{
Hosts: []string{makeHost(app.Name)},
Ports: portList,
Location: v1alpha3.ServiceEntry_MESH_INTERNAL,
Resolution: v1alpha3.ServiceEntry_STATIC,
Endpoints: endpoints,
}
return se, nil
}