Skip to content

Commit

Permalink
refactor: split main.tf into cluster/node scoped files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Nix committed Nov 15, 2022
1 parent 04fa746 commit b7540ee
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 161 deletions.
161 changes: 161 additions & 0 deletions lacework_cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
locals {
cluster_config_data = templatefile("${path.module}/config_cluster.tmpl", {
lacework_cluster_name = var.lacework_cluster_name
lacework_cluster_region = var.lacework_cluster_region
lacework_cluster_type = var.lacework_cluster_type
lacework_server_url = var.lacework_server_url
})
cluster_config_name = "${var.lacework_config_name}-${random_id.cluster_config_name_tail.hex}"
merged_cluster_config = var.enable_cluster_agent ? yamlencode(merge(yamldecode(local.cluster_config_data), var.lacework_cluster_configuration)) : ""
}

resource "random_id" "cluster_config_name_tail" {
byte_length = 8
keepers = {
data = local.merged_cluster_config
}
}

resource "kubernetes_service_account" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}
}

resource "kubernetes_cluster_role" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-role"
}

rule {
api_groups = ["*"]
resources = ["*"]
verbs = ["get", "list"]
}
}

resource "kubernetes_cluster_role_binding" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-role-binding"
}

role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "${var.lacework_agent_name}-cluster-role"
}

subject {
kind = "ServiceAccount"
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}

depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector
]
}

resource "kubernetes_secret" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = local.cluster_config_name
namespace = var.namespace

labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}

data = {
"config.yaml" = local.merged_cluster_config
}
}

resource "kubernetes_deployment" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster"
namespace = var.namespace

labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}

spec {
selector {
match_labels = {
name = "${var.lacework_agent_name}-cluster"
}
}

template {
metadata {
labels = {
name = "${var.lacework_agent_name}-cluster"
}

annotations = {
lacework_config_version = kubernetes_secret.lacework_k8s_collector[0].metadata.0.resource_version
}
}

spec {
service_account_name = "${var.lacework_agent_name}-cluster-sa"
termination_grace_period_seconds = 20

container {
name = "${var.lacework_agent_name}-cluster"
image = var.lacework_cluster_image
image_pull_policy = var.lacework_cluster_image_pull_policy

env {
name = "LaceworkAccessToken"
value_from {
secret_key_ref {
name = "${var.lacework_agent_name}-access-token"
key = "agent-access-token"
}
}
}

volume_mount {
name = "cfgmap"
mount_path = "/config"
}
}

volume {
name = "cfgmap"
secret {
secret_name = local.cluster_config_name
items {
key = "config.yaml"
path = "config.yaml"
}
}
}
}
}
}

depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector,
kubernetes_secret.lacework_k8s_collector,
kubernetes_cluster_role_binding.lacework_k8s_collector,
]
}
163 changes: 2 additions & 161 deletions main.tf → lacework_node.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
locals {
cluster_config_data = templatefile("${path.module}/config_cluster.tmpl", {
lacework_cluster_name = var.lacework_cluster_name
lacework_cluster_region = var.lacework_cluster_region
lacework_cluster_type = var.lacework_cluster_type
lacework_server_url = var.lacework_server_url
})
node_config_data = templatefile("${path.module}/config_node.tmpl", {
lacework_agent_autoupgrade = var.lacework_agent_autoupgrade
lacework_agent_interface_connection_size = var.lacework_agent_interface_connection_size
Expand All @@ -15,10 +9,8 @@ locals {
lacework_proxy_url = var.lacework_proxy_url
lacework_server_url = var.lacework_server_url
})
cluster_config_name = "${var.lacework_config_name}-${random_id.cluster_config_name_tail.hex}"
node_config_name = "${var.lacework_config_name}-${random_id.node_config_name_tail.hex}"
merged_cluster_config = var.enable_cluster_agent ? yamlencode(merge(yamldecode(local.cluster_config_data), var.lacework_cluster_configuration)) : ""
merged_node_config = jsonencode(merge(jsondecode(local.node_config_data), var.lacework_agent_configuration))
node_config_name = "${var.lacework_config_name}-${random_id.node_config_name_tail.hex}"
merged_node_config = jsonencode(merge(jsondecode(local.node_config_data), var.lacework_agent_configuration))
}

resource "random_id" "node_config_name_tail" {
Expand All @@ -28,13 +20,6 @@ resource "random_id" "node_config_name_tail" {
}
}

resource "random_id" "cluster_config_name_tail" {
byte_length = 8
keepers = {
data = local.merged_cluster_config
}
}

resource "kubernetes_secret" "lacework_access_token" {
metadata {
name = "${var.lacework_agent_name}-access-token"
Expand Down Expand Up @@ -323,147 +308,3 @@ resource "kubernetes_daemonset" "lacework_datacollector" {
}
}
}

resource "kubernetes_service_account" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}
}

resource "kubernetes_cluster_role" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-role"
}

rule {
api_groups = ["*"]
resources = ["*"]
verbs = ["get", "list"]
}
}

resource "kubernetes_cluster_role_binding" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster-role-binding"
}

role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "${var.lacework_agent_name}-cluster-role"
}

subject {
kind = "ServiceAccount"
name = "${var.lacework_agent_name}-cluster-sa"
namespace = var.namespace
}

depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector
]
}

resource "kubernetes_secret" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = local.cluster_config_name
namespace = var.namespace

labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}

data = {
"config.yaml" = local.merged_cluster_config
}
}

resource "kubernetes_deployment" "lacework_k8s_collector" {
count = var.enable_cluster_agent ? 1 : 0

metadata {
name = "${var.lacework_agent_name}-cluster"
namespace = var.namespace

labels = {
tier = "monitoring"
app = "${var.lacework_agent_name}-cluster"
}
}

spec {
selector {
match_labels = {
name = "${var.lacework_agent_name}-cluster"
}
}

template {
metadata {
labels = {
name = "${var.lacework_agent_name}-cluster"
}

annotations = {
lacework_config_version = kubernetes_secret.lacework_k8s_collector[0].metadata.0.resource_version
}
}

spec {
service_account_name = "${var.lacework_agent_name}-cluster-sa"
termination_grace_period_seconds = 20

container {
name = "${var.lacework_agent_name}-cluster"
image = var.lacework_cluster_image
image_pull_policy = var.lacework_cluster_image_pull_policy

env {
name = "LaceworkAccessToken"
value_from {
secret_key_ref {
name = "${var.lacework_agent_name}-access-token"
key = "agent-access-token"
}
}
}

volume_mount {
name = "cfgmap"
mount_path = "/config"
}
}

volume {
name = "cfgmap"
secret {
secret_name = local.cluster_config_name
items {
key = "config.yaml"
path = "config.yaml"
}
}
}
}
}
}

depends_on = [
kubernetes_service_account.lacework_k8s_collector,
kubernetes_cluster_role.lacework_k8s_collector,
kubernetes_secret.lacework_k8s_collector,
kubernetes_cluster_role_binding.lacework_k8s_collector,
]
}

0 comments on commit b7540ee

Please sign in to comment.