Skip to content

Commit

Permalink
Merge pull request #8809 from sbueringer/pr-fixup-get-k-pods
Browse files Browse the repository at this point in the history
🌱 test/e2e: Fixup dump kube-system pods
  • Loading branch information
k8s-ci-robot authored Jun 7, 2023
2 parents 824de64 + 8763299 commit e61a5d1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
6 changes: 4 additions & 2 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
})

// If the cluster still exists, dump kube-system pods in the workload cluster before deleting the cluster.
// If the cluster still exists, dump kube-system pods of the workload cluster before deleting the cluster.
if err := clusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(cluster), &clusterv1.Cluster{}); err == nil {
framework.DumpKubeSystemPods(ctx, framework.DumpKubeSystemPodsInput{
Byf("Dumping kube-system Pods of Cluster %s", klog.KObj(cluster))
framework.DumpKubeSystemPodsForCluster(ctx, framework.DumpKubeSystemPodsForClusterInput{
Lister: clusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name).GetClient(),
Cluster: cluster,
LogPath: filepath.Join(artifactFolder, "clusters", cluster.Name, "resources"),
})
}
Expand Down
62 changes: 25 additions & 37 deletions test/framework/alltypes_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

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

// GetKubeSystemPodsInput is the input for GetKubeSystemPods.
type GetKubeSystemPodsInput struct {
Lister Lister
}

// GetKubeSystemPods reads all pods in the kube-system namespace.
// Note: This function intentionally retrieves Pods as Unstructured, because we need the Pods
// as Unstructured eventually.
func GetKubeSystemPods(ctx context.Context, input GetKubeSystemPodsInput) []*unstructured.Unstructured {
Expect(ctx).NotTo(BeNil(), "ctx is required for GetKubeSystemPods")
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for GetKubeSystemPods")

podList := new(unstructured.UnstructuredList)
podList.SetAPIVersion(corev1.SchemeGroupVersion.String())
podList.SetKind("Pod")
if err := input.Lister.List(ctx, podList, client.InNamespace(metav1.NamespaceSystem)); err != nil {
Fail(fmt.Sprintf("failed to list Pods in kube-system: %v", err))
}

objList := []*unstructured.Unstructured{}
for i := range podList.Items {
obj := podList.Items[i]
objList = append(objList, &obj)
}

return objList
}

// getClusterAPITypes returns the list of TypeMeta to be considered for the move discovery phase.
// This list includes all the types belonging to CAPI providers.
func getClusterAPITypes(ctx context.Context, lister Lister) []metav1.TypeMeta {
Expand Down Expand Up @@ -158,24 +132,38 @@ func DumpAllResources(ctx context.Context, input DumpAllResourcesInput) {
}
}

// DumpKubeSystemPodsInput is the input for DumpKubeSystemPods.
type DumpKubeSystemPodsInput struct {
// DumpKubeSystemPodsForClusterInput is the input for DumpKubeSystemPodsForCluster.
type DumpKubeSystemPodsForClusterInput struct {
Lister Lister
LogPath string
Cluster *clusterv1.Cluster
}

// DumpKubeSystemPods dumps kube-system Pods to YAML.
func DumpKubeSystemPods(ctx context.Context, input DumpKubeSystemPodsInput) {
// DumpKubeSystemPodsForCluster dumps kube-system Pods to YAML.
func DumpKubeSystemPodsForCluster(ctx context.Context, input DumpKubeSystemPodsForClusterInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for DumpAllResources")
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for DumpAllResources")
Expect(input.Cluster).NotTo(BeNil(), "input.Cluster is required for DumpAllResources")

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

for i := range resources {
r := resources[i]
dumpObject(r, input.LogPath)
for i := range podList.Items {
dumpObject(&podList.Items[i], input.LogPath)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (p *clusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace
mp := &machinePools.Items[i]
err := p.logCollector.CollectMachinePoolLog(ctx, p.GetClient(), mp, path.Join(outputPath, "machine-pools", mp.GetName()))
if err != nil {
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
// NB. we are treating failures in collecting logs as a non-blocking operation (best effort)
fmt.Printf("Failed to get logs for MachinePool %s, Cluster %s: %v\n", mp.GetName(), klog.KRef(namespace, name), err)
}
}
Expand Down

0 comments on commit e61a5d1

Please sign in to comment.