Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Apr 11, 2023
1 parent 80ac51c commit 4984840
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 85 deletions.
5 changes: 2 additions & 3 deletions cmd/integration/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// activateCmd represents the activate command
Expand All @@ -16,7 +15,7 @@ var activateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
intName := args[0]

integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()
// Check if the integation exists
err := integration.Activate(intName, namespace)
if err != nil {
Expand All @@ -26,7 +25,7 @@ var activateCmd = &cobra.Command{

color.Yellow("Activating analyzer for integration %s", intName)

// TODO:
// Write the integration to the config file

color.Green("Activate integration %s", intName)
},
Expand Down
4 changes: 1 addition & 3 deletions cmd/integration/deactivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// deactivateCmd represents the deactivate command
Expand All @@ -19,8 +18,7 @@ var deactivateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
intName := args[0]

// Check if the integation exists
integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()

if err := integration.Deactivate(intName, namespace); err != nil {
color.Red("Error: %v", err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

var (
namespace string
name string
)

// IntegrationCmd represents the integrate command
var IntegrationCmd = &cobra.Command{
Use: "integration",
Short: "Intergrate another tool into K8sGPT",
Long: `Intergrate another tool into K8sGPT. For example:
k8sgpt integration add trivy
k8sgpt integration activate trivy
This would allow you to deploy trivy into your cluster and use a K8sGPT analyzer to parse trivy results.`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -23,6 +23,5 @@ var IntegrationCmd = &cobra.Command{
}

func init() {
IntegrationCmd.PersistentFlags().StringVarP(&name, "name", "a", "", "The name of the integration")
IntegrationCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "The namespace to use for the integration")
}
29 changes: 24 additions & 5 deletions cmd/integration/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package integration

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// listCmd represents the list command
Expand All @@ -15,12 +15,31 @@ var listCmd = &cobra.Command{
Short: "Lists built-in integrations",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {

integration := viper.Get("integration").(*integration.Integration)
integration := integration.NewIntegration()
integrations := integration.List()

for _, integration := range integrations {
fmt.Printf("> %s\n", color.GreenString(integration))
fmt.Println(color.YellowString("Active:"))
for _, i := range integrations {
b, err := integration.IsActivate(i)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if b {
fmt.Printf("> %s\n", color.GreenString(i))
}
}

fmt.Println(color.YellowString("Unused: "))
for _, i := range integrations {
b, err := integration.IsActivate(i)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !b {
fmt.Printf("> %s\n", color.GreenString(i))
}
}
},
}
Expand Down
5 changes: 0 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"github.com/k8sgpt-ai/k8sgpt/cmd/integration"
in "github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -59,10 +58,6 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)")
rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", kubeconfigPath, "Path to a kubeconfig. Only required if out-of-cluster.")

// Integration start up
integration := in.NewIntegration()
viper.Set("integration", integration)
}

// initConfig reads in config file and ENV variables if set.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
k8s.io/api v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.26.3
k8s.io/kubectl v0.26.3
)

require (
Expand Down Expand Up @@ -154,7 +155,6 @@ require (
k8s.io/component-base v0.26.3 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230327201221-f5883ff37f0c // indirect
k8s.io/kubectl v0.26.3 // indirect
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749 // indirect
oras.land/oras-go v1.2.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
11 changes: 6 additions & 5 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/schollz/progressbar/v3"
"github.com/spf13/viper"
Expand All @@ -20,7 +21,7 @@ type Analysis struct {
Filters []string
Client *kubernetes.Client
AIClient ai.IAI
Results []analyzer.Result
Results []common.Result
Namespace string
NoCache bool
Explain bool
Expand All @@ -34,9 +35,9 @@ const (
)

type JsonOutput struct {
Status AnalysisStatus `json:"status"`
Problems int `json:"problems"`
Results []analyzer.Result `json:"results"`
Status AnalysisStatus `json:"status"`
Problems int `json:"problems"`
Results []common.Result `json:"results"`
}

func (a *Analysis) RunAnalysis() error {
Expand All @@ -45,7 +46,7 @@ func (a *Analysis) RunAnalysis() error {

analyzerMap := analyzer.GetAnalyzerMap()

analyzerConfig := analyzer.Analyzer{
analyzerConfig := common.Analyzer{
Client: a.Client,
Context: a.Context,
Namespace: a.Namespace,
Expand Down
17 changes: 9 additions & 8 deletions pkg/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package analysis
import (
"encoding/json"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/stretchr/testify/require"
"testing"

"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/stretchr/testify/require"
)

func TestAnalysis_NoProblemJsonOutput(t *testing.T) {

analysis := Analysis{
Results: []analyzer.Result{},
Results: []common.Result{},
Namespace: "default",
}

expected := JsonOutput{
Status: StateOK,
Problems: 0,
Results: []analyzer.Result{},
Results: []common.Result{},
}

gotJson, err := analysis.JsonOutput()
Expand All @@ -40,7 +41,7 @@ func TestAnalysis_NoProblemJsonOutput(t *testing.T) {

func TestAnalysis_ProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
Results: []common.Result{
{
"Deployment",
"test-deployment",
Expand All @@ -54,7 +55,7 @@ func TestAnalysis_ProblemJsonOutput(t *testing.T) {
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 1,
Results: []analyzer.Result{
Results: []common.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem"},
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestAnalysis_ProblemJsonOutput(t *testing.T) {

func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
Results: []common.Result{
{
"Deployment",
"test-deployment",
Expand All @@ -96,7 +97,7 @@ func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 2,
Results: []analyzer.Result{
Results: []common.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem", "another-test-problem"},
Expand Down
37 changes: 30 additions & 7 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package analyzer

type IAnalyzer interface {
Analyze(analysis Analyzer) ([]Result, error)
}
import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
)

var coreAnalyzerMap = map[string]IAnalyzer{
var coreAnalyzerMap = map[string]common.IAnalyzer{
"Pod": PodAnalyzer{},
"ReplicaSet": ReplicaSetAnalyzer{},
"PersistentVolumeClaim": PvcAnalyzer{},
Expand All @@ -13,7 +18,7 @@ var coreAnalyzerMap = map[string]IAnalyzer{
"StatefulSet": StatefulSetAnalyzer{},
}

var additionalAnalyzerMap = map[string]IAnalyzer{
var additionalAnalyzerMap = map[string]common.IAnalyzer{
"HorizontalPodAutoScaler": HpaAnalyzer{},
"PodDisruptionBudget": PdbAnalyzer{},
}
Expand All @@ -31,9 +36,9 @@ func ListFilters() ([]string, []string) {
return coreKeys, additionalKeys
}

func GetAnalyzerMap() map[string]IAnalyzer {
func GetAnalyzerMap() map[string]common.IAnalyzer {

mergedMap := make(map[string]IAnalyzer)
mergedMap := make(map[string]common.IAnalyzer)

// add core analyzer
for key, value := range coreAnalyzerMap {
Expand All @@ -45,5 +50,23 @@ func GetAnalyzerMap() map[string]IAnalyzer {
mergedMap[key] = value
}

integrationProvider := integration.NewIntegration()

for _, i := range integrationProvider.List() {
b, err := integrationProvider.IsActivate(i)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
if b {
in, err := integrationProvider.Get(i)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
in.AddAnalyzer(&mergedMap)
}
}

return mergedMap
}
1 change: 1 addition & 0 deletions pkg/analyzer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"context"

"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
10 changes: 6 additions & 4 deletions pkg/analyzer/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package analyzer

import (
"fmt"

"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type HpaAnalyzer struct{}

func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {

list, err := a.Client.GetClient().AutoscalingV1().HorizontalPodAutoscalers(a.Namespace).List(a.Context, metav1.ListOptions{})
if err != nil {
return nil, err
}

var preAnalysis = map[string]PreAnalysis{}
var preAnalysis = map[string]common.PreAnalysis{}

for _, hpa := range list.Items {
var failures []string
Expand Down Expand Up @@ -54,7 +56,7 @@ func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}

if len(failures) > 0 {
preAnalysis[fmt.Sprintf("%s/%s", hpa.Namespace, hpa.Name)] = PreAnalysis{
preAnalysis[fmt.Sprintf("%s/%s", hpa.Namespace, hpa.Name)] = common.PreAnalysis{
HorizontalPodAutoscalers: hpa,
FailureDetails: failures,
}
Expand All @@ -63,7 +65,7 @@ func (HpaAnalyzer) Analyze(a Analyzer) ([]Result, error) {
}

for key, value := range preAnalysis {
var currentAnalysis = Result{
var currentAnalysis = common.Result{
Kind: "HorizontalPodAutoscaler",
Name: key,
Error: value.FailureDetails,
Expand Down
Loading

0 comments on commit 4984840

Please sign in to comment.