From 6d5c1eff9c4015c9074aade938775616f4b9cb34 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Mon, 26 Nov 2018 19:21:41 +0800 Subject: [PATCH] [REV] enable empty ingress for outer service --- db/dao/dao.go | 5 ++-- db/mysql/dao/gateway.go | 22 +++------------ worker/appm/conversion/gateway.go | 45 ++++++++++++++++++------------- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/db/dao/dao.go b/db/dao/dao.go index c2f522cb3..225910042 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -355,15 +355,14 @@ type RuleExtensionDao interface { type HttpRuleDao interface { Dao GetHttpRuleByID(id string) (*model.HTTPRule, error) - GetHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.HTTPRule, error) - DeleteHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.HTTPRule, error) + GetHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.HTTPRule, error) DeleteHttpRuleByID(id string) error } // TcpRuleDao - type TcpRuleDao interface { Dao - GetTcpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.TCPRule, error) + GetTcpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) ([]*model.TCPRule, error) GetTcpRuleByID(id string) (*model.TCPRule, error) DeleteTcpRule(tcpRule *model.TCPRule) error } diff --git a/db/mysql/dao/gateway.go b/db/mysql/dao/gateway.go index a79539a5e..5c76b8484 100644 --- a/db/mysql/dao/gateway.go +++ b/db/mysql/dao/gateway.go @@ -163,8 +163,8 @@ func (h *HttpRuleDaoImpl) GetHttpRuleByID(id string) (*model.HTTPRule, error) { // GetHttpRuleByServiceIDAndContainerPort gets a HTTPRule based on serviceID and containerPort func (h *HttpRuleDaoImpl) GetHttpRuleByServiceIDAndContainerPort(serviceID string, - containerPort int) (*model.HTTPRule, error) { - httpRule := &model.HTTPRule{} + containerPort int) ([]*model.HTTPRule, error) { + var httpRule []*model.HTTPRule if err := h.DB.Where("service_id = ? and container_port = ?", serviceID, containerPort).Find(httpRule).Error; err != nil { if err == gorm.ErrRecordNotFound { @@ -175,20 +175,6 @@ func (h *HttpRuleDaoImpl) GetHttpRuleByServiceIDAndContainerPort(serviceID strin return httpRule, nil } -func (h *HttpRuleDaoImpl) DeleteHttpRuleByServiceIDAndContainerPort(serviceID string, - containerPort int) (*model.HTTPRule, error) { - httpRule, err := h.GetHttpRuleByServiceIDAndContainerPort(serviceID, containerPort) - if err != nil { - return nil, err - } - if err := h.DB.Where("service_id = ? and container_port = ?", serviceID, - containerPort).Delete(httpRule).Error; err != nil { - return nil, err - } - - return httpRule, nil -} - func (h *HttpRuleDaoImpl) DeleteHttpRuleByID(id string) error { httpRule := &model.HTTPRule{ UUID: id, @@ -232,8 +218,8 @@ func (t *TcpRuleDaoTmpl) UpdateModel(mo model.Interface) error { // GetTcpRuleByServiceIDAndContainerPort gets a TCPRule based on serviceID and containerPort func (s *TcpRuleDaoTmpl) GetTcpRuleByServiceIDAndContainerPort(serviceID string, - containerPort int) (*model.TCPRule, error) { - result := &model.TCPRule{} + containerPort int) ([]*model.TCPRule, error) { + var result []*model.TCPRule if err := s.DB.Where("service_id = ? and container_port = ?", serviceID, containerPort).Find(result).Error; err != nil { if err == gorm.ErrRecordNotFound { diff --git a/worker/appm/conversion/gateway.go b/worker/appm/conversion/gateway.go index 0f681a742..a94eef235 100644 --- a/worker/appm/conversion/gateway.go +++ b/worker/appm/conversion/gateway.go @@ -157,7 +157,9 @@ func (a *AppServiceBuild) Build() ([]*corev1.Service, []*extensions.Ingress, []* if err != nil { return nil, nil, nil, err } - ingresses = append(ingresses, ings...) + if ings != nil && len(ings) > 0 { + ingresses = append(ingresses, ings...) + } if secret != nil { secrets = append(secrets, secret) } @@ -181,40 +183,45 @@ func (a *AppServiceBuild) Build() ([]*corev1.Service, []*extensions.Ingress, []* // ApplyRules applies http rules and tcp rules func (a AppServiceBuild) ApplyRules(port *model.TenantServicesPort, service *corev1.Service) ([]*extensions.Ingress, *corev1.Secret, error) { - httpRule, err := a.dbmanager.HttpRuleDao().GetHttpRuleByServiceIDAndContainerPort(port.ServiceID, - port.ContainerPort) // TODO: http rule should be more than one + httpRules, err := a.dbmanager.HttpRuleDao().GetHttpRuleByServiceIDAndContainerPort(port.ServiceID, + port.ContainerPort) if err != nil { logrus.Infof("Can't get HTTPRule corresponding to ServiceID(%s): %v", port.ServiceID, err) } - tcpRule, err := a.dbmanager.TcpRuleDao().GetTcpRuleByServiceIDAndContainerPort(port.ServiceID, - port.ContainerPort) // TODO: tcp rule should be more than one + tcpRules, err := a.dbmanager.TcpRuleDao().GetTcpRuleByServiceIDAndContainerPort(port.ServiceID, + port.ContainerPort) if err != nil { logrus.Infof("Can't get TCPRule corresponding to ServiceID(%s): %v", port.ServiceID, err) } - if httpRule == nil && tcpRule == nil { - return nil, nil, fmt.Errorf("Can't find HTTPRule or TCPRule for Outer Service(%s)", port.ServiceID) - } // create ingresses var ingresses []*extensions.Ingress var secret *corev1.Secret // http - if httpRule != nil { - ing, sec, err := a.applyHTTPRule(httpRule, port, service) - if err != nil { - return nil, nil, err + if httpRules != nil && len(httpRules) > 0 { + for _, httpRule := range httpRules { + ing, sec, err := a.applyHTTPRule(httpRule, port, service) + if err != nil { + logrus.Errorf("Unexpected error occurred while applying http rule: %v", err) + // skip the failed rule + continue + } + ingresses = append(ingresses, ing) + secret = sec } - ingresses = append(ingresses, ing) - secret = sec } // tcp - if tcpRule != nil { - ing, err := applyTCPRule(tcpRule, service, a.tenant.UUID) - if err != nil { - return nil, nil, err + if tcpRules != nil && len(tcpRules) > 0 { + for _, tcpRule := range tcpRules { + ing, err := applyTCPRule(tcpRule, service, a.tenant.UUID) + if err != nil { + logrus.Errorf("Unexpected error occurred while applying tcp rule: %v", err) + // skip the failed rule + continue + } + ingresses = append(ingresses, ing) } - ingresses = append(ingresses, ing) } return ingresses, secret, nil