Skip to content

Commit b6ef1c8

Browse files
committed
Collect more info for debugging kubernetes#124136
1 parent 00236ae commit b6ef1c8

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

test/integration/volume/persistent_volumes_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,25 @@ func TestPersistentVolumeProvisionMultiPVCs(t *testing.T) {
10581058
defer testClient.CoreV1().PersistentVolumes().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
10591059
defer testClient.StorageV1().StorageClasses().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
10601060

1061+
// Watch all events in the namespace, and save them to artifacts for debugging.
1062+
// TODO: This is a temporary solution to debug flaky tests `panic: test timed out after 10m0s`.
1063+
// We should remove this once https://github.com/kubernetes/kubernetes/issues/124136 is fixed.
1064+
go func() {
1065+
w, err := testClient.EventsV1().Events(ns.Name).Watch(tCtx, metav1.ListOptions{})
1066+
if err != nil {
1067+
return
1068+
}
1069+
for {
1070+
select {
1071+
case event := <-w.ResultChan():
1072+
reportToArtifacts(t.Name()+"-events.text", event.Object)
1073+
case <-tCtx.Done():
1074+
w.Stop()
1075+
return
1076+
}
1077+
}
1078+
}()
1079+
10611080
storageClass := storage.StorageClass{
10621081
TypeMeta: metav1.TypeMeta{
10631082
Kind: "StorageClass",
@@ -1090,7 +1109,7 @@ func TestPersistentVolumeProvisionMultiPVCs(t *testing.T) {
10901109

10911110
// Wait until the controller provisions and binds all of them
10921111
for i := 0; i < objCount; i++ {
1093-
waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound)
1112+
waitForAnyPersistentVolumeClaimPhaseAndReportIt(t, watchPVC, v1.ClaimBound)
10941113
klog.V(1).Infof("%d claims bound", i+1)
10951114
}
10961115
klog.V(2).Infof("TestPersistentVolumeProvisionMultiPVCs: claims are bound")
@@ -1474,6 +1493,21 @@ func waitForAnyPersistentVolumeClaimPhase(w watch.Interface, phase v1.Persistent
14741493
}
14751494
}
14761495

1496+
func waitForAnyPersistentVolumeClaimPhaseAndReportIt(t *testing.T, w watch.Interface, phase v1.PersistentVolumeClaimPhase) {
1497+
for {
1498+
event := <-w.ResultChan()
1499+
reportToArtifacts(t.Name()+"-watched-pvcs.text", event)
1500+
claim, ok := event.Object.(*v1.PersistentVolumeClaim)
1501+
if !ok {
1502+
continue
1503+
}
1504+
if claim.Status.Phase == phase {
1505+
klog.V(2).Infof("claim %q is %s", claim.Name, phase)
1506+
break
1507+
}
1508+
}
1509+
}
1510+
14771511
func waitForPersistentVolumeClaimStorageClass(t *testing.T, claimName, scName string, w watch.Interface, duration time.Duration) (*v1.PersistentVolumeClaim, bool) {
14781512
stopTimer := time.NewTimer(duration)
14791513
defer stopTimer.Stop()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package volume
18+
19+
import (
20+
"encoding/json"
21+
"os"
22+
"path/filepath"
23+
24+
"k8s.io/klog/v2"
25+
)
26+
27+
func reportToArtifacts(filename string, obj any) {
28+
if os.Getenv("ARTIFACTS") == "" {
29+
return
30+
}
31+
path := filepath.Join(os.Getenv("ARTIFACTS"), filename)
32+
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
33+
if err != nil {
34+
klog.Error("Error opening file:", err)
35+
return
36+
}
37+
defer func() {
38+
if err := file.Close(); err != nil {
39+
klog.Error("Error closing file:", err)
40+
}
41+
}()
42+
43+
content, err := json.Marshal(obj)
44+
if err != nil {
45+
klog.Error("Error marshalling to json:", err)
46+
return
47+
}
48+
content = append(content, '\n')
49+
_, err = file.Write(content)
50+
if err != nil {
51+
klog.Error("Error writing to file:", err)
52+
return
53+
}
54+
}

0 commit comments

Comments
 (0)