diff --git a/README.md b/README.md index 53e45e4e1b..e4d74b11ae 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ you will be able to write your own analyzers. - [x] rsAnalyzer - [x] serviceAnalyzer - [x] eventAnalyzer +- [x] ingressAnalyzer ## Usage diff --git a/pkg/analyzer/analysis.go b/pkg/analyzer/analysis.go index 07ef4b146f..b05ef07b55 100644 --- a/pkg/analyzer/analysis.go +++ b/pkg/analyzer/analysis.go @@ -3,6 +3,7 @@ package analyzer import ( appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" ) type AnalysisConfiguration struct { @@ -17,6 +18,7 @@ type PreAnalysis struct { ReplicaSet appsv1.ReplicaSet PersistentVolumeClaim v1.PersistentVolumeClaim Endpoint v1.Endpoints + Ingress networkingv1.Ingress } type Analysis struct { diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index d45e90cec7..627928b361 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -34,6 +34,11 @@ func RunAnalysis(ctx context.Context, config *AnalysisConfiguration, if err != nil { return err } + + err = AnalyzeIngress(ctx, config, client, aiClient, analysisResults) + if err != nil { + return err + } return nil } diff --git a/pkg/analyzer/ingressAnalyzer.go b/pkg/analyzer/ingressAnalyzer.go new file mode 100644 index 0000000000..6b094cb3b0 --- /dev/null +++ b/pkg/analyzer/ingressAnalyzer.go @@ -0,0 +1,58 @@ +package analyzer + +import ( + "context" + "fmt" + + "github.com/k8sgpt-ai/k8sgpt/pkg/ai" + "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" + "github.com/k8sgpt-ai/k8sgpt/pkg/util" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func AnalyzeIngress(ctx context.Context, config *AnalysisConfiguration, client *kubernetes.Client, aiClient ai.IAI, + analysisResults *[]Analysis) error { + + list, err := client.GetClient().NetworkingV1().Ingresses(config.Namespace).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + + var preAnalysis = map[string]PreAnalysis{} + + for _, ing := range list.Items { + var failures []string + // loop over rules + for _, rule := range ing.Spec.Rules { + // loop over paths + for _, path := range rule.HTTP.Paths { + _, err := client.GetClient().CoreV1().Services(ing.Namespace).Get(ctx, path.Backend.Service.Name, metav1.GetOptions{}) + if err != nil { + failures = append(failures, fmt.Sprintf("Ingress uses the service %s/%s which does not exist.", ing.Namespace, path.Backend.Service.Name)) + } + } + } + + if len(failures) > 0 { + preAnalysis[fmt.Sprintf("%s/%s", ing.Namespace, ing.Name)] = PreAnalysis{ + Ingress: ing, + FailureDetails: failures, + } + } + + } + + for key, value := range preAnalysis { + var currentAnalysis = Analysis{ + Kind: "Ingress", + Name: key, + Error: value.FailureDetails, + } + + parent, _ := util.GetParent(client, value.Endpoint.ObjectMeta) + currentAnalysis.ParentObject = parent + *analysisResults = append(*analysisResults, currentAnalysis) + } + + return nil +}