6
6
package v1beta1
7
7
8
8
import (
9
+ "context"
9
10
"encoding/json"
10
11
"errors"
11
12
"fmt"
@@ -15,6 +16,7 @@ import (
15
16
cLog "github.com/DataDog/chaos-controller/log"
16
17
"github.com/DataDog/chaos-controller/o11y/metrics"
17
18
"github.com/DataDog/chaos-controller/utils"
19
+
18
20
"github.com/robfig/cron"
19
21
"go.uber.org/zap"
20
22
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -121,6 +123,22 @@ func (d *DisruptionCron) ValidateCreate() (_ admission.Warnings, err error) {
121
123
return nil , err
122
124
}
123
125
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
+
124
142
// send informative event to disruption cron to broadcast
125
143
d .emitEvent (EventDisruptionCronCreated )
126
144
0 commit comments