/
registrator.go
90 lines (78 loc) · 2.51 KB
/
registrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package registrator
import (
"context"
"time"
"github.com/henderiw-k8s-lcnc/discovery/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
defaultWaitTime = 1 * time.Second
defaultRegistrationCheckInterval = 10 * time.Second
defaultMaxServiceFail = 3
)
// Option can be used to manipulate Register config.
type Option func(Registrator)
type WatchOptions struct {
// RetriveServices defines if service details are required
// as part of ServiceResponse(s)
RetriveServices bool
}
// TargetController defines the interfaces for the target controller
type Registrator interface {
//options
// add a logger to the Registrator
//WithLogger(log logging.Logger)
// Register
Register(ctx context.Context, s *Service)
// DeRegister
DeRegister(ctx context.Context, id string)
// Query
Query(ctx context.Context, serviceName string, tags []string) ([]*Service, error)
// GetEndpointAddress returns the address/port of the serviceEndpoint
GetEndpointAddress(ctx context.Context, serviceName string, tags []string) (string, error)
// Watch
// 1 channel per service to watch
Watch(ctx context.Context, serviceName string, tags []string, opts WatchOptions) chan *ServiceResponse
// all services through 1 channel
WatchCh(ctx context.Context, serviceName string, tags []string, opts WatchOptions, ch chan *ServiceResponse)
//
StopWatch(serviceName string)
}
type Service struct {
Name string // service name
ID string // service instance
Port int // service port
Address string // service address
Tags []string // service tags
HealthChecks []HealthKind // what type of health check kinds are needed to test the service
}
type ServiceResponse struct {
ServiceName string
ServiceInstances []*Service
Err error
}
type HealthKind string
const (
HealthKindTTL HealthKind = "ttl"
HealthKindGRPC HealthKind = "grpc"
)
type Options struct {
//Scheme *runtime.Scheme
ServiceDiscoveryDcName string
ServiceDiscovery discovery.ServiceDiscoveryType
ServiceDiscoveryNamespace string
Address string
}
func New(ctx context.Context, config *rest.Config, o *Options) (Registrator, error) {
switch o.ServiceDiscovery {
case discovery.ServiceDiscoveryTypeK8s:
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return newK8sRegistrator(ctx, c, o.ServiceDiscoveryNamespace)
default:
return newNopRegistrator(o), nil
}
}