Skip to content

Commit 45505f9

Browse files
authored
Merge pull request #1 from mutablelogic/dev
Release will openldap fixed and coredns added
2 parents d350b69 + e3f3942 commit 45505f9

24 files changed

+471
-392
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ provider "nomad" {
2020
}
2121
```
2222

23+
24+
## coredns
25+
26+
DNS server which could be used to resolve nomad services into dns records
27+
28+
* [Documentation](https://coredns.io/)
29+
* [Terraform Example](examples/coredns.tf)
30+
* [Nomad Job](coredns/nomad/coredns.hcl)
31+
32+
TODO:
33+
* [ ] In progress
34+
* [ ] Add nomad plugin
35+
* [ ] All nomad jobs will need to use the coredns service as a dns_server option
36+
2337
## nginx
2438

2539
Web server and reverse proxy, which can be placed on several nodes

_examples/coredns.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
module "coredns" {
3+
source = "github.com/mutablelogic/tf-nomad//coredns"
4+
5+
// Required parameters
6+
dc = local.datacenter // Nomad datacenter for the cluster
7+
namespace = local.namespace // Nomad namespace for the cluster
8+
9+
// Optional parameters
10+
enabled = true
11+
hosts = ["cm3"] // Host constraint for the job
12+
port = 53 // Port to expose for plaintext connections
13+
}

examples/grafana.tf renamed to _examples/grafana.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
// Example grafana dashboard example
33
module "grafana" {
4-
source = "github.com/mutablelogic/tf-nomad/grafana"
4+
source = "github.com/mutablelogic/tf-nomad//grafana"
55

66
// Required parameters
77
dc = local.datacenter // Nomad datacenter for the cluster
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

coredns/config/Corefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.:53 {
2+
forward . 8.8.8.8 9.9.9.9
3+
log
4+
errors
5+
}

coredns/input.tf

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
variable "dc" {
3+
type = string
4+
description = "Data center name"
5+
}
6+
7+
variable "namespace" {
8+
type = string
9+
description = "Nomad namespace"
10+
default = "default"
11+
}
12+
13+
variable "enabled" {
14+
type = bool
15+
description = "If false, then no job is deployed"
16+
default = true
17+
}
18+
19+
variable "docker_tag" {
20+
type = string
21+
description = "Version of the docker image to use, defaults to latest"
22+
default = "latest"
23+
}
24+
25+
variable "hosts" {
26+
type = list(string)
27+
description = "List of hosts to deploy on. If empty, one allocation will be created"
28+
default = []
29+
}
30+
31+
variable "port" {
32+
type = number
33+
description = "Port to expose plaintext service"
34+
default = 53
35+
}

coredns/locals.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
locals {
3+
docker_image = "coredns/coredns:${var.docker_tag}"
4+
docker_always_pull = var.docker_tag == "latest" ? true : false
5+
}

coredns/main.tf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
resource "nomad_job" "coredns" {
3+
count = var.enabled ? 1 : 0
4+
jobspec = file("${path.module}/nomad/coredns.hcl")
5+
6+
hcl2 {
7+
allow_fs = true
8+
vars = {
9+
dc = jsonencode([var.dc])
10+
namespace = var.namespace
11+
docker_image = local.docker_image
12+
docker_always_pull = jsonencode(local.docker_always_pull)
13+
hosts = jsonencode(var.hosts)
14+
port = var.port
15+
corefile = file("${path.module}/config/Corefile")
16+
}
17+
}
18+
}

coredns/nomad/coredns.hcl

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
// coredns for service discovery
3+
// Docker Image: https://hub.docker.com/r/coredns/coredns/
4+
5+
///////////////////////////////////////////////////////////////////////////////
6+
// VARIABLES
7+
8+
variable "dc" {
9+
description = "data centers that the job is eligible to run in"
10+
type = list(string)
11+
}
12+
13+
variable "namespace" {
14+
description = "namespace that the job runs in"
15+
type = string
16+
default = "default"
17+
}
18+
19+
variable "hosts" {
20+
description = "host constraint for the job, defaults to one host"
21+
type = list(string)
22+
default = []
23+
}
24+
25+
variable "service_provider" {
26+
description = "Service provider, either consul or nomad"
27+
type = string
28+
default = "nomad"
29+
}
30+
31+
variable "docker_image" {
32+
description = "Docker image"
33+
type = string
34+
}
35+
36+
variable "docker_always_pull" {
37+
description = "Pull docker image on every job restart"
38+
type = bool
39+
default = false
40+
}
41+
42+
variable "port" {
43+
description = "Port for plaintext connections"
44+
type = number
45+
default = 53
46+
}
47+
48+
variable "corefile" {
49+
description = "Configuration file for coredns"
50+
type = string
51+
}
52+
53+
///////////////////////////////////////////////////////////////////////////////
54+
// LOCALS
55+
56+
locals {
57+
core_file = format("%s/data/Corefile", NOMAD_ALLOC_DIR)
58+
}
59+
60+
///////////////////////////////////////////////////////////////////////////////
61+
// JOB
62+
63+
job "coredns" {
64+
type = "service"
65+
datacenters = var.dc
66+
namespace = var.namespace
67+
68+
update {
69+
min_healthy_time = "10s"
70+
healthy_deadline = "5m"
71+
health_check = "task_states"
72+
}
73+
74+
/////////////////////////////////////////////////////////////////////////////////
75+
76+
group "coredns" {
77+
count = length(var.hosts) == 0 ? 1 : length(var.hosts)
78+
79+
dynamic "constraint" {
80+
for_each = length(var.hosts) == 0 ? [] : [join(",", var.hosts)]
81+
content {
82+
attribute = node.unique.name
83+
operator = "set_contains_any"
84+
value = constraint.value
85+
}
86+
}
87+
88+
network {
89+
port "dns" {
90+
static = var.port
91+
to = 53
92+
}
93+
}
94+
95+
service {
96+
tags = ["dns"]
97+
name = "coredns-dns"
98+
port = "dns"
99+
provider = var.service_provider
100+
}
101+
102+
ephemeral_disk {
103+
migrate = true
104+
}
105+
106+
task "daemon" {
107+
driver = "docker"
108+
109+
template {
110+
destination = local.core_file
111+
data = var.corefile
112+
}
113+
114+
config {
115+
image = var.docker_image
116+
force_pull = var.docker_always_pull
117+
ports = ["dns"]
118+
args = ["-conf", local.core_file]
119+
}
120+
121+
} // task "daemon"
122+
} // group "coredns"
123+
} // job "coredns"

openldap/input.tf

+7-11
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,8 @@ variable "port" {
3636

3737
variable "data" {
3838
type = string
39-
description = "Directory for data persistence, required"
40-
}
41-
42-
variable "ldif" {
43-
type = string
44-
description = "Directory for additional ldif files, optional"
45-
}
46-
47-
variable "schema" {
48-
type = string
49-
description = "Directory for additional schema files, optional"
39+
description = "Directory for data persistence"
40+
default = ""
5041
}
5142

5243
variable "admin_password" {
@@ -59,3 +50,8 @@ variable "basedn" {
5950
description = "LDAP distinguished name (required)"
6051
type = string
6152
}
53+
54+
variable "organization" {
55+
description = "Organization name (required)"
56+
type = string
57+
}

openldap/ldif/group.ldif

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dn: cn={{ env "NOMAD_META_group" }},ou={{ env "NOMAD_META_groups" }},{{ env "NOMAD_META_basedn" }}
2+
cn: {{ env "NOMAD_META_group" }}
3+
gidNumber: {{ env "NOMAD_META_gid" }}
4+
objectClass: posixGroup
5+
objectClass: groupOfMembers
6+
objectClass: top
7+
description: {{ env "NOMAD_META_description" }}
8+

openldap/ldif/root.ldif

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Root
3+
dn: {{ env "NOMAD_META_basedn" }}
4+
objectClass: top
5+
objectClass: domain
6+
dc: {{ env "NOMAD_META_organization" }}
7+
description: Organization
8+
9+
# Groups
10+
dn: ou={{ env "NOMAD_META_groups" }},{{ env "NOMAD_META_basedn" }}
11+
ou: {{ env "NOMAD_META_groups" }}
12+
objectClass: top
13+
objectClass: organizationalUnit
14+
description: User groups
15+
16+
# Users
17+
dn: ou={{ env "NOMAD_META_users" }},{{ env "NOMAD_META_basedn" }}
18+
ou: {{ env "NOMAD_META_users" }}
19+
objectClass: top
20+
objectClass: organizationalUnit
21+
description: Users
22+

openldap/ldif/tree.ldif

-51
This file was deleted.

openldap/main.tf

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ resource "nomad_job" "ldap" {
1313
hosts = jsonencode(var.hosts)
1414
port = var.port
1515
data = var.data
16-
ldif = var.ldif
17-
schema = var.schema
1816
admin_password = var.admin_password
1917
basedn = var.basedn
18+
organization = var.organization
19+
20+
# LDIF templates which are only applied when the data directory is empty (first run)
21+
ldif = jsonencode({
22+
"root" = file("${path.module}/ldif/root.ldif")
23+
})
24+
schema = jsonencode({
25+
"rfc2307bis" = file("${path.module}/schema/rfc2307bis.ldif")
26+
})
2027
}
2128
}
2229
}

0 commit comments

Comments
 (0)