diff --git a/.cz.yaml b/.cz.yaml new file mode 100644 index 0000000..b1981ec --- /dev/null +++ b/.cz.yaml @@ -0,0 +1,8 @@ +--- +commitizen: + major_version_zero: true + name: cz_gitmoji + tag_format: $version + update_changelog_on_bump: true + version_provider: scm + version_scheme: semver2 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..867570d --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "terraform" # See documentation for possible values + directory: "/" # Location of package manifests + schedule: + interval: "daily" diff --git a/.gitignore b/.gitignore index 7c1cae9..403ffa9 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,3 @@ override.tf.json # Ignore CLI configuration files .terraformrc terraform.rc - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..23556c8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,99 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + # Git style + - id: check-added-large-files + - id: check-merge-conflict + - id: check-vcs-permalinks + - id: forbid-new-submodules + - id: no-commit-to-branch + + # Common errors + - id: end-of-file-fixer + - id: trailing-whitespace + args: [--markdown-linebreak-ext=md] + exclude: CHANGELOG.md + - id: check-yaml + - id: check-merge-conflict + - id: check-executables-have-shebangs + + # Cross platform + - id: check-case-conflict + - id: mixed-line-ending + args: [--fix=lf] + + # Security + - id: detect-aws-credentials + args: ['--allow-missing-credentials'] + - id: detect-private-key + +# JSON5 Linter +- repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + # https://prettier.io/docs/en/options.html#parser + files: '.json5$' + +# Terraform Hooks +- repo: https://github.com/antonbabenko/pre-commit-terraform + rev: v1.92.2 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases + hooks: + - id: terraform_fmt + args: + - --hook-config=--parallelism-ci-cpu-cores=2 + - id: terraform_docs + args: + - --hook-config=--parallelism-ci-cpu-cores=2 + - id: terraform_tflint + name: Terraform validate with tflint + description: Validates all Terraform configuration files with TFLint. + require_serial: true + entry: hooks/terraform_tflint.sh + language: script + files: (\.tf|\.tfvars)$ + exclude: \.(terraform/.*|terragrunt-cache)$ + args: + - --hook-config=--parallelism-ci-cpu-cores=2 + - id: terragrunt_fmt + name: Terragrunt fmt + description: Rewrites all Terragrunt configuration files to a canonical format. + entry: hooks/terragrunt_fmt.sh + language: script + files: (\.hcl)$ + exclude: \.(terraform/.*|terragrunt-cache)$ + args: + - --hook-config=--parallelism-ci-cpu-cores=2 + # - id: terragrunt_validate + # name: Terragrunt validate + # description: Validates all Terragrunt configuration files. + # entry: hooks/terragrunt_validate.sh + # language: script + # files: (\.hcl)$ + # exclude: \.(terraform/.*|terragrunt-cache)$ + # args: + # - --hook-config=--parallelism-ci-cpu-cores=2 + # - id: terragrunt_validate_inputs + # name: Terragrunt validate inputs + # description: Validates Terragrunt unused and undefined inputs. + # entry: hooks/terragrunt_validate_inputs.sh + # language: script + # files: (\.hcl)$ + # exclude: \.(terraform/.*|terragrunt-cache)$ + # args: + # - --hook-config=--parallelism-ci-cpu-cores=2 + # - id: terragrunt_providers_lock + # name: Terragrunt providers lock + # description: Updates provider signatures in dependency lock files using terragrunt. + # entry: hooks/terragrunt_providers_lock.sh + # language: script + # files: (terragrunt|\.terraform\.lock)\.hcl$ + # exclude: \.(terraform/.*|terragrunt-cache)$ + # args: + # - --hook-config=--parallelism-ci-cpu-cores=2 + +- repo: https://github.com/ljnsn/cz-conventional-gitmoji + rev: v0.3.3 + hooks: + - id: conventional-gitmoji diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 0000000..6e39031 --- /dev/null +++ b/.releaserc.json @@ -0,0 +1,36 @@ +{ + "branches": [ + "main", + "master" + ], + "ci": false, + "plugins": [ + "@semantic-release/commit-analyzer", + "@semantic-release/release-notes-generator", + [ + "@semantic-release/github", + { + "successComment": + "This ${issue.pull_request ? 'PR is included' : 'issue has been resolved'} in version ${nextRelease.version} :tada:", + "labels": false, + "releasedLabels": false + } + ], + [ + "@semantic-release/changelog", + { + "changelogFile": "CHANGELOG.md", + "changelogTitle": "# Changelog\n\nAll notable changes to this project will be documented in this file." + } + ], + [ + "@semantic-release/git", + { + "assets": [ + "CHANGELOG.md" + ], + "message": "chore(release): version ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ] + ] +} diff --git a/.terraform-docs.yml b/.terraform-docs.yml new file mode 100644 index 0000000..fabfb8d --- /dev/null +++ b/.terraform-docs.yml @@ -0,0 +1,44 @@ +formatter: markdown table + +header-from: main.tf +footer-from: "" + +sections: +## hide: [] + show: + - data-sources + - header + - footer + - inputs + - modules + - outputs + - providers + - requirements + - resources + +output: + file: README.md + mode: inject + template: |- + + {{ .Content }} + + +output-values: + enabled: false + from: "" + +sort: + enabled: true + by: name + +settings: + anchor: true + color: true + default: true + description: true + escape: true + indent: 2 + required: true + sensitive: true + type: true diff --git a/.tflint.hcl b/.tflint.hcl new file mode 100644 index 0000000..f63db7d --- /dev/null +++ b/.tflint.hcl @@ -0,0 +1,22 @@ +config { + module = true + force = false + disabled_by_default = false + +} + +rule "aws_instance_invalid_type" { + enabled = true +} + +plugin "aws" { + enabled = true + version = "0.32.0" + source = "github.com/terraform-linters/tflint-ruleset-aws" +} + +plugin "terraform" { + enabled = true + version = "0.9.0" + source = "github.com/terraform-linters/tflint-ruleset-terraform" +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1ddcff0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,17 @@ +## Unreleased + +### 🎨🏗️ Style & Architecture + +- **repo**: add repo features pre-commit and commitizen + +# CHANGELOG +* 0.0.2 -- 2024-07-30 + - formatting + - added module default outputs + - updated images module + - add prometheus_namespace as output +* 0.0.1 -- 2024-07-22 + - add force_delete and lifecycle policies to copy_images + - updated version.tf to 0.0.1 + - included module.images in depends on helm charts + - created changelog diff --git a/README.md b/README.md index bab6556..a47a7de 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # tfmod-prometheus -Prometheus is an open source monitoring and alerting tool designed for dynamic cloud systems and especially well suited for monitoring microservice architectures. Prometheus collects and stores metrics as time series data. Metrics are a key means in understanding an applications behavior and a critical facet of operations support. Collectively, "it collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true." - [prometheus.io](https://prometheus.io/) +Prometheus is an open source monitoring and alerting tool designed for dynamic cloud systems and especially well suited for monitoring microservice architectures. Prometheus collects and stores metrics as time series data. Metrics are a key means in understanding an applications behavior and a critical facet of operations support. Collectively, "it collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true." - [prometheus.io](https://prometheus.io/) This modules uses Helm to deploy Prometheus to a cluster using the official chart. By default this will install dependent charts/components: - [alertmanager](https://github.com/prometheus-community/helm-charts/tree/main/charts/alertmanager) @@ -10,18 +10,8 @@ This modules uses Helm to deploy Prometheus to a cluster using the official char Versions are module variables, but latest versions are intended to be be reflected in the default values. -# CHANGELOG -* 0.0.2 -- 2024-07-30 - - formatting - - added module default outputs - - updated images module - - add prometheus_namespace as output - - add --web.enable-remote-write-receiver -* 0.0.1 -- 2024-07-22 - - add force_delete and lifecycle policies to copy_images - - updated version.tf to 0.0.1 - - included module.images in depends on helm charts - - created changelog + + ## Requirements @@ -29,17 +19,15 @@ Versions are module variables, but latest versions are intended to be be reflect | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 0.13 | -| [aws](#requirement\_aws) | >= 5.14.0 | | [helm](#requirement\_helm) | >= 2.11.0 | | [kubernetes](#requirement\_kubernetes) | >= 2.23.0 | -| [null](#requirement\_null) | >= 3.2.1 | ## Providers | Name | Version | |------|---------| -| [helm](#provider\_helm) | >= 2.11.0 | -| [kubernetes](#provider\_kubernetes) | >= 2.23.0 | +| [helm](#provider\_helm) | 2.14.0 | +| [kubernetes](#provider\_kubernetes) | 2.31.0 | ## Modules @@ -53,7 +41,6 @@ Versions are module variables, but latest versions are intended to be be reflect |------|------| | [helm_release.prometheus](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource | | [kubernetes_namespace.ns](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) | resource | -| [kubernetes_namespace.existing-ns](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/namespace) | data source | ## Inputs @@ -61,13 +48,12 @@ Versions are module variables, but latest versions are intended to be be reflect |------|-------------|------|---------|:--------:| | [alertmanager\_tag](#input\_alertmanager\_tag) | The image tag of the alertmanager image. | `string` | `"v0.27.0"` | no | | [cluster\_name](#input\_cluster\_name) | The name of the cluster into which prometheus will be installed. | `string` | n/a | yes | -| [create\_namespace](#input\_create\_namespace) | Indicates whether the `namespace` needs to be created ('true') or already exists (not `true`) | `bool` | `true` | no | | [kube\_state\_metrics\_tag](#input\_kube\_state\_metrics\_tag) | The image tag of the kube-state-metrics image. | `string` | `"v2.13.0"` | no | | [namespace](#input\_namespace) | The namespace to install the prometheus components. Defaults to 'prometheus' | `string` | `"prometheus"` | no | | [node\_exporter\_tag](#input\_node\_exporter\_tag) | The image tag of the node-exporter image. | `string` | `"v1.8.2"` | no | | [profile](#input\_profile) | AWS\_PROFILE to use to apply the terraform script. | `string` | `""` | no | -| [prometheus\_chart\_version](#input\_prometheus\_chart\_version) | The version of prometheus to install into the cluster. | `string` | `"25.24.1"` | no | -| [prometheus\_config\_reloader\_tag](#input\_prometheus\_config\_reloader\_tag) | The image tag of the prometheus-config-reloader image. | `string` | `"v0.75.1"` | no | +| [prometheus\_chart\_version](#input\_prometheus\_chart\_version) | The version of prometheus to install into the cluster. | `string` | `"25.25.0"` | no | +| [prometheus\_config\_reloader\_tag](#input\_prometheus\_config\_reloader\_tag) | The image tag of the prometheus-config-reloader image. | `string` | `"v0.75.2"` | no | | [prometheus\_server\_tag](#input\_prometheus\_server\_tag) | The image tag of prometheus server to install into the cluster. | `string` | `"v2.53.1"` | no | | [pushgateway\_tag](#input\_pushgateway\_tag) | The image tag of the pushgateway image. | `string` | `"v1.9.0"` | no | | [rwo\_storage\_class](#input\_rwo\_storage\_class) | Specify the storage class for read/write/once persistent volumes. | `string` | `"gp3-encrypted"` | no | @@ -76,11 +62,11 @@ Versions are module variables, but latest versions are intended to be be reflect | Name | Description | |------|-------------| -| [alertmanager\_headless\_internal\_endpoint](#output\_alertmanager\_headless\_internal\_endpoint) | n/a | -| [alertmanager\_internal\_endpoint](#output\_alertmanager\_internal\_endpoint) | n/a | +| [alertmanager\_headless\_internal\_endpoint](#output\_alertmanager\_headless\_internal\_endpoint) | headless internal endpoint for alertmanager | +| [alertmanager\_internal\_endpoint](#output\_alertmanager\_internal\_endpoint) | internal endpoint for alertmanager | | [module\_name](#output\_module\_name) | The name of this module. | | [module\_version](#output\_module\_version) | The version of this module. | -| [prometheus\_namespace](#output\_prometheus\_namespace) | n/a | -| [prometheus\_server\_internal\_endpoint](#output\_prometheus\_server\_internal\_endpoint) | n/a | -| [pushgateway\_internal\_endpoint](#output\_pushgateway\_internal\_endpoint) | n/a | +| [prometheus\_namespace](#output\_prometheus\_namespace) | namespace for prometheus | +| [prometheus\_server\_internal\_endpoint](#output\_prometheus\_server\_internal\_endpoint) | internal endpoint for prometheus | +| [pushgateway\_internal\_endpoint](#output\_pushgateway\_internal\_endpoint) | internal endpoint for pushgateway | diff --git a/copy_images.tf b/copy_images.tf index 8bb3eab..a64dbbb 100644 --- a/copy_images.tf +++ b/copy_images.tf @@ -83,16 +83,6 @@ module "images" { image_config = local.image_config tags = {} - ### optional - ## account_alias = "" - ## account_id = "" - ## destination_password = "" - ## destination_username = "" - ## override_prefixes = {} - ## region = "" - ## source_password = "" - ## source_username = "" - enable_lifecycle_policy = true lifecycle_policy_all = true force_delete = true diff --git a/examples/simple/main.tf b/examples/simple/main.tf index b2ed1d7..fb5e808 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -1,3 +1,20 @@ +terraform { + required_version = ">= 0.13" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.14.0" + } + helm = { + source = "hashicorp/helm" + version = ">= 2.11.0" + } + kubernetes = { + source = "hashicorp/kubernetes" + version = ">= 2.23.0" + } + } +} provider "aws" { profile = local.profile region = local.region @@ -34,18 +51,6 @@ locals { namespace = "prometheus" create_namespace = "true" - # tags = { - # project_number = "fs0000000078" - # project_name = "csvd_platformbaseline" - # project_role = "csvd_platformbaseline_app" - # organization = "census:ocio:csvd" - # created_by = "luther.coleman.mcginty@census.gov" - # created_for = "luther.coleman.mcginty@census.gov" - # created_reason = "Testing eks module operation" - # Terraform = "true" - # Terragrunt = "true" - # test_adjustment = "testing adding tags" - # } } module "prometheus" { @@ -55,4 +60,4 @@ module "prometheus" { cluster_name = local.cluster_name namespace = local.namespace create_namespace = local.create_namespace -} \ No newline at end of file +} diff --git a/main.tf b/main.tf index 949f768..12cdc83 100644 --- a/main.tf +++ b/main.tf @@ -1,25 +1,22 @@ - locals { - alertmanager_internal_hostname = format("prometheus-alertmanager.%v.svc.cluster.local", local.ns) + alertmanager_internal_hostname = format("prometheus-alertmanager.%v.svc.cluster.local", kubernetes_namespace.ns.metadata[0].name) alertmanager_internal_port_number = 9093 alertmanager_internal_url = format("http://%v:%v/", local.alertmanager_internal_hostname, local.alertmanager_internal_port_number) - headless_alertmanager_internal_hostname = format("prometheus-alertmanager-headless.%v.svc.cluster.local", local.ns) + headless_alertmanager_internal_hostname = format("prometheus-alertmanager-headless.%v.svc.cluster.local", kubernetes_namespace.ns.metadata[0].name) headless_alertmanager_internal_port_number = 9093 headless_alertmanager_internal_url = format("http://%v:%v/", local.alertmanager_internal_hostname, local.alertmanager_internal_port_number) - pushgateway_internal_hostname = format("prometheus-prometheus-pushgateway.%v.svc.cluster.local", local.ns) + pushgateway_internal_hostname = format("prometheus-prometheus-pushgateway.%v.svc.cluster.local", kubernetes_namespace.ns.metadata[0].name) pushgateway_internal_port_number = 9091 pushgateway_internal_url = format("http://%v:%v/", local.pushgateway_internal_hostname, local.pushgateway_internal_port_number) - server_internal_hostname = format("prometheus-server.%v.svc.cluster.local", local.ns) + server_internal_hostname = format("prometheus-server.%v.svc.cluster.local", kubernetes_namespace.ns.metadata[0].name) server_internal_port_number = 9090 server_internal_url = format("http://%v:%v/", local.server_internal_hostname, local.server_internal_port_number) } resource "kubernetes_namespace" "ns" { - count = var.create_namespace == true ? 1 : 0 - metadata { name = var.namespace labels = { @@ -28,35 +25,20 @@ resource "kubernetes_namespace" "ns" { } } -data "kubernetes_namespace" "existing-ns" { - count = var.create_namespace == true ? 0 : 1 - - metadata { - name = var.namespace - } -} - -locals { - ns = try(kubernetes_namespace.ns[0].metadata[0].name, data.kubernetes_namespace.existing-ns[0].metadata[0].name) -} - resource "helm_release" "prometheus" { depends_on = [ module.images, ] chart = "prometheus" name = "prometheus" - namespace = local.ns + namespace = kubernetes_namespace.ns.metadata[0].name version = var.prometheus_chart_version repository = "https://prometheus-community.github.io/helm-charts" # Global set { - name = "server.extraArgs.web.enable-remote-write-receiver" - value = "null" - # kubectl -n prometheus describe deployment prometheus-server - # Will show in the args list as: - # --web=map[enable-remote-write-receiver:] + name = "server.web.enable-remote-write-receiver" + value = "" } # Prometheus chart: diff --git a/outputs.tf b/outputs.tf index ed51506..ac3c50c 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,5 @@ output "alertmanager_internal_endpoint" { + description = "internal endpoint for alertmanager" value = { hostname = local.alertmanager_internal_hostname port_number = local.alertmanager_internal_port_number @@ -7,6 +8,7 @@ output "alertmanager_internal_endpoint" { } output "alertmanager_headless_internal_endpoint" { + description = "headless internal endpoint for alertmanager" value = { hostname = local.headless_alertmanager_internal_hostname port_number = local.headless_alertmanager_internal_port_number @@ -15,6 +17,7 @@ output "alertmanager_headless_internal_endpoint" { } output "pushgateway_internal_endpoint" { + description = "internal endpoint for pushgateway" value = { hostname = local.pushgateway_internal_hostname port_number = local.pushgateway_internal_port_number @@ -23,6 +26,7 @@ output "pushgateway_internal_endpoint" { } output "prometheus_server_internal_endpoint" { + description = "internal endpoint for prometheus" value = { hostname = local.server_internal_hostname port_number = local.server_internal_port_number @@ -31,7 +35,8 @@ output "prometheus_server_internal_endpoint" { } output "prometheus_namespace" { - value = local.ns + description = "namespace for prometheus" + value = kubernetes_namespace.ns.metadata[0].name } ################################################################################ @@ -40,10 +45,10 @@ output "prometheus_namespace" { output "module_name" { description = "The name of this module." - value = local._module_name + value = local.module_name } output "module_version" { description = "The version of this module." - value = local._module_version + value = local.module_version } diff --git a/requirements.tf b/requirements.tf index 32e5c6f..5f150af 100644 --- a/requirements.tf +++ b/requirements.tf @@ -2,10 +2,6 @@ terraform { required_version = ">= 0.13" required_providers { - aws = { - source = "hashicorp/aws" - version = ">= 5.14.0" - } helm = { source = "hashicorp/helm" version = ">= 2.11.0" @@ -14,9 +10,5 @@ terraform { source = "hashicorp/kubernetes" version = ">= 2.23.0" } - null = { - source = "hashicorp/null" - version = ">= 3.2.1" - } } } diff --git a/variables.tf b/variables.tf index 37bba80..483b268 100644 --- a/variables.tf +++ b/variables.tf @@ -15,18 +15,18 @@ variable "namespace" { default = "prometheus" } -variable "create_namespace" { - description = "Indicates whether the `namespace` needs to be created ('true') or already exists (not `true`)" - type = bool - default = true -} +# variable "create_namespace" { +# description = "Indicates whether the `namespace` needs to be created ('true') or already exists (not `true`)" +# type = bool +# default = true +# } # helm repo add prometheus-community https://prometheus-community.github.io/helm-charts # helm search repo prometheus-community/prometheus | head -2 variable "prometheus_chart_version" { description = "The version of prometheus to install into the cluster." type = string - default = "25.24.1" + default = "25.25.0" } # The `APP VERSION` of the output found while determining the chart version @@ -40,7 +40,7 @@ variable "prometheus_server_tag" { variable "prometheus_config_reloader_tag" { description = "The image tag of the prometheus-config-reloader image." type = string - default = "v0.75.1" + default = "v0.75.2" } # The `APP VERSION` of the output: diff --git a/version.tf b/version.tf index 98adbea..55397e7 100644 --- a/version.tf +++ b/version.tf @@ -1,4 +1,4 @@ locals { - _module_name = "tfmod-prometheus" - _module_version = "0.0.2" + module_name = "tfmod-prometheus" + module_version = "0.0.2" }