Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple apisix clusters #73

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions conf/conf.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
etcd:
host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
- "http://127.0.0.1:2379" # multiple etcd address, if your etcd cluster enables TLS, please use https scheme,
prefix: /apisix # apisix configurations prefix
timeout: 30 # 30 seconds
#user: root # root username for etcd
#password: 5tHkHhYkjr6cQY # root password for etcd
tls:
#cert: /path/to/cert # path of certificate used by the etcd client
#key: /path/to/key # path of key used by the etcd client
- host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
- "http://127.0.0.1:2379" # multiple etcd address, if your etcd cluster enables TLS, please use https scheme,
prefix: /apisix # apisix configurations prefix
timeout: 30 # 30 seconds
#user: root # root username for etcd
#password: 5tHkHhYkjr6cQY # root password for etcd
tls:
#cert: /path/to/cert # path of certificate used by the etcd client
#key: /path/to/key # path of key used by the etcd client

verify: true # whether to verify the etcd endpoint certificate when setup a TLS connection to etcd,
# the default value is true, e.g. the certificate will be verified strictly.
verify: true # whether to verify the etcd endpoint certificate when setup a TLS connection to etcd,
# the default value is true, e.g. the certificate will be verified strictly.
log:
level: warn
path: apisix-seed.log # path is the file to write logs to. Backup log files will be retained in the same directory
Expand Down
15 changes: 8 additions & 7 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DisBuilder func([]byte) (interface{}, error)

var (
WorkDir = "."
ETCDConfig *Etcd
ETCDConfigs []*Etcd = make([]*Etcd, 0)
LogConfig *Log
DisConfigs = make(map[string]interface{})
DisBuilders = make(map[string]DisBuilder)
Expand Down Expand Up @@ -44,7 +44,7 @@ type Log struct {
}

type Config struct {
Etcd Etcd
Etcd []Etcd
Log Log
Discovery map[string]interface{}
}
Expand Down Expand Up @@ -80,9 +80,10 @@ func InitConf() {
}

initLogConfig(config.Log)

if len(config.Etcd.Host) > 0 {
initEtcdConfig(config.Etcd)
for _, singleEtcd := range config.Etcd {
if len(singleEtcd.Host) > 0 {
initEtcdConfig(singleEtcd)
}
}
}
}
Expand All @@ -99,13 +100,13 @@ func initEtcdConfig(conf Etcd) {
prefix = conf.Prefix
}

ETCDConfig = &Etcd{
ETCDConfigs = append(ETCDConfigs, &Etcd{
Host: host,
User: conf.User,
Password: conf.Password,
TLS: conf.TLS,
Prefix: prefix,
}
})
}

func initLogConfig(conf Log) {
Expand Down
14 changes: 7 additions & 7 deletions internal/core/storer/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ func InitStore(key string, opt GenericStoreOption, stg Interface) error {
return nil
}

func InitStores(stg Interface) (err error) {
func InitStores(stg Interface, conf *conf.Etcd) (err error) {
err = InitStore("routes", GenericStoreOption{
BasePath: conf.ETCDConfig.Prefix + "/routes",
Prefix: conf.ETCDConfig.Prefix,
BasePath: conf.Prefix + "/routes",
Prefix: conf.Prefix,
}, stg)
if err != nil {
return
}

err = InitStore("services", GenericStoreOption{
BasePath: conf.ETCDConfig.Prefix + "/services",
Prefix: conf.ETCDConfig.Prefix,
BasePath: conf.Prefix + "/services",
Prefix: conf.Prefix,
}, stg)
if err != nil {
return
}

err = InitStore("upstreams", GenericStoreOption{
BasePath: conf.ETCDConfig.Prefix + "/upstreams",
Prefix: conf.ETCDConfig.Prefix,
BasePath: conf.Prefix + "/upstreams",
Prefix: conf.Prefix,
}, stg)
if err != nil {
return
Expand Down
53 changes: 30 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,44 @@ func main() {
log.Fatal(err)
}

etcdClient, err := storer.NewEtcd(conf.ETCDConfig)
if err != nil {
panic(err)
}
rewriters := make([]components.Rewriter, 0)

wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
err := storer.InitStores(etcdClient)
for _, config := range conf.ETCDConfigs {
etcdClient, err := storer.NewEtcd(config)
if err != nil {
panic(err)
}
}()
go func() {
defer wg.Done()
err := discoverer.InitDiscoverers()
if err != nil {
panic(err)

wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
err := storer.InitStores(etcdClient, config)
if err != nil {
panic(err)
}
}()
go func() {
defer wg.Done()
err := discoverer.InitDiscoverers()
if err != nil {
panic(err)
}
}()
wg.Wait()

rewriter := components.Rewriter{
Prefix: config.Prefix,
}
}()
wg.Wait()

rewriter := components.Rewriter{
Prefix: conf.ETCDConfig.Prefix,
rewriter.Init()
rewriters = append(rewriters, rewriter)
}
for _, rewriter := range rewriters {
defer rewriter.Close()
}
rewriter.Init()
defer rewriter.Close()

watcher := components.Watcher{}
err = watcher.Init()
err := watcher.Init()
if err != nil {
log.Error(err.Error())
return
Expand Down