Skip to content

Commit

Permalink
Fix realized state error check (vmware-tanzu#885)
Browse files Browse the repository at this point in the history
Signed-off-by: Yanjun Zhou <[email protected]>
  • Loading branch information
yanjunz97 authored Nov 18, 2024
1 parent 7fa44c2 commit def6c84
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
23 changes: 21 additions & 2 deletions pkg/nsx/services/realizestate/realize_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ type RealizeStateService struct {
common.Service
}

type RealizeError struct {
type RealizeStateError struct {
message string
}

func (e *RealizeStateError) Error() string {
return e.message
}

func NewRealizeStateError(msg string) *RealizeStateError {
return &RealizeStateError{message: msg}
}

func InitializeRealizeState(service common.Service) *RealizeStateService {
Expand All @@ -28,7 +37,8 @@ func InitializeRealizeState(service common.Service) *RealizeStateService {
}

func IsRealizeStateError(err error) bool {
return err.Error() == model.GenericPolicyRealizedResource_STATE_ERROR
_, ok := err.(*RealizeStateError)
return ok
}

// CheckRealizeState allows the caller to check realize status of entityType with retries.
Expand All @@ -55,6 +65,15 @@ func (service *RealizeStateService) CheckRealizeState(backoff wait.Backoff, inte
if *result.State == model.GenericPolicyRealizedResource_STATE_REALIZED {
return nil
}
if *result.State == model.GenericPolicyRealizedResource_STATE_ERROR {
var errMsg []string
for _, alarm := range result.Alarms {
if alarm.Message != nil {
errMsg = append(errMsg, *alarm.Message)
}
}
return NewRealizeStateError(fmt.Sprintf("%s realized with errors: %s", entityType, errMsg))
}
}
return fmt.Errorf("%s not realized", entityType)
})
Expand Down
70 changes: 70 additions & 0 deletions pkg/nsx/services/realizestate/realize_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package realizestate

import (
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)

type fakeRealizedEntitiesClient struct{}

func (c *fakeRealizedEntitiesClient) List(orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) {
return model.GenericPolicyRealizedResourceListResult{
Results: []model.GenericPolicyRealizedResource{},
}, nil
}

func TestRealizeStateService_CheckRealizeState(t *testing.T) {
commonService := common.Service{
NSXClient: &nsx.Client{
RealizedEntitiesClient: &fakeRealizedEntitiesClient{},
NsxConfig: &config.NSXOperatorConfig{
CoeConfig: &config.CoeConfig{
Cluster: "k8scl-one:test",
},
},
},
NSXConfig: &config.NSXOperatorConfig{
CoeConfig: &config.CoeConfig{
Cluster: "k8scl-one:test",
},
},
}
s := &RealizeStateService{
Service: commonService,
}
patches := gomonkey.ApplyFunc((*fakeRealizedEntitiesClient).List, func(c *fakeRealizedEntitiesClient, orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) {
return model.GenericPolicyRealizedResourceListResult{
Results: []model.GenericPolicyRealizedResource{
{
State: common.String(model.GenericPolicyRealizedResource_STATE_ERROR),
Alarms: []model.PolicyAlarmResource{
{Message: common.String("mocked error")},
},
EntityType: common.String("RealizedLogicalPort"),
},
},
}, nil
})
defer patches.Reset()

backoff := wait.Backoff{
Duration: 1 * time.Second,
Factor: 2.0,
Jitter: 0,
Steps: 6,
}
err := s.CheckRealizeState(backoff, "/orgs/default/projects/default/vpcs/vpc/subnets/subnet/ports/port", "RealizedLogicalPort")

realizeStateError, ok := err.(*RealizeStateError)
assert.True(t, ok)
assert.Equal(t, realizeStateError.Error(), "RealizedLogicalPort realized with errors: [mocked error]")
}
4 changes: 3 additions & 1 deletion pkg/nsx/services/subnetport/subnetport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
mock_client "github.com/vmware-tanzu/nsx-operator/pkg/mock/controller-runtime/client"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/realizestate"
)

var (
Expand Down Expand Up @@ -244,8 +245,9 @@ func TestSubnetPortService_CreateOrUpdateSubnetPort(t *testing.T) {
namespaceCR.UID = "ns1"
return nil
})

patches := gomonkey.ApplyMethodSeq(service.NSXClient.RealizedEntitiesClient, "List", []gomonkey.OutputCell{{
Values: gomonkey.Params{model.GenericPolicyRealizedResourceListResult{}, fmt.Errorf("ERROR")},
Values: gomonkey.Params{model.GenericPolicyRealizedResourceListResult{}, realizestate.NewRealizeStateError("realized state error")},
Times: 1,
}})
return patches
Expand Down

0 comments on commit def6c84

Please sign in to comment.