Skip to content

Commit a39057a

Browse files
committed
Add documentation on apiClient generator
This adds docs for the apiClient generator, how to use it and examples.
1 parent 1f65535 commit a39057a

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

docs/README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ We currently provide these generators:
136136
- pullRequests
137137
- gitRepository
138138
- matrix
139+
- apiClient
139140

140141
### List generator
141142

@@ -429,6 +430,133 @@ spec:
429430
name: go-demo-repo
430431
```
431432

433+
### apiClient generator
434+
435+
This generator is configured to poll an HTTP endpoint and parse the result as the generated values.
436+
437+
This will poll an endpoint on the interval, instead of using the simpler to use PullRequest generator, you can access GitHub's API with the APIClient generator.
438+
439+
The PullRequest generator is simpler to use, and works across multiple different git-providers.
440+
441+
The GitHub [documentation](https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests) for the API endpoint shows:
442+
443+
```shell
444+
curl \
445+
-H "Accept: application/vnd.github+json" \
446+
-H "Authorization: Bearer <YOUR-TOKEN>"\
447+
-H "X-GitHub-Api-Version: 2022-11-28" \
448+
https://api.github.com/repos/OWNER/REPO/pulls
449+
```
450+
This can be translated into...
451+
```yaml
452+
apiVersion: templates.weave.works/v1alpha1
453+
kind: GitOpsSet
454+
metadata:
455+
labels:
456+
app.kubernetes.io/name: gitopsset
457+
app.kubernetes.io/instance: gitopsset-sample
458+
app.kubernetes.io/part-of: gitopssets-controller
459+
app.kubernetes.io/managed-by: kustomize
460+
app.kubernetes.io/created-by: gitopssets-controller
461+
name: api-client-sample
462+
spec:
463+
generators:
464+
- apiClient:
465+
interval: 5m
466+
endpoint: https://api.github.com/repos/bigkevmcd/go-demo/pulls
467+
headersRef:
468+
name: github-secret
469+
kind: Secret
470+
templates:
471+
- content:
472+
apiVersion: source.toolkit.fluxcd.io/v1beta2
473+
kind: GitRepository
474+
metadata:
475+
name: "pr-{{ .Element.id | toJson}}-gitrepository"
476+
namespace: default
477+
spec:
478+
interval: 5m0s
479+
url: "{{ .Element.head.repo.clone_url }}"
480+
ref:
481+
branch: "{{ .Element.head.ref }}"
482+
- content:
483+
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
484+
kind: Kustomization
485+
metadata:
486+
name: "pr-{{ .Element.id | toJson }}-demo"
487+
namespace: default
488+
spec:
489+
interval: 5m
490+
path: "./examples/kustomize/environments/dev"
491+
prune: true
492+
targetNamespace: "{{ .Element.head.ref }}-ns"
493+
sourceRef:
494+
kind: GitRepository
495+
name: "pr-{{ .Element.id | toJson }}-gitrepository"
496+
```
497+
As with the [Pull Request generator](#pullrequests-generator), this also requires a secret token to be able to access the API
498+
499+
We need to pass this as an HTTP header.
500+
```yaml
501+
apiVersion: v1
502+
kind: Secret
503+
metadata:
504+
name: github-secret
505+
namespace: default
506+
type: Opaque
507+
stringData:
508+
Accept: application/vnd.github+json
509+
Authorization: Bearer ghp_<redacted>
510+
X-GitHub-Api-Version: "2022-11-28"
511+
```
512+
The keys in the secret match the command-line example using curl.
513+
514+
Unlike the Pull Request generator, you need to figure out the paths to the
515+
elements yourself.
516+
517+
#### APIClient JSONPath
518+
519+
Not all APIs return an array of JSON objects, sometimes it's nested within a result type structure e.g.
520+
521+
```json
522+
{
523+
"things": [
524+
{
525+
"env": "dev",
526+
"team": "dev-team"
527+
},
528+
{
529+
"env": "production",
530+
"team": "opts-team"
531+
},
532+
{
533+
"env": "staging",
534+
"team": "opts-team"
535+
}
536+
]
537+
}
538+
```
539+
You can use JSONPath to extract the fields from this data...
540+
```
541+
apiVersion: templates.weave.works/v1alpha1
542+
kind: GitOpsSet
543+
metadata:
544+
labels:
545+
app.kubernetes.io/name: gitopsset
546+
app.kubernetes.io/instance: gitopsset-sample
547+
app.kubernetes.io/part-of: gitopssets-controller
548+
app.kubernetes.io/managed-by: kustomize
549+
app.kubernetes.io/created-by: gitopssets-controller
550+
name: api-client-sample
551+
spec:
552+
generators:
553+
- apiClient:
554+
interval: 5m
555+
endpoint: https://api.example.com/demo
556+
jsonPath: "{ $.things }"
557+
```
558+
This will generate three maps for templates, with just the _env_ and _team_ keys.
559+
432560
## Templating functions
433561
434562
Currently, the [Sprig](http://masterminds.github.io/sprig/) functions are available in the templating, with some functions removed[^sprig] for security reasons.

0 commit comments

Comments
 (0)