Skip to content

Commit

Permalink
feat: enable offline mode for the analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Jun 19, 2024
1 parent 3f80bba commit f3eef9b
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 2 deletions.
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

0 comments on commit f3eef9b

Please sign in to comment.