This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws-fluent-bit.tf
205 lines (178 loc) · 7.16 KB
/
aws-fluent-bit.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
locals {
aws_fluent_bit = merge(
local.helm_defaults,
{
name = "aws-for-fluent-bit"
namespace = "aws-for-fluent-bit"
chart = "aws-for-fluent-bit"
repository = "https://clusterfrak-dynamics.github.io/eks-charts"
service_account_name = "aws-for-fluent-bit"
create_iam_resources_kiam = false
create_iam_resources_irsa = true
enabled = false
chart_version = "0.1.3"
version = "2.3.0"
iam_policy_override = ""
default_network_policy = true
containers_log_retention_in_days = 180
},
var.aws_fluent_bit
)
values_aws_fluent_bit = <<VALUES
firehose:
enabled: false
kinesis:
enabled: false
cloudWatch:
enabled: true
region: "${data.aws_region.current.name}"
logGroupName: "${local.aws_fluent_bit["enabled"] ? aws_cloudwatch_log_group.eks-aws-fluent-bit-log-group[0].name : ""}"
autoCreateGroup: false
image:
tag: ${local.aws_fluent_bit["version"]}
serviceAccount:
annotations:
eks.amazonaws.com/role-arn: "${local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["create_iam_resources_irsa"] ? module.iam_assumable_role_aws_fluent_bit.this_iam_role_arn : ""}"
tolerations:
- operator: Exists
priorityClassName: "${local.priority_class_ds["create"] ? kubernetes_priority_class.kubernetes_addons_ds[0].metadata[0].name : ""}"
VALUES
}
module "iam_assumable_role_aws_fluent_bit" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "~> v2.0"
create_role = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["create_iam_resources_irsa"]
role_name = "tf-eks-${var.cluster-name}-aws-fluent-bit-irsa"
provider_url = replace(var.eks["cluster_oidc_issuer_url"], "https://", "")
role_policy_arns = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["create_iam_resources_irsa"] ? [aws_iam_policy.eks-aws-fluent-bit[0].arn] : []
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.aws_fluent_bit["namespace"]}:${local.aws_fluent_bit["service_account_name"]}"]
}
resource "aws_iam_policy" "eks-aws-fluent-bit" {
count = local.aws_fluent_bit["enabled"] && (local.aws_fluent_bit["create_iam_resources_kiam"] || local.aws_fluent_bit["create_iam_resources_irsa"]) ? 1 : 0
name = "tf-eks-${var.cluster-name}-aws-fluent-bit"
policy = local.aws_fluent_bit["iam_policy_override"] == "" ? data.aws_iam_policy_document.aws_fluent_bit.json : local.aws_fluent_bit["iam_policy_override"]
}
data "aws_iam_policy_document" "aws_fluent_bit" {
statement {
effect = "Allow"
actions = [
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["*"]
}
}
resource "aws_cloudwatch_log_group" "eks-aws-fluent-bit-log-group" {
count = local.aws_fluent_bit["enabled"] ? 1 : 0
name = "/aws/eks/${var.cluster-name}/containers"
retention_in_days = local.aws_fluent_bit["containers_log_retention_in_days"]
}
resource "aws_iam_role" "eks-aws-fluent-bit-kiam" {
name = "tf-eks-${var.cluster-name}-aws-fluent-bit-kiam"
count = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["create_iam_resources_kiam"] ? 1 : 0
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.eks-kiam-server-role[count.index].arn}"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "eks-aws-fluent-bit-kiam" {
count = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["create_iam_resources_kiam"] ? 1 : 0
role = aws_iam_role.eks-aws-fluent-bit-kiam[count.index].name
policy_arn = aws_iam_policy.eks-aws-fluent-bit[count.index].arn
}
resource "kubernetes_namespace" "aws_fluent_bit" {
count = local.aws_fluent_bit["enabled"] ? 1 : 0
metadata {
annotations = {
"iam.amazonaws.com/permitted" = "${local.aws_fluent_bit["create_iam_resources_kiam"] ? aws_iam_role.eks-aws-fluent-bit-kiam[0].arn : "^$"}"
}
labels = {
name = local.aws_fluent_bit["namespace"]
}
name = local.aws_fluent_bit["namespace"]
}
}
resource "helm_release" "aws_fluent_bit" {
count = local.aws_fluent_bit["enabled"] ? 1 : 0
repository = local.aws_fluent_bit["repository"]
name = local.aws_fluent_bit["name"]
chart = local.aws_fluent_bit["chart"]
version = local.aws_fluent_bit["chart_version"]
timeout = local.aws_fluent_bit["timeout"]
force_update = local.aws_fluent_bit["force_update"]
recreate_pods = local.aws_fluent_bit["recreate_pods"]
wait = local.aws_fluent_bit["wait"]
atomic = local.aws_fluent_bit["atomic"]
cleanup_on_fail = local.aws_fluent_bit["cleanup_on_fail"]
dependency_update = local.aws_fluent_bit["dependency_update"]
disable_crd_hooks = local.aws_fluent_bit["disable_crd_hooks"]
disable_webhooks = local.aws_fluent_bit["disable_webhooks"]
render_subchart_notes = local.aws_fluent_bit["render_subchart_notes"]
replace = local.aws_fluent_bit["replace"]
reset_values = local.aws_fluent_bit["reset_values"]
reuse_values = local.aws_fluent_bit["reuse_values"]
skip_crds = local.aws_fluent_bit["skip_crds"]
verify = local.aws_fluent_bit["verify"]
values = [
local.values_aws_fluent_bit,
local.aws_fluent_bit["extra_values"]
]
namespace = kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]
depends_on = [
helm_release.kiam
]
}
resource "kubernetes_network_policy" "aws_fluent_bit_default_deny" {
count = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["default_network_policy"] ? 1 : 0
metadata {
name = "${kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]}-default-deny"
namespace = kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]
}
spec {
pod_selector {
}
policy_types = ["Ingress"]
}
}
resource "kubernetes_network_policy" "aws_fluent_bit_allow_namespace" {
count = local.aws_fluent_bit["enabled"] && local.aws_fluent_bit["default_network_policy"] ? 1 : 0
metadata {
name = "${kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]}-allow-namespace"
namespace = kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]
}
spec {
pod_selector {
}
ingress {
from {
namespace_selector {
match_labels = {
name = kubernetes_namespace.aws_fluent_bit.*.metadata.0.name[count.index]
}
}
}
}
policy_types = ["Ingress"]
}
}