Skip to content

Conversation

@kane8n
Copy link
Contributor

@kane8n kane8n commented Jun 10, 2025

Summary

If Spec.Update.Path is specified, only the manifest under this Path is required, no other files.
Therefore, SparseCheckout is valid.

Set the Path specified by Spec.Update.Path to SparseCheckoutDirectory so that only the necessary files are downloaded.

Feature Gate

To enable this feature, the controller must be started with the flag --feature-gates=GitSparseCheckout=true.

@kane8n kane8n force-pushed the git-sparse-checkout-when-update-path-specify branch 2 times, most recently from 2dd1868 to 0dfd5e8 Compare June 10, 2025 11:16
Copy link
Member

@makkes makkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement. I would like to see this this feature aligned with the API for GitRepository resources that we implemented in fluxcd/source-controller#1774 and make it an explicit field in the API instead of a feature flag of the controller. Not sure how @stefanprodan or @matheuscscp think about this.

@stefanprodan
Copy link
Member

make it an explicit field in the API instead of a feature flag

We don't have a path field in GitRepository so we had to add a dedicated field for it. For image automation we know the path that we should set as sparse checkout as it's already in the API spec. Ideally image automation should always use sparse checkout but given that this feature is experimental in Git itself and brand new in go-git, I would not enable the feature flag by default. Sparse checkout can break at any time, the Git protocol may change, then the go-git project needs to adapt to it, etc.

@kane8n kane8n force-pushed the git-sparse-checkout-when-update-path-specify branch from 0dfd5e8 to 8be4dad Compare June 13, 2025 00:29

// GitSparseCheckout
// opt-out from v0.42
GitSparseCheckout: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this opt-in for now. We can switch this to opt-out once sparse checkout is no longer an experimental feature in Git.

Comment on lines 93 to 94
// When a path is specified, sparse checkout will be enabled to limit the checkout scope
// to the specified directory, reducing clone size and time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this, since we'll have sparse checkout as an opt-in flag.

@kane8n kane8n force-pushed the git-sparse-checkout-when-update-path-specify branch from 8be4dad to d73a337 Compare June 13, 2025 10:22
@kane8n
Copy link
Contributor Author

kane8n commented Jun 13, 2025

@stefanprodan
Thanks for the feedback!
I've addressed your comments and pushed the changes.

@kane8n kane8n requested a review from stefanprodan June 13, 2025 10:26
@stefanprodan
Copy link
Member

Thanks @kane8n for this contribution! I'm putting this on hold as we need to do a patch release today. After that I'm going to ask you to rebase and then we can merge it. This feature will be included in Flux 2.7

@stefanprodan stefanprodan added hold Issues and pull requests put on hold and removed hold Issues and pull requests put on hold labels Jun 13, 2025
@stefanprodan
Copy link
Member

@kane8n please rebase

@kane8n kane8n force-pushed the git-sparse-checkout-when-update-path-specify branch from d73a337 to 5ceb013 Compare June 17, 2025 01:16
@kane8n
Copy link
Contributor Author

kane8n commented Jun 17, 2025

@stefanprodan
Rebase done! Please check it.

}
}

// WithCheckoutOptionRecurseSubmodules is a CheckoutOption option to configure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// WithCheckoutOptionRecurseSubmodules is a CheckoutOption option to configure
// WithCheckoutOptionSparseCheckoutDirectories is a CheckoutOption option to configure

// SparseCheckoutDirectories.
func WithCheckoutOptionSparseCheckoutDirectories(updatePath string) CheckoutOption {
return func(cc *repository.CloneConfig) {
cc.SparseCheckoutDirectories = []string{filepath.Clean(updatePath)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should skip enabling sparse checkout when the path is the repo root e.g. path: ./

@kane8n kane8n force-pushed the git-sparse-checkout-when-update-path-specify branch from 5ceb013 to 3999c65 Compare June 17, 2025 09:00
@kane8n
Copy link
Contributor Author

kane8n commented Jun 17, 2025

@stefanprodan
Thanks!
I have applied the corrections and pushed them.

func WithCheckoutOptionSparseCheckoutDirectories(updatePath string) CheckoutOption {
return func(cc *repository.CloneConfig) {
cleanedPath := filepath.Clean(updatePath)
if cleanedPath == "." {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefanprodan
Use filepath.Clean to remove the leading ./ prefix.
Should I use the same implementation as source-controller for consistency?

sample

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, let's leave it like this, SC could drop the custom code and use filepath.Clean

@stefanprodan stefanprodan changed the title SparseCheckout when Spec.Update.Path specified Add support for Git sparse checkout when .spec.update.path is specified Jun 17, 2025
@stefanprodan stefanprodan added the enhancement New feature or request label Jun 17, 2025
Copy link
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @kane8n 🏅

PS. Please open a PR on the website repo to add the feature gate in here: https://github.com/fluxcd/website/blob/main/content/en/flux/components/image/options.md#feature-gates

@stefanprodan stefanprodan merged commit 124fd9f into fluxcd:main Jun 17, 2025
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants