Skip to content

Commit 9bb4d86

Browse files
committed
Move cron_rollout_helpers into api/v1beta1
1 parent d97a45d commit 9bb4d86

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

controllers/cron_rollout_helpers.go renamed to api/v1beta1/cron_rollout_helpers.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// under the Apache License Version 2.0.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2025 Datadog, Inc.
5-
package controllers
5+
package v1beta1
66

77
import (
88
"context"
@@ -11,7 +11,6 @@ import (
1111
"sort"
1212
"time"
1313

14-
chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1"
1514
cLog "github.com/DataDog/chaos-controller/log"
1615
"go.uber.org/zap"
1716
appsv1 "k8s.io/api/apps/v1"
@@ -25,14 +24,14 @@ import (
2524
)
2625

2726
const (
28-
DisruptionCronNameLabel = chaosv1beta1.GroupName + "/disruption-cron-name"
29-
DisruptionRolloutNameLabel = chaosv1beta1.GroupName + "/disruption-rollout-name"
27+
DisruptionCronNameLabel = GroupName + "/disruption-cron-name"
28+
DisruptionRolloutNameLabel = GroupName + "/disruption-rollout-name"
3029
)
3130

3231
// GetChildDisruptions retrieves disruptions associated with a resource by its label.
3332
// Most of the time, this will return an empty list as disruptions are typically short-lived objects.
34-
func GetChildDisruptions(ctx context.Context, cl client.Client, log *zap.SugaredLogger, namespace, labelKey, labelVal string) (*chaosv1beta1.DisruptionList, error) {
35-
disruptions := &chaosv1beta1.DisruptionList{}
33+
func GetChildDisruptions(ctx context.Context, cl client.Client, log *zap.SugaredLogger, namespace, labelKey, labelVal string) (*DisruptionList, error) {
34+
disruptions := &DisruptionList{}
3635
labelSelector := labels.SelectorFromSet(labels.Set{labelKey: labelVal})
3736

3837
if err := cl.List(ctx, disruptions, client.InNamespace(namespace), &client.ListOptions{LabelSelector: labelSelector}); err != nil {
@@ -45,7 +44,7 @@ func GetChildDisruptions(ctx context.Context, cl client.Client, log *zap.Sugared
4544

4645
// GetTargetResource retrieves the specified target resource (Deployment or StatefulSet).
4746
// It returns the target resource object and any error encountered during retrieval.
48-
func GetTargetResource(ctx context.Context, cl client.Client, targetResource *chaosv1beta1.TargetResourceSpec, namespace string) (client.Object, error) {
47+
func GetTargetResource(ctx context.Context, cl client.Client, targetResource *TargetResourceSpec, namespace string) (client.Object, error) {
4948
var targetObj client.Object
5049

5150
switch targetResource.Kind {
@@ -65,7 +64,7 @@ func GetTargetResource(ctx context.Context, cl client.Client, targetResource *ch
6564

6665
// CheckTargetResourceExists determines if the target resource exists.
6766
// Returns a boolean indicating presence and an error if one occurs.
68-
func CheckTargetResourceExists(ctx context.Context, cl client.Client, targetResource *chaosv1beta1.TargetResourceSpec, namespace string) (bool, error) {
67+
func CheckTargetResourceExists(ctx context.Context, cl client.Client, targetResource *TargetResourceSpec, namespace string) (bool, error) {
6968
_, err := GetTargetResource(ctx, cl, targetResource, namespace)
7069

7170
if apierrors.IsNotFound(err) {
@@ -79,7 +78,7 @@ func CheckTargetResourceExists(ctx context.Context, cl client.Client, targetReso
7978

8079
// GetSelectors retrieves the labels of the specified target resource (Deployment or StatefulSet).
8180
// Returns a set of labels to be used as Disruption selectors and an error if retrieval fails.
82-
func GetSelectors(ctx context.Context, cl client.Client, targetResource *chaosv1beta1.TargetResourceSpec, namespace string) (labels *metav1.LabelSelector, err error) {
81+
func GetSelectors(ctx context.Context, cl client.Client, targetResource *TargetResourceSpec, namespace string) (labels *metav1.LabelSelector, err error) {
8382
targetObj, err := GetTargetResource(ctx, cl, targetResource, namespace)
8483
if err != nil {
8584
return nil, err
@@ -104,10 +103,10 @@ func GetSelectors(ctx context.Context, cl client.Client, targetResource *chaosv1
104103

105104
// createBaseDisruption generates a basic Disruption object using the provided owner and disruptionSpec.
106105
// The returned Disruption object has its basic details set, but it's not saved or stored anywhere yet.
107-
func createBaseDisruption(owner metav1.Object, disruptionSpec *chaosv1beta1.DisruptionSpec) *chaosv1beta1.Disruption {
106+
func createBaseDisruption(owner metav1.Object, disruptionSpec *DisruptionSpec) *Disruption {
108107
name := generateDisruptionName(owner)
109108

110-
return &chaosv1beta1.Disruption{
109+
return &Disruption{
111110
ObjectMeta: metav1.ObjectMeta{
112111
Name: name,
113112
Namespace: owner.GetNamespace(),
@@ -121,7 +120,7 @@ func createBaseDisruption(owner metav1.Object, disruptionSpec *chaosv1beta1.Disr
121120
// setDisruptionAnnotations updates the annotations of a given Disruption object with those of its owner.
122121
// It sets a scheduled time annotation using the provided scheduledTime.
123122
// It parses the UserInfo annotation if it exists and sets user-related annotations.
124-
func setDisruptionAnnotations(disruption *chaosv1beta1.Disruption, owner metav1.Object, scheduledTime time.Time) error {
123+
func setDisruptionAnnotations(disruption *Disruption, owner metav1.Object, scheduledTime time.Time) error {
125124
disruption.CopyOwnerAnnotations(owner)
126125

127126
disruption.SetScheduledAtAnnotation(scheduledTime)
@@ -131,7 +130,7 @@ func setDisruptionAnnotations(disruption *chaosv1beta1.Disruption, owner metav1.
131130

132131
// overwriteDisruptionSelectors updates the selectors of a given Disruption object based on the provided targetResource.
133132
// Returns an error if fetching selectors from the target resource fails.
134-
func overwriteDisruptionSelectors(ctx context.Context, cl client.Client, disruption *chaosv1beta1.Disruption, targetResource *chaosv1beta1.TargetResourceSpec, namespace string) error {
133+
func overwriteDisruptionSelectors(ctx context.Context, cl client.Client, disruption *Disruption, targetResource *TargetResourceSpec, namespace string) error {
135134
// Get selectors from target resource
136135
selectors, err := GetSelectors(ctx, cl, targetResource, namespace)
137136
if err != nil {
@@ -156,7 +155,7 @@ func overwriteDisruptionSelectors(ctx context.Context, cl client.Client, disrupt
156155
// CreateDisruptionFromTemplate constructs a Disruption object based on the provided owner, disruptionSpec, and targetResource.
157156
// The function sets annotations, overwrites selectors, and associates the Disruption with its owner.
158157
// It returns the constructed Disruption or an error if any step fails.
159-
func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme *runtime.Scheme, owner metav1.Object, targetResource *chaosv1beta1.TargetResourceSpec, disruptionSpec *chaosv1beta1.DisruptionSpec, scheduledTime time.Time, log *zap.SugaredLogger) (*chaosv1beta1.Disruption, error) {
158+
func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme *runtime.Scheme, owner metav1.Object, targetResource *TargetResourceSpec, disruptionSpec *DisruptionSpec, scheduledTime time.Time, log *zap.SugaredLogger) (*Disruption, error) {
160159
disruption := createBaseDisruption(owner, disruptionSpec)
161160

162161
ownerNameLabel := getOwnerNameLabel(owner)
@@ -178,7 +177,7 @@ func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme
178177
}
179178

180179
// getScheduledTimeForDisruption returns the scheduled time for a particular disruption.
181-
func getScheduledTimeForDisruption(log *zap.SugaredLogger, disruption *chaosv1beta1.Disruption) time.Time {
180+
func getScheduledTimeForDisruption(log *zap.SugaredLogger, disruption *Disruption) time.Time {
182181
parsedTime, err := disruption.GetScheduledAtAnnotation()
183182
if err != nil {
184183
log.Errorw("unable to parse schedule time for child disruption", "err", err, cLog.DisruptionNameKey, disruption.Name)
@@ -189,7 +188,7 @@ func getScheduledTimeForDisruption(log *zap.SugaredLogger, disruption *chaosv1be
189188
}
190189

191190
// GetMostRecentScheduleTime returns the most recent scheduled time from a list of disruptions.
192-
func GetMostRecentScheduleTime(log *zap.SugaredLogger, disruptions *chaosv1beta1.DisruptionList) time.Time {
191+
func GetMostRecentScheduleTime(log *zap.SugaredLogger, disruptions *DisruptionList) time.Time {
193192
length := len(disruptions.Items)
194193
if length == 0 {
195194
return time.Time{}
@@ -209,9 +208,9 @@ func GetMostRecentScheduleTime(log *zap.SugaredLogger, disruptions *chaosv1beta1
209208
// It returns a formatted string name.
210209
func generateDisruptionName(owner metav1.Object) string {
211210
switch typedOwner := owner.(type) {
212-
case *chaosv1beta1.DisruptionCron:
211+
case *DisruptionCron:
213212
return fmt.Sprintf("disruption-cron-%s", typedOwner.GetName())
214-
case *chaosv1beta1.DisruptionRollout:
213+
case *DisruptionRollout:
215214
return fmt.Sprintf("disruption-rollout-%s", typedOwner.GetName())
216215
}
217216

@@ -222,9 +221,9 @@ func generateDisruptionName(owner metav1.Object) string {
222221
// It returns the label string.
223222
func getOwnerNameLabel(owner metav1.Object) string {
224223
switch owner.(type) {
225-
case *chaosv1beta1.DisruptionCron:
224+
case *DisruptionCron:
226225
return DisruptionCronNameLabel
227-
case *chaosv1beta1.DisruptionRollout:
226+
case *DisruptionRollout:
228227
return DisruptionRolloutNameLabel
229228
}
230229

controllers/disruption_cron_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (r *DisruptionCronReconciler) Reconcile(ctx context.Context, req ctrl.Reque
111111
return ctrl.Result{}, nil
112112
}
113113

114-
disruptions, err := GetChildDisruptions(ctx, r.Client, r.log, instance.Namespace, DisruptionCronNameLabel, instance.Name)
114+
disruptions, err := chaosv1beta1.GetChildDisruptions(ctx, r.Client, r.log, instance.Namespace, chaosv1beta1.DisruptionCronNameLabel, instance.Name)
115115
if err != nil {
116116
return ctrl.Result{}, nil
117117
}
@@ -179,7 +179,7 @@ func (r *DisruptionCronReconciler) Reconcile(ctx context.Context, req ctrl.Reque
179179
r.log.Infow("processing current run", "currentRun", missedRun.Format(time.UnixDate))
180180

181181
// Create disruption for current run
182-
disruption, err := CreateDisruptionFromTemplate(ctx, r.Client, r.Scheme, instance, &instance.Spec.TargetResource, &instance.Spec.DisruptionTemplate, missedRun, r.log)
182+
disruption, err := chaosv1beta1.CreateDisruptionFromTemplate(ctx, r.Client, r.Scheme, instance, &instance.Spec.TargetResource, &instance.Spec.DisruptionTemplate, missedRun, r.log)
183183

184184
if err != nil {
185185
r.log.Warnw("unable to construct disruption from template", "err", err)
@@ -230,7 +230,7 @@ func (r *DisruptionCronReconciler) Reconcile(ctx context.Context, req ctrl.Reque
230230
// updateLastScheduleTime updates the LastScheduleTime in the status of a DisruptionCron instance
231231
// based on the most recent schedule time among the given disruptions.
232232
func (r *DisruptionCronReconciler) updateLastScheduleTime(ctx context.Context, instance *chaosv1beta1.DisruptionCron, disruptions *chaosv1beta1.DisruptionList) error {
233-
mostRecentScheduleTime := GetMostRecentScheduleTime(r.log, disruptions) // find the last run so we can update the status
233+
mostRecentScheduleTime := chaosv1beta1.GetMostRecentScheduleTime(r.log, disruptions) // find the last run so we can update the status
234234
if !mostRecentScheduleTime.IsZero() {
235235
instance.Status.LastScheduleTime = &metav1.Time{Time: mostRecentScheduleTime}
236236
return r.Client.Status().Update(ctx, instance)
@@ -246,7 +246,7 @@ func (r *DisruptionCronReconciler) updateLastScheduleTime(ctx context.Context, i
246246
// - error: Represents any error that occurred during the execution of the function.
247247
func (r *DisruptionCronReconciler) updateTargetResourcePreviouslyMissing(ctx context.Context, instance *chaosv1beta1.DisruptionCron) (bool, bool, error) {
248248
disruptionCronDeleted := false
249-
targetResourceExists, err := CheckTargetResourceExists(ctx, r.Client, &instance.Spec.TargetResource, instance.Namespace)
249+
targetResourceExists, err := chaosv1beta1.CheckTargetResourceExists(ctx, r.Client, &instance.Spec.TargetResource, instance.Namespace)
250250

251251
if err != nil {
252252
return targetResourceExists, disruptionCronDeleted, err

controllers/disruption_rollout_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (r *DisruptionRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Re
7777
return ctrl.Result{}, nil
7878
}
7979

80-
disruptions, err := GetChildDisruptions(ctx, r.Client, r.log, instance.Namespace, DisruptionRolloutNameLabel, instance.Name)
80+
disruptions, err := chaosv1beta1.GetChildDisruptions(ctx, r.Client, r.log, instance.Namespace, chaosv1beta1.DisruptionRolloutNameLabel, instance.Name)
8181
if err != nil {
8282
return ctrl.Result{}, nil
8383
}
@@ -138,7 +138,7 @@ func (r *DisruptionRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Re
138138

139139
// Create disruption
140140
scheduledTime := time.Now()
141-
disruption, err := CreateDisruptionFromTemplate(ctx, r.Client, r.Scheme, instance, &instance.Spec.TargetResource, &instance.Spec.DisruptionTemplate, scheduledTime, r.log)
141+
disruption, err := chaosv1beta1.CreateDisruptionFromTemplate(ctx, r.Client, r.Scheme, instance, &instance.Spec.TargetResource, &instance.Spec.DisruptionTemplate, scheduledTime, r.log)
142142

143143
if err != nil {
144144
r.log.Warnw("unable to construct disruption from template", "err", err)
@@ -173,7 +173,7 @@ func (r *DisruptionRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Re
173173
// updateLastScheduleTime updates the LastScheduleTime in the status of a DisruptionRollout instance
174174
// based on the most recent schedule time among the given disruptions.
175175
func (r *DisruptionRolloutReconciler) updateLastScheduleTime(ctx context.Context, instance *chaosv1beta1.DisruptionRollout, disruptions *chaosv1beta1.DisruptionList) error {
176-
mostRecentScheduleTime := GetMostRecentScheduleTime(r.log, disruptions) // find the last run so we can update the status
176+
mostRecentScheduleTime := chaosv1beta1.GetMostRecentScheduleTime(r.log, disruptions) // find the last run so we can update the status
177177
if !mostRecentScheduleTime.IsZero() {
178178
instance.Status.LastScheduleTime = &metav1.Time{Time: mostRecentScheduleTime}
179179
return r.Client.Status().Update(ctx, instance)
@@ -189,7 +189,7 @@ func (r *DisruptionRolloutReconciler) updateLastScheduleTime(ctx context.Context
189189
// - error: Represents any error that occurred during the execution of the function.
190190
func (r *DisruptionRolloutReconciler) updateTargetResourcePreviouslyMissing(ctx context.Context, instance *chaosv1beta1.DisruptionRollout) (bool, bool, error) {
191191
disruptionRolloutDeleted := false
192-
targetResourceExists, err := CheckTargetResourceExists(ctx, r.Client, &instance.Spec.TargetResource, instance.Namespace)
192+
targetResourceExists, err := chaosv1beta1.CheckTargetResourceExists(ctx, r.Client, &instance.Spec.TargetResource, instance.Namespace)
193193

194194
if err != nil {
195195
return targetResourceExists, disruptionRolloutDeleted, err

0 commit comments

Comments
 (0)