Dragonfly2
185 строк · 4.6 Кб
1/*
2* Copyright 2020 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17//go:generate mockgen -destination mocks/dynconfig_mock.go -source dynconfig.go -package mocks
18
19package config
20
21import (
22"errors"
23"time"
24
25"google.golang.org/grpc/credentials"
26"google.golang.org/grpc/resolver"
27
28managerv1 "d7y.io/api/v2/pkg/apis/manager/v1"
29
30managerclient "d7y.io/dragonfly/v2/pkg/rpc/manager/client"
31)
32
33type SourceType string
34
35const (
36// LocalSourceType represents read configuration from local file.
37LocalSourceType = "local"
38
39// ManagerSourceType represents pulling configuration from manager.
40ManagerSourceType = "manager"
41)
42
43// Watch dynconfig interval.
44var watchInterval = 10 * time.Second
45
46type DynconfigData struct {
47SeedPeers []*managerv1.SeedPeer
48Schedulers []*managerv1.Scheduler
49ObjectStorage *managerv1.ObjectStorage
50}
51
52type Dynconfig interface {
53// Get the dynamic seed peers config.
54GetSeedPeers() ([]*managerv1.SeedPeer, error)
55
56// Get the dynamic schedulers resolve addrs.
57GetResolveSchedulerAddrs() ([]resolver.Address, error)
58
59// Get the dynamic schedulers config.
60GetSchedulers() ([]*managerv1.Scheduler, error)
61
62// Get the dynamic schedulers cluster id.
63GetSchedulerClusterID() uint64
64
65// Get the dynamic object storage config.
66GetObjectStorage() (*managerv1.ObjectStorage, error)
67
68// Get the dynamic config.
69Get() (*DynconfigData, error)
70
71// Refresh refreshes dynconfig in cache.
72Refresh() error
73
74// Register allows an instance to register itself to listen/observe events.
75Register(Observer)
76
77// Deregister allows an instance to remove itself from the collection of observers/listeners.
78Deregister(Observer)
79
80// Notify publishes new events to listeners.
81Notify() error
82
83// OnNotify allows an event to be published to the dynconfig.
84// Used for listening changes of the local configuration.
85OnNotify(*DaemonOption)
86
87// Serve the dynconfig listening service.
88Serve() error
89
90// Stop the dynconfig listening service.
91Stop() error
92}
93
94type dynconfig struct {
95sourceType SourceType
96managerClient managerclient.V1
97cacheDir string
98transportCredentials credentials.TransportCredentials
99}
100
101type Observer interface {
102// OnNotify allows an event to be published to interface implementations.
103OnNotify(*DynconfigData)
104}
105
106// DynconfigOption is a functional option for configuring the dynconfig.
107type DynconfigOption func(d *dynconfig) error
108
109// WithManagerClient set the manager client.
110func WithManagerClient(c managerclient.V1) DynconfigOption {
111return func(d *dynconfig) error {
112d.managerClient = c
113return nil
114}
115}
116
117// WithCacheDir set the cache dir.
118func WithCacheDir(dir string) DynconfigOption {
119return func(d *dynconfig) error {
120d.cacheDir = dir
121return nil
122}
123}
124
125// WithTransportCredentials returns a DialOption which configures a connection
126// level security credentials (e.g., TLS/SSL).
127func WithTransportCredentials(creds credentials.TransportCredentials) DynconfigOption {
128return func(d *dynconfig) error {
129d.transportCredentials = creds
130return nil
131}
132}
133
134// New returns a new dynconfig interface.
135func NewDynconfig(sourceType SourceType, cfg *DaemonOption, options ...DynconfigOption) (Dynconfig, error) {
136d := &dynconfig{
137sourceType: sourceType,
138}
139
140for _, opt := range options {
141if err := opt(d); err != nil {
142return nil, err
143}
144}
145
146if err := d.validate(); err != nil {
147return nil, err
148}
149
150var (
151di Dynconfig
152err error
153)
154switch sourceType {
155case ManagerSourceType:
156di, err = newDynconfigManager(cfg, d.managerClient, d.cacheDir, d.transportCredentials)
157if err != nil {
158return nil, err
159}
160case LocalSourceType:
161di, err = newDynconfigLocal(cfg, d.transportCredentials)
162if err != nil {
163return nil, err
164}
165default:
166return nil, errors.New("unknown source type")
167}
168
169return di, nil
170}
171
172// validate dynconfig parameters.
173func (d *dynconfig) validate() error {
174if d.sourceType == ManagerSourceType {
175if d.managerClient == nil {
176return errors.New("manager dynconfig requires parameter ManagerClient")
177}
178
179if d.cacheDir == "" {
180return errors.New("manager dynconfig requires parameter CacheDir")
181}
182}
183
184return nil
185}
186