Skip to content

Commit 2338818

Browse files
committed
oc adm must-gather: Peform cleanup on signal
The command now removes any temporary resources on signal received, i.e. the whole temp namespace generally, or just the pods in case --run-namespace is specified.
1 parent 0c158fb commit 2338818

File tree

12 files changed

+258
-185
lines changed

12 files changed

+258
-185
lines changed

pkg/cli/admin/inspect/admission_webhooks.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package inspect
22

33
import (
4+
"context"
45
"fmt"
56
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
67
"k8s.io/apimachinery/pkg/util/errors"
@@ -20,41 +21,41 @@ func (c *mutatingWebhookConfigList) addItem(obj interface{}) error {
2021
return nil
2122
}
2223

23-
func gatherMutatingAdmissionWebhook(context *resourceContext, info *resource.Info, o *InspectOptions) error {
24+
func gatherMutatingAdmissionWebhook(ctx context.Context, rCtx *resourceContext, info *resource.Info, o *InspectOptions) error {
2425
structuredObj, err := toStructuredObject[admissionregistrationv1.MutatingWebhookConfiguration, admissionregistrationv1.MutatingWebhookConfigurationList](info.Object)
2526
if err != nil {
26-
return gatherGenericObject(context, info, o)
27+
return gatherGenericObject(ctx, rCtx, info, o)
2728
}
2829

2930
errs := []error{}
3031
switch castObj := structuredObj.(type) {
3132
case *admissionregistrationv1.MutatingWebhookConfiguration:
32-
if err := gatherMutatingAdmissionWebhookRelated(context, o, castObj); err != nil {
33+
if err := gatherMutatingAdmissionWebhookRelated(ctx, rCtx, o, castObj); err != nil {
3334
errs = append(errs, err)
3435
}
3536

3637
case *admissionregistrationv1.MutatingWebhookConfigurationList:
3738
for _, webhook := range castObj.Items {
38-
if err := gatherMutatingAdmissionWebhookRelated(context, o, &webhook); err != nil {
39+
if err := gatherMutatingAdmissionWebhookRelated(ctx, rCtx, o, &webhook); err != nil {
3940
errs = append(errs, err)
4041
}
4142
}
4243

4344
}
4445

45-
if err := gatherGenericObject(context, info, o); err != nil {
46+
if err := gatherGenericObject(ctx, rCtx, info, o); err != nil {
4647
errs = append(errs, err)
4748
}
4849
return errors.NewAggregate(errs)
4950
}
5051

51-
func gatherMutatingAdmissionWebhookRelated(context *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.MutatingWebhookConfiguration) error {
52+
func gatherMutatingAdmissionWebhookRelated(ctx context.Context, rCtx *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.MutatingWebhookConfiguration) error {
5253
errs := []error{}
5354
for _, webhook := range webhookConfig.Webhooks {
5455
if webhook.ClientConfig.Service == nil {
5556
continue
5657
}
57-
if err := gatherNamespaces(context, o, webhook.ClientConfig.Service.Namespace); err != nil {
58+
if err := gatherNamespaces(ctx, rCtx, o, webhook.ClientConfig.Service.Namespace); err != nil {
5859
errs = append(errs, err)
5960
}
6061
}
@@ -75,41 +76,41 @@ func (c *validatingWebhookConfigList) addItem(obj interface{}) error {
7576
return nil
7677
}
7778

78-
func gatherValidatingAdmissionWebhook(context *resourceContext, info *resource.Info, o *InspectOptions) error {
79+
func gatherValidatingAdmissionWebhook(ctx context.Context, rCtx *resourceContext, info *resource.Info, o *InspectOptions) error {
7980
structuredObj, err := toStructuredObject[admissionregistrationv1.ValidatingWebhookConfiguration, admissionregistrationv1.ValidatingWebhookConfigurationList](info.Object)
8081
if err != nil {
81-
return gatherGenericObject(context, info, o)
82+
return gatherGenericObject(ctx, rCtx, info, o)
8283
}
8384

8485
errs := []error{}
8586
switch castObj := structuredObj.(type) {
8687
case *admissionregistrationv1.ValidatingWebhookConfiguration:
87-
if err := gatherValidatingAdmissionWebhookRelated(context, o, castObj); err != nil {
88+
if err := gatherValidatingAdmissionWebhookRelated(ctx, rCtx, o, castObj); err != nil {
8889
errs = append(errs, err)
8990
}
9091

9192
case *admissionregistrationv1.ValidatingWebhookConfigurationList:
9293
for _, webhook := range castObj.Items {
93-
if err := gatherValidatingAdmissionWebhookRelated(context, o, &webhook); err != nil {
94+
if err := gatherValidatingAdmissionWebhookRelated(ctx, rCtx, o, &webhook); err != nil {
9495
errs = append(errs, err)
9596
}
9697
}
9798

9899
}
99100

100-
if err := gatherGenericObject(context, info, o); err != nil {
101+
if err := gatherGenericObject(ctx, rCtx, info, o); err != nil {
101102
errs = append(errs, err)
102103
}
103104
return errors.NewAggregate(errs)
104105
}
105106

106-
func gatherValidatingAdmissionWebhookRelated(context *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.ValidatingWebhookConfiguration) error {
107+
func gatherValidatingAdmissionWebhookRelated(ctx context.Context, rCtx *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.ValidatingWebhookConfiguration) error {
107108
errs := []error{}
108109
for _, webhook := range webhookConfig.Webhooks {
109110
if webhook.ClientConfig.Service == nil {
110111
continue
111112
}
112-
if err := gatherNamespaces(context, o, webhook.ClientConfig.Service.Namespace); err != nil {
113+
if err := gatherNamespaces(ctx, rCtx, o, webhook.ClientConfig.Service.Namespace); err != nil {
113114
errs = append(errs, err)
114115
}
115116
}

pkg/cli/admin/inspect/apiextensions.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package inspect
22

33
import (
4+
"context"
45
"fmt"
56
"k8s.io/apimachinery/pkg/util/errors"
67
"k8s.io/cli-runtime/pkg/resource"
@@ -21,35 +22,35 @@ func (c *customResourceDefinitionList) addItem(obj interface{}) error {
2122
return nil
2223
}
2324

24-
func gatherCustomResourceDefinition(context *resourceContext, info *resource.Info, o *InspectOptions) error {
25+
func gatherCustomResourceDefinition(ctx context.Context, rCtx *resourceContext, info *resource.Info, o *InspectOptions) error {
2526
structuredObj, err := toStructuredObject[apiextensionsv1.CustomResourceDefinition, apiextensionsv1.CustomResourceDefinitionList](info.Object)
2627
if err != nil {
27-
return gatherGenericObject(context, info, o)
28+
return gatherGenericObject(ctx, rCtx, info, o)
2829
}
2930

3031
errs := []error{}
3132
switch castObj := structuredObj.(type) {
3233
case *apiextensionsv1.CustomResourceDefinition:
33-
if err := gatherCustomResourceDefinitionRelated(context, o, castObj); err != nil {
34+
if err := gatherCustomResourceDefinitionRelated(ctx, rCtx, o, castObj); err != nil {
3435
errs = append(errs, err)
3536
}
3637

3738
case *apiextensionsv1.CustomResourceDefinitionList:
3839
for _, webhook := range castObj.Items {
39-
if err := gatherCustomResourceDefinitionRelated(context, o, &webhook); err != nil {
40+
if err := gatherCustomResourceDefinitionRelated(ctx, rCtx, o, &webhook); err != nil {
4041
errs = append(errs, err)
4142
}
4243
}
4344

4445
}
4546

46-
if err := gatherGenericObject(context, info, o); err != nil {
47+
if err := gatherGenericObject(ctx, rCtx, info, o); err != nil {
4748
errs = append(errs, err)
4849
}
4950
return errors.NewAggregate(errs)
5051
}
5152

52-
func gatherCustomResourceDefinitionRelated(context *resourceContext, o *InspectOptions, crd *apiextensionsv1.CustomResourceDefinition) error {
53+
func gatherCustomResourceDefinitionRelated(ctx context.Context, rCtx *resourceContext, o *InspectOptions, crd *apiextensionsv1.CustomResourceDefinition) error {
5354
if crd.Spec.Conversion == nil {
5455
return nil
5556
}
@@ -63,5 +64,5 @@ func gatherCustomResourceDefinitionRelated(context *resourceContext, o *InspectO
6364
return nil
6465
}
6566

66-
return gatherNamespaces(context, o, crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace)
67+
return gatherNamespaces(ctx, rCtx, o, crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace)
6768
}

pkg/cli/admin/inspect/inspect.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func NewCmdInspect(streams genericiooptions.IOStreams) *cobra.Command {
108108
Run: func(c *cobra.Command, args []string) {
109109
kcmdutil.CheckErr(o.Complete(args))
110110
kcmdutil.CheckErr(o.Validate())
111-
kcmdutil.CheckErr(o.Run())
111+
kcmdutil.CheckErr(o.Run(context.TODO()))
112112
},
113113
}
114114

@@ -197,7 +197,7 @@ func (o *InspectOptions) Validate() error {
197197
return nil
198198
}
199199

200-
func (o *InspectOptions) Run() error {
200+
func (o *InspectOptions) Run(ctx context.Context) error {
201201
if len(o.eventFile) > 0 {
202202
return createEventFilterPageFromFile(o.eventFile, o.DestDir)
203203
}
@@ -240,7 +240,7 @@ func (o *InspectOptions) Run() error {
240240
return err
241241
}
242242

243-
if err := inspectDiscovery(context.Background(), o.DestDir, discoveryClient); err != nil {
243+
if err := inspectDiscovery(ctx, o.DestDir, discoveryClient); err != nil {
244244
allErrs = append(allErrs, fmt.Errorf("failed inspecting discovery: %w", err))
245245
}
246246

@@ -259,9 +259,9 @@ func (o *InspectOptions) Run() error {
259259
}
260260

261261
// finally, gather polymorphic resources specified by the user
262-
ctx := NewResourceContext(serverResources)
262+
rCtx := NewResourceContext(serverResources)
263263
for _, info := range infos {
264-
err := InspectResource(info, ctx, o)
264+
err := InspectResource(ctx, info, rCtx, o)
265265
if err != nil {
266266
allErrs = append(allErrs, err)
267267
}
@@ -281,13 +281,13 @@ func (o *InspectOptions) Run() error {
281281
}
282282

283283
// gatherConfigResourceData gathers all config.openshift.io resources
284-
func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceContext) error {
284+
func (o *InspectOptions) gatherConfigResourceData(ctx context.Context, destDir string, rCtx *resourceContext) error {
285285
// determine if we've already collected configResourceData
286-
if ctx.visited.Has(configResourceDataKey) {
286+
if rCtx.visited.Has(configResourceDataKey) {
287287
klog.V(1).Infof("Skipping previously-collected config.openshift.io resource data")
288288
return nil
289289
}
290-
ctx.visited.Insert(configResourceDataKey)
290+
rCtx.visited.Insert(configResourceDataKey)
291291

292292
klog.V(1).Infof("Gathering config.openshift.io resource data...\n")
293293

@@ -303,15 +303,15 @@ func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceC
303303

304304
errs := []error{}
305305
for _, resource := range resources {
306-
resourceList, err := o.dynamicClient.Resource(resource).List(context.TODO(), metav1.ListOptions{})
306+
resourceList, err := o.dynamicClient.Resource(resource).List(ctx, metav1.ListOptions{})
307307
if err != nil {
308308
errs = append(errs, err)
309309
continue
310310
}
311311

312312
objToPrint := runtime.Object(resourceList)
313313
filename := fmt.Sprintf("%s.yaml", resource.Resource)
314-
if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), objToPrint); err != nil {
314+
if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), objToPrint); err != nil {
315315
errs = append(errs, err)
316316
continue
317317
}
@@ -324,13 +324,13 @@ func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceC
324324
}
325325

326326
// gatherOperatorResourceData gathers all kubeapiserver.operator.openshift.io resources
327-
func (o *InspectOptions) gatherOperatorResourceData(destDir string, ctx *resourceContext) error {
327+
func (o *InspectOptions) gatherOperatorResourceData(ctx context.Context, destDir string, rCtx *resourceContext) error {
328328
// determine if we've already collected operatorResourceData
329-
if ctx.visited.Has(operatorResourceDataKey) {
329+
if rCtx.visited.Has(operatorResourceDataKey) {
330330
klog.V(1).Infof("Skipping previously-collected operator.openshift.io resource data")
331331
return nil
332332
}
333-
ctx.visited.Insert(operatorResourceDataKey)
333+
rCtx.visited.Insert(operatorResourceDataKey)
334334

335335
// ensure destination path exists
336336
if err := os.MkdirAll(destDir, os.ModePerm); err != nil {
@@ -344,15 +344,15 @@ func (o *InspectOptions) gatherOperatorResourceData(destDir string, ctx *resourc
344344

345345
errs := []error{}
346346
for _, resource := range resources {
347-
resourceList, err := o.dynamicClient.Resource(resource).List(context.TODO(), metav1.ListOptions{})
347+
resourceList, err := o.dynamicClient.Resource(resource).List(ctx, metav1.ListOptions{})
348348
if err != nil {
349349
errs = append(errs, err)
350350
continue
351351
}
352352

353353
objToPrint := runtime.Object(resourceList)
354354
filename := fmt.Sprintf("%s.yaml", resource.Resource)
355-
if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), objToPrint); err != nil {
355+
if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), objToPrint); err != nil {
356356
errs = append(errs, err)
357357
continue
358358
}

pkg/cli/admin/inspect/namespace.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func namespaceResourcesToCollect() []schema.GroupResource {
3535
}
3636
}
3737

38-
func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error {
38+
func (o *InspectOptions) gatherNamespaceData(ctx context.Context, baseDir, namespace string) error {
3939
fmt.Fprintf(o.Out, "Gathering data for ns/%s...\n", namespace)
4040

4141
destDir := path.Join(baseDir, namespaceResourcesDirname, namespace)
@@ -45,7 +45,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error {
4545
return err
4646
}
4747

48-
ns, err := o.kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
48+
ns, err := o.kubeClient.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
4949
if err != nil { // If we can't get the namespace we need to exit out
5050
return err
5151
}
@@ -54,7 +54,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error {
5454
errs := []error{}
5555
// write namespace.yaml file
5656
filename := fmt.Sprintf("%s.yaml", namespace)
57-
if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), ns); err != nil {
57+
if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), ns); err != nil {
5858
errs = append(errs, err)
5959
}
6060

@@ -67,7 +67,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error {
6767

6868
// collect specific resource information for namespace
6969
for gvr := range resourcesTypesToStore {
70-
list, err := o.dynamicClient.Resource(gvr).Namespace(namespace).List(context.TODO(), metav1.ListOptions{})
70+
list, err := o.dynamicClient.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
7171
if err != nil {
7272
errs = append(errs, err)
7373
}
@@ -81,7 +81,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error {
8181
klog.V(1).Infof(" Gathering data for pod %q\n", pod.GetName())
8282
structuredPod := &corev1.Pod{}
8383
runtime.DefaultUnstructuredConverter.FromUnstructured(pod.Object, structuredPod)
84-
if err := o.gatherPodData(path.Join(destDir, "/pods/"+pod.GetName()), namespace, structuredPod); err != nil {
84+
if err := o.gatherPodData(ctx, path.Join(destDir, "/pods/"+pod.GetName()), namespace, structuredPod); err != nil {
8585
errs = append(errs, err)
8686
continue
8787
}

0 commit comments

Comments
 (0)