Skip to content

Commit

Permalink
feat: Add support for watchedNamespaces and watchedPodNamePrefixes (#33)
Browse files Browse the repository at this point in the history
* feat: Add support for watchedNamespaces and watchedPodNamePrefixes

* prefix version number with v to maintain consistency with image tags

---------

Co-authored-by: able8 <[email protected]>
  • Loading branch information
able8 and able8 authored May 12, 2023
1 parent 007bfc7 commit 55903d0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2023-05-11

## [v1.4.0] - 2023-05-12
### Added
- Add support for `watchedNamespaces` and `watchedPodNamePrefixes` [#14](https://github.com/airwallex/k8s-pod-restart-info-collector/issues/14)

## [v1.3.0] - 2023-05-11
### Added
- 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)

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

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

## [1.2.0] - 2023-01-03
## [v1.2.0] - 2023-01-03
### Added
- Parameterize pod restart count

## [1.1.0] - 2022-09-19
## [v1.1.0] - 2022-09-19
### Added
- Support ignoring specific namespaces and pods

## [1.0.0] - 2022-08-29
## [v1.0.0] - 2022-08-29
### Added
- Initial release as Open-Source under the Apache License v2.0
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ helm uninstall k8s-pod-restart-info-collector
| `ignoreRestartCount` | The number of pod restart count to ignore | default: `"30"`
| `ignoredNamespaces` | A comma-separated list of namespaces to ignore | default: `""`
| `ignoredPodNamePrefixes` | A comma-separated list of pod name prefixes to ignore | default: `""`
| `watchedNamespaces` | A comma-separated list of namespaces to watch, default is all ("")| default: `""`
| `watchedPodNamePrefixes` | A comma-separated list of pod name prefixes to watch, default is all ("")| default: `""`
| `ignoreRestartsWithExitCodeZero` | Whether restart events with an exit code of 0 should be ignored | default: `false`
| `slackWebhookUrl` | Slack webhook URL | required if slackWebhooUrlSecretKeyRef is not present |
| `slackWebhookurlSecretKeyRef.key` | Slack webhook URL SecretKeyRef.key | |
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
TAG="v1.3.0"
TAG="v1.4.0"
docker buildx build --platform linux/amd64 -t devopsairwallex/k8s-pod-restart-info-collector:${TAG} .
docker push devopsairwallex/k8s-pod-restart-info-collector:${TAG}

Expand Down
4 changes: 2 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func NewController(clientset kubernetes.Interface, slack Slack) *Controller {
return
}

if isIgnoredNamespace(newPod.Namespace) {
if !isWatchedNamespace(newPod.Namespace) || isIgnoredNamespace(newPod.Namespace) {
return
}

if isIgnoredPod(newPod.Name) {
if !isWatchedPod(newPod.Name) || isIgnoredPod(newPod.Name) {
return
}

Expand Down
4 changes: 4 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ spec:
value: {{ .Values.ignoreRestartCount | quote}}
- name: IGNORED_NAMESPACES
value: {{ .Values.ignoredNamespaces | quote}}
- name: WATCHED_NAMESPACES
value: {{ .Values.watchedNamespaces | quote}}
- name: WATCHED_POD_NAME_PREFIXES
value: {{ .Values.watchedPodNamePrefixes | quote}}
- name: IGNORED_POD_NAME_PREFIXES
value: {{ .Values.ignoredPodNamePrefixes | quote}}
- name: IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO
Expand Down
7 changes: 6 additions & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ ignoredNamespaces: ""
# A comma-separated list of pod name prefixes to ignore
ignoredPodNamePrefixes: ""

# A comma-separated list of namespaces to watch, default is all ("")
watchedNamespaces: ""
# A comma-separated list of pod name prefixes to watch, default is all ("").
watchedPodNamePrefixes: ""

# Whether restart events with an exit code of 0 should be ignored, true or false
ignoreRestartsWithExitCodeZero: false

image:
repository: devopsairwallex/k8s-pod-restart-info-collector
tag: "v1.3.0"
tag: "v1.4.0"

resources:
limits:
Expand Down
32 changes: 32 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ func isIgnoredPod(name string) bool {
return false
}

func isWatchedNamespace(namespace string) bool {
watchedNamespacesEnv := os.Getenv("WATCHED_NAMESPACES")
if watchedNamespacesEnv == "" {
return true
}
watchedNamespaces := strings.Split(watchedNamespacesEnv, ",")
for _, watchedNamespace := range watchedNamespaces {
if watchedNamespace == namespace {
return true
}
}

// Turn off logging as there are too many logs.
// klog.Infof("Ignore: namespace %s is not on the watched namespace list\n", namespace)
return false
}

func isWatchedPod(name string) bool {
watchedPodNamePrefixesEnv := os.Getenv("WATCHED_POD_NAME_PREFIXES")
if watchedPodNamePrefixesEnv == "" {
return true
}
watchedPodNamePrefixes := strings.Split(watchedPodNamePrefixesEnv, ",")
for _, watchedPodNamePrefix := range watchedPodNamePrefixes {
if strings.HasPrefix(name, watchedPodNamePrefix) {
return true
}
}
// klog.Infof("Ignore: pod %s doesn't have the watched pod name prefixes\n", name)
return false
}

func shouldIgnoreRestartsWithExitCodeZero(status v1.ContainerStatus) bool {
if os.Getenv("IGNORE_RESTARTS_WITH_EXIT_CODE_ZERO") != "true" {
return false
Expand Down

0 comments on commit 55903d0

Please sign in to comment.