Skip to content

Commit e61a5d1

Browse files
authored
Merge pull request #8809 from sbueringer/pr-fixup-get-k-pods
🌱 test/e2e: Fixup dump kube-system pods
2 parents 824de64 + 8763299 commit e61a5d1

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

test/e2e/common.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
7979
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
8080
})
8181

82-
// If the cluster still exists, dump kube-system pods in the workload cluster before deleting the cluster.
82+
// If the cluster still exists, dump kube-system pods of the workload cluster before deleting the cluster.
8383
if err := clusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(cluster), &clusterv1.Cluster{}); err == nil {
84-
framework.DumpKubeSystemPods(ctx, framework.DumpKubeSystemPodsInput{
84+
Byf("Dumping kube-system Pods of Cluster %s", klog.KObj(cluster))
85+
framework.DumpKubeSystemPodsForCluster(ctx, framework.DumpKubeSystemPodsForClusterInput{
8586
Lister: clusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name).GetClient(),
87+
Cluster: cluster,
8688
LogPath: filepath.Join(artifactFolder, "clusters", cluster.Name, "resources"),
8789
})
8890
}

test/framework/alltypes_helpers.go

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3535
"k8s.io/apimachinery/pkg/runtime"
36+
"k8s.io/apimachinery/pkg/util/wait"
37+
"k8s.io/klog/v2"
3638
"sigs.k8s.io/controller-runtime/pkg/client"
3739
"sigs.k8s.io/yaml"
3840

@@ -77,34 +79,6 @@ func GetCAPIResources(ctx context.Context, input GetCAPIResourcesInput) []*unstr
7779
return objList
7880
}
7981

80-
// GetKubeSystemPodsInput is the input for GetKubeSystemPods.
81-
type GetKubeSystemPodsInput struct {
82-
Lister Lister
83-
}
84-
85-
// GetKubeSystemPods reads all pods in the kube-system namespace.
86-
// Note: This function intentionally retrieves Pods as Unstructured, because we need the Pods
87-
// as Unstructured eventually.
88-
func GetKubeSystemPods(ctx context.Context, input GetKubeSystemPodsInput) []*unstructured.Unstructured {
89-
Expect(ctx).NotTo(BeNil(), "ctx is required for GetKubeSystemPods")
90-
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for GetKubeSystemPods")
91-
92-
podList := new(unstructured.UnstructuredList)
93-
podList.SetAPIVersion(corev1.SchemeGroupVersion.String())
94-
podList.SetKind("Pod")
95-
if err := input.Lister.List(ctx, podList, client.InNamespace(metav1.NamespaceSystem)); err != nil {
96-
Fail(fmt.Sprintf("failed to list Pods in kube-system: %v", err))
97-
}
98-
99-
objList := []*unstructured.Unstructured{}
100-
for i := range podList.Items {
101-
obj := podList.Items[i]
102-
objList = append(objList, &obj)
103-
}
104-
105-
return objList
106-
}
107-
10882
// getClusterAPITypes returns the list of TypeMeta to be considered for the move discovery phase.
10983
// This list includes all the types belonging to CAPI providers.
11084
func getClusterAPITypes(ctx context.Context, lister Lister) []metav1.TypeMeta {
@@ -158,24 +132,38 @@ func DumpAllResources(ctx context.Context, input DumpAllResourcesInput) {
158132
}
159133
}
160134

161-
// DumpKubeSystemPodsInput is the input for DumpKubeSystemPods.
162-
type DumpKubeSystemPodsInput struct {
135+
// DumpKubeSystemPodsForClusterInput is the input for DumpKubeSystemPodsForCluster.
136+
type DumpKubeSystemPodsForClusterInput struct {
163137
Lister Lister
164138
LogPath string
139+
Cluster *clusterv1.Cluster
165140
}
166141

167-
// DumpKubeSystemPods dumps kube-system Pods to YAML.
168-
func DumpKubeSystemPods(ctx context.Context, input DumpKubeSystemPodsInput) {
142+
// DumpKubeSystemPodsForCluster dumps kube-system Pods to YAML.
143+
func DumpKubeSystemPodsForCluster(ctx context.Context, input DumpKubeSystemPodsForClusterInput) {
169144
Expect(ctx).NotTo(BeNil(), "ctx is required for DumpAllResources")
170145
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for DumpAllResources")
146+
Expect(input.Cluster).NotTo(BeNil(), "input.Cluster is required for DumpAllResources")
171147

172-
resources := GetKubeSystemPods(ctx, GetKubeSystemPodsInput{
173-
Lister: input.Lister,
148+
// Note: We intentionally retrieve Pods as Unstructured because we need the Pods as Unstructured for dumpObject.
149+
podList := new(unstructured.UnstructuredList)
150+
podList.SetAPIVersion(corev1.SchemeGroupVersion.String())
151+
podList.SetKind("Pod")
152+
var listErr error
153+
_ = wait.PollUntilContextTimeout(ctx, retryableOperationInterval, retryableOperationTimeout, true, func(ctx context.Context) (bool, error) {
154+
if listErr = input.Lister.List(ctx, podList, client.InNamespace(metav1.NamespaceSystem)); listErr != nil {
155+
return false, nil //nolint:nilerr
156+
}
157+
return true, nil
174158
})
159+
if listErr != nil {
160+
// NB. we are treating failures in collecting kube-system pods as a non-blocking operation (best effort)
161+
fmt.Printf("Failed to list Pods in kube-system for Cluster %s: %v\n", klog.KObj(input.Cluster), listErr)
162+
return
163+
}
175164

176-
for i := range resources {
177-
r := resources[i]
178-
dumpObject(r, input.LogPath)
165+
for i := range podList.Items {
166+
dumpObject(&podList.Items[i], input.LogPath)
179167
}
180168
}
181169

test/framework/cluster_proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func (p *clusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace
315315
mp := &machinePools.Items[i]
316316
err := p.logCollector.CollectMachinePoolLog(ctx, p.GetClient(), mp, path.Join(outputPath, "machine-pools", mp.GetName()))
317317
if err != nil {
318-
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
318+
// NB. we are treating failures in collecting logs as a non-blocking operation (best effort)
319319
fmt.Printf("Failed to get logs for MachinePool %s, Cluster %s: %v\n", mp.GetName(), klog.KRef(namespace, name), err)
320320
}
321321
}

0 commit comments

Comments
 (0)