Skip to content

Commit

Permalink
feat: Add with-doc flag to enable/disable kubernetes doc
Browse files Browse the repository at this point in the history
Signed-off-by: David Sabatie <david.sabatie@notrenet.com>
  • Loading branch information
golgoth31 committed May 28, 2023
1 parent de6e55c commit 5c2c0db
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 30 deletions.
5 changes: 4 additions & 1 deletion cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
namespace string
anonymize bool
maxConcurrency int
withDoc bool
)

// AnalyzeCmd represents the problems command
Expand All @@ -45,7 +46,7 @@ var AnalyzeCmd = &cobra.Command{

// AnalysisResult configuration
config, err := analysis.NewAnalysis(backend,
language, filters, namespace, nocache, explain, maxConcurrency)
language, filters, namespace, nocache, explain, maxConcurrency, withDoc)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
Expand Down Expand Up @@ -91,4 +92,6 @@ func init() {
AnalyzeCmd.Flags().StringVarP(&language, "language", "l", "english", "Languages to use for AI (e.g. 'English', 'Spanish', 'French', 'German', 'Italian', 'Portuguese', 'Dutch', 'Russian', 'Chinese', 'Japanese', 'Korean')")
// add max concurrency
AnalyzeCmd.Flags().IntVarP(&maxConcurrency, "max-concurrency", "m", 10, "Maximum number of concurrent requests to the Kubernetes API server")
// kubernetes doc flag
AnalyzeCmd.Flags().BoolVarP(&withDoc, "with-doc", "d", false, "Give me the official documentation of the involved field")
}
25 changes: 20 additions & 5 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync"

"github.com/fatih/color"
openapi_v2 "github.com/google/gnostic/openapiv2"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
Expand All @@ -45,6 +46,7 @@ type Analysis struct {
Explain bool
MaxConcurrency int
AnalysisAIProvider string // The name of the AI Provider used for this analysis
WithDoc bool
}

type AnalysisStatus string
Expand All @@ -63,7 +65,7 @@ type JsonOutput struct {
Results []common.Result `json:"results"`
}

func NewAnalysis(backend string, language string, filters []string, namespace string, noCache bool, explain bool, maxConcurrency int) (*Analysis, error) {
func NewAnalysis(backend string, language string, filters []string, namespace string, noCache bool, explain bool, maxConcurrency int, withDoc bool) (*Analysis, error) {
var configAI ai.AIConfiguration
err := viper.UnmarshalKey("ai", &configAI)
if err != nil {
Expand Down Expand Up @@ -128,6 +130,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
Explain: explain,
MaxConcurrency: maxConcurrency,
AnalysisAIProvider: backend,
WithDoc: withDoc,
}, nil
}

Expand All @@ -136,11 +139,23 @@ func (a *Analysis) RunAnalysis() {

coreAnalyzerMap, analyzerMap := analyzer.GetAnalyzerMap()

// we get the openapi schema from the server only if required by the flag "with-doc"
openapiSchema := &openapi_v2.Document{}
if a.WithDoc {
var openApiErr error

openapiSchema, openApiErr = a.Client.Client.Discovery().OpenAPISchema()
if openApiErr != nil {
a.Errors = append(a.Errors, fmt.Sprintf("[KubernetesDoc] %s", openApiErr))
}
}

analyzerConfig := common.Analyzer{
Client: a.Client,
Context: a.Context,
Namespace: a.Namespace,
AIClient: a.AIClient,
Client: a.Client,
Context: a.Context,
Namespace: a.Namespace,
AIClient: a.AIClient,
OpenapiSchema: openapiSchema,
}

semaphore := make(chan struct{}, a.MaxConcurrency)
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (analyzer CronJobAnalyzer) Analyze(a common.Analyzer) ([]common.Result, err
Group: "batch",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (d DeploymentAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error)
Group: "apps",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
Group: "autoscaling",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (IngressAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
Group: "networking",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/netpol.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (NetworkPolicyAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error)
Group: "networking",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (PdbAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
Group: "policy",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (ServiceAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
Group: "",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (StatefulSetAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
Group: "apps",
Version: "v1",
},
Discovery: a.Client.Client.Discovery(),
OpenapiSchema: a.OpenapiSchema,
}

AnalyzerErrorsMetric.DeletePartialMatch(map[string]string{
Expand Down
14 changes: 8 additions & 6 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"

trivy "github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
openapi_v2 "github.com/google/gnostic/openapiv2"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -31,12 +32,13 @@ type IAnalyzer interface {
}

type Analyzer struct {
Client *kubernetes.Client
Context context.Context
Namespace string
AIClient ai.IAI
PreAnalysis map[string]PreAnalysis
Results []Result
Client *kubernetes.Client
Context context.Context
Namespace string
AIClient ai.IAI
PreAnalysis map[string]PreAnalysis
Results []Result
OpenapiSchema *openapi_v2.Document
}

type PreAnalysis struct {
Expand Down
12 changes: 7 additions & 5 deletions pkg/kubernetes/apireference.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ func (k *K8sApiReference) GetApiDocV2(field string) string {
paths := strings.Split(field, ".")
group := strings.Split(k.ApiVersion.Group, ".")

openapiSchema, err := k.Discovery.OpenAPISchema()
if err != nil {
return ""
}
// openapiSchema := openapi_v2.Document{}

// openapiSchema, err := k.Discovery.OpenAPISchema()
// if err != nil {
// return ""
// }

definitions := openapiSchema.GetDefinitions().GetAdditionalProperties()
definitions := k.OpenapiSchema.GetDefinitions().GetAdditionalProperties()

// extract the startpoint by searching the highest leaf corresponding to the requested group qnd kind
for _, prop := range definitions {
Expand Down
9 changes: 4 additions & 5 deletions pkg/kubernetes/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kubernetes

import (
openapi_v2 "github.com/google/gnostic/openapiv2"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
Expand All @@ -16,8 +16,7 @@ type Client struct {
}

type K8sApiReference struct {
ApiVersion schema.GroupVersion
Kind string
// Property string
Discovery discovery.DiscoveryInterface
ApiVersion schema.GroupVersion
Kind string
OpenapiSchema *openapi_v2.Document
}
1 change: 1 addition & 0 deletions pkg/server/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) (
i.Nocache,
i.Explain,
int(i.MaxConcurrency),
false, // Kubernetes Doc disabled in server mode
)
if err != nil {
return &schemav1.AnalyzeResponse{}, err
Expand Down

0 comments on commit 5c2c0db

Please sign in to comment.