Skip to content
Draft
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
13 changes: 13 additions & 0 deletions src/cli/internal/clientconfig/clientconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/deckhouse/virtualization/api/client/kubeclient"
Expand Down Expand Up @@ -49,3 +50,15 @@ func ClientAndNamespaceFromContext(ctx context.Context) (client kubeclient.Clien
}
return client, namespace, overridden, nil
}

func GetRESTConfig(ctx context.Context) (*rest.Config, error) {
clientConfig, ok := ctx.Value(clientConfigKey).(clientcmd.ClientConfig)
if !ok {
return nil, fmt.Errorf("unable to get client config from context")
}
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}
return config, nil
}
130 changes: 130 additions & 0 deletions src/cli/internal/cmd/collectdebuginfo/collectdebuginfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package collectdebuginfo

import (
"context"
"fmt"
"io"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/dynamic"

"github.com/deckhouse/virtualization/api/client/kubeclient"
"github.com/deckhouse/virtualization/src/cli/internal/clientconfig"
"github.com/deckhouse/virtualization/src/cli/internal/templates"
)

func NewCommand() *cobra.Command {
bundle := &DebugBundle{}
cmd := &cobra.Command{
Use: "collect-debug-info (VirtualMachine)",
Short: "Collect debug information for VM: configuration, events, and logs.",
Example: usage(),
Args: templates.ExactArgs("collect-debug-info", 1),
RunE: bundle.Run,
}

cmd.Flags().BoolVar(&bundle.saveLogs, "with-logs", false, "Include pod logs in output")
cmd.Flags().BoolVar(&bundle.debug, "debug", false, "Enable debug output for permission errors")
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}

type DebugBundle struct {
saveLogs bool
debug bool
dynamicClient dynamic.Interface
stdout io.Writer
stderr io.Writer
resourceCount int
}

func usage() string {
return ` # Collect debug info for VirtualMachine 'myvm':
{{ProgramName}} collect-debug-info myvm
{{ProgramName}} collect-debug-info myvm.mynamespace
{{ProgramName}} collect-debug-info myvm -n mynamespace
# Include pod logs:
{{ProgramName}} collect-debug-info --with-logs myvm`
}

func (b *DebugBundle) Run(cmd *cobra.Command, args []string) error {
client, defaultNamespace, _, err := clientconfig.ClientAndNamespaceFromContext(cmd.Context())
if err != nil {
return err
}

namespace, name, err := templates.ParseTarget(args[0])
if err != nil {
return err
}
if namespace == "" {
namespace = defaultNamespace
}

// Get dynamic client for internal resources
config, err := clientconfig.GetRESTConfig(cmd.Context())
if err != nil {
return fmt.Errorf("failed to get REST config: %w", err)
}
b.dynamicClient, err = dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create dynamic client: %w", err)
}

// Set output writers
b.stdout = cmd.OutOrStdout()
b.stderr = cmd.ErrOrStderr()

// Collect and output resources immediately
if err := b.collectResources(cmd.Context(), client, namespace, name); err != nil {
return err
}

return nil
}

func (b *DebugBundle) collectResources(ctx context.Context, client kubeclient.Client, namespace, vmName string) error {
// Collect VM and core resources
if err := b.collectVMResources(ctx, client, namespace, vmName); err != nil {
return fmt.Errorf("failed to collect VM resources: %w", err)
}

// Collect block devices
if err := b.collectBlockDevices(ctx, client, namespace, vmName); err != nil {
return fmt.Errorf("failed to collect block devices: %w", err)
}

// Collect pods (and logs if requested)
if err := b.collectPods(ctx, client, namespace, vmName); err != nil {
return fmt.Errorf("failed to collect pods: %w", err)
}

return nil
}

func (b *DebugBundle) handleError(resourceType, resourceName string, err error) bool {
if errors.IsForbidden(err) || errors.IsUnauthorized(err) {
if b.debug {
fmt.Fprintf(b.stderr, "Warning: Skipping %s/%s: permission denied\n", resourceType, resourceName)
}
return true // Skip this resource
}
return false // Don't skip, propagate error
}
Loading
Loading