Skip to content

Commit

Permalink
Add support for Git submodules with go-git
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Mar 30, 2021
1 parent 5486321 commit d3a9679
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 19 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ type GitRepositorySpec struct {
// +kubebuilder:default:=go-git
// +optional
GitImplementation string `json:"gitImplementation,omitempty"`

// When enabled, after the clone is created, initializes all submodules within,
// using their default settings.
// This option is available only when using the 'go-git' GitImplementation.
// +optional
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`
}

// GitRepositoryRef defines the Git ref used for pull and checkout operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ spec:
interval:
description: The interval at which to check for repository updates.
type: string
recurseSubmodules:
description: When enabled, after the clone is created, initializes
all submodules within, using their default settings. This option
is available only when using the 'go-git' GitImplementation.
type: boolean
ref:
description: The Git reference to checkout and monitor for changes,
defaults to master branch.
Expand Down
5 changes: 4 additions & 1 deletion controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
}
}

checkoutStrategy, err := strategy.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
checkoutStrategy, err := strategy.CheckoutStrategyForRef(
repository.Spec.Reference,
repository.Spec.GitImplementation,
repository.Spec.RecurseSubmodules)
if err != nil {
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
}
Expand Down
28 changes: 28 additions & 0 deletions docs/api/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@ string
Defaults to go-git, valid values are (&lsquo;go-git&rsquo;, &lsquo;libgit2&rsquo;).</p>
</td>
</tr>
<tr>
<td>
<code>recurseSubmodules</code><br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>When enabled, after the clone is created, initializes all submodules within,
using their default settings.
This option is available only when using the &lsquo;go-git&rsquo; GitImplementation.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -1246,6 +1260,20 @@ string
Defaults to go-git, valid values are (&lsquo;go-git&rsquo;, &lsquo;libgit2&rsquo;).</p>
</td>
</tr>
<tr>
<td>
<code>recurseSubmodules</code><br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>When enabled, after the clone is created, initializes all submodules within,
using their default settings.
This option is available only when using the &lsquo;go-git&rsquo; GitImplementation.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
42 changes: 42 additions & 0 deletions docs/spec/v1beta1/gitrepositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ type GitRepositoryRef struct {
// The Git commit SHA to checkout, if specified Tag filters will be ignored.
// +optional
Commit string `json:"commit,omitempty"`

// When enabled, after the clone is created, initializes all submodules within,
// using their default settings.
// This option is available only when using the 'go-git' GitImplementation.
// +optional
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`
}
```

Expand Down Expand Up @@ -434,6 +440,42 @@ kubectl create secret generic pgp-public-keys \
--from-file=author2.asc
```

### Git submodules

With `spec.recurseSubmodules` you can configure the controller to
clone a specific branch including its Git submodules:

```yaml
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
name: repo-with-submodules
namespace: default
spec:
interval: 1m
url: https://github.com/<organization>/<repository>
secretRef:
name: https-credentials
ref:
branch: main
recurseSubmodules: true
---
apiVersion: v1
kind: Secret
metadata:
name: https-credentials
namespace: default
type: Opaque
data:
username: <GitHub Username>
password: <GitHub Token>
```

Note that deploy keys can't be used to pull submodules from private repositories
as GitHub and GitLab doesn't allow a deploy key to be reused across repositories.
You have to use either HTTPS token-based authentication, or an SSH key belonging
to a user that has access to the main repository and all its submodules.

## Status examples

Successful sync:
Expand Down
39 changes: 25 additions & 14 deletions pkg/git/gogit/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,30 @@ import (
"github.com/fluxcd/source-controller/pkg/git"
)

func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git.CheckoutStrategy {
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, recurseSubmodules bool) git.CheckoutStrategy {
switch {
case ref == nil:
return &CheckoutBranch{branch: git.DefaultBranch}
case ref.SemVer != "":
return &CheckoutSemVer{semVer: ref.SemVer}
return &CheckoutSemVer{semVer: ref.SemVer, recurseSubmodules: recurseSubmodules}
case ref.Tag != "":
return &CheckoutTag{tag: ref.Tag}
return &CheckoutTag{tag: ref.Tag, recurseSubmodules: recurseSubmodules}
case ref.Commit != "":
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit}
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit, recurseSubmodules: recurseSubmodules}
if strategy.branch == "" {
strategy.branch = git.DefaultBranch
}
return strategy
case ref.Branch != "":
return &CheckoutBranch{branch: ref.Branch}
return &CheckoutBranch{branch: ref.Branch, recurseSubmodules: recurseSubmodules}
default:
return &CheckoutBranch{branch: git.DefaultBranch}
}
}

type CheckoutBranch struct {
branch string
branch string
recurseSubmodules bool
}

func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
Expand All @@ -67,7 +68,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
Progress: nil,
Tags: extgogit.NoTags,
CABundle: auth.CABundle,
Expand All @@ -87,7 +88,8 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
}

type CheckoutTag struct {
tag string
tag string
recurseSubmodules bool
}

func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
Expand All @@ -99,7 +101,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
Progress: nil,
Tags: extgogit.NoTags,
CABundle: auth.CABundle,
Expand All @@ -119,8 +121,9 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
}

type CheckoutCommit struct {
branch string
commit string
branch string
commit string
recurseSubmodules bool
}

func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
Expand All @@ -131,7 +134,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
SingleBranch: true,
NoCheckout: false,
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
Progress: nil,
Tags: extgogit.NoTags,
CABundle: auth.CABundle,
Expand All @@ -158,7 +161,8 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
}

type CheckoutSemVer struct {
semVer string
semVer string
recurseSubmodules bool
}

func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
Expand All @@ -173,7 +177,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
RemoteName: git.DefaultOrigin,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
Progress: nil,
Tags: extgogit.AllTags,
CABundle: auth.CABundle,
Expand Down Expand Up @@ -262,3 +266,10 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g

return &Commit{commit}, fmt.Sprintf("%s/%s", t, head.Hash().String()), nil
}

func recurseSubmodules(recurse bool) extgogit.SubmoduleRescursivity {
if recurse {
return extgogit.DefaultSubmoduleRecursionDepth
}
return extgogit.NoRecurseSubmodules
}
2 changes: 1 addition & 1 deletion pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/fluxcd/source-controller/pkg/git"
)

func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git.CheckoutStrategy {
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, recurseSubmodules bool) git.CheckoutStrategy {
switch {
case ref == nil:
return &CheckoutBranch{branch: git.DefaultBranch}
Expand Down
6 changes: 3 additions & 3 deletions pkg/git/strategy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"github.com/fluxcd/source-controller/pkg/git/libgit2"
)

func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (git.CheckoutStrategy, error) {
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string, recurseSubmodules bool) (git.CheckoutStrategy, error) {
switch gitImplementation {
case sourcev1.GoGitImplementation:
return gogit.CheckoutStrategyForRef(ref), nil
return gogit.CheckoutStrategyForRef(ref, recurseSubmodules), nil
case sourcev1.LibGit2Implementation:
return libgit2.CheckoutStrategyForRef(ref), nil
return libgit2.CheckoutStrategyForRef(ref, recurseSubmodules), nil
default:
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
}
Expand Down

0 comments on commit d3a9679

Please sign in to comment.