Skip to content

Commit def6c84

Browse files
authored
Fix realized state error check (vmware-tanzu#885)
Signed-off-by: Yanjun Zhou <[email protected]>
1 parent 7fa44c2 commit def6c84

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

pkg/nsx/services/realizestate/realize_state.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ type RealizeStateService struct {
1818
common.Service
1919
}
2020

21-
type RealizeError struct {
21+
type RealizeStateError struct {
22+
message string
23+
}
24+
25+
func (e *RealizeStateError) Error() string {
26+
return e.message
27+
}
28+
29+
func NewRealizeStateError(msg string) *RealizeStateError {
30+
return &RealizeStateError{message: msg}
2231
}
2332

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

3039
func IsRealizeStateError(err error) bool {
31-
return err.Error() == model.GenericPolicyRealizedResource_STATE_ERROR
40+
_, ok := err.(*RealizeStateError)
41+
return ok
3242
}
3343

3444
// CheckRealizeState allows the caller to check realize status of entityType with retries.
@@ -55,6 +65,15 @@ func (service *RealizeStateService) CheckRealizeState(backoff wait.Backoff, inte
5565
if *result.State == model.GenericPolicyRealizedResource_STATE_REALIZED {
5666
return nil
5767
}
68+
if *result.State == model.GenericPolicyRealizedResource_STATE_ERROR {
69+
var errMsg []string
70+
for _, alarm := range result.Alarms {
71+
if alarm.Message != nil {
72+
errMsg = append(errMsg, *alarm.Message)
73+
}
74+
}
75+
return NewRealizeStateError(fmt.Sprintf("%s realized with errors: %s", entityType, errMsg))
76+
}
5877
}
5978
return fmt.Errorf("%s not realized", entityType)
6079
})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package realizestate
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/agiledragon/gomonkey/v2"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
10+
"k8s.io/apimachinery/pkg/util/wait"
11+
12+
"github.com/vmware-tanzu/nsx-operator/pkg/config"
13+
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
14+
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
15+
)
16+
17+
type fakeRealizedEntitiesClient struct{}
18+
19+
func (c *fakeRealizedEntitiesClient) List(orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) {
20+
return model.GenericPolicyRealizedResourceListResult{
21+
Results: []model.GenericPolicyRealizedResource{},
22+
}, nil
23+
}
24+
25+
func TestRealizeStateService_CheckRealizeState(t *testing.T) {
26+
commonService := common.Service{
27+
NSXClient: &nsx.Client{
28+
RealizedEntitiesClient: &fakeRealizedEntitiesClient{},
29+
NsxConfig: &config.NSXOperatorConfig{
30+
CoeConfig: &config.CoeConfig{
31+
Cluster: "k8scl-one:test",
32+
},
33+
},
34+
},
35+
NSXConfig: &config.NSXOperatorConfig{
36+
CoeConfig: &config.CoeConfig{
37+
Cluster: "k8scl-one:test",
38+
},
39+
},
40+
}
41+
s := &RealizeStateService{
42+
Service: commonService,
43+
}
44+
patches := gomonkey.ApplyFunc((*fakeRealizedEntitiesClient).List, func(c *fakeRealizedEntitiesClient, orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) {
45+
return model.GenericPolicyRealizedResourceListResult{
46+
Results: []model.GenericPolicyRealizedResource{
47+
{
48+
State: common.String(model.GenericPolicyRealizedResource_STATE_ERROR),
49+
Alarms: []model.PolicyAlarmResource{
50+
{Message: common.String("mocked error")},
51+
},
52+
EntityType: common.String("RealizedLogicalPort"),
53+
},
54+
},
55+
}, nil
56+
})
57+
defer patches.Reset()
58+
59+
backoff := wait.Backoff{
60+
Duration: 1 * time.Second,
61+
Factor: 2.0,
62+
Jitter: 0,
63+
Steps: 6,
64+
}
65+
err := s.CheckRealizeState(backoff, "/orgs/default/projects/default/vpcs/vpc/subnets/subnet/ports/port", "RealizedLogicalPort")
66+
67+
realizeStateError, ok := err.(*RealizeStateError)
68+
assert.True(t, ok)
69+
assert.Equal(t, realizeStateError.Error(), "RealizedLogicalPort realized with errors: [mocked error]")
70+
}

pkg/nsx/services/subnetport/subnetport_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
mock_client "github.com/vmware-tanzu/nsx-operator/pkg/mock/controller-runtime/client"
2222
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
2323
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
24+
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/realizestate"
2425
)
2526

2627
var (
@@ -244,8 +245,9 @@ func TestSubnetPortService_CreateOrUpdateSubnetPort(t *testing.T) {
244245
namespaceCR.UID = "ns1"
245246
return nil
246247
})
248+
247249
patches := gomonkey.ApplyMethodSeq(service.NSXClient.RealizedEntitiesClient, "List", []gomonkey.OutputCell{{
248-
Values: gomonkey.Params{model.GenericPolicyRealizedResourceListResult{}, fmt.Errorf("ERROR")},
250+
Values: gomonkey.Params{model.GenericPolicyRealizedResourceListResult{}, realizestate.NewRealizeStateError("realized state error")},
249251
Times: 1,
250252
}})
251253
return patches

0 commit comments

Comments
 (0)