Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable offline mode for the analyzers #1162

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var (
withDoc bool
interactiveMode bool
customAnalysis bool
offlineMode bool
rcaPath string
)

// AnalyzeCmd represents the problems command
Expand All @@ -48,6 +50,10 @@ var AnalyzeCmd = &cobra.Command{
Long: `This command will find problems within your Kubernetes cluster and
provide you with a list of issues that need to be resolved`,
Run: func(cmd *cobra.Command, args []string) {
if offlineMode && rcaPath == "" {
color.Red("Offline mode of Analysis needs RCA path to be provided to extract the data")
os.Exit(1)
}
// Create analysis configuration first.
config, err := analysis.NewAnalysis(
backend,
Expand All @@ -59,6 +65,8 @@ var AnalyzeCmd = &cobra.Command{
maxConcurrency,
withDoc,
interactiveMode,
offlineMode,
rcaPath,
)

if err != nil {
Expand Down Expand Up @@ -139,4 +147,6 @@ func init() {
// custom analysis flag
AnalyzeCmd.Flags().BoolVarP(&customAnalysis, "custom-analysis", "z", false, "Enable custom analyzers")

AnalyzeCmd.Flags().BoolVar(&offlineMode, "offline-mode", false, "Run Analyzer in Offline mode from RCA collected data")
AnalyzeCmd.Flags().StringVar(&rcaPath, "rca-path", "", "Path Container RCA collected from RCA Collector infra")
}
12 changes: 11 additions & 1 deletion pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes/local"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -79,11 +80,20 @@ func NewAnalysis(
maxConcurrency int,
withDoc bool,
interactiveMode bool,
offlineMode bool,
rcaPath string,
) (*Analysis, error) {
// Get kubernetes client from viper.
kubecontext := viper.GetString("kubecontext")
kubeconfig := viper.GetString("kubeconfig")
client, err := kubernetes.NewClient(kubecontext, kubeconfig)
var client *kubernetes.Client
var err error
if offlineMode {
client = &kubernetes.Client{}
client.Client, client.CtrlClient = local.GetLocalClient(rcaPath)
} else {
client, err = kubernetes.NewClient(kubecontext, kubeconfig)
}
if err != nil {
return nil, fmt.Errorf("initialising kubernetes client: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (c *Client) GetCtrlClient() ctrl.Client {
return c.CtrlClient
}

func (c *Client) SetKubernetesClient(k kubernetes.Interface) {
c.Client = k
}

func (c *Client) SetCtrlClient(k ctrl.Client) {
c.CtrlClient = k
}

func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
var config *rest.Config
config, err := rest.InClusterConfig()
Expand Down
Loading