Skip to content

Commit 30f137f

Browse files
authored
Fail sync if we have >10 warnings and >10% of actions have resulted in a warning. (#570)
1 parent ef39a62 commit 30f137f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pkg/sync/state.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type State interface {
3636
SetShouldSkipEntitlementsAndGrants()
3737
ShouldSkipGrants() bool
3838
SetShouldSkipGrants()
39+
GetCompletedActionsCount() uint64
3940
}
4041

4142
// ActionOp represents a sync operation.
@@ -156,6 +157,7 @@ type state struct {
156157
shouldFetchRelatedResources bool
157158
shouldSkipEntitlementsAndGrants bool
158159
shouldSkipGrants bool
160+
completedActionsCount uint64
159161
}
160162

161163
// serializedToken is used to serialize the token to JSON. This separate object is used to avoid having exported fields
@@ -169,7 +171,7 @@ type serializedToken struct {
169171
ShouldFetchRelatedResources bool `json:"should_fetch_related_resources,omitempty"`
170172
ShouldSkipEntitlementsAndGrants bool `json:"should_skip_entitlements_and_grants,omitempty"`
171173
ShouldSkipGrants bool `json:"should_skip_grants,omitempty"`
172-
ShouldSkipEntitlements bool `json:"should_skip_entitlements,omitempty"`
174+
CompletedActionsCount uint64 `json:"completed_actions_count,omitempty"`
173175
}
174176

175177
// push adds a new action to the stack. If there is no current state, the action is directly set to current, else
@@ -197,6 +199,7 @@ func (st *state) pop() *Action {
197199
}
198200

199201
ret := *st.currentAction
202+
st.completedActionsCount++
200203

201204
if len(st.actions) > 0 {
202205
st.currentAction = &st.actions[len(st.actions)-1]
@@ -243,10 +246,12 @@ func (st *state) Unmarshal(input string) error {
243246
st.shouldSkipEntitlementsAndGrants = token.ShouldSkipEntitlementsAndGrants
244247
st.shouldSkipGrants = token.ShouldSkipGrants
245248
st.shouldFetchRelatedResources = token.ShouldFetchRelatedResources
249+
st.completedActionsCount = token.CompletedActionsCount
246250
} else {
247251
st.actions = nil
248252
st.entitlementGraph = nil
249253
st.currentAction = &Action{Op: InitOp}
254+
st.completedActionsCount = 0
250255
}
251256

252257
return nil
@@ -266,6 +271,7 @@ func (st *state) Marshal() (string, error) {
266271
ShouldFetchRelatedResources: st.shouldFetchRelatedResources,
267272
ShouldSkipEntitlementsAndGrants: st.shouldSkipEntitlementsAndGrants,
268273
ShouldSkipGrants: st.shouldSkipGrants,
274+
CompletedActionsCount: st.completedActionsCount,
269275
})
270276
if err != nil {
271277
return "", err
@@ -406,3 +412,9 @@ func (st *state) ParentResourceTypeID(ctx context.Context) string {
406412

407413
return c.ParentResourceTypeID
408414
}
415+
416+
func (st *state) GetCompletedActionsCount() uint64 {
417+
st.mtx.RLock()
418+
defer st.mtx.RUnlock()
419+
return st.completedActionsCount
420+
}

pkg/sync/syncer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,12 @@ func (s *syncer) Sync(ctx context.Context) error {
475475
return err
476476
}
477477

478-
// TODO: count actions divided by warnings and error if warning percentage is too high
478+
// If we have more than 10 warnings and more than 10% of actions ended in a warning, exit the sync.
479479
if len(warnings) > 10 {
480-
return fmt.Errorf("too many warnings, exiting sync. warnings: %v", warnings)
480+
completedActionsCount := s.state.GetCompletedActionsCount()
481+
if completedActionsCount > 0 && float64(len(warnings))/float64(completedActionsCount) > 0.1 {
482+
return fmt.Errorf("too many warnings, exiting sync. warnings: %v completed actions: %d", warnings, completedActionsCount)
483+
}
481484
}
482485
select {
483486
case <-runCtx.Done():

0 commit comments

Comments
 (0)