Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 50d76c5

Browse files
authored
Merge pull request #23 from displague/separate_modules
refactored modules
2 parents ff7f2c8 + 3480f90 commit 50d76c5

37 files changed

+593
-181
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.tfstate
33
*.tfstate.backup
44
*.terraform.tfstate.lock.info
5+
*.tfvars
56

67
# Module directory
78
.terraform/

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ Create a `main.tf` file in a new directory with the following contents:
3333
```hcl
3434
module "k8s" {
3535
source = "linode/k8s/linode"
36+
3637
linode_token = "YOUR TOKEN HERE"
3738
}
3839
```
3940

4041
That's all it takes to get started!
4142

43+
Pin to a specific module version using `version = "..."` to avoid upgrading to a version with breaking changes. Upgrades to this module could potentially replace all master and worker nodes resulting in data loss. The `terraform plan` will report this, but it may not be obvious.
44+
45+
```hcl
46+
module "k8s" {
47+
source = "linode/k8s/linode"
48+
version = "0.1.0"
49+
50+
linode_token = "YOUR TOKEN HERE"
51+
}
52+
```
53+
4254
Choose a Terraform workspace name (because the default is `default`). In this example we've chosen `linode`. The workspace name will be used as a prefix for Linode resource created in this cluster, for example: `linode-master-1`, `linode-node-1`. Alternate workspaces can be created and selected to change clusters.
4355

4456
```bash
@@ -164,6 +176,14 @@ Or if you won't be submitting changes, you can use `terraform init`:
164176
terraform init --from-module=linode/k8s/linode linode-k8s
165177
```
166178

179+
### Modules
180+
181+
This terraform modules is composed of three sub-modules for reuse and separation of concerns.
182+
183+
* Instance - Accepts all necessary Linode Instance provisioning variables and performs CoreOS Container Linux common tasks for the Linode environment.
184+
* Master - Uses the Instance module as a base and futher provisions a Kubernetes control-plane.
185+
* Node - Uses the Instance module as a base and further provisions a Kubernetes worker joined to a control-plane using module parameters.
186+
167187
### Contribution Guidelines
168188

169189
Would you like to improve the `terraform-linode-k8s` module? Please start [here](https://github.com/linode/terraform-linode-k8s/blob/master/.github/CONTRIBUTING.md).

example/main.tf

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// # Work In Progress Example
2+
//
3+
// This generally demonstrates how to use the terraform-linode-k8s module, however there
4+
// are some provisioning errors with the usage of the helm provider here. The
5+
// install.sh script in this example/ directory is equivalent.
6+
//
7+
// ## TODO
8+
// - Fix timeouts (set better "depends_on" values)
9+
// - Fix permission issue with helm listing configmaps from the kube-system namespace:
10+
// https://github.com/terraform-providers/terraform-provider-helm/issues/77
11+
12+
module "linode_k8s" {
13+
# Work against a branch:
14+
# source = "git::https://github.com/linode/terraform-linode-k8s?ref=some_branch"
15+
#
16+
# Or download a tagged releases
17+
source = "linode/k8s/linode"
18+
version = "0.1.0"
19+
20+
nodes = "${var.nodes}"
21+
linode_token = "${var.linode_token}"
22+
}
23+
24+
variable "nodes" {
25+
default = "3"
26+
}
27+
28+
variable "linode_token" {
29+
description = "Linode APIv4 Token"
30+
}
31+
32+
variable "linode_domain" {
33+
description = "Domain managed by Linode Domain Manager"
34+
}
35+
36+
provider "kubernetes" {
37+
config_path = "${module.linode_k8s.kubectl_config}"
38+
}
39+
40+
resource "kubernetes_service_account" "tiller" {
41+
metadata {
42+
name = "tiller"
43+
namespace = "kube-system"
44+
}
45+
}
46+
47+
resource "kubernetes_cluster_role_binding" "tiller" {
48+
depends_on = ["kubernetes_service_account.tiller"]
49+
50+
metadata {
51+
name = "tiller"
52+
}
53+
54+
role_ref {
55+
api_group = "rbac.authorization.k8s.io"
56+
kind = "ClusterRole"
57+
name = "cluster-admin"
58+
}
59+
60+
subject {
61+
kind = "User"
62+
name = "system:serviceaccount:kube-system:tiller"
63+
}
64+
}
65+
66+
resource null_resource "tiller" {
67+
depends_on = ["kubernetes_cluster_role_binding.tiller"]
68+
69+
provisioner "local-exec" {
70+
environment {
71+
KUBECONFIG = "${module.linode_k8s.kubectl_config}"
72+
}
73+
74+
command = "helm init --service-account tiller --wait"
75+
}
76+
}
77+
78+
provider "helm" {
79+
service_account = "tiller"
80+
namespace = "kube-system"
81+
install_tiller = false
82+
83+
kubernetes {
84+
config_path = "${module.linode_k8s.kubectl_config}"
85+
}
86+
}
87+
88+
resource "helm_repository" "incubator" {
89+
name = "incubator"
90+
url = "https://kubernetes-charts-incubator.storage.googleapis.com"
91+
}
92+
93+
resource "helm_release" "wordpress" {
94+
depends_on = ["helm_release.mysqlha"]
95+
name = "stable"
96+
chart = "wordpress"
97+
version = "5.0.1"
98+
values = ["${file("${path.module}/values/wordpress.values.yaml")}"]
99+
100+
set {
101+
name = "ingress.hosts[0].name"
102+
value = "wordpress.${var.linode_domain}"
103+
}
104+
}
105+
106+
resource "helm_release" "mysqlha" {
107+
name = "${helm_repository.incubator.name}"
108+
chart = "mysqlha"
109+
version = "0.4.0"
110+
values = ["${file("${path.module}/values/mysqlha.values.yaml")}"]
111+
}
112+
113+
resource "helm_release" "traefik" {
114+
name = "stable"
115+
chart = "traefik"
116+
version = "1.55.1"
117+
values = ["${file("${path.module}/values/traefik.values.yaml")}"]
118+
119+
set {
120+
name = "service.annotations.external-dns\\.alpha\\.kubernetes\\.io/hostname\\.io/hostname"
121+
value = "dashboard.${var.linode_domain}"
122+
}
123+
124+
set {
125+
name = "dashboard.domain"
126+
value = "dashboard.${var.linode_domain}"
127+
}
128+
}

main.tf

+49-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
provider "linode" {
2-
token = "${var.linode_token}"
3-
version = "1.3.0"
2+
token = "${var.linode_token}"
3+
version = "1.4.0"
44
}
55

66
provider "external" {
@@ -12,7 +12,53 @@ resource "null_resource" "preflight-checks" {
1212
triggers {
1313
key = "${uuid()}"
1414
}
15+
16+
provisioner "local-exec" {
17+
command = "${path.module}/scripts/local/preflight.sh"
18+
}
19+
}
20+
21+
module "masters" {
22+
source = "./modules/masters"
23+
label_prefix = "${var.cluster_name == "" ? terraform.workspace : var.cluster_name}"
24+
node_class = "master"
25+
node_count = "${var.masters}"
26+
node_type = "${var.server_type_master}"
27+
linode_token = "${var.linode_token}"
28+
29+
k8s_version = "${var.k8s_version}"
30+
k8s_feature_gates = "${var.k8s_feature_gates}"
31+
cni_version = "${var.cni_version}"
32+
ssh_public_key = "${var.ssh_public_key}"
33+
region = "${var.region}"
34+
linode_group = "${var.cluster_name}"
35+
36+
//todo variable instead of workspace?
37+
cluster_name = "${var.cluster_name == "" ? terraform.workspace : var.cluster_name}"
38+
}
39+
40+
module "nodes" {
41+
source = "./modules/nodes"
42+
label_prefix = "${var.cluster_name == "" ? terraform.workspace : var.cluster_name}"
43+
node_class = "node"
44+
node_count = "${var.nodes}"
45+
node_type = "${var.server_type_node}"
46+
47+
k8s_version = "${var.k8s_version}"
48+
k8s_feature_gates = "${var.k8s_feature_gates}"
49+
cni_version = "${var.cni_version}"
50+
ssh_public_key = "${var.ssh_public_key}"
51+
region = "${var.region}"
52+
linode_group = "${var.cluster_name}"
53+
kubeadm_join_command = "${module.masters.kubeadm_join_command}"
54+
}
55+
56+
resource "null_resource" "local_kubectl" {
57+
// todo
58+
depends_on = ["module.masters"]
59+
1560
provisioner "local-exec" {
16-
command = "${path.module}/scripts/local/preflight.sh"
61+
command = "${path.module}/scripts/local/kubectl-conf.sh ${terraform.workspace} ${module.masters.k8s_master_public_ip} ${module.masters.k8s_master_private_ip} ${var.ssh_public_key}"
62+
on_failure = "continue"
1763
}
1864
}

master.tf

-88
This file was deleted.

modules/instances/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## CoreOS Kubernetes Instances
2+
3+
This module provisions a Linode Instance using CoreOS while staging some Kubernetes tooling.

modules/instances/main.tf

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
data "linode_instance_type" "type" {
2+
id = "${var.node_type}"
3+
}
4+
5+
resource "linode_instance" "instance" {
6+
count = "${var.node_count}"
7+
region = "${var.region}"
8+
label = "${var.label_prefix == "" ? "" : "${var.label_prefix}-"}${var.node_class}-${count.index + 1}"
9+
group = "${var.linode_group}"
10+
type = "${var.node_type}"
11+
private_ip = "${var.private_ip}"
12+
13+
disk {
14+
label = "boot"
15+
size = "${data.linode_instance_type.type.disk}"
16+
authorized_keys = ["${chomp(file(var.ssh_public_key))}"]
17+
image = "linode/containerlinux"
18+
}
19+
20+
config {
21+
label = "${var.node_class}"
22+
23+
kernel = "linode/direct-disk"
24+
25+
devices {
26+
sda = {
27+
disk_label = "boot"
28+
}
29+
}
30+
}
31+
32+
provisioner "file" {
33+
source = "${path.module}/scripts/"
34+
destination = "/tmp"
35+
36+
connection {
37+
user = "core"
38+
timeout = "300s"
39+
}
40+
}
41+
42+
provisioner "remote-exec" {
43+
inline = [
44+
"set -e",
45+
"chmod +x /tmp/start.sh && sudo /tmp/start.sh",
46+
"chmod +x /tmp/linode-network.sh && sudo /tmp/linode-network.sh ${self.private_ip_address} ${self.label}",
47+
"chmod +x /tmp/kubeadm-install.sh && sudo /tmp/kubeadm-install.sh ${var.k8s_version} ${var.cni_version} ${self.label} ${var.use_public ? self.ip_address : self.private_ip_address} ${var.k8s_feature_gates}",
48+
]
49+
50+
connection {
51+
user = "core"
52+
timeout = "300s"
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)