Skip to content

Commit f7c5f69

Browse files
committed
git: add docs for usage with Gerrit
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 192b9e0 commit f7c5f69

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

docs/spec/v1beta1/imageupdateautomations.md

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ spec:
398398
name: fluxcdbot
399399
```
400400
There are over 70 available functions. Some of them are defined by the [Go template language](https://pkg.go.dev/text/template) itself. Most of the others are part of the [Sprig template library](http://masterminds.github.io/sprig/).
401+
401402
### Push
402403

403404
The optional `push` field defines how commits are pushed to the origin.
@@ -463,21 +464,102 @@ spec:
463464
branch: auto
464465
```
465466
466-
In the following snippet, updates and commits will be made on the `auto` branch locally.
467-
The commits will be then pushed to the `auto` branch and then using the `refs/heads/auto:refs/heads/main`
468-
refspec:
467+
In the following snippet, updates and commits will be made on the `main` branch locally.
468+
The commits will be then pushed using the `refs/heads/main:refs/heads/auto` refspec:
469469

470470
```yaml
471471
spec:
472472
git:
473473
checkout:
474474
ref:
475475
branch: main
476+
push:
477+
refspec: refs/heads/main:refs/heads/auto
478+
```
479+
480+
#### Gerrit
481+
482+
483+
[Gerrit](https://www.gerritcodereview.com/) operates differently from a
484+
standard Git server. Rather than sending individual commits to a branch,
485+
all changes are bundled into a single commit. This commit requires a distinct
486+
identifier separate from the commit SHA. Additionally, instead of initiating
487+
a Pull Request between branches, the commit is pushed using a refspec:
488+
`HEAD:refs/for/main`.
489+
490+
As the image-automation-controller is primarily designed to work with
491+
standard Git servers, these special characteristics necessitate a few
492+
workarounds. The following is an example configuration that works
493+
well with Gerrit:
494+
495+
```yaml
496+
spec:
497+
git:
498+
checkout:
499+
ref:
500+
branch: main
501+
commit:
502+
author:
503+
email: flux@localdomain
504+
name: flux
505+
messageTemplate: |
506+
Perform automatic image update
507+
508+
Automation name: {{ .AutomationObject }}
509+
510+
Files:
511+
{{ range $filename, $_ := .Updated.Files -}}
512+
- {{ $filename }}
513+
{{ end }}
514+
Objects:
515+
{{ range $resource, $_ := .Updated.Objects -}}
516+
- {{ $resource.Kind }} {{ $resource.Name }}
517+
{{ end }}
518+
Images:
519+
{{ range .Updated.Images -}}
520+
- {{ . }}
521+
{{ end }}
522+
{{- $ChangeId := .AutomationObject -}}
523+
{{- $ChangeId = printf "%s%s" $ChangeId ( .Updated.Files | toString ) -}}
524+
{{- $ChangeId = printf "%s%s" $ChangeId ( .Updated.Objects | toString ) -}}
525+
{{- $ChangeId = printf "%s%s" $ChangeId ( .Updated.Images | toString ) }}
526+
Change-Id: {{ printf "I%s" ( sha256sum $ChangeId | trunc 40 ) }}
476527
push:
477528
branch: auto
478529
refspec: refs/heads/auto:refs/heads/main
479530
```
480531

532+
This instructs the image-automation-controller to clone the repository using the
533+
`main` branch but execute its update logic and commit with the provided message
534+
template on the `auto` branch. Commits are then pushed to the `auto` branch,
535+
followed by pushing the `HEAD` of the `auto` branch to the `HEAD` of the remote
536+
`main` branch. The message template ensures the inclusion of a [Change-Id](https://gerrit-review.googlesource.com/Documentation/concept-changes.html#change-id)
537+
at the bottom of the commit message.
538+
539+
The initial branch push aims to prevent multiple
540+
[Patch Sets](https://gerrit-review.googlesource.com/Documentation/concept-patch-sets.html).
541+
If we exclude `.push.branch` and only specify
542+
`.push.refspec: refs/heads/main:refs/heads/main`, the desired [Change](https://gerrit-review.googlesource.com/Documentation/concept-changes.html)
543+
can be created as intended. However, when the controller freshly clones the
544+
`main` branch while a Change is open, it executes its update logic on `main`,
545+
leading to new commits being pushed with the same changes to the existing open
546+
Change. Specifying `.push.branch` circumvents this by instructing the controller
547+
to apply the update logic to the `auto` branch, already containing the desired
548+
commit. This approach is also recommended in the
549+
[Gerrit documentation](https://gerrit-review.googlesource.com/Documentation/intro-gerrit-walkthrough-github.html#create-change).
550+
551+
Another thing to note is the syntax of `.push.refspec`. Instead of it being
552+
`HEAD:refs/for/main`, commonly used by Gerrit users, we specify the full
553+
refname `refs/heads/auto` in the source part of the refpsec.
554+
555+
**Note:** A known limitation of using the image-automation-controller with
556+
Gerrit involves handling multiple concurrent Changes. This is due to the
557+
calculation of the Change-Id, relying on factors like file names and image
558+
tags. If the controller introduces a new file or modifies a previously updated
559+
image tag to a different one, it leads to a distinct Change-Id for the commit.
560+
Consequently, this action will trigger the creation of an additional Change,
561+
even when an existing Change containing outdated modifications remains open.
562+
481563
## Update strategy
482564

483565
The `.spec.update` field specifies how to carry out updates on the git repository. There is one

0 commit comments

Comments
 (0)