Skip to content

Commit

Permalink
Merge pull request #29 from datarootsio/issue_18_host_aliases
Browse files Browse the repository at this point in the history
Adding the possibility to add host aliases
  • Loading branch information
sam-dumont authored Aug 6, 2020
2 parents 9c854a9 + 2f6ce74 commit a0d872b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
57 changes: 34 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,30 @@ However, you have to be consistent across variables, you cannot mix styles.

## Inputs

| Name | Description | Type | Default | Required |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------- | :------: |
| annotations | Map of annotations to add on containers. | `map(string)` | `{}` | no |
| args | Arguments to pass to the container | `any` | `{}` | no |
| command | Command that the container will run | `any` | `{}` | no |
| environment\_variables | Map of environment variables to inject in containers. | `any` | `{}` | no |
| environment\_variables\_from\_secret | Map of environment variables to inject in containers, from existing secrets. | `any` | `{}` | no |
| hpa | settings for the horizontal pod autoscaler | `any` | <pre>{<br> "enabled": false,<br> "max_replicas": 6,<br> "min_replicas": 2,<br> "target_cpu": 80<br>}</pre> | no |
| image | The image to deploy. | `any` | n/a | yes |
| image\_pull\_secrets | List of image pull secrets to use with the containers | `list(string)` | `[]` | no |
| inject\_linkerd | Add the necessary annotations for linkerd injection | `bool` | `false` | no |
| liveness\_probes | Map of liveness probes per container. Pass the regular terraform object as is : https://www.terraform.io/docs/providers/kubernetes/r/deployment.html#liveness_probe-1 | `any` | n/a | yes |
| name | The name of the deployment. Will be used for all other resources | `string` | n/a | yes |
| namespace | The namespace where this deployment will live. Must exists. | `string` | n/a | yes |
| node\_selector | Map of labels and values for node selection | `map(string)` | `{}` | no |
| ports | Map of ports to expose, and associated settings. | `any` | `{}` | no |
| readiness\_probes | Map of readiness probes per container. Pass the regular terraform object as is : https://www.terraform.io/docs/providers/kubernetes/r/deployment.html#readiness_probe-1 | `any` | n/a | yes |
| replicas | Amount of replicas | `number` | `1` | no |
| resources\_limits | Map of resources limits to assign to the container | `map` | <pre>{<br> "cpu": "0.2",<br> "memory": "256Mi"<br>}</pre> | no |
| resources\_requests | Map of resources requests to assign to the container | `map` | <pre>{<br> "cpu": "0.1",<br> "memory": "128Mi"<br>}</pre> | no |
| volume\_mounts | Map of volumes to mount. | `any` | `{}` | no |
| volumes\_mounts\_from\_config\_map | Map of volumes to mount from config maps. | `any` | `{}` | no |
| volumes\_mounts\_from\_secret | Map of volumes to mount from secrets. | `any` | `{}` | no |
| Name | Description | Type | Default | Required |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- | :------: |
| annotations | Map of annotations to add on containers. | `map(string)` | `{}` | no |
| args | Arguments to pass to the container | `any` | `{}` | no |
| command | Command that the container will run | `any` | `{}` | no |
| environment\_variables | Map of environment variables to inject in containers. | `any` | `{}` | no |
| environment\_variables\_from\_secret | Map of environment variables to inject in containers, from existing secrets. | `any` | `{}` | no |
| host\_aliases | Host aliases to set up in the pod. | `map(list(string))` | `{}` | no |
| hpa | settings for the horizontal pod autoscaler | `any` | <pre>{<br> "enabled": false,<br> "max_replicas": 6,<br> "min_replicas": 2,<br> "target_cpu": 80<br>}</pre> | no |
| image | The image to deploy. | `any` | n/a | yes |
| image\_pull\_secrets | List of image pull secrets to use with the containers | `list(string)` | `[]` | no |
| inject\_linkerd | Add the necessary annotations for linkerd injection | `bool` | `false` | no |
| liveness\_probes | Map of liveness probes per container. Pass the regular terraform object as is : https://www.terraform.io/docs/providers/kubernetes/r/deployment.html#liveness_probe-1 | `any` | n/a | yes |
| name | The name of the deployment. Will be used for all other resources | `string` | n/a | yes |
| namespace | The namespace where this deployment will live. Must exists. | `string` | n/a | yes |
| node\_selector | Map of labels and values for node selection | `map(string)` | `{}` | no |
| ports | Map of ports to expose, and associated settings. | `any` | `{}` | no |
| readiness\_probes | Map of readiness probes per container. Pass the regular terraform object as is : https://www.terraform.io/docs/providers/kubernetes/r/deployment.html#readiness_probe-1 | `any` | n/a | yes |
| replicas | Amount of replicas | `number` | `1` | no |
| resources\_limits | Map of resources limits to assign to the container | `map` | <pre>{<br> "cpu": "0.2",<br> "memory": "256Mi"<br>}</pre> | no |
| resources\_requests | Map of resources requests to assign to the container | `map` | <pre>{<br> "cpu": "0.1",<br> "memory": "128Mi"<br>}</pre> | no |
| volume\_mounts | Map of volumes to mount. | `any` | `{}` | no |
| volumes\_mounts\_from\_config\_map | Map of volumes to mount from config maps. | `any` | `{}` | no |
| volumes\_mounts\_from\_secret | Map of volumes to mount from secrets. | `any` | `{}` | no |

## Example values

Expand Down Expand Up @@ -259,6 +260,16 @@ annotations = {
}
```

### Host aliases

A map `{string = list(string)}` defining host aliases to set up in the pod.
```hcl
host_aliases = {
"127.0.0.1" = ["foo.bar"],
"127.0.0.128" = ["bar.baz", "baz.qux"]
}
```

## Terraform plan output with the example values


Expand Down
7 changes: 7 additions & 0 deletions deployment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ resource "kubernetes_deployment" "container" {
}

spec {
dynamic "host_aliases" {
for_each = var.host_aliases
content {
hostnames = host_aliases.value
ip = host_aliases.key
}
}

automount_service_account_token = true

Expand Down
5 changes: 5 additions & 0 deletions doc.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ annotations = {
"bar" = "baz"
}

host_aliases = {
"127.0.0.1" = ["foo.bar"],
"8.8.8.8" = ["bar.baz", "baz.qux"]
}

environment_variables_from_secret = {
"container-a" = {
"FOO_SECRET" = {
Expand Down
10 changes: 8 additions & 2 deletions test/terraform_apply_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestApplyAndDestroyWithDefaultValues(t *testing.T) {
pod := pods[0]
container := pod.Spec.Containers[0]

assert.Equal(t,len(pods),2)
assert.Equal(t, len(pods), 2)
assert.Equal(t, "training/webapp:latest", container.Image)
assert.NotContains(t, pod.ObjectMeta.Annotations, "linkerd.io/inject")
assert.Contains(t, pod.ObjectMeta.Annotations, "foo")
Expand Down Expand Up @@ -135,6 +135,10 @@ func TestApplyAndDestroyWithSingleContainer(t *testing.T) {

options.Vars["image_pull_secrets"] = []string{"'my-secret'", "'my-other-secret'"}

options.Vars["host_aliases"] = map[string]interface{}{
"127.0.0.1": []string{"foo.bar", "bar.baz"},
}

options.Vars["liveness_probes"] = map[string]interface{}{
"tcp_socket": map[string]interface{}{
"port": 5000,
Expand Down Expand Up @@ -176,12 +180,14 @@ func TestApplyAndDestroyWithSingleContainer(t *testing.T) {
pod := pods[0]
container := pod.Spec.Containers[0]

assert.Equal(t,len(pods),1)
assert.Equal(t, len(pods), 1)
assert.Equal(t, "training/webapp:latest", container.Image)
assert.Contains(t, pod.ObjectMeta.Annotations, "linkerd.io/inject")
assert.Contains(t, pod.ObjectMeta.Annotations, "foo")
assert.Contains(t, pod.ObjectMeta.Annotations, "bar")
assert.Equal(t, "bar", pod.ObjectMeta.Annotations["foo"])
assert.EqualValues(t, "127.0.0.1", pod.Spec.HostAliases[0].IP)
assert.EqualValues(t, []string{"foo.bar", "bar.baz"}, pod.Spec.HostAliases[0].Hostnames)
assert.Equal(t, "enabled", pod.ObjectMeta.Annotations["linkerd.io/inject"])
}

Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ variable "inject_linkerd" {
description = "Add the necessary annotations for linkerd injection"
}

variable "host_aliases" {
type = map(list(string))
default = {}
}

variable "args" {
type = any
description = "Arguments to pass to the container"
Expand Down

0 comments on commit a0d872b

Please sign in to comment.