Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ut #4789

Closed
wants to merge 4 commits into from
Closed

fix ut #4789

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions mocks/pkg/ovs/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,19 +666,18 @@ func (c *Controller) checkGatewayReady() error {

func (c *Controller) cleanDuplicatedChassis(node *v1.Node) error {
// if multi chassis has the same node name, delete all of them
chassises, err := c.OVNSbClient.GetAllChassisByHost(node.Name)
if err != nil {
klog.Errorf("failed to list chassis %v", err)
return err
var err error
if _, err := c.OVNSbClient.GetChassisByHost(node.Name); err == nil {
return nil
}
if len(*chassises) > 1 {
klog.Warningf("node %s has multiple chassis", node.Name)
klog.Errorf("failed to get chassis for node %s: %v", node.Name, err)
if errors.Is(err, ovs.ErrOneNodeMultiChassis) {
if err := c.OVNSbClient.DeleteChassisByHost(node.Name); err != nil {
klog.Errorf("failed to delete chassis for node %s: %v", node.Name, err)
return err
}
}
return nil
return err
}

func (c *Controller) retryDelDupChassis(attempts, sleep int, f func(node *v1.Node) error, node *v1.Node) (err error) {
Expand Down
1 change: 0 additions & 1 deletion pkg/ovs/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ type Common interface {
type Chassis interface {
DeleteChassis(chassisName string) error
DeleteChassisByHost(node string) error
GetAllChassisByHost(nodeName string) (*[]ovnsb.Chassis, error)
GetChassisByHost(nodeName string) (*ovnsb.Chassis, error)
GetChassis(chassisName string, ignoreNotFound bool) (*ovnsb.Chassis, error)
GetKubeOvnChassisses() (*[]ovnsb.Chassis, error)
Expand Down
24 changes: 20 additions & 4 deletions pkg/ovs/ovn-nb-suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type OvnClientTestSuite struct {
ovnSBClient *OVNSbClient

failedOvnNBClient *OVNNbClient
failedOvnSBClient *OVNSbClient
ovnLegacyClient *LegacyClient

ovsSocket string
Expand All @@ -40,6 +41,10 @@ func emptyNbDatabaseModel() (model.ClientDBModel, error) {
return model.NewClientDBModel("OVN_Northbound", map[string]model.Model{})
}

func emptySbDatabaseModel() (model.ClientDBModel, error) {
return model.NewClientDBModel("OVN_Southbound", map[string]model.Model{})
}

func (suite *OvnClientTestSuite) SetupSuite() {
fmt.Println("set up ovn client test suite")
// setup ovn nb client schema
Expand Down Expand Up @@ -73,6 +78,21 @@ func (suite *OvnClientTestSuite) SetupSuite() {

// setup ovn sb client
sbClientSchema := ovnsb.Schema()

// setup failed case ovn sb client
emptySbDBModel, err := emptySbDatabaseModel()
require.NoError(suite.T(), err)

fakeSBServer, sbSock1 := newOVSDBServer(suite.T(), "fake-sb", emptySbDBModel, sbClientSchema)
sbEndpoint1 := fmt.Sprintf("unix:%s", sbSock1)
require.FileExists(suite.T(), sbSock1)
failedOvnSBClient, err := newOvnSbClient(suite.T(), sbEndpoint1, 10)
require.NoError(suite.T(), err)
suite.failedOvnSBClient = failedOvnSBClient
// close the server to simulate the failed case
fakeSBServer.Close()
require.NoFileExists(suite.T(), sbSock1)

sbClientDBModel, err := ovnsb.FullDatabaseModel()
require.NoError(suite.T(), err)

Expand Down Expand Up @@ -1024,10 +1044,6 @@ func (suite *OvnClientTestSuite) Test_ListChassis() {
suite.testListChassis()
}

func (suite *OvnClientTestSuite) Test_GetAllChassisByHost() {
suite.testGetAllChassisByHost()
}

func (suite *OvnClientTestSuite) Test_GetChassisByHost() {
suite.testGetChassisByHost()
}
Expand Down
28 changes: 3 additions & 25 deletions pkg/ovs/ovn-sb-chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
)

var ErrOneNodeMultiChassis = errors.New("OneNodeMultiChassis")

func (c *OVNSbClient) UpdateChassis(chassis *ovnsb.Chassis, fields ...interface{}) error {
op, err := c.ovsDbClient.Where(chassis).Update(chassis, fields...)
if err != nil {
Expand Down Expand Up @@ -83,30 +85,6 @@ func (c *OVNSbClient) ListChassis() (*[]ovnsb.Chassis, error) {
return &css, nil
}

func (c *OVNSbClient) GetAllChassisByHost(nodeName string) (*[]ovnsb.Chassis, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

chassisList := make([]ovnsb.Chassis, 0)
if err := c.ovsDbClient.WhereCache(func(chassis *ovnsb.Chassis) bool {
return chassis.Hostname == nodeName
}).List(ctx, &chassisList); err != nil {
klog.Error(err)
return nil, fmt.Errorf("failed to list Chassis with host name=%s: %w", nodeName, err)
}
if len(chassisList) == 0 {
err := fmt.Errorf("failed to get Chassis with with host name=%s", nodeName)
klog.Error(err)
return nil, err
}
if len(chassisList) != 1 {
err := fmt.Errorf("found more than one Chassis with with host name=%s", nodeName)
klog.Error(err)
return nil, err
}
return &chassisList, nil
}

func (c *OVNSbClient) GetChassisByHost(nodeName string) (*ovnsb.Chassis, error) {
if nodeName == "" {
err := errors.New("failed to get Chassis with empty hostname")
Expand All @@ -131,7 +109,7 @@ func (c *OVNSbClient) GetChassisByHost(nodeName string) (*ovnsb.Chassis, error)
if len(chassisList) != 1 {
err := fmt.Errorf("found more than one Chassis with host name=%s", nodeName)
klog.Error(err)
return nil, err
return nil, ErrOneNodeMultiChassis
}

// #nosec G602
Expand Down
Loading
Loading