Skip to content

Commit 7035a58

Browse files
committed
Sparse Checkout Directories in GitRepositories.
- Add `.spec.sparseCheckout` and `.status.observedSparseCheckout` fields to `GitRepository`. - Add controller support to send the sparse checkout directories to go-git via pkg methods. - Add tests for testing the observed sparse checkout behavior. - Add docs describing the new fields. Signed-off-by: Dipti Pai <diptipai89@outlook.com>
1 parent 414b7db commit 7035a58

7 files changed

+156
-2
lines changed

api/v1/gitrepository_types.go

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ type GitRepositorySpec struct {
148148
// should be included in the Artifact produced for this GitRepository.
149149
// +optional
150150
Include []GitRepositoryInclude `json:"include,omitempty"`
151+
152+
// SparseCheckout specifies a list of directories to checkout when cloning
153+
// the repository. If specified, only these directories are included in the
154+
// Artifact produced for this GitRepository.
155+
// +optional
156+
SparseCheckout []string `json:"sparseCheckout,omitempty"`
151157
}
152158

153159
// GitRepositoryInclude specifies a local reference to a GitRepository which
@@ -266,6 +272,11 @@ type GitRepositoryStatus struct {
266272
// +optional
267273
ObservedInclude []GitRepositoryInclude `json:"observedInclude,omitempty"`
268274

275+
// ObservedSparseCheckout is the observed list of directories used to
276+
// produce the current Artifact.
277+
// +optional
278+
ObservedSparseCheckout []string `json:"observedSparseCheckout,omitempty"`
279+
269280
// SourceVerificationMode is the last used verification mode indicating
270281
// which Git object(s) have been verified.
271282
// +optional

api/v1/zz_generated.deepcopy.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ spec:
174174
required:
175175
- name
176176
type: object
177+
sparseCheckout:
178+
description: |-
179+
SparseCheckout specifies a list of directories to checkout when cloning
180+
the repository. If specified, only these directories are included in the
181+
Artifact produced for this GitRepository.
182+
items:
183+
type: string
184+
type: array
177185
suspend:
178186
description: |-
179187
Suspend tells the controller to suspend the reconciliation of this
@@ -443,6 +451,13 @@ spec:
443451
ObservedRecurseSubmodules is the observed resource submodules
444452
configuration used to produce the current Artifact.
445453
type: boolean
454+
observedSparseCheckout:
455+
description: |-
456+
ObservedSparseCheckout is the observed list of directories used to
457+
produce the current Artifact.
458+
items:
459+
type: string
460+
type: array
446461
sourceVerificationMode:
447462
description: |-
448463
SourceVerificationMode is the last used verification mode indicating

docs/api/v1/source.md

+41
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,20 @@ the GitRepository as cloned from the URL, using their default settings.</p>
523523
should be included in the Artifact produced for this GitRepository.</p>
524524
</td>
525525
</tr>
526+
<tr>
527+
<td>
528+
<code>sparseCheckout</code><br>
529+
<em>
530+
[]string
531+
</em>
532+
</td>
533+
<td>
534+
<em>(Optional)</em>
535+
<p>SparseCheckout specifies a list of directories to checkout when cloning
536+
the repository. If specified, only these directories are included in the
537+
Artifact produced for this GitRepository.</p>
538+
</td>
539+
</tr>
526540
</table>
527541
</td>
528542
</tr>
@@ -1863,6 +1877,20 @@ the GitRepository as cloned from the URL, using their default settings.</p>
18631877
should be included in the Artifact produced for this GitRepository.</p>
18641878
</td>
18651879
</tr>
1880+
<tr>
1881+
<td>
1882+
<code>sparseCheckout</code><br>
1883+
<em>
1884+
[]string
1885+
</em>
1886+
</td>
1887+
<td>
1888+
<em>(Optional)</em>
1889+
<p>SparseCheckout specifies a list of directories to checkout when cloning
1890+
the repository. If specified, only these directories are included in the
1891+
Artifact produced for this GitRepository.</p>
1892+
</td>
1893+
</tr>
18661894
</tbody>
18671895
</table>
18681896
</div>
@@ -1983,6 +2011,19 @@ produce the current Artifact.</p>
19832011
</tr>
19842012
<tr>
19852013
<td>
2014+
<code>observedSparseCheckout</code><br>
2015+
<em>
2016+
[]string
2017+
</em>
2018+
</td>
2019+
<td>
2020+
<em>(Optional)</em>
2021+
<p>ObservedSparseCheckout is the observed list of directories used to
2022+
produce the current Artifact.</p>
2023+
</td>
2024+
</tr>
2025+
<tr>
2026+
<td>
19862027
<code>sourceVerificationMode</code><br>
19872028
<em>
19882029
<a href="#source.toolkit.fluxcd.io/v1.GitVerificationMode">

docs/spec/v1/gitrepositories.md

+43
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,28 @@ list](#default-exclusions), and may overrule the [`.sourceignore` file
590590
exclusions](#sourceignore-file). See [excluding files](#excluding-files)
591591
for more information.
592592

593+
### Sparse checkout
594+
595+
`.spec.sparseCheckout` is an optional field to specify list of directories to
596+
checkout when cloning the repository. If specified, only the specified directory
597+
contents will be present in the artifact produced for this repository.
598+
599+
```yaml
600+
apiVersion: source.toolkit.fluxcd.io/v1
601+
kind: GitRepository
602+
metadata:
603+
name: podinfo
604+
namespace: default
605+
spec:
606+
interval: 5m
607+
url: https://github.com/stefanprodan/podinfo
608+
ref:
609+
branch: master
610+
sparseCheckout:
611+
- charts
612+
- kustomize
613+
```
614+
593615
### Suspend
594616

595617
`.spec.suspend` is an optional field to suspend the reconciliation of a
@@ -1132,6 +1154,27 @@ status:
11321154
...
11331155
```
11341156

1157+
### Observed Sparse Checkout
1158+
1159+
The source-controller reports observed sparse checkout in the GitRepository's
1160+
`.status.observedSparseCheckout`. The observed sparse checkout is the latest
1161+
`.spec.sparseCheckout` value which resulted in a [ready
1162+
state](#ready-gitrepository), or stalled due to error it can not recover from
1163+
without human intervention. The value is the same as the [sparseCheckout in
1164+
spec](#sparse-checkout). It indicates the sparse checkout configuration used in
1165+
building the current artifact in storage. It is also used by the controller to
1166+
determine if an artifact needs to be rebuilt.
1167+
1168+
Example:
1169+
```yaml
1170+
status:
1171+
...
1172+
observedSparseCheckout:
1173+
- charts
1174+
- kustomize
1175+
...
1176+
```
1177+
11351178
### Source Verification Mode
11361179

11371180
The source-controller reports the Git object(s) it verified in the Git

internal/controller/gitrepository_controller.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
812812
obj.Status.ObservedIgnore = obj.Spec.Ignore
813813
obj.Status.ObservedRecurseSubmodules = obj.Spec.RecurseSubmodules
814814
obj.Status.ObservedInclude = obj.Spec.Include
815+
obj.Status.ObservedSparseCheckout = obj.Spec.SparseCheckout
815816

816817
// Remove the deprecated symlink.
817818
// TODO(hidde): remove 2 minor versions from introduction of v1.
@@ -886,8 +887,9 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context, obj *sourcev1
886887
authOpts *git.AuthOptions, proxyOpts *transport.ProxyOptions, dir string, optimized bool) (*git.Commit, error) {
887888
// Configure checkout strategy.
888889
cloneOpts := repository.CloneConfig{
889-
RecurseSubmodules: obj.Spec.RecurseSubmodules,
890-
ShallowClone: true,
890+
RecurseSubmodules: obj.Spec.RecurseSubmodules,
891+
SparseCheckoutDirectories: obj.Spec.SparseCheckout,
892+
ShallowClone: true,
891893
}
892894
if ref := obj.Spec.Reference; ref != nil {
893895
cloneOpts.Branch = ref.Branch
@@ -1172,6 +1174,14 @@ func gitContentConfigChanged(obj *sourcev1.GitRepository, includes *artifactSet)
11721174
if requiresVerification(obj) {
11731175
return true
11741176
}
1177+
if len(obj.Spec.SparseCheckout) != len(obj.Status.ObservedSparseCheckout) {
1178+
return true
1179+
}
1180+
for index, dir := range obj.Spec.SparseCheckout {
1181+
if dir != obj.Status.ObservedSparseCheckout[index] {
1182+
return true
1183+
}
1184+
}
11751185

11761186
// Convert artifactSet to index addressable artifacts and ensure that it and
11771187
// the included artifacts include all the include from the spec.

internal/controller/gitrepository_controller_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,30 @@ func TestGitContentConfigChanged(t *testing.T) {
30733073
},
30743074
want: false,
30753075
},
3076+
{
3077+
name: "unobserved sparse checkout",
3078+
obj: sourcev1.GitRepository{
3079+
Spec: sourcev1.GitRepositorySpec{SparseCheckout: []string{"a/b/c", "x/y/z"}},
3080+
Status: sourcev1.GitRepositoryStatus{ObservedSparseCheckout: []string{"a/b/c"}},
3081+
},
3082+
want: true,
3083+
},
3084+
{
3085+
name: "unobserved case sensitive sparse checkout",
3086+
obj: sourcev1.GitRepository{
3087+
Spec: sourcev1.GitRepositorySpec{SparseCheckout: []string{"a/b/c", "x/y/Z"}},
3088+
Status: sourcev1.GitRepositoryStatus{ObservedSparseCheckout: []string{"a/b/c", "x/y/z"}},
3089+
},
3090+
want: true,
3091+
},
3092+
{
3093+
name: "observed sparse checkout",
3094+
obj: sourcev1.GitRepository{
3095+
Spec: sourcev1.GitRepositorySpec{SparseCheckout: []string{"a/b/c", "x/y/z"}},
3096+
Status: sourcev1.GitRepositoryStatus{ObservedSparseCheckout: []string{"a/b/c", "x/y/z"}},
3097+
},
3098+
want: false,
3099+
},
30763100
{
30773101
name: "unobserved include",
30783102
obj: sourcev1.GitRepository{

0 commit comments

Comments
 (0)