[ADD] delete related talbes when deleting service

This commit is contained in:
GLYASAI 2019-03-16 21:34:07 +08:00
parent 5f3d72ff61
commit 327f0914a3
8 changed files with 96 additions and 8 deletions

View File

@ -216,6 +216,32 @@ func (g *GatewayAction) DeleteHTTPRule(req *apimodel.DeleteHTTPRuleStruct) (stri
return svcID, nil return svcID, nil
} }
// DeleteHTTPRule deletes http rule, including certificate and rule extensions
func (g *GatewayAction) DeleteHTTPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error {
// delete http rule
rules, err := g.dbmanager.HTTPRuleDaoTransactions(tx).ListByServiceID(sid)
if err != nil {
return err
}
for _, rule := range rules {
if err := g.dbmanager.CertificateDaoTransactions(tx).DeleteCertificateByID(rule.CertificateID); err != nil {
return err
}
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(rule.UUID); err != nil {
return err
}
if err := g.dbmanager.GwRuleConfigDaoTransactions(tx).DeleteByRuleID(rule.UUID); err != nil {
return err
}
if err := g.dbmanager.HTTPRuleDaoTransactions(tx).DeleteHTTPRuleByID(rule.UUID); err != nil {
return err
}
}
return nil
}
// AddCertificate adds certificate to db if it doesn't exists // AddCertificate adds certificate to db if it doesn't exists
func (g *GatewayAction) AddCertificate(req *apimodel.AddHTTPRuleStruct, tx *gorm.DB) error { func (g *GatewayAction) AddCertificate(req *apimodel.AddHTTPRuleStruct, tx *gorm.DB) error {
cert := &model.Certificate{ cert := &model.Certificate{
@ -385,7 +411,7 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
return "", err return "", err
} }
// delete tcp rule // delete tcp rule
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteTCPRule(tcpRule); err != nil { if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByID(tcpRule.UUID); err != nil {
tx.Rollback() tx.Rollback()
return "", err return "", err
} }
@ -404,6 +430,25 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
return tcpRule.ServiceID, nil return tcpRule.ServiceID, nil
} }
// DeleteTCPRule deletes a tcp rule
func (g *GatewayAction) DeleteTCPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error {
rules, err := db.GetManager().TCPRuleDaoTransactions(tx).GetTCPRuleByServiceID(sid)
if err != nil {
return err
}
for _, rule := range rules {
// delete rule extensions
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(rule.UUID); err != nil {
return err
}
// delete tcp rule
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByID(rule.UUID); err != nil {
return err
}
}
return nil
}
// AddRuleExtensions adds rule extensions to db if any of they doesn't exists // AddRuleExtensions adds rule extensions to db if any of they doesn't exists
func (g *GatewayAction) AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct, func (g *GatewayAction) AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct,
tx *gorm.DB) error { tx *gorm.DB) error {
@ -567,7 +612,7 @@ func (g *GatewayAction) RuleConfig(req *apimodel.RuleConfigReq) error {
setheaders := make(map[string]string) setheaders := make(map[string]string)
for _, item := range req.Body.SetHeaders { for _, item := range req.Body.SetHeaders {
// filter same key // filter same key
setheaders["set-header-" + item.Key] = item.Value setheaders["set-header-"+item.Key] = item.Value
} }
for k, v := range setheaders { for k, v := range setheaders {
configs = append(configs, &model.GwRuleConfig{ configs = append(configs, &model.GwRuleConfig{

View File

@ -29,6 +29,7 @@ type GatewayHandler interface {
AddHTTPRule(req *apimodel.AddHTTPRuleStruct) (string, error) AddHTTPRule(req *apimodel.AddHTTPRuleStruct) (string, error)
UpdateHTTPRule(req *apimodel.UpdateHTTPRuleStruct) (string, error) UpdateHTTPRule(req *apimodel.UpdateHTTPRuleStruct) (string, error)
DeleteHTTPRule(req *apimodel.DeleteHTTPRuleStruct) (string, error) DeleteHTTPRule(req *apimodel.DeleteHTTPRuleStruct) (string, error)
DeleteHTTPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error
AddCertificate(req *apimodel.AddHTTPRuleStruct, tx *gorm.DB) error AddCertificate(req *apimodel.AddHTTPRuleStruct, tx *gorm.DB) error
UpdateCertificate(req apimodel.AddHTTPRuleStruct, httpRule *dbmodel.HTTPRule, tx *gorm.DB) error UpdateCertificate(req apimodel.AddHTTPRuleStruct, httpRule *dbmodel.HTTPRule, tx *gorm.DB) error
@ -36,6 +37,7 @@ type GatewayHandler interface {
AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, error) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, error)
UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort int) (string, error) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort int) (string, error)
DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string, error) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string, error)
DeleteTCPRuleByServiceIDWithTransaction(sid string, tx *gorm.DB) error
AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct, tx *gorm.DB) error AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct, tx *gorm.DB) error

View File

@ -1728,6 +1728,7 @@ func (s *ServiceAction) TransServieToDelete(serviceID string) error {
db.GetManager().TenantServiceRelationDaoTransactions(tx).DELRelationsByServiceID, db.GetManager().TenantServiceRelationDaoTransactions(tx).DELRelationsByServiceID,
db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).DELServiceLBMappingPortByServiceID, db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).DELServiceLBMappingPortByServiceID,
db.GetManager().TenantServiceVolumeDaoTransactions(tx).DeleteTenantServiceVolumesByServiceID, db.GetManager().TenantServiceVolumeDaoTransactions(tx).DeleteTenantServiceVolumesByServiceID,
db.GetManager().TenantServiceConfigFileDaoTransactions(tx).DelByServiceID,
db.GetManager().ServiceProbeDaoTransactions(tx).DELServiceProbesByServiceID, db.GetManager().ServiceProbeDaoTransactions(tx).DELServiceProbesByServiceID,
db.GetManager().TenantServicePluginRelationDaoTransactions(tx).DeleteALLRelationByServiceID, db.GetManager().TenantServicePluginRelationDaoTransactions(tx).DeleteALLRelationByServiceID,
db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).DeleteAllPluginMappingPortByServiceID, db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).DeleteAllPluginMappingPortByServiceID,
@ -1736,6 +1737,16 @@ func (s *ServiceAction) TransServieToDelete(serviceID string) error {
db.GetManager().TenantServiceLabelDaoTransactions(tx).DeleteLabelByServiceID, db.GetManager().TenantServiceLabelDaoTransactions(tx).DeleteLabelByServiceID,
db.GetManager().HTTPRuleDaoTransactions(tx).DeleteHTTPRuleByServiceID, db.GetManager().HTTPRuleDaoTransactions(tx).DeleteHTTPRuleByServiceID,
db.GetManager().TCPRuleDaoTransactions(tx).DeleteTCPRuleByServiceID, db.GetManager().TCPRuleDaoTransactions(tx).DeleteTCPRuleByServiceID,
db.GetManager().ThirdPartySvcDiscoveryCfgDaoTransactions(tx).DeleteByServiceID,
db.GetManager().EndpointsDaoTransactions(tx).DeleteByServiceID,
}
if err := GetGatewayHandler().DeleteTCPRuleByServiceIDWithTransaction(serviceID, tx); err != nil {
tx.Rollback()
return err
}
if err := GetGatewayHandler().DeleteHTTPRuleByServiceIDWithTransaction(serviceID, tx); err != nil {
tx.Rollback()
return err
} }
for _, del := range deleteServicePropertyFunc { for _, del := range deleteServicePropertyFunc {
if err := del(serviceID); err != nil { if err := del(serviceID); err != nil {

View File

@ -239,6 +239,7 @@ type TenantServiceConfigFileDao interface {
Dao Dao
GetByVolumeName(sid, volumeName string) (*model.TenantServiceConfigFile, error) GetByVolumeName(sid, volumeName string) (*model.TenantServiceConfigFile, error)
DelByVolumeID(sid string, volumeName string) error DelByVolumeID(sid string, volumeName string) error
DelByServiceID(sid string) error
} }
//TenantServiceLBMappingPortDao vs lb mapping port dao //TenantServiceLBMappingPortDao vs lb mapping port dao
@ -394,7 +395,8 @@ type TCPRuleDao interface {
Dao Dao
GetTCPRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.TCPRule, error) GetTCPRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.TCPRule, error)
GetTCPRuleByID(id string) (*model.TCPRule, error) GetTCPRuleByID(id string) (*model.TCPRule, error)
DeleteTCPRule(tcpRule *model.TCPRule) error GetTCPRuleByServiceID(sid string) ([]*model.TCPRule, error)
DeleteByID(uuid string) error
DeleteTCPRuleByServiceID(serviceID string) error DeleteTCPRuleByServiceID(serviceID string) error
ListByServiceID(serviceID string) ([]*model.TCPRule, error) ListByServiceID(serviceID string) ([]*model.TCPRule, error)
} }
@ -421,6 +423,7 @@ type EndpointsDao interface {
DelByUUID(uuid string) error DelByUUID(uuid string) error
List(sid string) ([]*model.Endpoint, error) List(sid string) ([]*model.Endpoint, error)
ListIsOnline(sid string) ([]*model.Endpoint, error) ListIsOnline(sid string) ([]*model.Endpoint, error)
DeleteByServiceID(sid string) error
} }
// ThirdPartySvcDiscoveryCfgDao is an interface for defining method // ThirdPartySvcDiscoveryCfgDao is an interface for defining method
@ -428,6 +431,7 @@ type EndpointsDao interface {
type ThirdPartySvcDiscoveryCfgDao interface { type ThirdPartySvcDiscoveryCfgDao interface {
Dao Dao
GetByServiceID(sid string) (*model.ThirdPartySvcDiscoveryCfg, error) GetByServiceID(sid string) (*model.ThirdPartySvcDiscoveryCfg, error)
DeleteByServiceID(sid string) error
} }
// GwRuleConfigDao is the interface that wraps the required methods to execute // GwRuleConfigDao is the interface that wraps the required methods to execute

View File

@ -96,12 +96,17 @@ func (e *EndpointDaoImpl) ListIsOnline(sid string) ([]*model.Endpoint, error) {
// DelByUUID deletes endpoints matching uuid. // DelByUUID deletes endpoints matching uuid.
func (e *EndpointDaoImpl) DelByUUID(uuid string) error { func (e *EndpointDaoImpl) DelByUUID(uuid string) error {
if err := e.DB.Where("uuid=?", uuid).Delete(model.Endpoint{}).Error; err != nil { if err := e.DB.Where("uuid=?", uuid).Delete(&model.Endpoint{}).Error; err != nil {
return err return err
} }
return nil return nil
} }
// DeleteByServiceID delete endpoints based on service id.
func(e *EndpointDaoImpl) DeleteByServiceID(sid string) error {
return e.DB.Where("service_id=?", sid).Delete(&model.Endpoint{}).Error
}
// ThirdPartySvcDiscoveryCfgDaoImpl implements ThirdPartySvcDiscoveryCfgDao // ThirdPartySvcDiscoveryCfgDaoImpl implements ThirdPartySvcDiscoveryCfgDao
type ThirdPartySvcDiscoveryCfgDaoImpl struct { type ThirdPartySvcDiscoveryCfgDaoImpl struct {
DB *gorm.DB DB *gorm.DB
@ -141,3 +146,8 @@ func (t *ThirdPartySvcDiscoveryCfgDaoImpl) GetByServiceID(sid string) (*model.Th
} }
return &cfg, nil return &cfg, nil
} }
// DeleteByServiceID delete discovery config based on service id.
func(t *ThirdPartySvcDiscoveryCfgDaoImpl) DeleteByServiceID(sid string) error {
return t.DB.Where("service_id=?", sid).Delete(&model.ThirdPartySvcDiscoveryCfg{}).Error
}

View File

@ -283,9 +283,21 @@ func (t *TCPRuleDaoTmpl) GetTCPRuleByID(id string) (*model.TCPRule, error) {
return result, nil return result, nil
} }
// DeleteTCPRule deletes model.TCPRule // GetTCPRuleByServiceID gets a TCPRules based on service id.
func (t *TCPRuleDaoTmpl) DeleteTCPRule(tcpRule *model.TCPRule) error { func (t *TCPRuleDaoTmpl) GetTCPRuleByServiceID(sid string) ([]*model.TCPRule, error) {
return t.DB.Where("uuid = ?", tcpRule.UUID).Delete(tcpRule).Error var result []*model.TCPRule
if err := t.DB.Where("service_id = ?", sid).Find(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
return result, nil
}
// DeleteByID deletes model.TCPRule
func (t *TCPRuleDaoTmpl) DeleteByID(uuid string) error {
return t.DB.Where("uuid = ?", uuid).Delete(&model.TCPRule{}).Error
} }
// DeleteTCPRuleByServiceID deletes model.TCPRule // DeleteTCPRuleByServiceID deletes model.TCPRule

View File

@ -1037,6 +1037,11 @@ func (t *TenantServiceConfigFileDaoImpl) DelByVolumeID(sid, volumeName string) e
return t.DB.Where("service_id=? and volume_name = ?", sid, volumeName).Delete(&cfs).Error return t.DB.Where("service_id=? and volume_name = ?", sid, volumeName).Delete(&cfs).Error
} }
// DelByServiceID deletes config files according to service id.
func (t *TenantServiceConfigFileDaoImpl) DelByServiceID(sid string) error {
return t.DB.Where("service_id=?", sid).Delete(&model.TenantServiceConfigFile{}).Error
}
//TenantServiceLBMappingPortDaoImpl stream服务映射 //TenantServiceLBMappingPortDaoImpl stream服务映射
type TenantServiceLBMappingPortDaoImpl struct { type TenantServiceLBMappingPortDaoImpl struct {
DB *gorm.DB DB *gorm.DB

View File

@ -243,7 +243,6 @@ func (r *RuntimeServer) ListThirdPartyEndpoints(ctx context.Context, re *pb.Serv
} }
var pbeps []*pb.ThirdPartyEndpoint var pbeps []*pb.ThirdPartyEndpoint
exists := make(map[string]bool) exists := make(map[string]bool)
logrus.Debugf("Status from endpoints: %+v", as.GetEndpoints())
for _, ep := range as.GetEndpoints() { for _, ep := range as.GetEndpoints() {
if exists[ep.GetLabels()["uuid"]] { if exists[ep.GetLabels()["uuid"]] {
continue continue