Skip to content

Commit

Permalink
feat: init ingress analyzer (#138)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Mar 29, 2023
1 parent cad2b38 commit fe683b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ you will be able to write your own analyzers.
- [x] rsAnalyzer
- [x] serviceAnalyzer
- [x] eventAnalyzer
- [x] ingressAnalyzer

## Usage

Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +18,7 @@ type PreAnalysis struct {
ReplicaSet appsv1.ReplicaSet
PersistentVolumeClaim v1.PersistentVolumeClaim
Endpoint v1.Endpoints
Ingress networkingv1.Ingress
}

type Analysis struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/analyzer/ingressAnalyzer.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fe683b7

Please sign in to comment.