Skip to content

Commit 007bfc7

Browse files
able8able8
andauthored
feat: Add flag to ignore restart events with exit code 0 (#32)
Co-authored-by: able8 <able.lv@airwallex.com>
1 parent 291bce9 commit 007bfc7

7 files changed

Lines changed: 29 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.3.0] - 2023-05-11
8+
### Added
9+
- Add `ignoreRestartsWithExitCodeZero` flag to ignore restart events with an exit code of 0 [#22](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/22)
10+
711
## [1.2.1] - 2023-04-27
812
### Fixed
913
- Container resource specs showing wrong values [#26](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/26)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ helm uninstall k8s-pod-restart-info-collector
7979
| `ignoreRestartCount` | The number of pod restart count to ignore | default: `"30"`
8080
| `ignoredNamespaces` | A comma-separated list of namespaces to ignore | default: `""`
8181
| `ignoredPodNamePrefixes` | A comma-separated list of pod name prefixes to ignore | default: `""`
82+
| `ignoreRestartsWithExitCodeZero` | Whether restart events with an exit code of 0 should be ignored | default: `false`
8283
| `slackWebhookUrl` | Slack webhook URL | required if slackWebhooUrlSecretKeyRef is not present |
8384
| `slackWebhookurlSecretKeyRef.key` | Slack webhook URL SecretKeyRef.key | |
8485
| `slackWebhookurlSecretKeyRef.name` | Slack webhook URL SecretKeyRef.name | |

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
TAG="v1.2.1"
2+
TAG="v1.3.0"
33
docker buildx build --platform linux/amd64 -t devopsairwallex/k8s-pod-restart-info-collector:${TAG} .
44
docker push devopsairwallex/k8s-pod-restart-info-collector:${TAG}
55

controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ func (c *Controller) handlePod(pod *v1.Pod) error {
218218
continue
219219
}
220220

221-
klog.Infof("Handle: %s restarted! , restartCount: %d\n\n", podKey, status.RestartCount)
221+
if shouldIgnoreRestartsWithExitCodeZero(status) {
222+
klog.Infof("Ignore: %s restarted with ExitCode 0, restartCount: %d\n", podKey, status.RestartCount)
223+
continue
224+
}
225+
226+
klog.Infof("Handle: %s restarted, restartCount: %d\n", podKey, status.RestartCount)
222227

223228
podInfo, err := printPod(pod)
224229
if err != nil {

helm/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ spec:
4545
value: {{ .Values.ignoredNamespaces | quote}}
4646
- name: IGNORED_POD_NAME_PREFIXES
4747
value: {{ .Values.ignoredPodNamePrefixes | quote}}
48+
- name: IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO
49+
value: {{ .Values.ignoreRestartsWithExitCodeZero | quote}}
4850
- name: SLACK_WEBHOOK_URL
4951
valueFrom:
5052
{{- include "k8s-pod-restart-info-collector.SlackWebhookUrlSecret" . | indent 14 }}

helm/values.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ ignoredNamespaces: ""
1818
# A comma-separated list of pod name prefixes to ignore
1919
ignoredPodNamePrefixes: ""
2020

21+
# Whether restart events with an exit code of 0 should be ignored, true or false
22+
ignoreRestartsWithExitCodeZero: false
23+
2124
image:
2225
repository: devopsairwallex/k8s-pod-restart-info-collector
23-
tag: "v1.1.0"
26+
tag: "v1.3.0"
2427

2528
resources:
2629
limits:

helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ func isIgnoredPod(name string) bool {
5757
return false
5858
}
5959

60+
func shouldIgnoreRestartsWithExitCodeZero(status v1.ContainerStatus) bool {
61+
if os.Getenv("IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO") != "true" {
62+
return false
63+
}
64+
65+
if status.LastTerminationState.Terminated != nil && status.LastTerminationState.Terminated.ExitCode == 0 {
66+
return true
67+
}
68+
return false
69+
}
70+
6071
func getIgnoreRestartCount() int {
6172
ignoreRestartCount, err := strconv.Atoi(os.Getenv("IGNORE_RESTART_COUNT"))
6273
if err != nil {

0 commit comments

Comments
 (0)