-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from k8sgpt-ai/feat/additional-analyzers
- Loading branch information
Showing
12 changed files
with
584 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package analyzer | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
cron "github.com/robfig/cron/v3" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type CronJobAnalyzer struct{} | ||
|
||
func (analyzer CronJobAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) { | ||
var results []common.Result | ||
|
||
cronJobList, err := a.Client.GetClient().BatchV1().CronJobs("").List(a.Context, v1.ListOptions{}) | ||
if err != nil { | ||
return results, err | ||
} | ||
|
||
var preAnalysis = map[string]common.PreAnalysis{} | ||
|
||
for _, cronJob := range cronJobList.Items { | ||
var failures []common.Failure | ||
if cronJob.Spec.Suspend != nil && *cronJob.Spec.Suspend { | ||
failures = append(failures, common.Failure{ | ||
Text: fmt.Sprintf("CronJob %s is suspended", cronJob.Name), | ||
Sensitive: []common.Sensitive{ | ||
{ | ||
Unmasked: cronJob.Namespace, | ||
Masked: util.MaskString(cronJob.Namespace), | ||
}, | ||
{ | ||
Unmasked: cronJob.Name, | ||
Masked: util.MaskString(cronJob.Name), | ||
}, | ||
}, | ||
}) | ||
} else { | ||
// check the schedule format | ||
if _, err := CheckCronScheduleIsValid(cronJob.Spec.Schedule); err != nil { | ||
failures = append(failures, common.Failure{ | ||
Text: fmt.Sprintf("CronJob %s has an invalid schedule: %s", cronJob.Name, err.Error()), | ||
Sensitive: []common.Sensitive{ | ||
{ | ||
Unmasked: cronJob.Namespace, | ||
Masked: util.MaskString(cronJob.Namespace), | ||
}, | ||
{ | ||
Unmasked: cronJob.Name, | ||
Masked: util.MaskString(cronJob.Name), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// check the starting deadline | ||
if cronJob.Spec.StartingDeadlineSeconds != nil { | ||
deadline := time.Duration(*cronJob.Spec.StartingDeadlineSeconds) * time.Second | ||
if deadline < 0 { | ||
|
||
failures = append(failures, common.Failure{ | ||
Text: fmt.Sprintf("CronJob %s has a negative starting deadline", cronJob.Name), | ||
Sensitive: []common.Sensitive{ | ||
{ | ||
Unmasked: cronJob.Namespace, | ||
Masked: util.MaskString(cronJob.Namespace), | ||
}, | ||
{ | ||
Unmasked: cronJob.Name, | ||
Masked: util.MaskString(cronJob.Name), | ||
}, | ||
}, | ||
}) | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
if len(failures) > 0 { | ||
preAnalysis[cronJob.Name] = common.PreAnalysis{ | ||
FailureDetails: failures, | ||
} | ||
} | ||
|
||
for key, value := range preAnalysis { | ||
currentAnalysis := common.Result{ | ||
Kind: "CronJob", | ||
Name: key, | ||
Error: value.FailureDetails, | ||
} | ||
a.Results = append(results, currentAnalysis) | ||
} | ||
} | ||
|
||
return a.Results, nil | ||
} | ||
|
||
// Check CRON schedule format | ||
func CheckCronScheduleIsValid(schedule string) (bool, error) { | ||
_, err := cron.ParseStandard(schedule) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} |
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,126 @@ | ||
package analyzer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" | ||
"github.com/magiconair/properties/assert" | ||
batchv1 "k8s.io/api/batch/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestCronJobSuccess(t *testing.T) { | ||
clientset := fake.NewSimpleClientset(&batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-cronjob", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"analysisDate": "2022-04-01", | ||
}, | ||
Labels: map[string]string{ | ||
"app": "example-app", | ||
}, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "*/1 * * * *", | ||
ConcurrencyPolicy: "Allow", | ||
JobTemplate: batchv1.JobTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": "example-app", | ||
}, | ||
}, | ||
Spec: batchv1.JobSpec{ | ||
Template: v1.PodTemplateSpec{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Name: "example-container", | ||
Image: "nginx", | ||
}, | ||
}, | ||
RestartPolicy: v1.RestartPolicyOnFailure, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
config := common.Analyzer{ | ||
Client: &kubernetes.Client{ | ||
Client: clientset, | ||
}, | ||
Context: context.Background(), | ||
Namespace: "default", | ||
} | ||
|
||
analyzer := CronJobAnalyzer{} | ||
analysisResults, err := analyzer.Analyze(config) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, len(analysisResults), 0) | ||
} | ||
|
||
func TestCronJobBroken(t *testing.T) { | ||
clientset := fake.NewSimpleClientset(&batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-cronjob", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"analysisDate": "2022-04-01", | ||
}, | ||
Labels: map[string]string{ | ||
"app": "example-app", | ||
}, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "*** * * * *", | ||
ConcurrencyPolicy: "Allow", | ||
JobTemplate: batchv1.JobTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": "example-app", | ||
}, | ||
}, | ||
Spec: batchv1.JobSpec{ | ||
Template: v1.PodTemplateSpec{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Name: "example-container", | ||
Image: "nginx", | ||
}, | ||
}, | ||
RestartPolicy: v1.RestartPolicyOnFailure, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
config := common.Analyzer{ | ||
Client: &kubernetes.Client{ | ||
Client: clientset, | ||
}, | ||
Context: context.Background(), | ||
Namespace: "default", | ||
} | ||
|
||
analyzer := CronJobAnalyzer{} | ||
analysisResults, err := analyzer.Analyze(config) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, len(analysisResults), 1) | ||
assert.Equal(t, analysisResults[0].Name, "example-cronjob") | ||
assert.Equal(t, analysisResults[0].Kind, "CronJob") | ||
} |
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,62 @@ | ||
package analyzer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
) | ||
|
||
// DeploymentAnalyzer is an analyzer that checks for misconfigured Deployments | ||
type DeploymentAnalyzer struct { | ||
} | ||
|
||
// Analyze scans all namespaces for Deployments with misconfigurations | ||
func (d DeploymentAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) { | ||
|
||
deployments, err := a.Client.GetClient().AppsV1().Deployments("").List(context.Background(), v1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var preAnalysis = map[string]common.PreAnalysis{} | ||
|
||
for _, deployment := range deployments.Items { | ||
var failures []common.Failure | ||
if *deployment.Spec.Replicas != deployment.Status.Replicas { | ||
failures = append(failures, common.Failure{ | ||
Text: fmt.Sprintf("Deployment %s/%s has %d replicas but %d are available", deployment.Namespace, deployment.Name, *deployment.Spec.Replicas, deployment.Status.Replicas), | ||
Sensitive: []common.Sensitive{ | ||
{ | ||
Unmasked: deployment.Namespace, | ||
Masked: util.MaskString(deployment.Namespace), | ||
}, | ||
{ | ||
Unmasked: deployment.Name, | ||
Masked: util.MaskString(deployment.Name), | ||
}, | ||
}}) | ||
} | ||
if len(failures) > 0 { | ||
preAnalysis[fmt.Sprintf("%s/%s", deployment.Namespace, deployment.Name)] = common.PreAnalysis{ | ||
FailureDetails: failures, | ||
Deployment: deployment, | ||
} | ||
} | ||
|
||
} | ||
|
||
for key, value := range preAnalysis { | ||
var currentAnalysis = common.Result{ | ||
Kind: "Deployment", | ||
Name: key, | ||
Error: value.FailureDetails, | ||
} | ||
|
||
a.Results = append(a.Results, currentAnalysis) | ||
} | ||
|
||
return a.Results, nil | ||
} |
Oops, something went wrong.