@@ -39,14 +39,13 @@ import (
39
39
"k8s.io/apimachinery/pkg/util/yaml"
40
40
"sigs.k8s.io/controller-runtime/pkg/client"
41
41
v1 "sigs.k8s.io/gateway-api/apis/v1"
42
- configUtils "sigs.k8s.io/gateway-api/conformance/utils/config"
43
42
)
44
43
45
44
// ResourceManager handles creating/updating/deleting Kubernetes resources.
46
45
type ResourceManager struct {
47
46
K8sClient client.Client
48
47
FS embed.FS
49
- TimeoutConfig configUtils. TimeoutConfig
48
+ TimeoutConfig TimeoutConfig
50
49
}
51
50
52
51
// Apply creates or updates Kubernetes resources defined as Go objects.
@@ -55,8 +54,20 @@ func (rm *ResourceManager) Apply(resources []client.Object) error {
55
54
defer cancel ()
56
55
57
56
for _ , resource := range resources {
58
- if err := rm .K8sClient .Create (ctx , resource ); err != nil && ! apierrors .IsAlreadyExists (err ) {
59
- return fmt .Errorf ("error applying resource: %w" , err )
57
+ if err := rm .K8sClient .Get (ctx , client .ObjectKeyFromObject (resource ), resource ); err != nil {
58
+ if ! apierrors .IsNotFound (err ) {
59
+ return fmt .Errorf ("error getting resource: %w" , err )
60
+ }
61
+
62
+ if err := rm .K8sClient .Create (ctx , resource ); err != nil {
63
+ return fmt .Errorf ("error creating resource: %w" , err )
64
+ }
65
+
66
+ continue
67
+ }
68
+
69
+ if err := rm .K8sClient .Update (ctx , resource ); err != nil {
70
+ return fmt .Errorf ("error updating resource: %w" , err )
60
71
}
61
72
}
62
73
@@ -179,6 +190,10 @@ func (rm *ResourceManager) getFileContents(file string) (*bytes.Buffer, error) {
179
190
}
180
191
defer resp .Body .Close ()
181
192
193
+ if resp .StatusCode != http .StatusOK {
194
+ return nil , fmt .Errorf ("%d response when getting %s file contents" , resp .StatusCode , file )
195
+ }
196
+
182
197
manifests := new (bytes.Buffer )
183
198
count , err := manifests .ReadFrom (resp .Body )
184
199
if err != nil {
@@ -203,19 +218,28 @@ func (rm *ResourceManager) getFileContents(file string) (*bytes.Buffer, error) {
203
218
return bytes .NewBuffer (b ), nil
204
219
}
205
220
206
- // WaitForAppsReady waits for all apps in the specified namespace to be ready, or until the ctx timeout is reached.
207
- func (rm * ResourceManager ) WaitForAppsReady (k8sClient client.Client , namespace string ) error {
221
+ // WaitForAppsToBeReady waits for all apps in the specified namespace to be ready,
222
+ // or until the ctx timeout is reached.
223
+
224
+ func (rm * ResourceManager ) WaitForAppsToBeReady (namespace string ) error {
208
225
ctx , cancel := context .WithTimeout (context .Background (), rm .TimeoutConfig .CreateTimeout )
209
226
defer cancel ()
210
227
228
+ if err := rm .waitForPodsToBeReady (ctx , namespace ); err != nil {
229
+ return err
230
+ }
231
+
232
+ return rm .waitForGatewaysToBeReady (ctx , namespace )
233
+ }
234
+
235
+ func (rm * ResourceManager ) waitForPodsToBeReady (ctx context.Context , namespace string ) error {
211
236
return wait .PollUntilContextCancel (
212
237
ctx ,
213
238
500 * time .Millisecond ,
214
239
true , /* poll immediately */
215
240
func (ctx context.Context ) (bool , error ) {
216
- // first check for Pods to be ready
217
241
var podList core.PodList
218
- if err := k8sClient .List (ctx , & podList , client .InNamespace (namespace )); err != nil {
242
+ if err := rm . K8sClient .List (ctx , & podList , client .InNamespace (namespace )); err != nil {
219
243
return false , err
220
244
}
221
245
@@ -228,13 +252,23 @@ func (rm *ResourceManager) WaitForAppsReady(k8sClient client.Client, namespace s
228
252
}
229
253
}
230
254
231
- if podsReady ! = len (podList .Items ) {
232
- return false , nil
255
+ if podsReady = = len (podList .Items ) {
256
+ return true , nil
233
257
}
234
258
235
- // now check for Gateway to be programmed
259
+ return false , nil
260
+ },
261
+ )
262
+ }
263
+
264
+ func (rm * ResourceManager ) waitForGatewaysToBeReady (ctx context.Context , namespace string ) error {
265
+ return wait .PollUntilContextCancel (
266
+ ctx ,
267
+ 500 * time .Millisecond ,
268
+ true , /* poll immediately */
269
+ func (ctx context.Context ) (bool , error ) {
236
270
var gatewayList v1.GatewayList
237
- if err := k8sClient .List (ctx , & gatewayList , client .InNamespace (namespace )); err != nil {
271
+ if err := rm . K8sClient .List (ctx , & gatewayList , client .InNamespace (namespace )); err != nil {
238
272
return false , err
239
273
}
240
274
0 commit comments