Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions pkg/cli/admin/inspect/admission_webhooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspect

import (
"context"
"fmt"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/cli/admin/inspect/apiextensions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspect

import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/resource"
Expand All @@ -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
}
Expand All @@ -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)
}
30 changes: 17 additions & 13 deletions pkg/cli/admin/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}

Expand All @@ -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)
}
Expand All @@ -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")

Expand All @@ -303,15 +307,15 @@ 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
}

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
}
Expand All @@ -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 {
Expand All @@ -344,15 +348,15 @@ 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
}

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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/admin/inspect/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
Loading