Skip to content

Commit

Permalink
Don't attempt re-injection after Disruption Duration has ended (#483)
Browse files Browse the repository at this point in the history
* Dont attempt to reinject after duration has expired

Add PreviouslyInjected to enum validation

* Add a test

* Update controllers/disruption_controller_test.go

Co-authored-by: Joris Bonnefoy <[email protected]>

Co-authored-by: Joris Bonnefoy <[email protected]>
  • Loading branch information
ptnapoleon and Devatoria authored Jan 26, 2022
1 parent 4ff2255 commit 2873ca6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/disruption_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (dd DisruptionDuration) Duration() time.Duration {
type DisruptionStatus struct {
IsStuckOnRemoval bool `json:"isStuckOnRemoval,omitempty"`
IsInjected bool `json:"isInjected,omitempty"`
// +kubebuilder:validation:Enum=NotInjected;PartiallyInjected;Injected
// +ddmark:validation:Enum=NotInjected;PartiallyInjected;Injected
// +kubebuilder:validation:Enum=NotInjected;PartiallyInjected;Injected;PreviouslyInjected
// +ddmark:validation:Enum=NotInjected;PartiallyInjected;Injected;PreviouslyInjected
InjectionStatus chaostypes.DisruptionInjectionStatus `json:"injectionStatus,omitempty"`
// +nullable
Targets []string `json:"targets,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions chart/templates/crds/chaos.datadoghq.com_disruptions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ spec:
- NotInjected
- PartiallyInjected
- Injected
- PreviouslyInjected
type: string
isInjected:
type: boolean
Expand Down
33 changes: 32 additions & 1 deletion controllers/disruption_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,37 @@ func (r *DisruptionReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

return ctrl.Result{Requeue: true}, err
} else if calculateRemainingDuration(*instance) <= 0 {
if _, err := r.updateInjectionStatus(instance); err != nil {
r.log.Errorw("error updating disruption injection status", "error", err)

return ctrl.Result{}, fmt.Errorf("error updating disruption injection status: %w", err)
}

isCleaned, err := r.cleanDisruption(instance)
if err != nil {
return ctrl.Result{}, err
}

if !isCleaned {
requeueAfter := time.Duration(rand.Intn(5)+5) * time.Second //nolint:gosec

r.log.Infow(fmt.Sprintf("disruption has not been fully cleaned yet, re-queuing in %v", requeueAfter))

return ctrl.Result{
Requeue: true,
RequeueAfter: requeueAfter,
}, r.Update(context.Background(), instance)
}

requeueDelay := r.ExpiredDisruptionGCDelay

r.log.Infow("requeuing disruption to check for its expiration", "requeueDelay", requeueDelay.String())

return ctrl.Result{
Requeue: true,
RequeueAfter: requeueDelay,
}, nil
}

// retrieve targets from label selector
Expand Down Expand Up @@ -218,7 +249,7 @@ func (r *DisruptionReconciler) Reconcile(ctx context.Context, req ctrl.Request)
RequeueAfter: requeueAfter,
}, nil
}
requeueDelay := time.Duration(math.Max(calculateRemainingDuration(*instance).Seconds(), r.ExpiredDisruptionGCDelay.Seconds())) * time.Second
requeueDelay := calculateRemainingDuration(*instance)

r.log.Infow("requeuing disruption to check for its expiration", "requeueDelay", requeueDelay.String())

Expand Down
14 changes: 14 additions & 0 deletions controllers/disruption_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ var _ = Describe("Disruption Controller", func() {
By("Waiting for the disruption to expire naturally")
Eventually(func() error { return expectChaosPod(disruption, 0) }, timeout*2).Should(Succeed())

By("Waiting for the disruption to reach PreviouslyInjected")
Eventually(func() error {
if err := k8sClient.Get(context.Background(), instanceKey, disruption); err != nil {
return err
}

// check disruption injection status
if disruption.Status.InjectionStatus != chaostypes.DisruptionInjectionStatusPreviouslyInjected {
return fmt.Errorf("unexpected disruption status, current status is %s (expected PreviouslyInjected)", disruption.Status.InjectionStatus)
}

return nil
}, timeout*2).Should(Succeed())

By("Waiting for disruption to be removed")
Eventually(func() error { return k8sClient.Get(context.Background(), instanceKey, disruption) }, timeout).Should(MatchError("Disruption.chaos.datadoghq.com \"foo\" not found"))
})
Expand Down

0 comments on commit 2873ca6

Please sign in to comment.