-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
- Loading branch information
1 parent
5dcc190
commit 23071fd
Showing
11 changed files
with
527 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package analyzer | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
cron "github.com/robfig/cron/v3" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type CronJobAnalyzer struct{} | ||
|
||
func (analyzer CronJobAnalyzer) Analyze(config common.Analyzer) ([]common.Result, error) { | ||
var results []common.Result | ||
|
||
cronJobList, err := config.Client.GetClient().BatchV1().CronJobs("").List(config.Context, v1.ListOptions{}) | ||
if err != nil { | ||
return results, err | ||
} | ||
|
||
for _, cronJob := range cronJobList.Items { | ||
result := common.Result{ | ||
Kind: "CronJob", | ||
Name: cronJob.Name, | ||
} | ||
|
||
if cronJob.Spec.Suspend != nil && *cronJob.Spec.Suspend { | ||
result.Error = append(result.Error, common.Failure{ | ||
Text: fmt.Sprintf("CronJob %s is suspended", cronJob.Name), | ||
Sensitive: []common.Sensitive{}, | ||
}) | ||
} else { | ||
// check the schedule format | ||
if _, err := CheckCronScheduleIsValid(cronJob.Spec.Schedule); err != nil { | ||
result.Error = append(result.Error, common.Failure{ | ||
Text: fmt.Sprintf("CronJob %s has an invalid schedule: %s", cronJob.Name, cronJob.Spec.Schedule), | ||
Sensitive: []common.Sensitive{}, | ||
}) | ||
} | ||
|
||
// check the starting deadline | ||
if cronJob.Spec.StartingDeadlineSeconds != nil { | ||
deadline := time.Duration(*cronJob.Spec.StartingDeadlineSeconds) * time.Second | ||
if deadline < 0 { | ||
|
||
result = common.Result{ | ||
Kind: "CronJob", | ||
Name: cronJob.Name, | ||
Error: []common.Failure{ | ||
{ | ||
Text: fmt.Sprintf("CronJob %s has a negative starting deadline: %d seconds", cronJob.Name, *cronJob.Spec.StartingDeadlineSeconds), | ||
Sensitive: []common.Sensitive{}, | ||
}, | ||
}, | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
results = append(results, result) | ||
} | ||
|
||
return 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,130 @@ | ||
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) | ||
assert.Equal(t, analysisResults[0].Name, "example-cronjob") | ||
assert.Equal(t, analysisResults[0].Kind, "CronJob") | ||
assert.Equal(t, analysisResults[0].Error, "CronJob 'example-cronjob' has an annotation 'analysisDate', indicating it may need to be reviewed.") | ||
|
||
} | ||
|
||
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,46 @@ | ||
package analyzer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
) | ||
|
||
// 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) { | ||
|
||
var results []common.Result | ||
deployments, err := a.Client.GetClient().AppsV1().Deployments("").List(context.Background(), v1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, deployment := range deployments.Items { | ||
if *deployment.Spec.Replicas != deployment.Status.Replicas { | ||
failureDetails := []common.Failure{ | ||
{ | ||
Text: fmt.Sprintf("Deployment %s has a mismatch between the desired and actual replicas", deployment.Name), | ||
Sensitive: []common.Sensitive{}, | ||
}, | ||
} | ||
|
||
result := common.Result{ | ||
Kind: "Deployment", | ||
Name: fmt.Sprintf("%s/%s", deployment.Namespace, deployment.Name), | ||
Error: failureDetails, | ||
ParentObject: "", | ||
} | ||
|
||
results = append(results, result) | ||
} | ||
} | ||
|
||
return results, 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,64 @@ | ||
package analyzer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt/pkg/common" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" | ||
"github.com/magiconair/properties/assert" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestDeploymentAnalyzer(t *testing.T) { | ||
clientset := fake.NewSimpleClientset(&appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example", | ||
Namespace: "default", | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: func() *int32 { i := int32(3); return &i }(), | ||
Template: v1.PodTemplateSpec{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Name: "example-container", | ||
Image: "nginx", | ||
Ports: []v1.ContainerPort{ | ||
{ | ||
ContainerPort: 80, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Status: appsv1.DeploymentStatus{ | ||
Replicas: 2, | ||
AvailableReplicas: 1, | ||
}, | ||
}) | ||
|
||
config := common.Analyzer{ | ||
Client: &kubernetes.Client{ | ||
Client: clientset, | ||
}, | ||
Context: context.Background(), | ||
Namespace: "default", | ||
} | ||
|
||
deploymentAnalyzer := DeploymentAnalyzer{} | ||
analysisResults, err := deploymentAnalyzer.Analyze(config) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
assert.Equal(t, len(analysisResults), 1) | ||
assert.Equal(t, analysisResults[0].Kind, "Deployment") | ||
assert.Equal(t, analysisResults[0].Name, "default/example") | ||
assert.Equal(t, len(analysisResults[0].Error), 1) | ||
assert.Equal(t, analysisResults[0].Error[0].Text, "Deployment example has a mismatch between the desired and actual replicas") | ||
} |
Oops, something went wrong.