Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add check for too many of a kind #236

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/generated/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,22 @@ KubeLinter includes the following built-in checks:
{"port":22,"protocol":"TCP"}
```

## duplicate-kinds

**Enabled by default**: Yes

**Description**: Indicates when too many of a kind exist within a cluster

**Remediation**: Ensure to not have duplicate's of a kind

**Template**: [Duplicate Kinds](generated/templates.md#duplicate-kinds)

**Parameters**:

````json
{}
````

## unsafe-proc-mount

**Enabled by default**: No
Expand Down
14 changes: 14 additions & 0 deletions docs/generated/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,20 @@ KubeLinter supports the following templates:
]
```

## Duplicate Kinds

**Key**: `duplicate-kinds`

**Description**: Flag containers that have duplicates of a kind

**Supported Objects**: DeploymentLike

**Parameters**:

```json
[]
```

## Unsafe Proc Mount

**Key**: `unsafe-proc-mount`
Expand Down
7 changes: 7 additions & 0 deletions pkg/builtinchecks/yamls/duplicate-kinds.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "duplicate-kinds"
description: "Indicates when too many of a kind exist within a cluster"
remediation: "Ensure to not have duplicate's of a kind"
scope:
objectKinds:
- DeploymentLike
template: "duplicate-kinds"
5 changes: 5 additions & 0 deletions pkg/templates/duplicatekinds/internal/params/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package params

// Params represents the params accepted by this template.
type Params struct {
}
60 changes: 60 additions & 0 deletions pkg/templates/duplicatekinds/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package duplicatekinds


import (
"fmt"

"golang.stackrox.io/kube-linter/pkg/check"
"golang.stackrox.io/kube-linter/pkg/config"
"golang.stackrox.io/kube-linter/pkg/diagnostic"
"golang.stackrox.io/kube-linter/pkg/extract"
"golang.stackrox.io/kube-linter/pkg/lintcontext"
"golang.stackrox.io/kube-linter/pkg/objectkinds"
"golang.stackrox.io/kube-linter/pkg/templates"
"golang.stackrox.io/kube-linter/pkg/templates/duplicatekinds/internal/params"
v1 "k8s.io/api/core/v1"
)

type kindStruct struct{
str string
num int
}

/*{
{"deployment", 0},
{"daemonset", 0},
{"statefulset", 0},
{"service", 0},
}*/

func checkKindDuplicate(){

}

func init() {
templates.Register(check.Template{
HumanName: "Duplicate Kind found",
Key: "duplicate-kinds",
Description: "Flag when too many of a kind exist within a cluster",
SupportedObjectKinds: config.ObjectKindsDesc{
ObjectKinds: []string{objectkinds.*},
},
Parameters: params.ParamDescs,
ParseAndValidateParams: params.ParseAndValidate,
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) {
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic {
kind, found := object.K8sObject.(*v1.kind)
if !found {
return nil
}
var results []diagnostic.Diagnostic
for _, duplicatekinds := range p.ForbiddenServiceTypes {
if strings.EqualFold(string(service.Spec.Type), servicetype) {
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("%q Duplicate Kind found.", duplicatekinds)})
}
}
return results
}, nil
}),
})
}