Skip to content
Open
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
92 changes: 92 additions & 0 deletions cmd/collect-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"github.com/kubeslice/kubeslice-cli/pkg"
"github.com/kubeslice/kubeslice-cli/util"
"github.com/spf13/cobra"
)

var (
collectNamespace string
collectKubeconfig string
collectOutputFile string
collectComponents []string
collectLogs bool
collectConfigs bool
collectEvents bool
collectMetrics bool
)

var collectInfoCmd = &cobra.Command{
Use: "collect-info",
Aliases: []string{"ci", "collect"},
Short: "Collect debug information from KubeSlice components",
Long: `Collect comprehensive debug information from KubeSlice components in the cluster.

This command gathers logs, configurations, events, and other debug information from:
- KubeSlice Controller pods
- KubeSlice Worker pods
- KubeSlice Manager (enterprise)
- Calico networking components
- Prometheus monitoring (if installed)
- Kubernetes resources (CRDs, ConfigMaps, Secrets)

The output is packaged into a compressed tar.gz file for easy sharing and analysis.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Validate required flags
if collectNamespace == "" {
cmd.Help()
util.Fatalf("\n [ERROR] Namespace is required. Use -n or --namespace flag")
}

if collectOutputFile == "" {
cmd.Help()
util.Fatalf("\n [ERROR] Output file is required. Use --out flag")
}

// Set default components if none specified
if len(collectComponents) == 0 {
collectComponents = []string{"controller", "worker", "manager", "calico", "prometheus", "resources"}
}

// Set default collection types if none specified
if !collectLogs && !collectConfigs && !collectEvents && !collectMetrics {
collectLogs = true
collectConfigs = true
collectEvents = true
}

// Execute collection
pkg.CollectInfo(collectNamespace, collectKubeconfig, collectOutputFile, collectComponents, collectLogs, collectConfigs, collectEvents, collectMetrics)
},
}

func init() {
rootCmd.AddCommand(collectInfoCmd)

// Required flags
collectInfoCmd.Flags().StringVarP(&collectNamespace, "namespace", "n", "", "Namespace containing KubeSlice components (required)")
collectInfoCmd.Flags().StringVar(&collectOutputFile, "out", "", "Output file path for the collected information archive (required)")

// Optional flags
collectInfoCmd.Flags().StringVar(&collectKubeconfig, "kubeconfig", "", "Path to kubeconfig file (uses default if not specified)")
collectInfoCmd.Flags().StringSliceVar(&collectComponents, "components", []string{}, `Components to collect information from (comma-separated).
Supported values:
- controller: KubeSlice Controller components
- worker: KubeSlice Worker components
- manager: KubeSlice Manager (enterprise)
- calico: Calico networking components
- prometheus: Prometheus monitoring components
- resources: Kubernetes resources (CRDs, ConfigMaps, Secrets)
- all: All components (default if none specified)`)

collectInfoCmd.Flags().BoolVar(&collectLogs, "logs", false, "Collect pod logs (default: true if no collection type specified)")
collectInfoCmd.Flags().BoolVar(&collectConfigs, "configs", false, "Collect configuration files and manifests (default: true if no collection type specified)")
collectInfoCmd.Flags().BoolVar(&collectEvents, "events", false, "Collect Kubernetes events (default: true if no collection type specified)")
collectInfoCmd.Flags().BoolVar(&collectMetrics, "metrics", false, "Collect metrics and status information (default: false)")

// Mark required flags
collectInfoCmd.MarkFlagRequired("namespace")
collectInfoCmd.MarkFlagRequired("out")
}
85 changes: 85 additions & 0 deletions pkg/collect-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pkg

import (
"fmt"
"os"
"strings"
"time"

"github.com/kubeslice/kubeslice-cli/pkg/internal"
"github.com/kubeslice/kubeslice-cli/util"
)

// CollectInfo orchestrates the collection of debug information from KubeSlice components
func CollectInfo(namespace, kubeconfig, outputFile string, components []string, collectLogs, collectConfigs, collectEvents, collectMetrics bool) {
util.Printf("[INFO] Starting KubeSlice debug information collection")
util.Printf("[INFO] Target namespace: %s", namespace)
util.Printf("[INFO] Output file: %s", outputFile)

// Create temporary directory for collection
tempDir := fmt.Sprintf("kubeslice-debug-%s", time.Now().Format("20060102-150405"))
if err := os.MkdirAll(tempDir, 0755); err != nil {
util.Fatalf("[ERROR] Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

util.Printf("[SUCCESS] Created temporary directory: %s", tempDir)

// Initialize collection context
ctx := &internal.CollectionContext{
Namespace: namespace,
Kubeconfig: kubeconfig,
TempDir: tempDir,
Components: components,
CollectLogs: collectLogs,
CollectConfigs: collectConfigs,
CollectEvents: collectEvents,
CollectMetrics: collectMetrics,
}

// Collect information for each component
for _, component := range components {
switch strings.ToLower(component) {
case "controller", "ctrl":
util.Printf("[INFO] Collecting Controller information")
internal.CollectControllerInfo(ctx)
case "worker", "wrk":
util.Printf("[INFO] Collecting Worker information")
internal.CollectWorkerInfo(ctx)
case "manager", "mgr":
util.Printf("[INFO] Collecting Manager information")
internal.CollectManagerInfo(ctx)
case "calico":
util.Printf("[INFO] Collecting Calico information")
internal.CollectCalicoInfo(ctx)
case "prometheus", "prom":
util.Printf("[INFO] Collecting Prometheus information")
internal.CollectPrometheusInfo(ctx)
case "resources", "res":
util.Printf("[INFO] Collecting Kubernetes resources")
internal.CollectKubernetesResources(ctx)
case "all":
util.Printf("[INFO] Collecting all component information")
internal.CollectAllComponents(ctx)
default:
util.Printf("[WARN] Unknown component: %s, skipping", component)
}
}

// Create the final archive
util.Printf("[INFO] Creating debug information archive")
if err := internal.CreateArchive(tempDir, outputFile); err != nil {
util.Fatalf("[ERROR] Failed to create archive: %v", err)
}

// Verify the output file
if info, err := os.Stat(outputFile); err == nil {
sizeMB := float64(info.Size()) / (1024 * 1024)
util.Printf("[SUCCESS] Successfully created debug archive: %s (%.2f MB)", outputFile, sizeMB)
} else {
util.Printf("[WARN] Archive created but could not verify size: %s", outputFile)
}

util.Printf("[SUCCESS] Debug information collection completed successfully!")
util.Printf("[INFO] You can now share the archive file: %s", outputFile)
}
Loading