/
gateway_az_info_service.go
213 lines (195 loc) · 6.02 KB
/
gateway_az_info_service.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"encoding/json"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/erda-project/erda/modules/hepa/common/util"
. "github.com/erda-project/erda/modules/hepa/common/vars"
"github.com/erda-project/erda/modules/hepa/repository/orm"
"github.com/erda-project/erda/pkg/discover"
)
type AdminProjectDto struct {
Name string `json:"name"`
ClusterConfig map[string]string `json:"clusterConfig"`
}
type AdminRespDto struct {
Success bool `json:"success"`
Data AdminProjectDto `json:"data"`
}
const (
CT_K8S = "kubernetes"
CT_DCOS = "dcos"
CT_EDAS = "edas"
)
type ClusterRespDto struct {
Success bool `json:"success"`
Data ClusterInfoDto `json:"data"`
}
type ClusterInfoDto struct {
DiceClusterType string `json:"DICE_CLUSTER_TYPE"`
DiceRootDomain string `json:"DICE_ROOT_DOMAIN"`
MasterAddr string `json:"MASTER_VIP_ADDR"`
NetportalUrl string `json:"NETPORTAL_URL"`
}
type GatewayAzInfoServiceImpl struct {
engine *orm.OrmEngine
}
func NewGatewayAzInfoServiceImpl() (*GatewayAzInfoServiceImpl, error) {
engine, error := orm.GetSingleton()
if error != nil {
return nil, errors.Wrap(error, "new GatewayAzInfoServiceImpl failed")
}
return &GatewayAzInfoServiceImpl{engine}, nil
}
func (impl *GatewayAzInfoServiceImpl) GetAz(cond *orm.GatewayAzInfo) (string, error) {
azInfo, err := impl.GetAzInfo(cond)
if err != nil {
return "", err
}
return azInfo.Az, nil
}
func (impl *GatewayAzInfoServiceImpl) SelectByAny(cond *orm.GatewayAzInfo) ([]orm.GatewayAzInfo, error) {
var result []orm.GatewayAzInfo
if cond == nil {
return result, errors.New(ERR_INVALID_ARG)
}
err := orm.SelectByAny(impl.engine, &result, cond)
if err != nil {
return result, errors.Wrap(err, ERR_SQL_FAIL)
}
return result, nil
}
func (impl *GatewayAzInfoServiceImpl) SelectValidAz() ([]orm.GatewayAzInfo, error) {
var result []orm.GatewayAzInfo
err := orm.Select(impl.engine.Distinct("az"), &result, `master_addr != ""`)
if err != nil {
return result, errors.Wrap(err, ERR_SQL_FAIL)
}
return result, nil
}
func fillInfo(info *orm.GatewayAzInfo, clusterInfo ClusterInfoDto) {
clusterType := clusterInfo.DiceClusterType
switch clusterType {
case CT_K8S:
info.Type = orm.AT_K8S
case CT_DCOS:
info.Type = orm.AT_DCOS
case CT_EDAS:
info.Type = orm.AT_EDAS
default:
info.Type = orm.AT_UNKNOWN
}
info.WildcardDomain = clusterInfo.DiceRootDomain
if clusterInfo.MasterAddr == "" {
info.MasterAddr = ""
}
if clusterInfo.NetportalUrl == "" {
info.MasterAddr = clusterInfo.MasterAddr
} else {
info.MasterAddr = clusterInfo.NetportalUrl + "/" + clusterInfo.MasterAddr
}
}
func (impl *GatewayAzInfoServiceImpl) GetAzInfoByClusterName(name string) (*orm.GatewayAzInfo, error) {
info := &orm.GatewayAzInfo{
Az: name,
}
code, body, err := util.CommonRequest("GET", discover.Scheduler()+"/api/clusterinfo/"+name, nil, map[string]string{"Internal-Client": "hepa-gateway"})
if code >= 300 {
err = errors.Errorf("get cluster info failed, code:%d", code)
}
if err != nil {
return nil, err
}
clusterResp := &ClusterRespDto{}
err = json.Unmarshal(body, clusterResp)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal failed:%s", body)
}
if !clusterResp.Success {
return nil, errors.Errorf("request cluster info failed: resp[%s]", body)
}
fillInfo(info, clusterResp.Data)
return info, nil
}
func (impl *GatewayAzInfoServiceImpl) GetAzInfo(cond *orm.GatewayAzInfo) (*orm.GatewayAzInfo, error) {
if cond == nil || cond.ProjectId == "" || cond.Env == "" {
return nil, errors.New(ERR_INVALID_ARG)
}
info := &orm.GatewayAzInfo{}
exist, err := orm.GetByAny(impl.engine, info, cond)
if err != nil {
return nil, errors.WithStack(err)
}
now := time.Now()
if exist && info.Type != "" && (info.NeedUpdate == 0 || (now.Sub(info.UpdateTime).Seconds() < 60 &&
now.Sub(info.UpdateTime).Seconds() > 0)) {
return info, nil
}
code, body, err := util.CommonRequest("GET", discover.CoreServices()+"/api/projects/"+cond.ProjectId, nil,
map[string]string{"Internal-Client": "hepa-gateway"})
if err != nil {
err = errors.WithMessage(err, "request dice admin failed")
goto failback
}
if code < 300 {
data := &AdminRespDto{}
err = json.Unmarshal(body, data)
if err != nil {
err = errors.Wrapf(err, "unmarshal failed:%s", body)
goto failback
}
if !data.Success {
err = errors.Errorf("request dice admin failed: resp[%s]", body)
goto failback
}
az, ok := data.Data.ClusterConfig[strings.ToUpper(cond.Env)]
if !ok {
err = errors.Errorf("can't find az of info[%+v] in admin resp[%s]", cond, body)
goto failback
}
code, body, err = util.CommonRequest("GET", discover.Scheduler()+"/api/clusterinfo/"+az, nil, map[string]string{"Internal-Client": "hepa-gateway"})
if code >= 300 || err != nil {
goto failback
}
clusterResp := &ClusterRespDto{}
err = json.Unmarshal(body, clusterResp)
if err != nil {
err = errors.Wrapf(err, "unmarshal failed:%s", body)
goto failback
}
if !clusterResp.Success {
err = errors.Errorf("request cluster info failed: resp[%s]", body)
goto failback
}
fillInfo(info, clusterResp.Data)
info.Az = az
if exist {
_, _ = orm.Update(impl.engine, info, "az", "wildcard_domain", "type", "master_addr")
} else {
cond.NeedUpdate = 1
cond.Az = az
_, _ = orm.Insert(impl.engine, cond)
}
return info, nil
}
failback:
if exist {
log.Error(err)
return info, nil
}
return nil, err
}