Skip to content

Commit ca989ce

Browse files
committed
Block DisruptionCrons from being created if the target does not exist
1 parent 9bb4d86 commit ca989ce

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

api/v1beta1/disruption_cron_webhook.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package v1beta1
77

88
import (
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

Comments
 (0)