Skip to content

Commit

Permalink
Move expired triggers validation from validateGlobal into just valida… (
Browse files Browse the repository at this point in the history
#841)

* Move expired triggers validation from validateGlobal into just validateCreate

* Add ValidateUpdate unit tests for triggers
  • Loading branch information
ptnapoleon committed Mar 7, 2024
1 parent e7f2698 commit 368d17f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
10 changes: 0 additions & 10 deletions api/v1beta1/disruption_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,16 +613,6 @@ func (s DisruptionSpec) validateGlobalDisruptionScope() (retErr error) {
retErr = multierror.Append(retErr, fmt.Errorf("spec.trigger.inject.notBefore is %s, which is before your spec.trigger.createPods.notBefore of %s. inject.notBefore must come after createPods.notBefore if both are specified", s.Triggers.Inject.NotBefore, s.Triggers.CreatePods.NotBefore))
}
}

now := metav1.Now()

if !s.Triggers.Inject.IsZero() && !s.Triggers.Inject.NotBefore.IsZero() && s.Triggers.Inject.NotBefore.Before(&now) {
retErr = multierror.Append(retErr, fmt.Errorf("spec.trigger.inject.notBefore is %s, which is in the past. only values in the future are accepted", s.Triggers.Inject.NotBefore))
}

if !s.Triggers.CreatePods.IsZero() && !s.Triggers.CreatePods.NotBefore.IsZero() && s.Triggers.CreatePods.NotBefore.Before(&now) {
retErr = multierror.Append(retErr, fmt.Errorf("spec.trigger.createPods.notBefore is %s, which is in the past. only values in the future are accepted", s.Triggers.CreatePods.NotBefore))
}
}

// Rule: pulse compatibility
Expand Down
12 changes: 12 additions & 0 deletions api/v1beta1/disruption_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ func (r *Disruption) ValidateCreate() error {
return err
}

if !r.Spec.Triggers.IsZero() {
now := metav1.Now()

if !r.Spec.Triggers.Inject.IsZero() && !r.Spec.Triggers.Inject.NotBefore.IsZero() && r.Spec.Triggers.Inject.NotBefore.Before(&now) {
return fmt.Errorf("spec.trigger.inject.notBefore is %s, which is in the past. only values in the future are accepted", r.Spec.Triggers.Inject.NotBefore)
}

if !r.Spec.Triggers.CreatePods.IsZero() && !r.Spec.Triggers.CreatePods.NotBefore.IsZero() && r.Spec.Triggers.CreatePods.NotBefore.Before(&now) {
return fmt.Errorf("spec.trigger.createPods.notBefore is %s, which is in the past. only values in the future are accepted", r.Spec.Triggers.CreatePods.NotBefore)
}
}

if maxDuration > 0 && r.Spec.Duration.Duration() > maxDuration {
return fmt.Errorf("you have specified a duration of %s, but the maximum duration allowed is %s, please specify a duration lower or equal than this value", r.Spec.Duration.Duration(), maxDuration)
}
Expand Down
37 changes: 37 additions & 0 deletions api/v1beta1/disruption_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,43 @@ var _ = Describe("Disruption", func() {
})
})

Describe("ValidateCreate-only invariants shouldn't affect Update", func() {
When("triggers.*.notBefore is in the past", func() {
It("triggers.inject should not return an error", func() {
triggers := DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Minute * 5 * -1)),
},
}

newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = triggers

oldDisruption.Spec.Duration = "30m"
oldDisruption.Spec.Triggers = triggers

err := newDisruption.ValidateUpdate(oldDisruption)
Expect(err).Should(Succeed())
})

It("triggers.createPods should not return an error", func() {
triggers := DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Hour * -1)),
},
}
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = triggers

oldDisruption.Spec.Duration = "30m"
oldDisruption.Spec.Triggers = triggers

err := newDisruption.ValidateUpdate(oldDisruption)
Expect(err).Should(Succeed())
})
})
})

Describe("hash changes expectations", func() {
When("nothing is updated", func() {
It("should succeed", func() {
Expand Down

0 comments on commit 368d17f

Please sign in to comment.