Skip to content

Commit e7d7d32

Browse files
authored
Merge branch 'main' into release/v0.41.x
2 parents f80db58 + c77a979 commit e7d7d32

File tree

5 files changed

+115
-8
lines changed

5 files changed

+115
-8
lines changed

docs/spec/v1beta2/imageupdateautomations.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,13 @@ destination reference. An example of a valid refspec is
558558
If both `.push.refspec` and `.push.branch` are specified, then the reconciler
559559
will push to both the destinations. This is particularly useful for working with
560560
Gerrit servers. For more information about this, please refer to the
561-
[Gerrit](#gerrit) section.
561+
[Gerrit](#gerrit) section. This can also be used to automatically open
562+
Pull-Requests in Gitea or Forgejo. See the [Gitea](#gitea) section for
563+
an example.
564+
565+
If only `.push.refspec` is set, without explicitly defining a `.push.branch`, the
566+
controller falls back to pushing to the branch from `checkoutRef` and *also*
567+
pushes to `.push.refspec`.
562568

563569
**Note:** If both `.push.refspec` and `.push.branch` are essentially equal to
564570
each other (for e.g.: `.push.refspec: refs/heads/main:refs/heads/main` and
@@ -958,6 +964,42 @@ image tag to a different one, it leads to a distinct Change-Id for the commit.
958964
Consequently, this action will trigger the creation of an additional Change,
959965
even when an existing Change containing outdated modifications remains open.
960966

967+
#### Gitea
968+
969+
[Gitea](https://docs.gitea.com/usage/agit) and [Forgejo](https://forgejo.org/docs/latest/user/agit-support/) each implement the AGit-Workflow.
970+
This means, the image-automation-controller is able to open a pull-request by
971+
pushing to a `refspec` like `HEAD:refs/for/main` with the apropriate [push-options](#push-options).
972+
973+
The following example opens a PR on a Gitea or Forgejo-Server:
974+
975+
```yaml
976+
apiVersion: image.toolkit.fluxcd.io/v1beta2
977+
kind: ImageUpdateAutomation
978+
metadata:
979+
name: my-automation
980+
namespace: flux-system
981+
spec:
982+
git:
983+
checkout:
984+
ref:
985+
branch: main
986+
push:
987+
branch: flux-updates
988+
refspec: refs/heads/flux-updates:refs/for/main
989+
options:
990+
topic: flux-updates
991+
title: Flux Image Update
992+
description: |-
993+
This PR is automatically opened by the fluxcd *image-automation-controller*
994+
commit:
995+
author:
996+
997+
name: fluxcd
998+
messageTemplate: '{{range .Changed.Changes}}{{print .OldValue}} -> {{println
999+
.NewValue}}{{end}}'
1000+
```
1001+
1002+
9611003
## ImageUpdateAutomation Status
9621004

9631005
### Observed Policies

internal/controller/imageupdateautomation_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ func (r *ImageUpdateAutomationReconciler) reconcile(ctx context.Context, sp *pat
396396
if r.features[features.GitShallowClone] {
397397
checkoutOpts = append(checkoutOpts, source.WithCheckoutOptionShallowClone())
398398
}
399+
if r.features[features.GitSparseCheckout] && obj.Spec.Update.Path != "" {
400+
checkoutOpts = append(checkoutOpts, source.WithCheckoutOptionSparseCheckoutDirectories(obj.Spec.Update.Path))
401+
}
402+
399403
// If full sync is still not needed, configure last observed commit to
400404
// perform optimized clone and obtain a non-concrete commit if the remote
401405
// has not changed.

internal/features/features.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const (
3434
// GitAllBranchReferences enables the download of all branch head references
3535
// when push branches are configured. When enabled fixes fluxcd/flux2#3384.
3636
GitAllBranchReferences = "GitAllBranchReferences"
37+
// GitSparseCheckout enables the use of sparse checkout when pulling source from
38+
// Git repositories.
39+
GitSparseCheckout = "GitSparseCheckout"
3740
// CacheSecretsAndConfigMaps controls whether Secrets and ConfigMaps should
3841
// be cached.
3942
//
@@ -55,6 +58,10 @@ var features = map[string]bool{
5558
// opt-out from v0.28
5659
GitAllBranchReferences: true,
5760

61+
// GitSparseCheckout
62+
// opt-in from v0.42
63+
GitSparseCheckout: false,
64+
5865
// CacheSecretsAndConfigMaps
5966
// opt-in from v0.29
6067
CacheSecretsAndConfigMaps: false,

internal/source/source.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24+
"path/filepath"
2425
"strings"
2526
"text/template"
2627
"time"
@@ -196,6 +197,19 @@ func WithCheckoutOptionShallowClone() CheckoutOption {
196197
}
197198
}
198199

200+
// WithCheckoutOptionSparseCheckoutDirectories is a CheckoutOption option to configure
201+
// SparseCheckoutDirectories.
202+
func WithCheckoutOptionSparseCheckoutDirectories(updatePath string) CheckoutOption {
203+
return func(cc *repository.CloneConfig) {
204+
cleanedPath := filepath.Clean(updatePath)
205+
if cleanedPath == "." {
206+
// Do not set SparseCheckoutDirectories if repository root is specified
207+
return
208+
}
209+
cc.SparseCheckoutDirectories = []string{cleanedPath}
210+
}
211+
}
212+
199213
// CheckoutSource clones and checks out the source. If a push branch is
200214
// configured that doesn't match with the checkout branch, a checkout to the
201215
// push branch is also performed. This ensures any change and push operation

internal/source/source_test.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,14 @@ func TestSourceManager_CheckoutSource(t *testing.T) {
222222

223223
func test_sourceManager_CheckoutSource(t *testing.T, proto string) {
224224
tests := []struct {
225-
name string
226-
autoGitSpec *imagev1.GitSpec
227-
gitRepoRef *sourcev1.GitRepositoryRef
228-
shallowClone bool
229-
lastObserved bool
230-
wantErr bool
231-
wantRef string
225+
name string
226+
autoGitSpec *imagev1.GitSpec
227+
gitRepoRef *sourcev1.GitRepositoryRef
228+
shallowClone bool
229+
sparseCheckoutDirectory string
230+
lastObserved bool
231+
wantErr bool
232+
wantRef string
232233
}{
233234
{
234235
name: "checkout for single branch",
@@ -275,6 +276,42 @@ func test_sourceManager_CheckoutSource(t *testing.T, proto string) {
275276
wantErr: false,
276277
wantRef: "main",
277278
},
279+
{
280+
name: "with sparse checkout",
281+
autoGitSpec: &imagev1.GitSpec{
282+
Push: &imagev1.PushSpec{Branch: "main"},
283+
Checkout: &imagev1.GitCheckoutSpec{
284+
Reference: sourcev1.GitRepositoryRef{Branch: "main"},
285+
},
286+
},
287+
sparseCheckoutDirectory: "testdata/appconfig/deploy.yaml",
288+
wantErr: false,
289+
wantRef: "main",
290+
},
291+
{
292+
name: "with sparse checkout for current directory",
293+
autoGitSpec: &imagev1.GitSpec{
294+
Push: &imagev1.PushSpec{Branch: "main"},
295+
Checkout: &imagev1.GitCheckoutSpec{
296+
Reference: sourcev1.GitRepositoryRef{Branch: "main"},
297+
},
298+
},
299+
sparseCheckoutDirectory: "./",
300+
wantErr: false,
301+
wantRef: "main",
302+
},
303+
{
304+
name: "with sparse checkout for different push branch",
305+
autoGitSpec: &imagev1.GitSpec{
306+
Push: &imagev1.PushSpec{Branch: "foo"},
307+
Checkout: &imagev1.GitCheckoutSpec{
308+
Reference: sourcev1.GitRepositoryRef{Branch: "main"},
309+
},
310+
},
311+
sparseCheckoutDirectory: "testdata/appconfig/deploy.yaml",
312+
wantErr: false,
313+
wantRef: "foo",
314+
},
278315
{
279316
name: "with last observed commit",
280317
autoGitSpec: &imagev1.GitSpec{
@@ -386,6 +423,9 @@ func test_sourceManager_CheckoutSource(t *testing.T, proto string) {
386423
if tt.shallowClone {
387424
opts = append(opts, WithCheckoutOptionShallowClone())
388425
}
426+
if tt.sparseCheckoutDirectory != "" {
427+
opts = append(opts, WithCheckoutOptionSparseCheckoutDirectories(tt.sparseCheckoutDirectory))
428+
}
389429
if tt.lastObserved {
390430
opts = append(opts, WithCheckoutOptionLastObserved(headRev))
391431
}

0 commit comments

Comments
 (0)