@@ -23,6 +23,8 @@ import (
23
23
log "github.com/sirupsen/logrus"
24
24
"golang.org/x/exp/slices"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
+ "k8s.io/apimachinery/pkg/runtime/schema"
26
28
"os"
27
29
"strings"
28
30
"testing"
47
49
}
48
50
)
49
51
52
+ func grabMetrics (shredderMetrics []string , t * testing.T ) []string {
53
+ results := make ([]string , 0 )
54
+ warnings := make ([]string , 0 )
55
+
56
+ for _ , shredderMetric := range shredderMetrics {
57
+ result , warning , err := prometheusQuery (shredderMetric )
58
+ if err != nil {
59
+ t .Errorf ("Error querying Prometheus: %v\n " , err )
60
+ }
61
+ warnings = append (warnings , warning ... )
62
+ results = append (results , result .String ())
63
+ }
64
+
65
+ if len (warnings ) > 0 {
66
+ t .Logf ("Warnings: %v\n " , strings .Join (warnings , "\n " ))
67
+ }
68
+
69
+ t .Logf ("Results: \n %v\n " , strings .Join (results , "\n " ))
70
+
71
+ return results
72
+ }
73
+
74
+ func prometheusQuery (query string ) (model.Value , v1.Warnings , error ) {
75
+
76
+ client , err := api .NewClient (api.Config {
77
+ Address : "http://localhost:30007" ,
78
+ })
79
+ if err != nil {
80
+ fmt .Printf ("Error creating client: %v\n " , err )
81
+ os .Exit (1 )
82
+ }
83
+
84
+ v1api := v1 .NewAPI (client )
85
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
86
+ defer cancel ()
87
+ return v1api .Query (ctx , query , time .Now (), v1 .WithTimeout (5 * time .Second ))
88
+ }
89
+
90
+ func compareTime (expirationTime time.Time , t * testing.T , ch chan time.Time ) {
91
+ currentTime := time .Now ().UTC ()
92
+
93
+ for ! currentTime .After (expirationTime .UTC ()) {
94
+ t .Logf ("Node TTL haven't expired yet: current time(UTC): %s, expire time(UTC): %s" , currentTime , expirationTime .UTC ())
95
+ time .Sleep (10 * time .Second )
96
+ currentTime = time .Now ().UTC ()
97
+
98
+ }
99
+ ch <- currentTime
100
+ }
101
+
50
102
// Validates that k8s-shredder cleanup a parked node after its TTL expires
51
103
func TestNodeIsCleanedUp (t * testing.T ) {
52
104
var err error
@@ -110,18 +162,7 @@ func TestNodeIsCleanedUp(t *testing.T) {
110
162
}
111
163
}
112
164
113
- func compareTime (expirationTime time.Time , t * testing.T , ch chan time.Time ) {
114
- currentTime := time .Now ().UTC ()
115
-
116
- for ! currentTime .After (expirationTime .UTC ()) {
117
- t .Logf ("Node TTL haven't expired yet: current time(UTC): %s, expire time(UTC): %s" , currentTime , expirationTime .UTC ())
118
- time .Sleep (10 * time .Second )
119
- currentTime = time .Now ().UTC ()
120
-
121
- }
122
- ch <- currentTime
123
- }
124
-
165
+ // Validates shredder metrics
125
166
func TestShredderMetrics (t * testing.T ) {
126
167
127
168
// Intentionally skipped the gauge metrics as they are going to be wiped out before every eviction loop
@@ -140,40 +181,43 @@ func TestShredderMetrics(t *testing.T) {
140
181
}
141
182
}
142
183
143
- func grabMetrics (shredderMetrics []string , t * testing.T ) []string {
144
- results := make ([]string , 0 )
145
- warnings := make ([]string , 0 )
184
+ func TestArgoRolloutRestartAt (t * testing.T ) {
185
+ var err error
146
186
147
- for _ , shredderMetric := range shredderMetrics {
148
- result , warning , err := prometheusQuery (shredderMetric )
149
- if err != nil {
150
- t .Errorf ("Error querying Prometheus: %v\n " , err )
151
- }
152
- warnings = append (warnings , warning ... )
153
- results = append (results , result .String ())
154
- }
187
+ appContext , err := utils .NewAppContext (config.Config {
188
+ ParkedNodeTTL : 30 * time .Second ,
189
+ EvictionLoopInterval : 10 * time .Second ,
190
+ RollingRestartThreshold : 0.1 ,
191
+ UpgradeStatusLabel : "shredder.ethos.adobe.net/upgrade-status" ,
192
+ ExpiresOnLabel : "shredder.ethos.adobe.net/parked-node-expires-on" ,
193
+ NamespacePrefixSkipInitialEviction : "" ,
194
+ RestartedAtAnnotation : "shredder.ethos.adobe.net/restartedAt" ,
195
+ AllowEvictionLabel : "shredder.ethos.adobe.net/allow-eviction" ,
196
+ ToBeDeletedTaint : "ToBeDeletedByClusterAutoscaler" ,
197
+ ArgoRolloutsAPIVersion : "v1alpha1" ,
198
+ }, false )
155
199
156
- if len ( warnings ) > 0 {
157
- t . Logf ( "Warnings : %v \n " , strings . Join ( warnings , " \n " ) )
200
+ if err != nil {
201
+ log . Fatalf ( "Failed to setup application context : %s " , err )
158
202
}
159
203
160
- t .Logf ("Results: \n %v\n " , strings .Join (results , "\n " ))
161
-
162
- return results
163
- }
204
+ gvr := schema.GroupVersionResource {
205
+ Group : "argoproj.io" ,
206
+ Version : appContext .Config .ArgoRolloutsAPIVersion ,
207
+ Resource : "rollouts" ,
208
+ }
164
209
165
- func prometheusQuery (query string ) (model.Value , v1.Warnings , error ) {
210
+ rollout , err := appContext .DynamicK8SClient .Resource (gvr ).Namespace ("ns-team-k8s-shredder-test" ).Get (appContext .Context , "test-app-argo-rollout" , metav1.GetOptions {})
211
+ if err != nil {
212
+ log .Fatalf ("Failed to get the Argo Rollout object: %s" , err )
213
+ }
214
+ _ , found , err := unstructured .NestedString (rollout .Object , "spec" , "restartAt" )
166
215
167
- client , err := api .NewClient (api.Config {
168
- Address : "http://localhost:30007" ,
169
- })
170
216
if err != nil {
171
- fmt .Printf ("Error creating client: %v\n " , err )
172
- os .Exit (1 )
217
+ log .Fatalf ("Failed to get the Argo Rollout spec.restartAt field: %s" , err )
173
218
}
174
219
175
- v1api := v1 .NewAPI (client )
176
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
177
- defer cancel ()
178
- return v1api .Query (ctx , query , time .Now (), v1 .WithTimeout (5 * time .Second ))
220
+ if ! found {
221
+ t .Fatalf ("Argo Rollout object does not have the spec.restartAt field set" )
222
+ }
179
223
}
0 commit comments