-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that string probe port match container port
- Loading branch information
Showing
10 changed files
with
280 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,7 @@ | ||
name: "probe-port" | ||
description: "Alert on probe port that does not match a port defined in container ports" | ||
remediation: "Ensure probe port matches a port defined in container ports." | ||
scope: | ||
objectKinds: | ||
- DeploymentLike | ||
template: "probe-port" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
} |
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,59 @@ | ||
package probeport | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.stackrox.io/kube-linter/internal/set" | ||
"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/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/probeport/internal/params" | ||
"golang.stackrox.io/kube-linter/pkg/templates/util" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Probe Port", | ||
Key: "probe-port", | ||
Description: "Flag unknown probe port", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) { | ||
return util.PerNonInitContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic { | ||
var portNames set.StringSet | ||
for _, port := range container.Ports { | ||
if name := port.Name; len(name) > 0 { | ||
portNames.Add(name) | ||
} | ||
} | ||
var results []diagnostic.Diagnostic | ||
for _, probe := range []*v1.Probe{container.LivenessProbe, container.ReadinessProbe, container.StartupProbe} { | ||
if probe == nil { | ||
continue | ||
} | ||
var port intstr.IntOrString | ||
if httpGet := probe.HTTPGet; httpGet != nil { | ||
port = httpGet.Port | ||
} else if tcpSocket := probe.TCPSocket; tcpSocket != nil { | ||
port = tcpSocket.Port | ||
} else { | ||
continue | ||
} | ||
if port.Type == intstr.String && !portNames.Contains(port.StrVal) && port.IntValue() == 0 { | ||
results = append(results, diagnostic.Diagnostic{ | ||
Message: fmt.Sprintf("probe port %q does not match a port in container %q.", port.StrVal, container.Name), | ||
}) | ||
} | ||
} | ||
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,122 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: no-probe | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: myapp | ||
template: | ||
metadata: | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp | ||
image: myimage | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: livenessProbe-int | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: myapp | ||
template: | ||
metadata: | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp | ||
image: myimage | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
livenessProbe: | ||
httpGet: | ||
port: 1234 | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: readinessProbe-int-str | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: myapp | ||
template: | ||
metadata: | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp | ||
image: myimage | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
readinessProbe: | ||
tcpCheck: | ||
port: "1234" | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: startupProbe-str-container-port-ok | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: myapp | ||
template: | ||
metadata: | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp | ||
image: myimage | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
ports: | ||
- containerPort: 1234 | ||
name: foo | ||
startupProbe: | ||
httpGet: | ||
port: foo | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: startupProbe-str-container-port-ko | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: myapp | ||
template: | ||
metadata: | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp | ||
image: myimage | ||
resources: | ||
limits: | ||
memory: "128Mi" | ||
cpu: "500m" | ||
ports: | ||
- containerPort: 1234 | ||
name: foo | ||
startupProbe: | ||
httpGet: | ||
port: bar |