diff --git a/pkg/cli/admin/inspect/admission_webhooks.go b/pkg/cli/admin/inspect/admission_webhooks.go index f792968a2c..0c314a65d0 100644 --- a/pkg/cli/admin/inspect/admission_webhooks.go +++ b/pkg/cli/admin/inspect/admission_webhooks.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" "k8s.io/apimachinery/pkg/util/errors" @@ -20,41 +21,41 @@ func (c *mutatingWebhookConfigList) addItem(obj interface{}) error { return nil } -func gatherMutatingAdmissionWebhook(context *resourceContext, info *resource.Info, o *InspectOptions) error { +func gatherMutatingAdmissionWebhook(ctx context.Context, resourceCtx *resourceContext, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[admissionregistrationv1.MutatingWebhookConfiguration, admissionregistrationv1.MutatingWebhookConfigurationList](info.Object) if err != nil { - return gatherGenericObject(context, info, o) + return gatherGenericObject(ctx, resourceCtx, info, o) } errs := []error{} switch castObj := structuredObj.(type) { case *admissionregistrationv1.MutatingWebhookConfiguration: - if err := gatherMutatingAdmissionWebhookRelated(context, o, castObj); err != nil { + if err := gatherMutatingAdmissionWebhookRelated(ctx, resourceCtx, o, castObj); err != nil { errs = append(errs, err) } case *admissionregistrationv1.MutatingWebhookConfigurationList: for _, webhook := range castObj.Items { - if err := gatherMutatingAdmissionWebhookRelated(context, o, &webhook); err != nil { + if err := gatherMutatingAdmissionWebhookRelated(ctx, resourceCtx, o, &webhook); err != nil { errs = append(errs, err) } } } - if err := gatherGenericObject(context, info, o); err != nil { + if err := gatherGenericObject(ctx, resourceCtx, info, o); err != nil { errs = append(errs, err) } return errors.NewAggregate(errs) } -func gatherMutatingAdmissionWebhookRelated(context *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.MutatingWebhookConfiguration) error { +func gatherMutatingAdmissionWebhookRelated(ctx context.Context, resourceCtx *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.MutatingWebhookConfiguration) error { errs := []error{} for _, webhook := range webhookConfig.Webhooks { if webhook.ClientConfig.Service == nil { continue } - if err := gatherNamespaces(context, o, webhook.ClientConfig.Service.Namespace); err != nil { + if err := gatherNamespaces(ctx, resourceCtx, o, webhook.ClientConfig.Service.Namespace); err != nil { errs = append(errs, err) } } @@ -75,41 +76,41 @@ func (c *validatingWebhookConfigList) addItem(obj interface{}) error { return nil } -func gatherValidatingAdmissionWebhook(context *resourceContext, info *resource.Info, o *InspectOptions) error { +func gatherValidatingAdmissionWebhook(ctx context.Context, resourceCtx *resourceContext, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[admissionregistrationv1.ValidatingWebhookConfiguration, admissionregistrationv1.ValidatingWebhookConfigurationList](info.Object) if err != nil { - return gatherGenericObject(context, info, o) + return gatherGenericObject(ctx, resourceCtx, info, o) } errs := []error{} switch castObj := structuredObj.(type) { case *admissionregistrationv1.ValidatingWebhookConfiguration: - if err := gatherValidatingAdmissionWebhookRelated(context, o, castObj); err != nil { + if err := gatherValidatingAdmissionWebhookRelated(ctx, resourceCtx, o, castObj); err != nil { errs = append(errs, err) } case *admissionregistrationv1.ValidatingWebhookConfigurationList: for _, webhook := range castObj.Items { - if err := gatherValidatingAdmissionWebhookRelated(context, o, &webhook); err != nil { + if err := gatherValidatingAdmissionWebhookRelated(ctx, resourceCtx, o, &webhook); err != nil { errs = append(errs, err) } } } - if err := gatherGenericObject(context, info, o); err != nil { + if err := gatherGenericObject(ctx, resourceCtx, info, o); err != nil { errs = append(errs, err) } return errors.NewAggregate(errs) } -func gatherValidatingAdmissionWebhookRelated(context *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.ValidatingWebhookConfiguration) error { +func gatherValidatingAdmissionWebhookRelated(ctx context.Context, resourceCtx *resourceContext, o *InspectOptions, webhookConfig *admissionregistrationv1.ValidatingWebhookConfiguration) error { errs := []error{} for _, webhook := range webhookConfig.Webhooks { if webhook.ClientConfig.Service == nil { continue } - if err := gatherNamespaces(context, o, webhook.ClientConfig.Service.Namespace); err != nil { + if err := gatherNamespaces(ctx, resourceCtx, o, webhook.ClientConfig.Service.Namespace); err != nil { errs = append(errs, err) } } diff --git a/pkg/cli/admin/inspect/apiextensions.go b/pkg/cli/admin/inspect/apiextensions.go index 0912ad412c..7849480e7c 100644 --- a/pkg/cli/admin/inspect/apiextensions.go +++ b/pkg/cli/admin/inspect/apiextensions.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" "k8s.io/apimachinery/pkg/util/errors" "k8s.io/cli-runtime/pkg/resource" @@ -21,35 +22,35 @@ func (c *customResourceDefinitionList) addItem(obj interface{}) error { return nil } -func gatherCustomResourceDefinition(context *resourceContext, info *resource.Info, o *InspectOptions) error { +func gatherCustomResourceDefinition(ctx context.Context, resourceCtx *resourceContext, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[apiextensionsv1.CustomResourceDefinition, apiextensionsv1.CustomResourceDefinitionList](info.Object) if err != nil { - return gatherGenericObject(context, info, o) + return gatherGenericObject(ctx, resourceCtx, info, o) } errs := []error{} switch castObj := structuredObj.(type) { case *apiextensionsv1.CustomResourceDefinition: - if err := gatherCustomResourceDefinitionRelated(context, o, castObj); err != nil { + if err := gatherCustomResourceDefinitionRelated(ctx, resourceCtx, o, castObj); err != nil { errs = append(errs, err) } case *apiextensionsv1.CustomResourceDefinitionList: for _, webhook := range castObj.Items { - if err := gatherCustomResourceDefinitionRelated(context, o, &webhook); err != nil { + if err := gatherCustomResourceDefinitionRelated(ctx, resourceCtx, o, &webhook); err != nil { errs = append(errs, err) } } } - if err := gatherGenericObject(context, info, o); err != nil { + if err := gatherGenericObject(ctx, resourceCtx, info, o); err != nil { errs = append(errs, err) } return errors.NewAggregate(errs) } -func gatherCustomResourceDefinitionRelated(context *resourceContext, o *InspectOptions, crd *apiextensionsv1.CustomResourceDefinition) error { +func gatherCustomResourceDefinitionRelated(ctx context.Context, resourceCtx *resourceContext, o *InspectOptions, crd *apiextensionsv1.CustomResourceDefinition) error { if crd.Spec.Conversion == nil { return nil } @@ -63,5 +64,5 @@ func gatherCustomResourceDefinitionRelated(context *resourceContext, o *InspectO return nil } - return gatherNamespaces(context, o, crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace) + return gatherNamespaces(ctx, resourceCtx, o, crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace) } diff --git a/pkg/cli/admin/inspect/inspect.go b/pkg/cli/admin/inspect/inspect.go index aba23b96f4..9966e68278 100644 --- a/pkg/cli/admin/inspect/inspect.go +++ b/pkg/cli/admin/inspect/inspect.go @@ -198,6 +198,10 @@ func (o *InspectOptions) Validate() error { } func (o *InspectOptions) Run() error { + return o.RunContext(context.TODO()) +} + +func (o *InspectOptions) RunContext(ctx context.Context) error { if len(o.eventFile) > 0 { return createEventFilterPageFromFile(o.eventFile, o.DestDir) } @@ -240,7 +244,7 @@ func (o *InspectOptions) Run() error { return err } - if err := inspectDiscovery(context.Background(), o.DestDir, discoveryClient); err != nil { + if err := inspectDiscovery(ctx, o.DestDir, discoveryClient); err != nil { allErrs = append(allErrs, fmt.Errorf("failed inspecting discovery: %w", err)) } @@ -259,9 +263,9 @@ func (o *InspectOptions) Run() error { } // finally, gather polymorphic resources specified by the user - ctx := NewResourceContext(serverResources) + resourceCtx := NewResourceContext(serverResources) for _, info := range infos { - err := InspectResource(info, ctx, o) + err := InspectResource(ctx, info, resourceCtx, o) if err != nil { allErrs = append(allErrs, err) } @@ -281,13 +285,13 @@ func (o *InspectOptions) Run() error { } // gatherConfigResourceData gathers all config.openshift.io resources -func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceContext) error { +func (o *InspectOptions) gatherConfigResourceData(ctx context.Context, destDir string, resourceCtx *resourceContext) error { // determine if we've already collected configResourceData - if ctx.visited.Has(configResourceDataKey) { + if resourceCtx.visited.Has(configResourceDataKey) { klog.V(1).Infof("Skipping previously-collected config.openshift.io resource data") return nil } - ctx.visited.Insert(configResourceDataKey) + resourceCtx.visited.Insert(configResourceDataKey) klog.V(1).Infof("Gathering config.openshift.io resource data...\n") @@ -303,7 +307,7 @@ func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceC errs := []error{} for _, resource := range resources { - resourceList, err := o.dynamicClient.Resource(resource).List(context.TODO(), metav1.ListOptions{}) + resourceList, err := o.dynamicClient.Resource(resource).List(ctx, metav1.ListOptions{}) if err != nil { errs = append(errs, err) continue @@ -311,7 +315,7 @@ func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceC objToPrint := runtime.Object(resourceList) filename := fmt.Sprintf("%s.yaml", resource.Resource) - if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), objToPrint); err != nil { + if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), objToPrint); err != nil { errs = append(errs, err) continue } @@ -324,13 +328,13 @@ func (o *InspectOptions) gatherConfigResourceData(destDir string, ctx *resourceC } // gatherOperatorResourceData gathers all kubeapiserver.operator.openshift.io resources -func (o *InspectOptions) gatherOperatorResourceData(destDir string, ctx *resourceContext) error { +func (o *InspectOptions) gatherOperatorResourceData(ctx context.Context, destDir string, resourceCtx *resourceContext) error { // determine if we've already collected operatorResourceData - if ctx.visited.Has(operatorResourceDataKey) { + if resourceCtx.visited.Has(operatorResourceDataKey) { klog.V(1).Infof("Skipping previously-collected operator.openshift.io resource data") return nil } - ctx.visited.Insert(operatorResourceDataKey) + resourceCtx.visited.Insert(operatorResourceDataKey) // ensure destination path exists if err := os.MkdirAll(destDir, os.ModePerm); err != nil { @@ -344,7 +348,7 @@ func (o *InspectOptions) gatherOperatorResourceData(destDir string, ctx *resourc errs := []error{} for _, resource := range resources { - resourceList, err := o.dynamicClient.Resource(resource).List(context.TODO(), metav1.ListOptions{}) + resourceList, err := o.dynamicClient.Resource(resource).List(ctx, metav1.ListOptions{}) if err != nil { errs = append(errs, err) continue @@ -352,7 +356,7 @@ func (o *InspectOptions) gatherOperatorResourceData(destDir string, ctx *resourc objToPrint := runtime.Object(resourceList) filename := fmt.Sprintf("%s.yaml", resource.Resource) - if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), objToPrint); err != nil { + if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), objToPrint); err != nil { errs = append(errs, err) continue } diff --git a/pkg/cli/admin/inspect/namespace.go b/pkg/cli/admin/inspect/namespace.go index a8adda9d93..93e44c131d 100644 --- a/pkg/cli/admin/inspect/namespace.go +++ b/pkg/cli/admin/inspect/namespace.go @@ -35,7 +35,7 @@ func namespaceResourcesToCollect() []schema.GroupResource { } } -func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error { +func (o *InspectOptions) gatherNamespaceData(ctx context.Context, baseDir, namespace string) error { fmt.Fprintf(o.Out, "Gathering data for ns/%s...\n", namespace) destDir := path.Join(baseDir, namespaceResourcesDirname, namespace) @@ -45,7 +45,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error { return err } - ns, err := o.kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{}) + ns, err := o.kubeClient.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) if err != nil { // If we can't get the namespace we need to exit out return err } @@ -54,7 +54,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error { errs := []error{} // write namespace.yaml file filename := fmt.Sprintf("%s.yaml", namespace) - if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), ns); err != nil { + if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), ns); err != nil { errs = append(errs, err) } @@ -67,7 +67,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error { // collect specific resource information for namespace for gvr := range resourcesTypesToStore { - list, err := o.dynamicClient.Resource(gvr).Namespace(namespace).List(context.TODO(), metav1.ListOptions{}) + list, err := o.dynamicClient.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{}) if err != nil { errs = append(errs, err) } @@ -81,7 +81,7 @@ func (o *InspectOptions) gatherNamespaceData(baseDir, namespace string) error { klog.V(1).Infof(" Gathering data for pod %q\n", pod.GetName()) structuredPod := &corev1.Pod{} runtime.DefaultUnstructuredConverter.FromUnstructured(pod.Object, structuredPod) - if err := o.gatherPodData(path.Join(destDir, "/pods/"+pod.GetName()), namespace, structuredPod); err != nil { + if err := o.gatherPodData(ctx, path.Join(destDir, "/pods/"+pod.GetName()), namespace, structuredPod); err != nil { errs = append(errs, err) continue } diff --git a/pkg/cli/admin/inspect/pod.go b/pkg/cli/admin/inspect/pod.go index 29e075f423..75d810d45d 100644 --- a/pkg/cli/admin/inspect/pod.go +++ b/pkg/cli/admin/inspect/pod.go @@ -17,14 +17,14 @@ import ( "k8s.io/klog/v2" ) -func (o *InspectOptions) gatherPodData(destDir, namespace string, pod *corev1.Pod) error { +func (o *InspectOptions) gatherPodData(ctx context.Context, destDir, namespace string, pod *corev1.Pod) error { // ensure destination path exists if err := os.MkdirAll(destDir, os.ModePerm); err != nil { return err } filename := fmt.Sprintf("%s.yaml", pod.Name) - if err := o.fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), pod); err != nil { + if err := o.fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), pod); err != nil { return err } @@ -32,13 +32,13 @@ func (o *InspectOptions) gatherPodData(destDir, namespace string, pod *corev1.Po // gather data for each container in the given pod for _, container := range pod.Spec.Containers { - if err := o.gatherContainerInfo(path.Join(destDir, "/"+container.Name), pod, container); err != nil { + if err := o.gatherContainerInfo(ctx, path.Join(destDir, "/"+container.Name), pod, container); err != nil { errs = append(errs, err) continue } } for _, container := range pod.Spec.InitContainers { - if err := o.gatherContainerInfo(path.Join(destDir, "/"+container.Name), pod, container); err != nil { + if err := o.gatherContainerInfo(ctx, path.Join(destDir, "/"+container.Name), pod, container); err != nil { errs = append(errs, err) continue } @@ -50,27 +50,27 @@ func (o *InspectOptions) gatherPodData(destDir, namespace string, pod *corev1.Po return nil } -func (o *InspectOptions) gatherContainerInfo(destDir string, pod *corev1.Pod, container corev1.Container) error { - if err := o.gatherContainerAllLogs(path.Join(destDir, "/"+container.Name), pod, &container); err != nil { +func (o *InspectOptions) gatherContainerInfo(ctx context.Context, destDir string, pod *corev1.Pod, container corev1.Container) error { + if err := o.gatherContainerAllLogs(ctx, path.Join(destDir, "/"+container.Name), pod, &container); err != nil { return err } return nil } -func (o *InspectOptions) gatherContainerAllLogs(destDir string, pod *corev1.Pod, container *corev1.Container) error { +func (o *InspectOptions) gatherContainerAllLogs(ctx context.Context, destDir string, pod *corev1.Pod, container *corev1.Container) error { // ensure destination path exists if err := os.MkdirAll(destDir, os.ModePerm); err != nil { return err } errs := []error{} - if err := o.gatherContainerLogs(path.Join(destDir, "/logs"), pod, container); err != nil { + if err := o.gatherContainerLogs(ctx, path.Join(destDir, "/logs"), pod, container); err != nil { errs = append(errs, filterContainerLogsErrors(err)) } if o.rotatedPodLogs { - if err := o.gatherContainerRotatedLogFiles(path.Join(destDir, "/logs/rotated"), pod, container); err != nil { + if err := o.gatherContainerRotatedLogFiles(ctx, path.Join(destDir, "/logs/rotated"), pod, container); err != nil { errs = append(errs, filterContainerLogsErrors(err)) } } @@ -99,7 +99,7 @@ func rotatedLogFilename(pod *corev1.Pod) (string, error) { return pod.Namespace + "_" + pod.Name + "_" + string(pod.GetUID()), nil } -func (o *InspectOptions) gatherContainerRotatedLogFiles(destDir string, pod *corev1.Pod, container *corev1.Container) error { +func (o *InspectOptions) gatherContainerRotatedLogFiles(ctx context.Context, destDir string, pod *corev1.Pod, container *corev1.Container) error { restClient := o.kubeClient.CoreV1().RESTClient() var innerErrs []error @@ -117,7 +117,7 @@ func (o *InspectOptions) gatherContainerRotatedLogFiles(destDir string, pod *cor req := restClient.Get().RequestURI(containerPath). SetHeader("Accept", "text/plain, */*") - res, err := req.Stream(context.TODO()) + res, err := req.Stream(ctx) if err != nil { return err } @@ -182,7 +182,7 @@ func (o *InspectOptions) gatherContainerRotatedLogFiles(destDir string, pod *cor SetHeader("Accept", "text/plain, */*"). SetHeader("Accept-Encoding", "gzip") - if err := o.fileWriter.WriteFromSource(path.Join(destDir, fileName), logsReq); err != nil { + if err := o.fileWriter.WriteFromSource(ctx, path.Join(destDir, fileName), logsReq); err != nil { innerErrs = append(innerErrs, err) } } @@ -194,7 +194,7 @@ func (o *InspectOptions) gatherContainerRotatedLogFiles(destDir string, pod *cor return utilerrors.NewAggregate(innerErrs) } -func (o *InspectOptions) gatherContainerLogs(destDir string, pod *corev1.Pod, container *corev1.Container) error { +func (o *InspectOptions) gatherContainerLogs(ctx context.Context, destDir string, pod *corev1.Pod, container *corev1.Container) error { // ensure destination path exists if err := os.MkdirAll(destDir, os.ModePerm); err != nil { return err @@ -222,14 +222,14 @@ func (o *InspectOptions) gatherContainerLogs(destDir string, pod *corev1.Pod, co } filename := "current.log" logsReq := o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions) - if err := o.fileWriter.WriteFromSource(path.Join(destDir, "/"+filename), logsReq); err != nil { + if err := o.fileWriter.WriteFromSource(ctx, path.Join(destDir, "/"+filename), logsReq); err != nil { innerErrs = append(innerErrs, err) // if we had an error, we will try again with an insecure backendproxy flag set logOptions.InsecureSkipTLSVerifyBackend = true logsReq = o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions) filename = "current.insecure.log" - if err := o.fileWriter.WriteFromSource(path.Join(destDir, "/"+filename), logsReq); err != nil { + if err := o.fileWriter.WriteFromSource(ctx, path.Join(destDir, "/"+filename), logsReq); err != nil { innerErrs = append(innerErrs, err) } } @@ -257,14 +257,14 @@ func (o *InspectOptions) gatherContainerLogs(destDir string, pod *corev1.Pod, co } logsReq := o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions) filename := "previous.log" - if err := o.fileWriter.WriteFromSource(path.Join(destDir, "/"+filename), logsReq); err != nil { + if err := o.fileWriter.WriteFromSource(ctx, path.Join(destDir, "/"+filename), logsReq); err != nil { innerErrs = append(innerErrs, err) // if we had an error, we will try again with an insecure backendproxy flag set logOptions.InsecureSkipTLSVerifyBackend = true logsReq = o.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, logOptions) filename = "previous.insecure.log" - if err := o.fileWriter.WriteFromSource(path.Join(destDir, "/"+filename), logsReq); err != nil { + if err := o.fileWriter.WriteFromSource(ctx, path.Join(destDir, "/"+filename), logsReq); err != nil { innerErrs = append(innerErrs, err) } } diff --git a/pkg/cli/admin/inspect/proxy.go b/pkg/cli/admin/inspect/proxy.go index 91fc697e5c..3f18fd0c60 100644 --- a/pkg/cli/admin/inspect/proxy.go +++ b/pkg/cli/admin/inspect/proxy.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" "os" "path" @@ -28,7 +29,7 @@ func (c *proxyList) addItem(obj interface{}) error { return nil } -func inspectProxyInfo(info *resource.Info, o *InspectOptions) error { +func inspectProxyInfo(ctx context.Context, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[configv1.Proxy, configv1.ProxyList](info.Object) if err != nil { return err @@ -51,7 +52,7 @@ func inspectProxyInfo(info *resource.Info, o *InspectOptions) error { if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { return err } - return o.fileWriter.WriteFromResource(path.Join(dirPath, filename), structuredObj) + return o.fileWriter.WriteFromResource(ctx, path.Join(dirPath, filename), structuredObj) } // elideProxy obfuscates the sensitive information from proxy object. diff --git a/pkg/cli/admin/inspect/resource.go b/pkg/cli/admin/inspect/resource.go index 917dd07ee5..8c27b1c060 100644 --- a/pkg/cli/admin/inspect/resource.go +++ b/pkg/cli/admin/inspect/resource.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" "os" "path" @@ -27,12 +28,12 @@ const ( // InspectResource receives an object to gather debugging data for, and a context to keep track of // already-seen objects when following related-object reference chains. -func InspectResource(info *resource.Info, context *resourceContext, o *InspectOptions) error { - if context.visited.Has(infoToContextKey(info)) { +func InspectResource(ctx context.Context, info *resource.Info, resourceCtx *resourceContext, o *InspectOptions) error { + if resourceCtx.visited.Has(infoToContextKey(info)) { klog.V(1).Infof("Skipping previously-inspected resource: %q ...", infoToContextKey(info)) return nil } - context.visited.Insert(infoToContextKey(info)) + resourceCtx.visited.Insert(infoToContextKey(info)) switch info.ResourceMapping().Resource.GroupResource() { case configv1.GroupVersion.WithResource("clusteroperators").GroupResource(): @@ -43,22 +44,22 @@ func InspectResource(info *resource.Info, context *resourceContext, o *InspectOp // first, gather config.openshift.io resource data errs := []error{} - if err := o.gatherConfigResourceData(path.Join(o.DestDir, "/cluster-scoped-resources/config.openshift.io"), context); err != nil { + if err := o.gatherConfigResourceData(ctx, path.Join(o.DestDir, "/cluster-scoped-resources/config.openshift.io"), resourceCtx); err != nil { errs = append(errs, err) } // then, gather operator.openshift.io resource data - if err := o.gatherOperatorResourceData(path.Join(o.DestDir, "/cluster-scoped-resources/operator.openshift.io"), context); err != nil { + if err := o.gatherOperatorResourceData(ctx, path.Join(o.DestDir, "/cluster-scoped-resources/operator.openshift.io"), resourceCtx); err != nil { errs = append(errs, err) } // save clusteroperator resources to disk - if err := gatherClusterOperatorResource(o.DestDir, unstr, o.fileWriter); err != nil { + if err := gatherClusterOperatorResource(ctx, o.DestDir, unstr, o.fileWriter); err != nil { errs = append(errs, err) } // obtain associated objects for the current resource - if err := gatherRelatedObjects(context, unstr, o); err != nil { + if err := gatherRelatedObjects(ctx, resourceCtx, unstr, o); err != nil { errs = append(errs, err) } @@ -66,21 +67,21 @@ func InspectResource(info *resource.Info, context *resourceContext, o *InspectOp case corev1.SchemeGroupVersion.WithResource("namespaces").GroupResource(): errs := []error{} - if err := o.gatherNamespaceData(o.DestDir, info.Name); err != nil { + if err := o.gatherNamespaceData(ctx, o.DestDir, info.Name); err != nil { errs = append(errs, err) } resourcesToCollect := namespaceResourcesToCollect() for _, resource := range resourcesToCollect { - if context.visited.Has(resourceToContextKey(resource, info.Name)) { + if resourceCtx.visited.Has(resourceToContextKey(resource, info.Name)) { continue } - resourceInfos, err := groupResourceToInfos(o.configFlags, resource, info.Name, context.serverResources) + resourceInfos, err := groupResourceToInfos(o.configFlags, resource, info.Name, resourceCtx.serverResources) if err != nil { errs = append(errs, err) continue } for _, resourceInfo := range resourceInfos { - if err := InspectResource(resourceInfo, context, o); err != nil { + if err := InspectResource(ctx, resourceInfo, resourceCtx, o); err != nil { errs = append(errs, err) continue } @@ -90,43 +91,43 @@ func InspectResource(info *resource.Info, context *resourceContext, o *InspectOp return errors.NewAggregate(errs) case corev1.SchemeGroupVersion.WithResource("secrets").GroupResource(): - if err := inspectSecretInfo(info, o); err != nil { + if err := inspectSecretInfo(ctx, info, o); err != nil { return err } return nil case routev1.GroupVersion.WithResource("routes").GroupResource(): - if err := inspectRouteInfo(info, o); err != nil { + if err := inspectRouteInfo(ctx, info, o); err != nil { return err } return nil case configv1.GroupVersion.WithResource("proxies").GroupResource(): - if err := inspectProxyInfo(info, o); err != nil { + if err := inspectProxyInfo(ctx, info, o); err != nil { return err } return nil case admissionregistrationv1.SchemeGroupVersion.WithResource("mutatingwebhookconfigurations").GroupResource(): - if err := gatherMutatingAdmissionWebhook(context, info, o); err != nil { + if err := gatherMutatingAdmissionWebhook(ctx, resourceCtx, info, o); err != nil { return err } return nil case admissionregistrationv1.SchemeGroupVersion.WithResource("validatingwebhookconfigurations").GroupResource(): - if err := gatherValidatingAdmissionWebhook(context, info, o); err != nil { + if err := gatherValidatingAdmissionWebhook(ctx, resourceCtx, info, o); err != nil { return err } return nil case apiextensionsv1.SchemeGroupVersion.WithResource("customresourcedefinitions").GroupResource(): - if err := gatherCustomResourceDefinition(context, info, o); err != nil { + if err := gatherCustomResourceDefinition(ctx, resourceCtx, info, o); err != nil { return err } return nil default: - return gatherGenericObject(context, info, o) + return gatherGenericObject(ctx, resourceCtx, info, o) } } @@ -194,12 +195,12 @@ func toStructuredObject[T any, TList any](obj runtime.Object) (runtime.Object, e return nil, fmt.Errorf("unhandled object type") } -func gatherGenericObject(context *resourceContext, info *resource.Info, o *InspectOptions) error { +func gatherGenericObject(ctx context.Context, resourceCtx *resourceContext, info *resource.Info, o *InspectOptions) error { errs := []error{} unstr, ok := info.Object.(*unstructured.Unstructured) if ok { // obtain associated objects for the current resource - if err := gatherRelatedObjects(context, unstr, o); err != nil { + if err := gatherRelatedObjects(ctx, resourceCtx, unstr, o); err != nil { errs = append(errs, err) } } @@ -213,26 +214,26 @@ func gatherGenericObject(context *resourceContext, info *resource.Info, o *Inspe return errors.NewAggregate(errs) } - if err := o.fileWriter.WriteFromResource(path.Join(dirPath, filename), info.Object); err != nil { + if err := o.fileWriter.WriteFromResource(ctx, path.Join(dirPath, filename), info.Object); err != nil { errs = append(errs, err) } return errors.NewAggregate(errs) } -func gatherRelatedObjects(context *resourceContext, unstr *unstructured.Unstructured, o *InspectOptions) error { +func gatherRelatedObjects(ctx context.Context, resourceCtx *resourceContext, unstr *unstructured.Unstructured, o *InspectOptions) error { relatedObjReferences, err := obtainRelatedObjects(unstr) if err != nil { return err } - return gatherMoreObjects(context, o, relatedObjReferences...) + return gatherMoreObjects(ctx, resourceCtx, o, relatedObjReferences...) } -func gatherMoreObjects(context *resourceContext, o *InspectOptions, relatedObjReferences ...*configv1.ObjectReference) error { +func gatherMoreObjects(ctx context.Context, resourceCtx *resourceContext, o *InspectOptions, relatedObjReferences ...*configv1.ObjectReference) error { errs := []error{} for _, relatedRef := range relatedObjReferences { - if context.visited.Has(objectRefToContextKey(relatedRef)) { + if resourceCtx.visited.Has(objectRefToContextKey(relatedRef)) { continue } @@ -243,7 +244,7 @@ func gatherMoreObjects(context *resourceContext, o *InspectOptions, relatedObjRe } for _, relatedInfo := range relatedInfos { - if err := InspectResource(relatedInfo, context, o); err != nil { + if err := InspectResource(ctx, relatedInfo, resourceCtx, o); err != nil { errs = append(errs, fmt.Errorf("skipping gathering %s due to error: %v", objectReferenceToString(relatedRef), err)) continue } @@ -253,7 +254,7 @@ func gatherMoreObjects(context *resourceContext, o *InspectOptions, relatedObjRe return errors.NewAggregate(errs) } -func gatherNamespaces(context *resourceContext, o *InspectOptions, namespaces ...string) error { +func gatherNamespaces(ctx context.Context, resourceCtx *resourceContext, o *InspectOptions, namespaces ...string) error { relatedObjReferences := []*configv1.ObjectReference{} for _, namespace := range namespaces { @@ -263,10 +264,10 @@ func gatherNamespaces(context *resourceContext, o *InspectOptions, namespaces .. }) } - return gatherMoreObjects(context, o, relatedObjReferences...) + return gatherMoreObjects(ctx, resourceCtx, o, relatedObjReferences...) } -func gatherClusterOperatorResource(baseDir string, obj *unstructured.Unstructured, fileWriter *MultiSourceFileWriter) error { +func gatherClusterOperatorResource(ctx context.Context, baseDir string, obj *unstructured.Unstructured, fileWriter *MultiSourceFileWriter) error { klog.V(1).Infof("Gathering cluster operator resource data...\n") // ensure destination path exists @@ -276,7 +277,7 @@ func gatherClusterOperatorResource(baseDir string, obj *unstructured.Unstructure } filename := fmt.Sprintf("%s.yaml", obj.GetName()) - return fileWriter.WriteFromResource(path.Join(destDir, "/"+filename), obj) + return fileWriter.WriteFromResource(ctx, path.Join(destDir, "/"+filename), obj) } func obtainRelatedObjects(obj *unstructured.Unstructured) ([]*configv1.ObjectReference, error) { diff --git a/pkg/cli/admin/inspect/route.go b/pkg/cli/admin/inspect/route.go index fac77aaeab..638219ddb0 100644 --- a/pkg/cli/admin/inspect/route.go +++ b/pkg/cli/admin/inspect/route.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" "os" "path" @@ -24,7 +25,7 @@ func (c *routeList) addItem(obj interface{}) error { return nil } -func inspectRouteInfo(info *resource.Info, o *InspectOptions) error { +func inspectRouteInfo(ctx context.Context, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[routev1.Route, routev1.RouteList](info.Object) if err != nil { return err @@ -47,7 +48,7 @@ func inspectRouteInfo(info *resource.Info, o *InspectOptions) error { if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { return err } - return o.fileWriter.WriteFromResource(path.Join(dirPath, filename), structuredObj) + return o.fileWriter.WriteFromResource(ctx, path.Join(dirPath, filename), structuredObj) } func elideRoute(route *routev1.Route) { diff --git a/pkg/cli/admin/inspect/secret.go b/pkg/cli/admin/inspect/secret.go index 1744086125..64e4b953d8 100644 --- a/pkg/cli/admin/inspect/secret.go +++ b/pkg/cli/admin/inspect/secret.go @@ -1,6 +1,7 @@ package inspect import ( + "context" "fmt" "os" "path" @@ -25,7 +26,7 @@ func (c *secretList) addItem(obj interface{}) error { return nil } -func inspectSecretInfo(info *resource.Info, o *InspectOptions) error { +func inspectSecretInfo(ctx context.Context, info *resource.Info, o *InspectOptions) error { structuredObj, err := toStructuredObject[corev1.Secret, corev1.SecretList](info.Object) if err != nil { return err @@ -49,7 +50,7 @@ func inspectSecretInfo(info *resource.Info, o *InspectOptions) error { if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { return err } - return o.fileWriter.WriteFromResource(path.Join(dirPath, filename), structuredObj) + return o.fileWriter.WriteFromResource(ctx, path.Join(dirPath, filename), structuredObj) } var publicSecretKeys = sets.NewString( diff --git a/pkg/cli/admin/inspect/writer.go b/pkg/cli/admin/inspect/writer.go index b78f8768af..23a30be83c 100644 --- a/pkg/cli/admin/inspect/writer.go +++ b/pkg/cli/admin/inspect/writer.go @@ -50,14 +50,14 @@ func (r *resourceWriterReadCloser) Close() error { type simpleFileWriter struct{} -func (f *simpleFileWriter) Write(filepath string, src fileWriterSource) error { +func (f *simpleFileWriter) Write(ctx context.Context, filepath string, src fileWriterSource) error { dest, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) if err != nil { return err } defer dest.Close() - readCloser, err := src.Stream(context.TODO()) + readCloser, err := src.Stream(ctx) if err != nil { return err } @@ -71,19 +71,19 @@ type MultiSourceFileWriter struct { printer printers.ResourcePrinter } -func (f *MultiSourceFileWriter) WriteFromSource(filepath string, source fileWriterSource) error { +func (f *MultiSourceFileWriter) WriteFromSource(ctx context.Context, filepath string, source fileWriterSource) error { writer := &simpleFileWriter{} - return writer.Write(filepath, source) + return writer.Write(ctx, filepath, source) } -func (f *MultiSourceFileWriter) WriteFromResource(filepath string, obj runtime.Object) error { +func (f *MultiSourceFileWriter) WriteFromResource(ctx context.Context, filepath string, obj runtime.Object) error { source := &resourceWriterSource{ obj: obj, printer: f.printer, } writer := &simpleFileWriter{} - return writer.Write(filepath, source) + return writer.Write(ctx, filepath, source) } func NewMultiSourceWriter(printer printers.ResourcePrinter) *MultiSourceFileWriter {