Skip to content

Commit

Permalink
feat(dbm-services): only one address under domain should skip release c…
Browse files Browse the repository at this point in the history
  • Loading branch information
xjxia authored and iSecloud committed Nov 18, 2024
1 parent 5654426 commit 4217971
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
26 changes: 26 additions & 0 deletions dbm-services/common/dbha/ha-module/client/name_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ func (c *NameServiceClient) GetDomainInfoByIp(ip string) ([]DomainInfo, error) {
return res.Detail, nil
}

// GetAddressNumberByDomain get address number under this domain name
func (c *NameServiceClient) GetAddressNumberByDomain(domainName string) (int, error) {
var res DomainRes
req := map[string]interface{}{
"db_cloud_token": c.Conf.BKConf.BkToken,
"bk_cloud_id": c.CloudId,
"domain_name": []string{domainName},
}

response, err := c.DoNew(http.MethodPost,
c.SpliceUrlByPrefix(c.Conf.UrlPre, constvar.GetDomainInfoUrl, ""), req, nil)
if err != nil {
return 0, err
}
if response.Code != 0 {
return 0, fmt.Errorf("%s failed, return code:%d, msg:%s", util.AtWhere(), response.Code, response.Msg)
}

err = json.Unmarshal(response.Data, &res)
if err != nil {
return 0, err
}

return res.RowsNum, nil
}

// GetDomainInfoByDomain get address info from dns by domain
func (c *NameServiceClient) GetDomainInfoByDomain(domainName string) ([]DomainInfo, error) {
var res DomainRes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ func (ins *MySQLProxySwitch) CheckSwitch() (bool, error) {
// DoSwitch mysql-proxy do switch
// delete ip under the entry
func (ins *MySQLProxySwitch) DoSwitch() error {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s] from all cluster entery", ins.Ip))
return ins.DeleteNameService(ins.Entry)
//1. delete name service
isSingle, err := ins.SingleAddressUnderDomain(ins.Entry)
if err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("check whether single address under domain failed:%s",
err.Error()))
return err
}
if isSingle {
return fmt.Errorf("only single address under this domain, skip release domain")
} else {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all cluster entry",
ins.Ip, ins.Port))
return ins.DeleteNameService(ins.Entry)
}
}

// ShowSwitchInstanceInfo display switch proxy info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,21 @@ func (ins *SpiderProxyLayerSwitch) DoSwitch() error {
)

//1. delete name service
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all domain entry",
ins.Ip, ins.Port))
if err := ins.DeleteNameService(ins.Entry); err != nil {
isSingle, err := ins.SingleAddressUnderDomain(ins.Entry)
if err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("check whether single address under domain failed:%s",
err.Error()))
return err
}
if isSingle {
return fmt.Errorf("only single address under this domain, skip release domain")
} else {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all domain entry",
ins.Ip, ins.Port))
if err := ins.DeleteNameService(ins.Entry); err != nil {
return err
}
}

//2. set all spider nodes
if err := ins.SetSpiderNodes(); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions dbm-services/common/dbha/ha-module/dbutil/db_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ func (ins *BaseSwitch) GetInfo(infoKey string) (bool, interface{}) {
}
}

// SingleAddressUnderDomain check whether only one address under domain
// if only one address under dns entry, return true
// if no dns entry found, return false
func (ins *BaseSwitch) SingleAddressUnderDomain(entry BindEntry) (bool, error) {
if entry.Dns == nil {
return false, nil
}
conf := ins.Config
dnsClient := client.NewNameServiceClient(&conf.NameServices.DnsConf, conf.GetCloudId())
for _, dns := range entry.Dns {
number, err := dnsClient.GetAddressNumberByDomain(dns.DomainName)
if err != nil {
return false, err
}
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("found %d address under domain %s",
number, dns.DomainName))
if number == 1 {
return true, nil
}
}

return false, nil
}

// DeleteNameService delete broken-down ip from entry
func (ins *BaseSwitch) DeleteNameService(entry BindEntry) error {
//flag refer to whether release name-service success
Expand Down

0 comments on commit 4217971

Please sign in to comment.