Skip to content

Commit

Permalink
feat: Introducing agent module (#319)
Browse files Browse the repository at this point in the history
* feat: introducing elk agent module

* feat: Updated README.md

* feat: Updated agent yal with es_host

* fix

---------

Co-authored-by: Jacopo <[email protected]>
  • Loading branch information
alessio-cialini and jacopocarlini authored Oct 14, 2024
1 parent 6bc45a2 commit 15bbe5e
Show file tree
Hide file tree
Showing 13 changed files with 3,774 additions and 0 deletions.
44 changes: 44 additions & 0 deletions elastic_agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Elastic Stack

This module allow the creation of Elastic Stack

## Configurations

## How to use it

TODO

<!-- markdownlint-disable -->
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | ~>3.30 |
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | ~> 2.0 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | ~> 2.27 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [kubernetes_manifest.elastic_agent](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|---------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|----------------|--------------------|:--------:|
| <a name="input_es_host"></a> [es\_host](#input\_es\_host) | Elastic Host | `string` | n/a | yes |
| <a name="input_dedicated_log_instance_name"></a> [dedicated\_log\_instance\_name](#input\_dedicated\_log\_instance\_name) | n/a | `list(string)` | n/a | yes |
| <a name="input_eck_version"></a> [eck\_version](#input\_eck\_version) | ECK (Elastic Cloud on Kubernetes) version, see: https://www.elastic.co/guide/en/cloud-on-k8s/index.html for futher versions | `string` | n/a | yes |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace for ECK Operator | `string` | `"elastic-system"` | no |

## Outputs

No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"template":{},
"_meta": {
"description": "Mappings for generic"
}
}
10 changes: 10 additions & 0 deletions elastic_agent/logs-generic/component_logs-generic-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"template":{
"settings": {
"index.lifecycle.name": "logs-generic"
}
},
"_meta": {
"description": "Settings for generic"
}
}
8 changes: 8 additions & 0 deletions elastic_agent/logs-generic/data_view.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data_view": {
"title": "logs-generic-*",
"name": "Generic log Data View",
"timeFieldName": "@timestamp"
},
"override": true
}
40 changes: 40 additions & 0 deletions elastic_agent/logs-generic/ilm_policy_logs-generic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_primary_shard_size": "50gb",
"max_age": "2d"
}
}
},
"warm": {
"min_age": "2d",
"actions": {
"set_priority": {
"priority": 50
}
}
},
"cold": {
"min_age": "4d",
"actions": {
"set_priority": {
"priority": 0
}
}
},
"delete": {
"min_age": "7d",
"actions": {
"delete": {}
}
}
},
"_meta": {
"description": "Policy for generic"
}
}
}
11 changes: 11 additions & 0 deletions elastic_agent/logs-generic/index_template_logs-generic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"index_patterns": [
"logs-generic-*"
],
"data_stream": {},
"composed_of": [ "logs-generic-mappings", "logs-generic-settings" ],
"priority": 500,
"_meta": {
"description": "Index template for generic"
}
}
66 changes: 66 additions & 0 deletions elastic_agent/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
locals {

#
# Other
#
logs_general_to_exclude_paths = distinct(flatten([
for instance_name in var.dedicated_log_instance_name : "'/var/log/containers/${instance_name}-*.log'"
]))

#https://raw.githubusercontent.com/elastic/elastic-agent/8.9/deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml
agent_yaml = templatefile("${path.module}/yaml/${var.eck_version}/agent.yaml", {

es_host = var.es_host
namespace = var.namespace
dedicated_log_instance_name = var.dedicated_log_instance_name
logs_general_to_exclude_paths = local.logs_general_to_exclude_paths

system_name = "system-1"
system_id = "id_system_1"
system_revision = 1

kubernetes_name = "kubernetes-1"
kubernetes_id = "id_kubernetes_1"
kubernetes_revision = 1

apm_name = "apm-1"
apm_id = "id_apm_1"
apm_revision = 1
})

}


#############
# Install Elastic Agent
#############
#data "kubectl_file_documents" "elastic_agent" {
# content = local.agent_yaml
#}
locals {
elastic_agent_defaultMode_converted = {
for value in [
for yaml in split(
"\n---\n",
"\n${replace(local.agent_yaml, "/(?m)^---[[:blank:]]*(#.*)?$/", "---")}\n"
) :
yamldecode(replace(replace(yaml, "/(?s:\nstatus:.*)$/", ""), "0640", "416")) #transform 'defaultMode' octal value (0640) to decimal value (416)
if trimspace(replace(yaml, "/(?m)(^[[:blank:]]*(#.*)?$)+/", "")) != ""
] : "${value["kind"]}--${value["metadata"]["name"]}" => value
}
}
# output "test" {
# value = local.elastic_agent_defaultMode_converted
# }

resource "kubernetes_manifest" "elastic_agent" {

for_each = local.elastic_agent_defaultMode_converted

manifest = each.value

field_manager {
force_conflicts = true
}
computed_fields = ["spec.template.spec.containers[0].resources"]
}
Empty file added elastic_agent/outputs.tf
Empty file.
23 changes: 23 additions & 0 deletions elastic_agent/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "es_host" {
description = "Elastic Host"
type = string
}

variable "namespace" {
description = "Namespace for ECK Operator"
type = string
default = "elastic-system"
}

variable "dedicated_log_instance_name" {
type = list(string)
}

variable "eck_version" {
type = string
description = "ECK (Elastic Cloud on Kubernetes) version, see: https://www.elastic.co/guide/en/cloud-on-k8s/index.html for futher versions"
validation {
condition = contains(["2.12", "2.9", "2.6"], var.eck_version)
error_message = "The ECK version supported is only 2.9 or 2.6"
}
}
19 changes: 19 additions & 0 deletions elastic_agent/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_version = ">= 1.3.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.30"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.27"
}

kubectl = {
source = "alekc/kubectl"
version = "~> 2.0"
}
}
}
Loading

0 comments on commit 15bbe5e

Please sign in to comment.