-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hpa analyzer and init additionalAnalyzers
Signed-off-by: Matthis Holleville <[email protected]>
- Loading branch information
1 parent
0e3ef8d
commit 3603872
Showing
7 changed files
with
140 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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 AnalyzeHpa(ctx context.Context, config *AnalysisConfiguration, client *kubernetes.Client, aiClient ai.IAI, | ||
analysisResults *[]Analysis) error { | ||
|
||
list, err := client.GetClient().AutoscalingV1().HorizontalPodAutoscalers(config.Namespace).List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var preAnalysis = map[string]PreAnalysis{} | ||
|
||
for _, hpa := range list.Items { | ||
var failures []string | ||
|
||
// check ScaleTargetRef exist | ||
scaleTargetRef := hpa.Spec.ScaleTargetRef | ||
scaleTargetRefNotFound := false | ||
|
||
switch scaleTargetRef.Kind { | ||
case "Deployment": | ||
_, err := client.GetClient().AppsV1().Deployments(config.Namespace).Get(ctx, scaleTargetRef.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
scaleTargetRefNotFound = true | ||
} | ||
case "ReplicationController": | ||
_, err := client.GetClient().CoreV1().ReplicationControllers(config.Namespace).Get(ctx, scaleTargetRef.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
scaleTargetRefNotFound = true | ||
} | ||
case "ReplicaSet": | ||
_, err := client.GetClient().AppsV1().ReplicaSets(config.Namespace).Get(ctx, scaleTargetRef.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
scaleTargetRefNotFound = true | ||
} | ||
case "StatefulSet": | ||
_, err := client.GetClient().AppsV1().Deployments(config.Namespace).Get(ctx, scaleTargetRef.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
scaleTargetRefNotFound = true | ||
} | ||
} | ||
|
||
if scaleTargetRefNotFound { | ||
failures = append(failures, fmt.Sprintf("HorizontalPodAutoscaler uses %s/%s as ScaleTargetRef which does not exist.", scaleTargetRef.Kind, scaleTargetRef.Name)) | ||
} | ||
|
||
if len(failures) > 0 { | ||
preAnalysis[fmt.Sprintf("%s/%s", hpa.Namespace, hpa.Name)] = PreAnalysis{ | ||
HorizontalPodAutoscalers: hpa, | ||
FailureDetails: failures, | ||
} | ||
} | ||
|
||
} | ||
|
||
for key, value := range preAnalysis { | ||
var currentAnalysis = Analysis{ | ||
Kind: "HorizontalPodAutoscaler", | ||
Name: key, | ||
Error: value.FailureDetails, | ||
} | ||
|
||
parent, _ := util.GetParent(client, value.Ingress.ObjectMeta) | ||
currentAnalysis.ParentObject = parent | ||
*analysisResults = append(*analysisResults, currentAnalysis) | ||
} | ||
|
||
return nil | ||
} |