From 97d53c73b77794cf056eecddcc4c1974637d3340 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Tue, 25 Jun 2024 18:25:58 +0545 Subject: [PATCH] docs: update gitops env var docs --- .../docs/playbooks/Actions/gitops.mdx | 262 ++++++++++++++---- 1 file changed, 201 insertions(+), 61 deletions(-) diff --git a/mission-control/docs/playbooks/Actions/gitops.mdx b/mission-control/docs/playbooks/Actions/gitops.mdx index f5f48d08..94e31bd1 100644 --- a/mission-control/docs/playbooks/Actions/gitops.mdx +++ b/mission-control/docs/playbooks/Actions/gitops.mdx @@ -1,81 +1,92 @@ --- title: GitOps --- -import Templating from "../../reference/playbooks/context.mdx" + +import Templating from '../../reference/playbooks/context.mdx' # GitOps Action GitOps action allows you to make commits and push to a remote repository. -```yaml title="update-pod-namespace.yaml" +```yaml title="edit-kubernetes-manifests-gitops.yaml" apiVersion: mission-control.flanksource.com/v1 kind: Playbook metadata: name: edit-kubernetes-manifests-gitops spec: title: 'Edit Kustomize Resource' - icon: flux + icon: 'flux' parameters: - default: 'chore: update $(.config.type)/$(.config.name)' - label: Commit Message - name: commit_message - - default: $(.config.config | toJSON | neat | json | toYAML) - label: "" - name: yamlInput + label: 'Commit Message' + name: 'commit_message' + - default: '$(.config.config | toJSON | neat | json | toYAML)' + label: 'Changes' + name: 'yamlInput' properties: - size: large - type: code - + size: 'large' + type: 'code' configs: - labelSelector: 'kustomize.toolkit.fluxcd.io/name' - - env: - - name: file_path - value: {{ .config.config | jq `.metadata.annotations["config.kubernetes.io/origin"]` | yaml).path }} - - name: kustomization_path - value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization").Config | json | jq `.spec.path` }} - - name: git_url - value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization/Kubernetes::GitRepository").Config | json | jq `.spec.url` }} - - name: git_branch - value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization/Kubernetes::GitRepository").Config | json | jq `.spec.ref.branch` }} - actions: - - name: Create Pull Request With Changes + - name: 'Create Pull Request With Changes' gitops: repo: - url: '$(.env.git_url)' - connection: - base: '$(.env.git_branch)' - branch: edit-manifest-$(random.Alpha 8) + url: 'https://github.com/flanksource/flux' + connection: 'connection://default/github' + base: 'main' + branch: 'edit-manifest-$(random.Alpha 8)' commit: author: '$(.user.name)' email: '$(.user.email)' - message: $(.params.commit_message) + message: '$(.params.commit_message)' pr: - title: $(.params.commit_message) + title: '$(.params.commit_message)' patches: - - path: '$(filepath.Join .env.kustomization_path .env.file_path)' + - path: 'prod/kustomization.yaml' yq: | select( .kind=="$(.config.config | jq `.kind`)" and .metadata.name=="$(.config.config | jq `.metadata.name`)" ) |= $(.params.yamlInput | yaml | toJSON) - ``` - - - + ### Repository - - ### Commit Detail | Field | Description | Scheme | Required | Templatable | @@ -120,22 +130,45 @@ Files create or delete existing files on the git repo. | `path` | Path to file | `string` | `true` | `true` | | `content` | Content of the file. Use the `$delete` keyword to delete an existing file | `string` | `true` | `true` | -## Env Vars - -GitOps action receives a set of commonly used [env vars](../../reference/playbooks/env-vars). These env vars are extracted by traversing up the Flux Kustomization & Git Repository that created the config resource the playbook is running against. -They are - - - -
- -In order for `git.file` to work, the `config.kubernetes.io/origin` annotation must be present in the config resource. You can set `buildMetadata: [originAnnotations]` in the kustomization file and the kustomization will -add the annotation with the correct path. +## Custom Origin Annotation + +In the example above, you saw a playbook that edited Flux-created manifests. However, there was a slight issue - it had some variables hardcoded, specifically the git repository, git branch, and the kustomization file path. When you have configurations from multiple clusters, you'll need to duplicate the playbook for each cluster, as the git repository and kustomization file path will differ. Even if you have multiple Flux kustomizations on a single cluster, you'll need to duplicate the playbook for each of those kustomizations. + +There's a better way to build a single generic GitOps playbook that works for configurations with any Flux source and kustomization. + +When a GitOps action is run against a config resource that has a `config.kubernetes.io/origin` annotation, the following [env vars](../../reference/playbooks/env-vars) are automatically available: + + + +These env vars are extracted by traversing up the Flux Kustomization and Git Repository that created the config resource the playbook is running against. We'll see this in detail shortly. + +To tag all the resources with that annotation, you'll need to add `originAnnotations` to the buildMetadata field as shown below: + +
```yaml title='kustomization.yaml' apiVersion: kustomize.config.k8s.io/v1beta1 @@ -143,7 +176,7 @@ kind: Kustomization // highlight-next-line buildMetadata: [originAnnotations] resources: - - monitoring +- monitoring ``` The above kustomization would add the following annotation to the config resource: @@ -157,9 +190,116 @@ metadata: path: monitoring/alertmanager.yaml ... ``` +
-## Templating +### Example + + + + +```yaml title="edit-kubernetes-manifests-gitops.yaml" +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: edit-kubernetes-manifests-gitops +spec: + title: 'Edit Kustomize Resource' + icon: flux + parameters: + - default: 'chore: update $(.config.type)/$(.config.name)' + label: Commit Message + name: commit_message + - default: $(.config.config | toJSON | neat | json | toYAML) + label: "" + name: yamlInput + properties: + size: large + type: code + configs: + - labelSelector: 'kustomize.toolkit.fluxcd.io/name' + env: + - name: file_path + value: {{ .config.config | jq `.metadata.annotations["config.kubernetes.io/origin"]` | yaml).path }} + - name: kustomization_path + value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization").Config | json | jq `.spec.path` }} + - name: git_url + value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization/Kubernetes::GitRepository").Config | json | jq `.spec.url` }} + - name: git_branch + value: {{ (catalog_traverse .config.id "Kubernetes::Kustomization/Kubernetes::GitRepository").Config | json | jq `.spec.ref.branch` }} + actions: + - name: Create Pull Request With Changes + gitops: + repo: + url: '$(.env.git_url)' + connection: 'connection://default/github' + base: '$(.env.git_branch)' + branch: edit-manifest-$(random.Alpha 8) + commit: + author: '$(.user.name)' + email: '$(.user.email)' + message: $(.params.commit_message) + pr: + title: $(.params.commit_message) + patches: + - path: '$(filepath.Join .env.kustomization_path .env.file_path)' + yq: | + select( + .kind=="$(.config.config | jq `.kind`)" and + .metadata.name=="$(.config.config | jq `.metadata.name`)" + ) |= $(.params.yamlInput | yaml | toJSON) +``` + + + - +```yaml title="edit-kubernetes-manifests-gitops.yaml" +apiVersion: mission-control.flanksource.com/v1 +kind: Playbook +metadata: + name: edit-kubernetes-manifests-gitops +spec: + title: 'Edit Kustomize Resource' + icon: flux + parameters: + - default: 'chore: update $(.config.type)/$(.config.name)' + label: Commit Message + name: commit_message + - default: $(.config.config | toJSON | neat | json | toYAML) + label: "" + name: yamlInput + properties: + size: large + type: code + configs: + - labelSelector: 'kustomize.toolkit.fluxcd.io/name' + actions: + - name: Create Pull Request With Changes + gitops: + repo: + url: '$(.env.git.url)' + connection: 'connection://default/github' + base: '$(.env.git.branch)' + branch: edit-manifest-$(random.Alpha 8) + commit: + author: '$(.user.name)' + email: '$(.user.email)' + message: $(.params.commit_message) + pr: + title: $(.params.commit_message) + patches: + - path: '$(filepath.Join .env.kustomization.path .env.git.file)' + yq: | + select( + .kind=="$(.config.config | jq `.kind`)" and + .metadata.name=="$(.config.config | jq `.metadata.name`)" + ) |= $(.params.yamlInput | yaml | toJSON) +``` + + + + + +## Templating +