66package v1beta1
77
88import (
9+ "context"
910 "encoding/json"
1011 "errors"
1112 "fmt"
@@ -15,6 +16,7 @@ import (
1516 cLog "github.com/DataDog/chaos-controller/log"
1617 "github.com/DataDog/chaos-controller/o11y/metrics"
1718 "github.com/DataDog/chaos-controller/utils"
19+
1820 "github.com/robfig/cron"
1921 "go.uber.org/zap"
2022 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -121,6 +123,22 @@ func (d *DisruptionCron) ValidateCreate() (_ admission.Warnings, err error) {
121123 return nil , err
122124 }
123125
126+ var exists bool
127+
128+ // CheckTargetResourceExists doesn't return apierrors.NotFound. Which means if there is an error,
129+ // we could not determine if the target existed, and should allow the Create.
130+ if exists , err = CheckTargetResourceExists (context .Background (), k8sClient , & d .Spec .TargetResource , d .Namespace ); err != nil {
131+ log .Errorw ("error checking if target resource exists" , "error" , err )
132+ } else if ! exists {
133+ log .Warnw ("rejecting disruption cron because target does not exist" ,
134+ "targetName" , d .Spec .TargetResource .Name ,
135+ "targetKind" , d .Spec .TargetResource .Kind ,
136+ "error" , err )
137+
138+ return nil , fmt .Errorf ("rejecting disruption cron because target %s %s/%s does not exist" ,
139+ d .Spec .TargetResource .Kind , d .Namespace , d .Spec .TargetResource .Name )
140+ }
141+
124142 // send informative event to disruption cron to broadcast
125143 d .emitEvent (EventDisruptionCronCreated )
126144
0 commit comments