2
2
// under the Apache License Version 2.0.
3
3
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4
4
// Copyright 2025 Datadog, Inc.
5
- package controllers
5
+ package v1beta1
6
6
7
7
import (
8
8
"context"
@@ -11,7 +11,6 @@ import (
11
11
"sort"
12
12
"time"
13
13
14
- chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1"
15
14
cLog "github.com/DataDog/chaos-controller/log"
16
15
"go.uber.org/zap"
17
16
appsv1 "k8s.io/api/apps/v1"
@@ -25,14 +24,14 @@ import (
25
24
)
26
25
27
26
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"
30
29
)
31
30
32
31
// GetChildDisruptions retrieves disruptions associated with a resource by its label.
33
32
// 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 {}
36
35
labelSelector := labels .SelectorFromSet (labels.Set {labelKey : labelVal })
37
36
38
37
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
45
44
46
45
// GetTargetResource retrieves the specified target resource (Deployment or StatefulSet).
47
46
// 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 ) {
49
48
var targetObj client.Object
50
49
51
50
switch targetResource .Kind {
@@ -65,7 +64,7 @@ func GetTargetResource(ctx context.Context, cl client.Client, targetResource *ch
65
64
66
65
// CheckTargetResourceExists determines if the target resource exists.
67
66
// 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 ) {
69
68
_ , err := GetTargetResource (ctx , cl , targetResource , namespace )
70
69
71
70
if apierrors .IsNotFound (err ) {
@@ -79,7 +78,7 @@ func CheckTargetResourceExists(ctx context.Context, cl client.Client, targetReso
79
78
80
79
// GetSelectors retrieves the labels of the specified target resource (Deployment or StatefulSet).
81
80
// 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 ) {
83
82
targetObj , err := GetTargetResource (ctx , cl , targetResource , namespace )
84
83
if err != nil {
85
84
return nil , err
@@ -104,10 +103,10 @@ func GetSelectors(ctx context.Context, cl client.Client, targetResource *chaosv1
104
103
105
104
// createBaseDisruption generates a basic Disruption object using the provided owner and disruptionSpec.
106
105
// 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 {
108
107
name := generateDisruptionName (owner )
109
108
110
- return & chaosv1beta1. Disruption {
109
+ return & Disruption {
111
110
ObjectMeta : metav1.ObjectMeta {
112
111
Name : name ,
113
112
Namespace : owner .GetNamespace (),
@@ -121,7 +120,7 @@ func createBaseDisruption(owner metav1.Object, disruptionSpec *chaosv1beta1.Disr
121
120
// setDisruptionAnnotations updates the annotations of a given Disruption object with those of its owner.
122
121
// It sets a scheduled time annotation using the provided scheduledTime.
123
122
// 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 {
125
124
disruption .CopyOwnerAnnotations (owner )
126
125
127
126
disruption .SetScheduledAtAnnotation (scheduledTime )
@@ -131,7 +130,7 @@ func setDisruptionAnnotations(disruption *chaosv1beta1.Disruption, owner metav1.
131
130
132
131
// overwriteDisruptionSelectors updates the selectors of a given Disruption object based on the provided targetResource.
133
132
// 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 {
135
134
// Get selectors from target resource
136
135
selectors , err := GetSelectors (ctx , cl , targetResource , namespace )
137
136
if err != nil {
@@ -156,7 +155,7 @@ func overwriteDisruptionSelectors(ctx context.Context, cl client.Client, disrupt
156
155
// CreateDisruptionFromTemplate constructs a Disruption object based on the provided owner, disruptionSpec, and targetResource.
157
156
// The function sets annotations, overwrites selectors, and associates the Disruption with its owner.
158
157
// 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 ) {
160
159
disruption := createBaseDisruption (owner , disruptionSpec )
161
160
162
161
ownerNameLabel := getOwnerNameLabel (owner )
@@ -178,7 +177,7 @@ func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme
178
177
}
179
178
180
179
// 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 {
182
181
parsedTime , err := disruption .GetScheduledAtAnnotation ()
183
182
if err != nil {
184
183
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
189
188
}
190
189
191
190
// 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 {
193
192
length := len (disruptions .Items )
194
193
if length == 0 {
195
194
return time.Time {}
@@ -209,9 +208,9 @@ func GetMostRecentScheduleTime(log *zap.SugaredLogger, disruptions *chaosv1beta1
209
208
// It returns a formatted string name.
210
209
func generateDisruptionName (owner metav1.Object ) string {
211
210
switch typedOwner := owner .(type ) {
212
- case * chaosv1beta1. DisruptionCron :
211
+ case * DisruptionCron :
213
212
return fmt .Sprintf ("disruption-cron-%s" , typedOwner .GetName ())
214
- case * chaosv1beta1. DisruptionRollout :
213
+ case * DisruptionRollout :
215
214
return fmt .Sprintf ("disruption-rollout-%s" , typedOwner .GetName ())
216
215
}
217
216
@@ -222,9 +221,9 @@ func generateDisruptionName(owner metav1.Object) string {
222
221
// It returns the label string.
223
222
func getOwnerNameLabel (owner metav1.Object ) string {
224
223
switch owner .(type ) {
225
- case * chaosv1beta1. DisruptionCron :
224
+ case * DisruptionCron :
226
225
return DisruptionCronNameLabel
227
- case * chaosv1beta1. DisruptionRollout :
226
+ case * DisruptionRollout :
228
227
return DisruptionRolloutNameLabel
229
228
}
230
229
0 commit comments