This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
generating-a-provider.md
352 lines (272 loc) · 11.1 KB
/
generating-a-provider.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Generating a Crossplane Provider
In this guide, we will generate a Crossplane provider based on an existing
Terraform provider using Terrajet.
We have chosen [Terraform GitHub provider] as an example, but the process will
be quite similar for any other Terraform provider.
## Generate
1. Generate a GitHub repository for the Crossplane provider by hitting the
"**Use this template**" button in [provider-jet-template] repository.
2. Clone the repository to your local and `cd` into the repository directory.
Fetch the [upbound/build] submodule by running the following:
```bash
make submodules
```
3. Replace `template` with your provider name.
1. Export `ProviderName`:
```bash
export ProviderNameLower=github
export ProviderNameUpper=GitHub
```
2. Run the `./hack/prepare.sh` script from repo root to prepare the repo, e.g., to
replace all occurrences of `template` with your provider name:
```bash
./hack/prepare.sh
```
4. To configure the Terraform provider to generate from, update the following
variables in `Makefile`:
```makefile
export TERRAFORM_PROVIDER_SOURCE := integrations/github
export TERRAFORM_PROVIDER_VERSION := 4.19.2
export TERRAFORM_PROVIDER_DOWNLOAD_NAME := terraform-provider-github
export TERRAFORM_PROVIDER_DOWNLOAD_URL_PREFIX := https://releases.hashicorp.com/terraform-provider-github/4.19.2
```
You can find `TERRAFORM_PROVIDER_SOURCE` and `TERRAFORM_PROVIDER_VERSION` in
[Terraform GitHub provider] documentation by hitting the "**USE PROVIDER**"
button. Check [this line in controller Dockerfile] to see how these
variables are used to build the provider plugin binary.
5. Implement `ProviderConfig` logic. In `provider-jet-template`, there is already
a boilerplate code in file `internal/clients/${ProviderNameLower}.go` which
takes care of properly fetching secret data referenced from `ProviderConfig`
resource.
For our GitHub provider, we need to check [Terraform documentation for provider
configuration] and provide the keys there:
```go
const (
keyBaseURL = "base_url"
keyOwner = "owner"
keyToken = "token"
// GitHub credentials environment variable names
envToken = "GITHUB_TOKEN"
)
func TerraformSetupBuilder(version, providerSource, providerVersion string) terraform.SetupFn {
...
// set provider configuration
ps.Configuration = map[string]interface{}{}
if v, ok := githubCreds[keyBaseURL]; ok {
ps.Configuration[keyBaseURL] = v
}
if v, ok := githubCreds[keyOwner]; ok {
ps.Configuration[keyOwner] = v
}
// set environment variables for sensitive provider configuration
ps.Env = []string{
fmt.Sprintf(fmtEnvVar, envToken, githubCreds[keyToken]),
}
return ps, nil
}
```
6. Before generating all resources that the provider has, let's go step by step
and only start with generating CRDs for [github_repository] and
[github_branch] Terraform resources.
To limit the resources to be generated, we need to provide an include list
option with `tjconfig.WithIncludeList` in file `config/provider.go`:
```go
pc := tjconfig.NewProviderWithSchema([]byte(providerSchema), resourcePrefix, modulePath,
tjconfig.WithDefaultResourceFn(defaultResourceFn),
tjconfig.WithIncludeList([]string{
"github_repository$",
"github_branch$",
}))
```
7. Finally, we would need to add some custom configurations for these two
resources as follows:
```bash
# Create custom configuration directory for whole repository group
mkdir config/repository
# Create custom configuration directory for whole branch group
mkdir config/branch
```
```bash
cat <<EOF > config/repository/config.go
package repository
import "github.com/crossplane/terrajet/pkg/config"
// Configure configures individual resources by adding custom ResourceConfigurators.
func Configure(p *config.Provider) {
p.AddResourceConfigurator("github_repository", func(r *config.Resource) {
// we need to override the default group that terrajet generated for
// this resource, which would be "github"
r.ShortGroup = "repository"
})
}
EOF
```
```bash
cat <<EOF > config/branch/config.go
package branch
import "github.com/crossplane/terrajet/pkg/config"
func Configure(p *config.Provider) {
p.AddResourceConfigurator("github_branch", func(r *config.Resource) {
// we need to override the default group that terrajet generated for
// this resource, which would be "github"
r.ShortGroup = "branch"
// Identifier for this resource is assigned by the provider. In other
// words it is not simply the name of the resource.
r.ExternalName = config.IdentifierFromProvider
// This resource need the repository in which branch would be created
// as an input. And by defining it as a reference to Repository
// object, we can build cross resource referencing. See
// repositoryRef in the example in the Testing section below.
r.References["repository"] = config.Reference{
Type: "github.com/crossplane-contrib/provider-jet-github/apis/repository/v1alpha1.Repository",
}
})
}
EOF
```
And register custom configurations in `config/provider.go`:
```diff
import (
...
tjconfig "github.com/crossplane/terrajet/pkg/config"
"github.com/crossplane/terrajet/pkg/types/conversion/cli"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+ "github.com/crossplane-contrib/provider-jet-github/config/branch"
+ "github.com/crossplane-contrib/provider-jet-github/config/repository"
)
func GetProvider() *tjconfig.Provider {
...
for _, configure := range []func(provider *tjconfig.Provider){
// add custom config functions
+ repository.Configure,
+ branch.Configure,
} {
configure(pc)
}
```
**_To learn more about custom resource configurations (in step 7), please see
the [Configuring a Resource](/docs/configuring-a-resource.md) document._**
8. Now we can generate our Terrajet Provider:
```bash
make generate
```
### Adding New Resources
To add more resources, please **follow the steps between 6-8 for each resource**.
Alternatively, you can drop the `tjconfig.WithIncludeList` option in provider
Configuration which would generate all resources, and you can add resource
configurations as a next step.
## Test
Now let's test our generated resources.
1. First, we will create example resources under the `examples` directory:
Create example directories for repository and branch groups:
```bash
mkdir examples/repository
mkdir examples/branch
# remove the sample directory which was an example in the template
rm -rf examples/sample
```
Create a provider secret template:
```bash
cat <<EOF > examples/providerconfig/secret.yaml.tmpl
apiVersion: v1
kind: Secret
metadata:
name: example-creds
namespace: crossplane-system
type: Opaque
stringData:
credentials: |
{
"token": "y0ur-t0k3n"
}
EOF
```
Create example for `repository` resource, which will use
`provider-jet-template` repo as template for the repository
to be created:
```bash
cat <<EOF > examples/repository/repository.yaml
apiVersion: repository.github.jet.crossplane.io/v1alpha1
kind: Repository
metadata:
name: hello-crossplane
spec:
forProvider:
description: "Managed with Crossplane Github Provider (generated with Terrajet)"
visibility: public
template:
- owner: crossplane-contrib
repository: provider-jet-template
providerConfigRef:
name: default
EOF
```
Create `branch` resource which refers to the above repository
managed resource:
```bash
cat <<EOF > examples/branch/branch.yaml
apiVersion: branch.github.jet.crossplane.io/v1alpha1
kind: Branch
metadata:
name: hello-terrajet
spec:
forProvider:
branch: hello-terrajet
repositoryRef:
name: hello-crossplane
providerConfigRef:
name: default
EOF
```
2. Generate a [Personal Access Token](https://github.com/settings/tokens) for
your Github account with `repo/public_repo` and `delete_repo` scopes.
3. Create `examples/providerconfig/secret.yaml` from
`examples/providerconfig/secret.yaml.tmpl` and set your token in the file:
```bash
GITHUB_TOKEN=<your-token-here>
cat examples/providerconfig/secret.yaml.tmpl | sed -e "s/y0ur-t0k3n/${GITHUB_TOKEN}/g" > examples/providerconfig/secret.yaml
```
4. Apply CRDs:
```bash
kubectl apply -f package/crds
```
5. Run the provider:
```bash
make run
```
6. Apply ProviderConfig and example manifests (_In another terminal since
the previous command is blocking_):
```bash
# Create "crossplane-system" namespace if not exists
kubectl create namespace crossplane-system --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -f examples/providerconfig/
kubectl apply -f examples/repository/repository.yaml
kubectl apply -f examples/branch/branch.yaml
```
7. Observe managed resources and wait until they are ready:
```bash
watch kubectl get managed
```
```bash
NAME READY SYNCED EXTERNAL-NAME AGE
branch.branch.github.jet.crossplane.io/hello-terrajet True True hello-crossplane:hello-terrajet 89s
NAME READY SYNCED EXTERNAL-NAME AGE
repository.repository.github.jet.crossplane.io/hello-crossplane True True hello-crossplane 89s
```
Verify that repo `hello-crossplane` and branch `hello-terrajet` created under
your GitHub account.
9. Cleanup
```bash
kubectl delete -f examples/branch/branch.yaml
kubectl delete -f examples/repository/repository.yaml
```
Verify that the repo got deleted once deletion is completed on the control
plane.
[comment]: <> (References)
[Terraform GitHub provider]: https://registry.terraform.io/providers/integrations/github/latest/docs
[provider-jet-template]: https://github.com/crossplane-contrib/provider-jet-template
[upbound/build]: https://github.com/upbound/build
[Terraform documentation for provider configuration]: https://registry.terraform.io/providers/integrations/github/latest/docs#argument-reference
[github_repository]: https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository
[github_branch]: https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch
[this line in controller Dockerfile]: https://github.com/crossplane-contrib/provider-jet-template/blob/d9a793dd8a304f09bb2e9694c47c1bade1b6b057/cluster/images/provider-jet-template-controller/Dockerfile#L18-L25
[terraform-plugin-sdk]: https://github.com/hashicorp/terraform-plugin-sdk