diff --git a/README.md b/README.md index 2a9fd5a..2fc66d2 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ The `//` is very important, it's a terraform specific syntax used to separate gi - [cluster](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/caas/kubernetes/cluster/) - [ingress](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/caas/kubernetes/ingress/) - [vts](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/caas/kubernetes/ingress/vts/) + - [pod](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/caas/kubernetes/pod/) - [workload](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/caas/kubernetes/workload/) - [cloud](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/cloud/) - [aws](https://git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors/tree/master/cloud/aws/) diff --git a/caas/kubernetes/pod/README.md b/caas/kubernetes/pod/README.md new file mode 100644 index 0000000..f22226e --- /dev/null +++ b/caas/kubernetes/pod/README.md @@ -0,0 +1,69 @@ +# CAAS KUBERNETES POD DataDog monitors + +## How to use this module + +``` +module "datadog-monitors-caas-kubernetes-pod" { + source = "git::ssh://git@git.fr.clara.net/claranet/pt-monitoring/projects/datadog/terraform/monitors.git//caas/kubernetes/pod?ref={revision}" + + environment = "${var.environment}" + message = "${module.datadog-message-alerting.alerting-message}" +} + +``` + +## Purpose + +Creates DataDog monitors with the following checks: + +- Kubernetes Pod CrashLoopBackOff +- Kubernetes Pod errors +- Kubernetes Pod phase status failed + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| crashloopbackoff\_enabled | Flag to enable Pod crashloopbackoff monitor | string | `"true"` | no | +| crashloopbackoff\_extra\_tags | Extra tags for Pod crashloopbackoff monitor | list | `[]` | no | +| crashloopbackoff\_message | Custom message for Pod crashloopbackoff monitor | string | `""` | no | +| crashloopbackoff\_silenced | Groups to mute for Pod crashloopbackoff monitor | map | `{}` | no | +| crashloopbackoff\_threshold\_critical | crashloopbackoff critical threshold | string | `"5"` | no | +| crashloopbackoff\_threshold\_warning | crashloopbackoff warning threshold | string | `"4"` | no | +| crashloopbackoff\_time\_aggregator | Monitor aggregator for Pod crashloopbackoff [available values: min, max or avg] | string | `"sum"` | no | +| crashloopbackoff\_timeframe | Monitor timeframe for Pod crashloopbackoff [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | string | `"last_10m"` | no | +| environment | Architecture environment | string | n/a | yes | +| error\_enabled | Flag to enable Pod errors monitor | string | `"true"` | no | +| error\_extra\_tags | Extra tags for Pod errors monitor | list | `[]` | no | +| error\_message | Custom message for Pod errors monitor | string | `""` | no | +| error\_silenced | Groups to mute for Pod errors monitor | map | `{}` | no | +| error\_threshold\_critical | error critical threshold | string | `"1"` | no | +| error\_threshold\_warning | error warning threshold | string | `"0.5"` | no | +| error\_time\_aggregator | Monitor aggregator for Pod errors [available values: min, max or avg] | string | `"sum"` | no | +| error\_timeframe | Monitor timeframe for Pod errors [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | string | `"last_15m"` | no | +| evaluation\_delay | Delay in seconds for the metric evaluation | string | `"15"` | no | +| filter\_tags\_custom | Tags used for custom filtering when filter_tags_use_defaults is false | string | `"*"` | no | +| filter\_tags\_custom\_excluded | Tags excluded for custom filtering when filter_tags_use_defaults is false | string | `""` | no | +| filter\_tags\_use\_defaults | Use default filter tags convention | string | `"true"` | no | +| message | Message sent when a monitor is triggered | string | n/a | yes | +| new\_host\_delay | Delay in seconds before monitor new resource | string | `"300"` | no | +| pod\_phase\_status\_enabled | Flag to enable Pod phase status monitor | string | `"true"` | no | +| pod\_phase\_status\_extra\_tags | Extra tags for Pod phase status monitor | list | `[]` | no | +| pod\_phase\_status\_message | Custom message for Pod phase status monitor | string | `""` | no | +| pod\_phase\_status\_silenced | Groups to mute for Pod phase status monitor | map | `{}` | no | +| pod\_phase\_status\_time\_aggregator | Monitor aggregator for Pod phase status [available values: min, max or avg] | string | `"max"` | no | +| pod\_phase\_status\_timeframe | Monitor timeframe for Pod phase status [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | string | `"last_5m"` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| crashloopbackoff\_id | id for monitor crashloopbackoff | +| error\_id | id for monitor error | +| pod\_phase\_status\_id | id for monitor pod_phase_status | + +## Related documentation + +* [Datadog metrics](https://docs.datadoghq.com/agent/kubernetes/metrics/) +* [Datadog documentation](https://docs.datadoghq.com/integrations/kubernetes/) +* [Datadog Blog](https://www.datadoghq.com/blog/monitor-kubernetes-docker/) diff --git a/caas/kubernetes/pod/inputs.tf b/caas/kubernetes/pod/inputs.tf new file mode 100644 index 0000000..6c50d09 --- /dev/null +++ b/caas/kubernetes/pod/inputs.tf @@ -0,0 +1,164 @@ +# Datadog global variables + +variable "environment" { + description = "Architecture environment" +} + +variable "filter_tags_use_defaults" { + description = "Use default filter tags convention" + default = "true" +} + +variable "filter_tags_custom" { + description = "Tags used for custom filtering when filter_tags_use_defaults is false" + default = "*" +} + +variable "filter_tags_custom_excluded" { + description = "Tags excluded for custom filtering when filter_tags_use_defaults is false" + default = "" +} + +variable "message" { + description = "Message sent when a monitor is triggered" +} + +variable "evaluation_delay" { + description = "Delay in seconds for the metric evaluation" + default = 15 +} + +variable "new_host_delay" { + description = "Delay in seconds before monitor new resource" + default = 300 +} + +# Datadog monitors variables + +variable "pod_phase_status_silenced" { + description = "Groups to mute for Pod phase status monitor" + type = "map" + default = {} +} + +variable "pod_phase_status_enabled" { + description = "Flag to enable Pod phase status monitor" + type = "string" + default = "true" +} + +variable "pod_phase_status_extra_tags" { + description = "Extra tags for Pod phase status monitor" + type = "list" + default = [] +} + +variable "pod_phase_status_message" { + description = "Custom message for Pod phase status monitor" + type = "string" + default = "" +} + +variable "pod_phase_status_time_aggregator" { + description = "Monitor aggregator for Pod phase status [available values: min, max or avg]" + type = "string" + default = "max" +} + +variable "pod_phase_status_timeframe" { + description = "Monitor timeframe for Pod phase status [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]" + type = "string" + default = "last_5m" +} + +variable "error_silenced" { + description = "Groups to mute for Pod errors monitor" + type = "map" + default = {} +} + +variable "error_enabled" { + description = "Flag to enable Pod errors monitor" + type = "string" + default = "true" +} + +variable "error_extra_tags" { + description = "Extra tags for Pod errors monitor" + type = "list" + default = [] +} + +variable "error_message" { + description = "Custom message for Pod errors monitor" + type = "string" + default = "" +} + +variable "error_time_aggregator" { + description = "Monitor aggregator for Pod errors [available values: min, max or avg]" + type = "string" + default = "sum" +} + +variable "error_timeframe" { + description = "Monitor timeframe for Pod errors [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]" + type = "string" + default = "last_15m" +} + +variable "error_threshold_critical" { + default = 1 + description = "error critical threshold" +} + +variable "error_threshold_warning" { + default = 0.5 + description = "error warning threshold" +} + +variable "crashloopbackoff_silenced" { + description = "Groups to mute for Pod crashloopbackoff monitor" + type = "map" + default = {} +} + +variable "crashloopbackoff_enabled" { + description = "Flag to enable Pod crashloopbackoff monitor" + type = "string" + default = "true" +} + +variable "crashloopbackoff_extra_tags" { + description = "Extra tags for Pod crashloopbackoff monitor" + type = "list" + default = [] +} + +variable "crashloopbackoff_message" { + description = "Custom message for Pod crashloopbackoff monitor" + type = "string" + default = "" +} + +variable "crashloopbackoff_time_aggregator" { + description = "Monitor aggregator for Pod crashloopbackoff [available values: min, max or avg]" + type = "string" + default = "sum" +} + +variable "crashloopbackoff_timeframe" { + description = "Monitor timeframe for Pod crashloopbackoff [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]" + type = "string" + default = "last_10m" +} + +variable "crashloopbackoff_threshold_critical" { + default = 5 + description = "crashloopbackoff critical threshold" +} + +variable "crashloopbackoff_threshold_warning" { + default = 4 + description = "crashloopbackoff warning threshold" +} diff --git a/caas/kubernetes/pod/modules.tf b/caas/kubernetes/pod/modules.tf new file mode 100644 index 0000000..9232867 --- /dev/null +++ b/caas/kubernetes/pod/modules.tf @@ -0,0 +1,42 @@ +module "filter-tags" { + source = "../../../common/filter-tags" + + environment = "${var.environment}" + resource = "kubernetes" + filter_tags_use_defaults = "${var.filter_tags_use_defaults}" + filter_tags_custom = "${var.filter_tags_custom}" + filter_tags_custom_excluded = "${var.filter_tags_custom_excluded}" +} + +module "filter-tags-phase" { + source = "../../../common/filter-tags" + + environment = "${var.environment}" + resource = "kubernetes" + filter_tags_use_defaults = "${var.filter_tags_use_defaults}" + filter_tags_custom = "${var.filter_tags_custom}" + filter_tags_custom_excluded = "${var.filter_tags_custom_excluded}" + extra_tags = ["!phase:pending,!phase:running,!phase:succeeded,!phase:unknown"] +} + +module "filter-tags-nocrashloopbackoff" { + source = "../../../common/filter-tags" + + environment = "${var.environment}" + resource = "kubernetes" + filter_tags_use_defaults = "${var.filter_tags_use_defaults}" + filter_tags_custom = "${var.filter_tags_custom}" + filter_tags_custom_excluded = "${var.filter_tags_custom_excluded}" + extra_tags = ["!reason:crashloopbackoff"] +} + +module "filter-tags-crashloopbackoff" { + source = "../../../common/filter-tags" + + environment = "${var.environment}" + resource = "kubernetes" + filter_tags_use_defaults = "${var.filter_tags_use_defaults}" + filter_tags_custom = "${var.filter_tags_custom}" + filter_tags_custom_excluded = "${var.filter_tags_custom_excluded}" + extra_tags = ["reason:crashloopbackoff"] +} diff --git a/caas/kubernetes/pod/monitors-k8s-pod.tf b/caas/kubernetes/pod/monitors-k8s-pod.tf new file mode 100644 index 0000000..0e44eac --- /dev/null +++ b/caas/kubernetes/pod/monitors-k8s-pod.tf @@ -0,0 +1,93 @@ +resource "datadog_monitor" "pod_phase_status" { + count = "${var.pod_phase_status_enabled == "true" ? 1 : 0}" + name = "[${var.environment}] Kubernetes Pod phase status failed" + type = "metric alert" + message = "${coalesce(var.pod_phase_status_message, var.message)}" + + query = < 0 + EOQ + + thresholds { + critical = 0 + } + + evaluation_delay = "${var.evaluation_delay}" + new_host_delay = "${var.new_host_delay}" + + notify_no_data = false + renotify_interval = 0 + notify_audit = false + timeout_h = 0 + include_tags = true + locked = false + require_full_window = true + + silenced = "${var.pod_phase_status_silenced}" + tags = ["env:${var.environment}", "type:caas", "provider:kubernetes", "resource:kubernetes-pod", "team:claranet", "created-by:terraform", "${var.pod_phase_status_extra_tags}"] +} + +resource "datadog_monitor" "error" { + count = "${var.error_enabled == "true" ? 1 : 0}" + name = "[${var.environment}] Kubernetes Pod errors {{#is_alert}}{{{comparator}}} {{threshold}} times ({{value}}){{/is_alert}}{{#is_warning}}{{{comparator}}} {{warn_threshold}} times ({{value}}){{/is_warning}}" + type = "metric alert" + message = "${coalesce(var.error_message, var.message)}" + + query = <= ${var.error_threshold_critical} + EOQ + + thresholds { + critical = "${var.error_threshold_critical}" + warning = "${var.error_threshold_warning}" + } + + evaluation_delay = "${var.evaluation_delay}" + new_host_delay = "${var.new_host_delay}" + + notify_no_data = false + renotify_interval = 0 + notify_audit = false + timeout_h = 0 + include_tags = true + locked = false + require_full_window = true + + silenced = "${var.error_silenced}" + tags = ["env:${var.environment}", "type:caas", "provider:kubernetes", "resource:kubernetes-pod", "team:claranet", "created-by:terraform", "${var.error_extra_tags}"] +} + +resource "datadog_monitor" "crashloopbackoff" { + count = "${var.crashloopbackoff_enabled == "true" ? 1 : 0}" + name = "[${var.environment}] Kubernetes Pod CrashLoopBackOff" + type = "metric alert" + message = "${coalesce(var.crashloopbackoff_message, var.message)}" + + query = < ${var.crashloopbackoff_threshold_critical} + EOQ + + thresholds { + critical = "${var.crashloopbackoff_threshold_critical}" + warning = "${var.crashloopbackoff_threshold_warning}" + } + + evaluation_delay = "${var.evaluation_delay}" + new_host_delay = "${var.new_host_delay}" + + notify_no_data = false + renotify_interval = 0 + notify_audit = false + timeout_h = 0 + include_tags = true + locked = false + require_full_window = true + + silenced = "${var.crashloopbackoff_silenced}" + tags = ["env:${var.environment}", "type:caas", "provider:kubernetes", "resource:kubernetes-pod", "team:claranet", "created-by:terraform", "${var.crashloopbackoff_extra_tags}"] +} diff --git a/caas/kubernetes/pod/outputs.tf b/caas/kubernetes/pod/outputs.tf new file mode 100644 index 0000000..9250444 --- /dev/null +++ b/caas/kubernetes/pod/outputs.tf @@ -0,0 +1,14 @@ +output "pod_phase_status_id" { + description = "id for monitor pod_phase_status" + value = "${datadog_monitor.pod_phase_status.*.id}" +} + +output "error_id" { + description = "id for monitor error" + value = "${datadog_monitor.error.*.id}" +} + +output "crashloopbackoff_id" { + description = "id for monitor crashloopbackoff" + value = "${datadog_monitor.crashloopbackoff.*.id}" +}