forked from bfenetworks/bfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_data_conf.go
147 lines (122 loc) · 4.08 KB
/
server_data_conf.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2019 Baidu, Inc.
//
// 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.
// for route traffic to backend cluster
package bfe_route
import (
"fmt"
)
import (
"github.com/baidu/go-lib/log"
)
import (
"github.com/baidu/bfe/bfe_config/bfe_route_conf/host_rule_conf"
"github.com/baidu/bfe/bfe_config/bfe_route_conf/route_rule_conf"
"github.com/baidu/bfe/bfe_config/bfe_route_conf/vip_rule_conf"
"github.com/baidu/bfe/bfe_route/bfe_cluster"
)
type ServerDataConf struct {
HostTable *HostTable
ClusterTable *ClusterTable
}
func newServerDataConf() *ServerDataConf {
c := new(ServerDataConf)
// initialize HostTable & ClusterTable
c.HostTable = newHostTable()
c.ClusterTable = newClusterTable()
return c
}
// LoadServerDataConf loades ServerDataConf config.
func LoadServerDataConf(hostFile, vipFile, routeFile, clusterConfFile string) (*ServerDataConf, error) {
s := newServerDataConf()
// load host table
if err := s.hostTableLoad(hostFile, vipFile, routeFile); err != nil {
return nil, fmt.Errorf("hostTableLoad Error %s", err)
}
// load cluster table
if err := s.clusterTableLoad(clusterConfFile); err != nil {
return nil, fmt.Errorf("clusterTableLoad Error %s", err)
}
// check host, route, cluster_conf dependent relationship
if err := s.check(); err != nil {
return nil, fmt.Errorf("ServerDataConf.check Error %s", err)
}
return s, nil
}
// hostTableLoad loades all data for host table from file.
func (s *ServerDataConf) hostTableLoad(hostFile, vipFile, routeFile string) error {
// load host rule from file
hostConf, err := host_rule_conf.HostRuleConfLoad(hostFile)
if err != nil {
log.Logger.Error("hostTableLoad():err in HostRuleConfLoad():%s", err.Error())
return err
}
// load vip rule from file
vipConf, err := vip_rule_conf.VipRuleConfLoad(vipFile)
if err != nil {
log.Logger.Error("vipTableLoad():err in VipRuleConfLoad():%s", err.Error())
return err
}
// load route conf from file
routeConf, err := route_rule_conf.RouteConfLoad(routeFile)
if err != nil {
log.Logger.Error("hostTableLoad():err in RouteConfLoad():%s", err.Error())
return err
}
// update to host table
s.HostTable.Update(hostConf, vipConf, routeConf)
return nil
}
func (s *ServerDataConf) clusterTableLoad(clusterConf string) error {
err := s.ClusterTable.Init(clusterConf)
if err != nil {
return err
}
log.Logger.Info("init cluster table success")
return nil
}
func (s *ServerDataConf) check() error {
// check product consistency in host and route
for product1 := range s.HostTable.productRouteTable {
find := false
for _, product2 := range s.HostTable.hostTagTable {
if product1 == product2 {
find = true
break
}
}
if !find {
return fmt.Errorf("product[%s] in route should exist in host!", product1)
}
}
// check cluster_name consistency in route and cluster_conf
for _, routeRules := range s.HostTable.productRouteTable {
for _, routeRule := range routeRules {
if _, err := s.ClusterTable.Lookup(routeRule.ClusterName); err != nil {
return fmt.Errorf("cluster[%s] in route should exist in cluster_conf",
routeRule.ClusterName)
}
}
}
return nil
}
// HostTableLookup find cluster name with given hostname.
// implement interface ServerDataConfInterface.
func (s *ServerDataConf) HostTableLookup(hostname string) (string, error) {
return s.HostTable.LookupProduct(hostname)
}
// ClusterTableLookup find backend with given cluster-name and request.
// implement interface ServerDataConfInterface.
func (s *ServerDataConf) ClusterTableLookup(clusterName string) (*bfe_cluster.BfeCluster, error) {
return s.ClusterTable.Lookup(clusterName)
}