Skip to content

Commit 019456c

Browse files
Add a more concrete eg of in-cluster config (#2324)
Co-authored-by: Aleksandr Rybolovlev <[email protected]>
1 parent d7cfb2c commit 019456c

File tree

6 files changed

+150
-124
lines changed

6 files changed

+150
-124
lines changed

_examples/in-cluster/README.md

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,129 @@
11
# Example: In-cluster
22

3-
Running terraform in a kubernetes cluster and using in-cluster config.
3+
Running Terraform in a Kubernetes cluster using in-cluster config.
44

5-
## Prerequisites
5+
## Steps
66

7-
*This example uses syntax elements specific to Terraform version 0.12+.
8-
It will not work out-of-the-box with Terraform 0.11.x and lower.*
7+
Executing Terraform in a Kubernetes cluster using an in-cluster config would require a service account with appropriate privileges attached to the Pod where Terraform is running.
98

9+
Below are the necessary steps to create a new service account `terraform` and grant permissions to create a Pod in a `default` namespace using the `kubernetes_pod_v1` Terraform resource as a namespaced resource example.
1010

11-
Standard run:
11+
1. Create a new service account:
1212

13-
```
14-
# terraform apply \
15-
-var "minikube_host_ip=$(minikube --profile kubernetes-1.16 ip)"
16-
```
13+
```yaml
14+
apiVersion: v1
15+
kind: ServiceAccount
16+
metadata:
17+
name: terraform
18+
```
1719
18-
With a custom build:
20+
1. Create a Role to grant permissions that are enought to manage Pods via Terraform:
1921
20-
```
21-
# terraform apply \
22-
-var "minikube_host_ip=$(minikube --profile kubernetes-1.16 ip)" \
23-
-var "in_cluster_provider_version=v1.10.1-dev" \
24-
-var "in_cluster_provider_url=https://storage.googleapis.com/my-custom-bucket/terraform-provider-kubernetes"
25-
```
22+
```yaml
23+
apiVersion: rbac.authorization.k8s.io/v1
24+
kind: Role
25+
metadata:
26+
name: terraform
27+
rules:
28+
- apiGroups:
29+
- ""
30+
resources:
31+
- pods
32+
verbs:
33+
- create
34+
- get
35+
- delete
36+
- patch
37+
```
38+
39+
1. Create a RoleBinding to attach service account `terraform` to the target Role:
40+
41+
```yaml
42+
apiVersion: rbac.authorization.k8s.io/v1
43+
kind: RoleBinding
44+
metadata:
45+
name: terraform
46+
roleRef:
47+
apiGroup: rbac.authorization.k8s.io
48+
kind: Role
49+
name: terraform
50+
subjects:
51+
- kind: ServiceAccount
52+
name: terraform
53+
```
54+
55+
1. Create a Pod that will initialize and apply Terraform code:
56+
57+
```yaml
58+
apiVersion: v1
59+
kind: Pod
60+
metadata:
61+
name: terraform
62+
spec:
63+
serviceAccount: terraform
64+
initContainers:
65+
- name: init
66+
image: "hashicorp/terraform"
67+
command: [ "terraform", "-chdir=/terraform", "init" ]
68+
volumeMounts:
69+
- name: terraform
70+
mountPath: /terraform
71+
containers:
72+
- name: apply
73+
image: "hashicorp/terraform"
74+
command: [ "terraform", "-chdir=/terraform", "apply", "-auto-approve" ]
75+
volumeMounts:
76+
- name: terraform
77+
mountPath: /terraform
78+
volumes:
79+
- name: terraform
80+
persistentVolumeClaim:
81+
claimName: terraform
82+
restartPolicy: Never
83+
```
84+
85+
Terraform code example that will work with the above configuration resides in files [`provider.tf`](provider.tf) and [`pod.tf`](pod.tf). As you can see, the provider configuration block is empty. In this case, all the necessary privileges are granted via the service account.
86+
87+
Let's extend the previous example with privileges that are enough to create a Namespace using the `kubernetes_namespace_v1` Terraform resource as a cluster-level resource example.
88+
89+
1. Create a ClusterRole to grant permissions that are enought to manage Pods via Terraform:
90+
91+
```yaml
92+
apiVersion: rbac.authorization.k8s.io/v1
93+
kind: ClusterRole
94+
metadata:
95+
name: terraform
96+
rules:
97+
- apiGroups:
98+
- ""
99+
resources:
100+
- namespaces
101+
verbs:
102+
- create
103+
- get
104+
- delete
105+
- list
106+
- patch
107+
- update
108+
```
109+
110+
1. Create a ClusterRoleBinding to attach service account `terraform` to the target ClusterRole:
111+
112+
```yaml
113+
apiVersion: rbac.authorization.k8s.io/v1
114+
kind: ClusterRoleBinding
115+
metadata:
116+
name: terraform
117+
roleRef:
118+
apiGroup: rbac.authorization.k8s.io
119+
kind: ClusterRole
120+
name: terraform
121+
subjects:
122+
- kind: ServiceAccount
123+
name: terraform
124+
namespace: default
125+
```
126+
127+
Terraform code example can be extanded with [`namespace.tf`](namespace.tf) file. To apply changes restart the Pod where Terraform is running.
128+
129+
Please, always consult with the security team and follow the guidance accepted in your organization when granting RBAC privileges in a Kubernetes cluster.

_examples/in-cluster/main.tf

Lines changed: 0 additions & 108 deletions
This file was deleted.

_examples/in-cluster/namespace.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "kubernetes_namespace_v1" "this" {
5+
metadata {
6+
name = "this"
7+
}
8+
}

_examples/in-cluster/pod.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "kubernetes_pod_v1" "this" {
5+
metadata {
6+
name = "this"
7+
namespace = "default"
8+
}
9+
spec {
10+
container {
11+
name = "this"
12+
image = "busybox"
13+
command = ["sleep", "infinity"]
14+
}
15+
}
16+
}

_examples/in-cluster/provider.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
provider "kubernetes" {}

website/docs/index.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ The provider uses the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` en
112112

113113
If you want to connect to a different cluster than the one terraform is running inside, configure the provider as [above](#credentials-config).
114114

115+
Find more comprehensive `in-cluster` config example [here](https://github.com/hashicorp/terraform-provider-kubernetes/tree/main/_examples/in-cluster).
116+
115117
## Exec plugins
116118

117119
Some cloud providers have short-lived authentication tokens that can expire relatively quickly. To ensure the Kubernetes provider is receiving valid credentials, an exec-based plugin can be used to fetch a new token before initializing the provider. For example, on EKS, the command `eks get-token` can be used:

0 commit comments

Comments
 (0)