Skip to content

Commit

Permalink
CHAOSPLT-308: Change DisruptionSpec's Triggers field to a pointer (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptnapoleon authored Jun 27, 2024
1 parent 37e2d48 commit 3c8b208
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
8 changes: 4 additions & 4 deletions api/v1beta1/disruption_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type DisruptionSpec struct {
Unsafemode *UnsafemodeSpec `json:"unsafeMode,omitempty"` // unsafemode spec used to turn off safemode safety nets
StaticTargeting bool `json:"staticTargeting,omitempty"` // enable dynamic targeting and cluster observation
// +nullable
Triggers DisruptionTriggers `json:"triggers,omitempty"` // alter the pre-injection lifecycle
Triggers *DisruptionTriggers `json:"triggers,omitempty"` // alter the pre-injection lifecycle
// +nullable
Pulse *DisruptionPulse `json:"pulse,omitempty"` // enable pulsing diruptions and specify the duration of the active state and the dormant state of the pulsing duration
Duration DisruptionDuration `json:"duration,omitempty"` // time from disruption creation until chaos pods are deleted and no more are created
Expand Down Expand Up @@ -302,7 +302,7 @@ type Disruption struct {
func (r *Disruption) TimeToInject() time.Time {
triggers := r.Spec.Triggers

if triggers.IsZero() {
if triggers == nil || triggers.IsZero() {
return r.CreationTimestamp.Time
}

Expand Down Expand Up @@ -334,7 +334,7 @@ func (r *Disruption) TimeToInject() time.Time {
func (r *Disruption) TimeToCreatePods() time.Time {
triggers := r.Spec.Triggers

if triggers.IsZero() {
if triggers == nil || triggers.IsZero() {
return r.CreationTimestamp.Time
}

Expand Down Expand Up @@ -610,7 +610,7 @@ func (s DisruptionSpec) validateGlobalDisruptionScope() (retErr error) {
}

// Rule: DisruptionTrigger
if !s.Triggers.IsZero() {
if s.Triggers != nil && !s.Triggers.IsZero() {
if !s.Triggers.Inject.IsZero() && !s.Triggers.CreatePods.IsZero() {
if !s.Triggers.Inject.NotBefore.IsZero() && !s.Triggers.CreatePods.NotBefore.IsZero() && s.Triggers.Inject.NotBefore.Before(&s.Triggers.CreatePods.NotBefore) {
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))
Expand Down
18 changes: 9 additions & 9 deletions api/v1beta1/disruption_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ var _ = Describe("Disruption", func() {
builderstest.NewDisruptionBuilder(), defaultCreationTimestamp),
Entry(
"should return triggers.createPods if triggers.inject is nil",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.NewTime(notBeforeTime),
Offset: "",
},
}), notBeforeTime),
Entry(
"should return inject.notBefore if set",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.NewTime(notBeforeTime),
Offset: "",
Expand All @@ -333,30 +333,30 @@ var _ = Describe("Disruption", func() {
}), notBeforeTime),
Entry(
"should return a time after creationTimestamp if inject.offset is set",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.Time{},
Offset: "1m",
},
}), notBeforeTime),
Entry(
"should return creationTimestamp if inject.NotBefore is before creationTimestamp",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.NewTime(defaultCreationTimestamp.Add(-time.Minute)),
},
}), defaultCreationTimestamp),
Entry(
"should return creationTimestamp + 5 minutes if createPods.offset is set",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.Time{},
Offset: "5m",
},
}), defaultCreationTimestamp.Add(time.Minute*5)),
Entry(
"should return creationTimestamp + 5 minutes if createPods.NotBefore is before creationTimestamp",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.NewTime(defaultCreationTimestamp.Add(-time.Minute * 5)),
},
Expand All @@ -376,15 +376,15 @@ var _ = Describe("Disruption", func() {
defaultCreationTimestamp),
Entry(
"should return creationTimestamp if triggers.createPods is nil",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
Inject: DisruptionTrigger{
Offset: "15m",
},
}),
defaultCreationTimestamp),
Entry(
"should return createPods.notBefore if set",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
Inject: DisruptionTrigger{
Offset: "15m",
},
Expand All @@ -396,7 +396,7 @@ var _ = Describe("Disruption", func() {
notBeforeTime),
Entry(
"should return a time after creationTimestamp if createPods.offset is set",
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(DisruptionTriggers{
builderstest.NewDisruptionBuilder().WithDisruptionTriggers(&DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.Time{},
Offset: "5m",
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/disruption_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (r *Disruption) ValidateCreate() (admission.Warnings, error) {
return nil, err
}

if !r.Spec.Triggers.IsZero() {
if r.Spec.Triggers != nil && !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) {
Expand Down
16 changes: 8 additions & 8 deletions api/v1beta1/disruption_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ var _ = Describe("Disruption", func() {
}

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

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

_, err := newDisruption.ValidateUpdate(oldDisruption)
Expect(err).Should(Succeed())
Expand All @@ -128,10 +128,10 @@ var _ = Describe("Disruption", func() {
},
}
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = triggers
newDisruption.Spec.Triggers = &triggers

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

_, err := newDisruption.ValidateUpdate(oldDisruption)
Expect(err).Should(Succeed())
Expand Down Expand Up @@ -419,7 +419,7 @@ var _ = Describe("Disruption", func() {
When("triggers.*.notBefore is in the past", func() {
It("triggers.inject should return an error", func() {
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = DisruptionTriggers{
newDisruption.Spec.Triggers = &DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Minute * 5 * -1)),
},
Expand All @@ -431,7 +431,7 @@ var _ = Describe("Disruption", func() {

It("triggers.createPods should return an error", func() {
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = DisruptionTriggers{
newDisruption.Spec.Triggers = &DisruptionTriggers{
CreatePods: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Hour * -1)),
},
Expand All @@ -446,7 +446,7 @@ var _ = Describe("Disruption", func() {
It("should not return an error", func() {
ddmarkMock.EXPECT().ValidateStructMultierror(mock.Anything, mock.Anything).Return(&multierror.Error{})
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = DisruptionTriggers{
newDisruption.Spec.Triggers = &DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Minute * 5)),
},
Expand All @@ -460,7 +460,7 @@ var _ = Describe("Disruption", func() {
When("triggers.inject.notBefore is before triggers.createPods.notBefore", func() {
It("should return an error", func() {
newDisruption.Spec.Duration = "30m"
newDisruption.Spec.Triggers = DisruptionTriggers{
newDisruption.Spec.Triggers = &DisruptionTriggers{
Inject: DisruptionTrigger{
NotBefore: metav1.NewTime(time.Now().Add(time.Minute * 5)),
},
Expand Down
6 changes: 5 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion builderstest/disruption.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDisruptionBuilder() *DisruptionBuilder {
}

// WithDisruptionTriggers sets the specified triggers of disruption.
func (b *DisruptionBuilder) WithDisruptionTriggers(triggers v1beta1.DisruptionTriggers) *DisruptionBuilder {
func (b *DisruptionBuilder) WithDisruptionTriggers(triggers *v1beta1.DisruptionTriggers) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
Expand Down
2 changes: 1 addition & 1 deletion chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
apiVersion: v2
name: chaos-controller
description: Datadog Chaos Controller chart
version: 2.7.0
version: 3.0.0

0 comments on commit 3c8b208

Please sign in to comment.