Skip to content

Commit 1735aa5

Browse files
authored
Merge pull request kubernetes-sigs#9416 from rvanderp3/ISSUE-9250
🌱 extend test/framework to collect workload cluster nodes
2 parents a151cf9 + edcd705 commit 1735aa5

File tree

3 files changed

+58
-34
lines changed

3 files changed

+58
-34
lines changed

docs/book/src/developer/providers/migrations/v1.5-to-v1.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ maintainers of providers and consumers of our Go API.
3434
### Other
3535
- `clusterctl move` can be blocked temporarily by a provider when an object to be moved is annotated with `clusterctl.cluster.x-k8s.io/block-move`.
3636
- `mdbook releaselink` has been changed to require a `repo` tag when used in markdown files for generating a book with `mdbook`.
37+
- `framework.DumpKubeSystemPodsForCluster` was renamed to `framework.DumpResourcesForCluster` to facilitate the gathering of additional workload cluster resources. Pods in `kube-system` and Nodes are gathered from workload clusters. `kube-system` Pod yaml available in `clusters/*/resources/Pod` and Node yaml is available in `clusters/*/resources/Node`.
3738

3839
### Suggested changes for providers
3940

test/e2e/common.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
. "github.com/onsi/ginkgo/v2"
2626
"github.com/onsi/gomega/types"
2727
corev1 "k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
2830
"k8s.io/klog/v2"
2931
"sigs.k8s.io/controller-runtime/pkg/client"
3032

@@ -79,13 +81,28 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
7981
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
8082
})
8183

82-
// If the cluster still exists, dump kube-system pods of the workload cluster before deleting the cluster.
84+
// If the cluster still exists, dump kube-system pods and nodes of the workload cluster before deleting the cluster.
8385
if err := clusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(cluster), &clusterv1.Cluster{}); err == nil {
84-
Byf("Dumping kube-system Pods of Cluster %s", klog.KObj(cluster))
85-
framework.DumpKubeSystemPodsForCluster(ctx, framework.DumpKubeSystemPodsForClusterInput{
86+
Byf("Dumping kube-system Pods and Nodes of Cluster %s", klog.KObj(cluster))
87+
framework.DumpResourcesForCluster(ctx, framework.DumpResourcesForClusterInput{
8688
Lister: clusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name).GetClient(),
8789
Cluster: cluster,
8890
LogPath: filepath.Join(artifactFolder, "clusters", cluster.Name, "resources"),
91+
Resources: []framework.DumpNamespaceAndGVK{
92+
{
93+
GVK: schema.GroupVersionKind{
94+
Version: corev1.SchemeGroupVersion.Version,
95+
Kind: "Pod",
96+
},
97+
Namespace: metav1.NamespaceSystem,
98+
},
99+
{
100+
GVK: schema.GroupVersionKind{
101+
Version: corev1.SchemeGroupVersion.Version,
102+
Kind: "Node",
103+
},
104+
},
105+
},
89106
})
90107
}
91108

test/framework/alltypes_helpers.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import (
2626

2727
. "github.com/onsi/ginkgo/v2"
2828
. "github.com/onsi/gomega"
29-
corev1 "k8s.io/api/core/v1"
3029
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3130
apierrors "k8s.io/apimachinery/pkg/api/errors"
3231
apimeta "k8s.io/apimachinery/pkg/api/meta"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3534
"k8s.io/apimachinery/pkg/runtime"
35+
"k8s.io/apimachinery/pkg/runtime/schema"
3636
"k8s.io/apimachinery/pkg/util/wait"
3737
"k8s.io/klog/v2"
3838
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -132,38 +132,44 @@ func DumpAllResources(ctx context.Context, input DumpAllResourcesInput) {
132132
}
133133
}
134134

135-
// DumpKubeSystemPodsForClusterInput is the input for DumpKubeSystemPodsForCluster.
136-
type DumpKubeSystemPodsForClusterInput struct {
137-
Lister Lister
138-
LogPath string
139-
Cluster *clusterv1.Cluster
135+
// DumpNamespaceAndGVK specifies a GVK and namespace to be dumped.
136+
type DumpNamespaceAndGVK struct {
137+
GVK schema.GroupVersionKind
138+
Namespace string
140139
}
141140

142-
// DumpKubeSystemPodsForCluster dumps kube-system Pods to YAML.
143-
func DumpKubeSystemPodsForCluster(ctx context.Context, input DumpKubeSystemPodsForClusterInput) {
144-
Expect(ctx).NotTo(BeNil(), "ctx is required for DumpAllResources")
145-
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for DumpAllResources")
146-
Expect(input.Cluster).NotTo(BeNil(), "input.Cluster is required for DumpAllResources")
147-
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
158-
})
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-
}
141+
// DumpResourcesForClusterInput is the input for DumpResourcesForCluster.
142+
type DumpResourcesForClusterInput struct {
143+
Lister Lister
144+
LogPath string
145+
Cluster *clusterv1.Cluster
146+
Resources []DumpNamespaceAndGVK
147+
}
164148

165-
for i := range podList.Items {
166-
dumpObject(&podList.Items[i], input.LogPath)
149+
// DumpResourcesForCluster dumps specified resources to yaml.
150+
func DumpResourcesForCluster(ctx context.Context, input DumpResourcesForClusterInput) {
151+
Expect(ctx).NotTo(BeNil(), "ctx is required for DumpResourcesForCluster")
152+
Expect(input.Lister).NotTo(BeNil(), "input.Lister is required for DumpResourcesForCluster")
153+
Expect(input.Cluster).NotTo(BeNil(), "input.Cluster is required for DumpResourcesForCluster")
154+
155+
for _, resource := range input.Resources {
156+
resourceList := new(unstructured.UnstructuredList)
157+
resourceList.SetGroupVersionKind(resource.GVK)
158+
var listErr error
159+
_ = wait.PollUntilContextTimeout(ctx, retryableOperationInterval, retryableOperationTimeout, true, func(ctx context.Context) (bool, error) {
160+
if listErr = input.Lister.List(ctx, resourceList, client.InNamespace(resource.Namespace)); listErr != nil {
161+
return false, nil //nolint:nilerr
162+
}
163+
return true, nil
164+
})
165+
if listErr != nil {
166+
// NB. we are treating failures in collecting resources as a non-blocking operation (best effort)
167+
fmt.Printf("Failed to list %s for Cluster %s: %v\n", resource.GVK.Kind, klog.KObj(input.Cluster), listErr)
168+
continue
169+
}
170+
for i := range resourceList.Items {
171+
dumpObject(&resourceList.Items[i], input.LogPath)
172+
}
167173
}
168174
}
169175

@@ -178,7 +184,7 @@ func dumpObject(resource runtime.Object, logPath string) {
178184
namespace := metaObj.GetNamespace()
179185
name := metaObj.GetName()
180186

181-
resourceFilePath := filepath.Clean(path.Join(logPath, namespace, kind, name+".yaml"))
187+
resourceFilePath := filepath.Clean(path.Join(logPath, kind, namespace, name+".yaml"))
182188
Expect(os.MkdirAll(filepath.Dir(resourceFilePath), 0750)).To(Succeed(), "Failed to create folder %s", filepath.Dir(resourceFilePath))
183189

184190
f, err := os.OpenFile(resourceFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)

0 commit comments

Comments
 (0)