mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-07-04 02:56:24 +00:00
feat: init ingress analyzer (#138)
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
This commit is contained in:
parent
cad2b38d03
commit
fe683b71b8
@ -64,6 +64,7 @@ you will be able to write your own analyzers.
|
|||||||
- [x] rsAnalyzer
|
- [x] rsAnalyzer
|
||||||
- [x] serviceAnalyzer
|
- [x] serviceAnalyzer
|
||||||
- [x] eventAnalyzer
|
- [x] eventAnalyzer
|
||||||
|
- [x] ingressAnalyzer
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package analyzer
|
|||||||
import (
|
import (
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
networkingv1 "k8s.io/api/networking/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AnalysisConfiguration struct {
|
type AnalysisConfiguration struct {
|
||||||
@ -17,6 +18,7 @@ type PreAnalysis struct {
|
|||||||
ReplicaSet appsv1.ReplicaSet
|
ReplicaSet appsv1.ReplicaSet
|
||||||
PersistentVolumeClaim v1.PersistentVolumeClaim
|
PersistentVolumeClaim v1.PersistentVolumeClaim
|
||||||
Endpoint v1.Endpoints
|
Endpoint v1.Endpoints
|
||||||
|
Ingress networkingv1.Ingress
|
||||||
}
|
}
|
||||||
|
|
||||||
type Analysis struct {
|
type Analysis struct {
|
||||||
|
@ -34,6 +34,11 @@ func RunAnalysis(ctx context.Context, config *AnalysisConfiguration,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = AnalyzeIngress(ctx, config, client, aiClient, analysisResults)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
pkg/analyzer/ingressAnalyzer.go
Normal file
58
pkg/analyzer/ingressAnalyzer.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user