Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 5e73b9f

Browse files
committed
relative path support for spk
1 parent a26326d commit 5e73b9f

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# Relative Paths for Bedrock Terraform Infrastructure
2+
3+
| Revision | Date | Author | Remarks |
4+
| -------: | ------------ | -------------- | ----------------- |
5+
| 0.1 | Mar-23, 2020 | Nathaniel Rose | Initial Draft |
6+
| 0.2 | Mar-26, 2020 | Nathaniel Rose | Changes Reflected |
7+
8+
## 1. Overview
9+
10+
Terraform modules use a source parameter to locate the resource module target to
11+
download in order to initialize a terraform environment for deployment. A module
12+
can call other modules, which lets you include the child module's resources into
13+
the configuration in a concise way. Modules can also be called multiple times,
14+
either within the same configuration or in separate configurations, allowing
15+
resource configurations to be packaged and re-used.
16+
17+
This document outlines multiple solution that seeks to validate alternative ways
18+
in which relative paths for child module may be supported in Bedrock
19+
implementation and the SPK cli tooling.
20+
21+
```
22+
module "servers" {
23+
source = "./app-cluster"
24+
25+
servers = 5
26+
}
27+
```
28+
29+
### Issues Addressed:
30+
31+
1. As an SRE, I want to use module sources that are versioned and localalized to
32+
the terraform templates repo.
33+
2. As an Operator, I want automated CI pipelines that can handle internal
34+
references for incoming PR that modify my custom Bedrock modules.
35+
36+
## 2. Solutions
37+
38+
An existing pull request for Bedrock currently exists that verifies relative
39+
path implementation with the use of Bedrock Provided Templates in
40+
[#1189](https://github.com/microsoft/bedrock/pull/1189). This design document
41+
seeks to propose interoperability of the relative paths with SPK tooling and
42+
adjustment of infrastructure pipeline.
43+
44+
In the current implementation on SPK infra practices, the tool is embedded into
45+
a generation pipeline that provisions terraform files respective to the modified
46+
infra HLD. `spk infra generate` provisions a terraform deployment template files
47+
based on the key values provided in the target directory's `definition.yaml`. If
48+
the `source` template in the `definition.yaml` uses relative paths in respects
49+
to the source, `infra generate` command will produce a terraform file with the
50+
module source out of scope from the provisioned terraform files.
51+
52+
When executing a `terraform init` on the generated deployment files it produces
53+
an error similar to the following:
54+
55+
```
56+
Initializing modules...
57+
- aks-gitops in
58+
- provider in
59+
- vnet in
60+
61+
Error: Unreadable module directory
62+
63+
Unable to evaluate directory symlink: lstat ../../azure: no such file or
64+
directory
65+
```
66+
67+
### 2.1 Munge together URL with relative Path in SPK Generate - **ACCEPTED DESIGN**
68+
69+
One option to address this issue is to directly modify the generated `.tf` files
70+
to reflect the respective module source. Inside the `infra generate` command we
71+
can use the `source`, `version`, and `template` values to modify the terraform
72+
child module source to a remote module source for terraform to download upon
73+
initialization.
74+
75+
Example:
76+
77+
**`Definition.yaml`**
78+
79+
```
80+
name: fabrikam
81+
source: "https://github.com/Microsoft/bedrock.git"
82+
template: cluster/environments/azure-single-keyvault
83+
version: nate.infra.relative.paths
84+
.
85+
.
86+
.
87+
```
88+
89+
Use a function to get the module path from root of remote repo in cached
90+
directory. (`pathFunction`)
91+
92+
> source= ~~https://~~`source`?ref=`version`//`pathFunction()`"
93+
94+
**Output `Main.tf`**
95+
96+
```
97+
terraform {
98+
backend "azurerm" {}
99+
}
100+
101+
data "azurerm_client_config" "current" {}
102+
103+
data "azurerm_resource_group" "cluster_rg" {
104+
name = var.resource_group_name
105+
}
106+
107+
data "azurerm_resource_group" "keyvault" {
108+
name = var.keyvault_resource_group
109+
}
110+
111+
data "azurerm_subnet" "vnet" {
112+
name = var.subnet_name
113+
virtual_network_name = var.vnet_name
114+
resource_group_name = data.azurerm_resource_group.keyvault.name
115+
}
116+
117+
module "aks-gitops" {
118+
source = "github.com/microsoft/bedrock?ref=master//cluster/azure/aks-gitops"
119+
}
120+
121+
# Create Azure Key Vault role for SP
122+
module "keyvault_flexvolume_role" {
123+
source = "github.com/microsoft/bedrock?ref=master//cluster/azure/keyvault_flexvol_role"
124+
}
125+
126+
# Deploy central keyvault flexvolume
127+
module "flex_volume" {
128+
source = "github.com/microsoft/bedrock?ref=master//cluster/azure/keyvault_flexvol"
129+
}
130+
```
131+
132+
Limitations:
133+
134+
- Manipulating user input data and output terraform files
135+
136+
### 2.2 Copy Modules to Alternative Configured Generated Directory
137+
138+
Another option is to copy modules from cached remote directory to the generated
139+
folder. This allows SPK to directly reference the parent module source with the
140+
generated terraform templates. In addition, this would also require the
141+
templates to be placed accordingly in respects to relative paths to parent
142+
modules. In Bedrock, the template folders are 3 levels down
143+
(../../../cluster/azure/keyvault_flex_vol")
144+
145+
```
146+
fabrikam/
147+
definition.yaml
148+
fabrikam-central/
149+
definition.yaml
150+
fabrikam-east/
151+
definition.yaml
152+
fabrikam-west/
153+
definition.yaml
154+
fabrikam-generated/
155+
cluster
156+
azure
157+
common
158+
minikube
159+
environments
160+
deployments
161+
fabrikam-central/
162+
main.tf
163+
variables.tf
164+
keyvault.tf
165+
terraform.tfvars
166+
backend.tfvars
167+
fabrikam-east/
168+
main.tf
169+
variables.tf
170+
keyvault.tf
171+
terraform.tfvars
172+
backend.tfvars
173+
fabrikam-west/
174+
main.tf
175+
variables.tf
176+
keyvault.tf
177+
terraform.tfvars
178+
backend.tfvars
179+
```
180+
181+
Limitations:
182+
183+
- Very coupled to current organization of Bedrock and will have breaking changes
184+
when introducing new folders at template /module levels
185+
- Copying modules twice local to agent, once for cache and again during generate
186+
187+
### 2.3 Use a symlink to reference the modules
188+
189+
A symbolic link, also termed a soft link, is a special kind of file that points
190+
to another file, much like a shortcut in Windows or a Macintosh alias. This will
191+
allow generate the alias the cached repo for all `spk infra generate` commands.
192+
193+
`ln -s modules /path/to/modules`
194+
195+
Inside of spk, the terraform files will now reference the symlink which is
196+
cached.
197+
198+
**Output `Main.tf`**
199+
200+
```
201+
terraform {
202+
backend "azurerm" {}
203+
}
204+
205+
data "azurerm_client_config" "current" {}
206+
207+
data "azurerm_resource_group" "cluster_rg" {
208+
name = var.resource_group_name
209+
}
210+
211+
data "azurerm_resource_group" "keyvault" {
212+
name = var.keyvault_resource_group
213+
}
214+
215+
data "azurerm_subnet" "vnet" {
216+
name = var.subnet_name
217+
virtual_network_name = var.vnet_name
218+
resource_group_name = data.azurerm_resource_group.keyvault.name
219+
}
220+
221+
module "aks-gitops" {
222+
source = "modules/azure/aks-gitops"
223+
}
224+
225+
# Create Azure Key Vault role for SP
226+
module "keyvault_flexvolume_role" {
227+
source = "modules/azure/keyvault_flexvol_role"
228+
}
229+
230+
# Deploy central keyvault flexvolume
231+
module "flex_volume" {
232+
source = "modules/azure/keyvault_flexvol"
233+
}
234+
```
235+
236+
Limitations:
237+
238+
- Modifying the terraform output files.
239+
- Added complexity with symlink alias which prevents ease of simply running
240+
terraform commands on native machine

0 commit comments

Comments
 (0)