Skip to content

Commit 55903d0

Browse files
able8able8
and
able8
authored
feat: Add support for watchedNamespaces and watchedPodNamePrefixes (#33)
* feat: Add support for watchedNamespaces and watchedPodNamePrefixes * prefix version number with v to maintain consistency with image tags --------- Co-authored-by: able8 <[email protected]>
1 parent 007bfc7 commit 55903d0

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ 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
7+
8+
## [v1.4.0] - 2023-05-12
9+
### Added
10+
- Add support for `watchedNamespaces` and `watchedPodNamePrefixes` [#14](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/14)
11+
12+
## [v1.3.0] - 2023-05-11
813
### Added
914
- 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)
1015

11-
## [1.2.1] - 2023-04-27
16+
## [v1.2.1] - 2023-04-27
1217
### Fixed
1318
- Container resource specs showing wrong values [#26](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/26)
1419

1520
### Improved
1621
- Add backticks to format slack message nicely [#25](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/25)
1722

18-
## [1.2.0] - 2023-01-03
23+
## [v1.2.0] - 2023-01-03
1924
### Added
2025
- Parameterize pod restart count
2126

22-
## [1.1.0] - 2022-09-19
27+
## [v1.1.0] - 2022-09-19
2328
### Added
2429
- Support ignoring specific namespaces and pods
2530

26-
## [1.0.0] - 2022-08-29
31+
## [v1.0.0] - 2022-08-29
2732
### Added
2833
- Initial release as Open-Source under the Apache License v2.0

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ 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+
| `watchedNamespaces` | A comma-separated list of namespaces to watch, default is all ("")| default: `""`
83+
| `watchedPodNamePrefixes` | A comma-separated list of pod name prefixes to watch, default is all ("")| default: `""`
8284
| `ignoreRestartsWithExitCodeZero` | Whether restart events with an exit code of 0 should be ignored | default: `false`
8385
| `slackWebhookUrl` | Slack webhook URL | required if slackWebhooUrlSecretKeyRef is not present |
8486
| `slackWebhookurlSecretKeyRef.key` | Slack webhook URL SecretKeyRef.key | |

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.3.0"
2+
TAG="v1.4.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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ func NewController(clientset kubernetes.Interface, slack Slack) *Controller {
5454
return
5555
}
5656

57-
if isIgnoredNamespace(newPod.Namespace) {
57+
if !isWatchedNamespace(newPod.Namespace) || isIgnoredNamespace(newPod.Namespace) {
5858
return
5959
}
6060

61-
if isIgnoredPod(newPod.Name) {
61+
if !isWatchedPod(newPod.Name) || isIgnoredPod(newPod.Name) {
6262
return
6363
}
6464

helm/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ spec:
4343
value: {{ .Values.ignoreRestartCount | quote}}
4444
- name: IGNORED_NAMESPACES
4545
value: {{ .Values.ignoredNamespaces | quote}}
46+
- name: WATCHED_NAMESPACES
47+
value: {{ .Values.watchedNamespaces | quote}}
48+
- name: WATCHED_POD_NAME_PREFIXES
49+
value: {{ .Values.watchedPodNamePrefixes | quote}}
4650
- name: IGNORED_POD_NAME_PREFIXES
4751
value: {{ .Values.ignoredPodNamePrefixes | quote}}
4852
- name: IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO

helm/values.yaml

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

21+
# A comma-separated list of namespaces to watch, default is all ("")
22+
watchedNamespaces: ""
23+
# A comma-separated list of pod name prefixes to watch, default is all ("").
24+
watchedPodNamePrefixes: ""
25+
2126
# Whether restart events with an exit code of 0 should be ignored, true or false
2227
ignoreRestartsWithExitCodeZero: false
2328

2429
image:
2530
repository: devopsairwallex/k8s-pod-restart-info-collector
26-
tag: "v1.3.0"
31+
tag: "v1.4.0"
2732

2833
resources:
2934
limits:

helpers.go

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

60+
func isWatchedNamespace(namespace string) bool {
61+
watchedNamespacesEnv := os.Getenv("WATCHED_NAMESPACES")
62+
if watchedNamespacesEnv == "" {
63+
return true
64+
}
65+
watchedNamespaces := strings.Split(watchedNamespacesEnv, ",")
66+
for _, watchedNamespace := range watchedNamespaces {
67+
if watchedNamespace == namespace {
68+
return true
69+
}
70+
}
71+
72+
// Turn off logging as there are too many logs.
73+
// klog.Infof("Ignore: namespace %s is not on the watched namespace list\n", namespace)
74+
return false
75+
}
76+
77+
func isWatchedPod(name string) bool {
78+
watchedPodNamePrefixesEnv := os.Getenv("WATCHED_POD_NAME_PREFIXES")
79+
if watchedPodNamePrefixesEnv == "" {
80+
return true
81+
}
82+
watchedPodNamePrefixes := strings.Split(watchedPodNamePrefixesEnv, ",")
83+
for _, watchedPodNamePrefix := range watchedPodNamePrefixes {
84+
if strings.HasPrefix(name, watchedPodNamePrefix) {
85+
return true
86+
}
87+
}
88+
// klog.Infof("Ignore: pod %s doesn't have the watched pod name prefixes\n", name)
89+
return false
90+
}
91+
6092
func shouldIgnoreRestartsWithExitCodeZero(status v1.ContainerStatus) bool {
6193
if os.Getenv("IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO") != "true" {
6294
return false

0 commit comments

Comments
 (0)