diff --git a/CHANGELOG.md b/CHANGELOG.md index 8315983..5bb3483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 9c2247e..b3929e5 100644 --- a/README.md +++ b/README.md @@ -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 | | diff --git a/build.sh b/build.sh index 8f20aff..4e81c8e 100644 --- a/build.sh +++ b/build.sh @@ -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} diff --git a/controller.go b/controller.go index e6e4e8a..f9404a7 100644 --- a/controller.go +++ b/controller.go @@ -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 } diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index 10b35f7..6c179c9 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -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 diff --git a/helm/values.yaml b/helm/values.yaml index 0ed90df..d1caa6c 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -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: diff --git a/helpers.go b/helpers.go index 1040e4f..ae57b1d 100644 --- a/helpers.go +++ b/helpers.go @@ -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