Skip to content

Commit fac1afa

Browse files
committed
Move git/common to git
Signed-off-by: Hidde Beydals <[email protected]>
1 parent 7e63ef8 commit fac1afa

File tree

12 files changed

+142
-141
lines changed

12 files changed

+142
-141
lines changed

controllers/gitrepository_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444

4545
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
4646
"github.com/fluxcd/source-controller/pkg/git"
47-
"github.com/fluxcd/source-controller/pkg/git/common"
47+
"github.com/fluxcd/source-controller/pkg/git/strategy"
4848
)
4949

5050
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=gitrepositories,verbs=get;list;watch;create;update;patch;delete
@@ -178,9 +178,9 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
178178
defer os.RemoveAll(tmpGit)
179179

180180
// determine auth method
181-
auth := &common.Auth{}
181+
auth := &git.Auth{}
182182
if repository.Spec.SecretRef != nil {
183-
authStrategy, err := git.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
183+
authStrategy, err := strategy.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
184184
if err != nil {
185185
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
186186
}
@@ -204,7 +204,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
204204
}
205205
}
206206

207-
checkoutStrategy, err := git.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
207+
checkoutStrategy, err := strategy.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
208208
if err != nil {
209209
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
210210
}

pkg/git/common/common.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

pkg/git/git.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,37 @@ limitations under the License.
1717
package git
1818

1919
import (
20-
"fmt"
20+
"context"
2121

22-
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
23-
"github.com/fluxcd/source-controller/pkg/git/common"
24-
"github.com/fluxcd/source-controller/pkg/git/gogit"
25-
"github.com/fluxcd/source-controller/pkg/git/libgit2"
22+
"github.com/go-git/go-git/v5/plumbing/transport"
23+
git2go "github.com/libgit2/git2go/v31"
24+
corev1 "k8s.io/api/core/v1"
2625
)
2726

28-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (common.CheckoutStrategy, error) {
29-
switch gitImplementation {
30-
case sourcev1.GoGitImplementation:
31-
return gogit.CheckoutStrategyForRef(ref), nil
32-
case sourcev1.LibGit2Implementation:
33-
return libgit2.CheckoutStrategyForRef(ref), nil
34-
default:
35-
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
36-
}
27+
const (
28+
DefaultOrigin = "origin"
29+
DefaultBranch = "master"
30+
DefaultPublicKeyAuthUser = "git"
31+
CAFile = "caFile"
32+
)
33+
34+
type Commit interface {
35+
Verify(secret corev1.Secret) error
36+
Hash() string
37+
}
38+
39+
type CheckoutStrategy interface {
40+
Checkout(ctx context.Context, path, url string, auth *Auth) (Commit, string, error)
41+
}
42+
43+
// TODO(hidde): candidate for refactoring, so that we do not directly
44+
// depend on implementation specifics here.
45+
type Auth struct {
46+
AuthMethod transport.AuthMethod
47+
CredCallback git2go.CredentialsCallback
48+
CertCallback git2go.CertificateCheckCallback
3749
}
3850

39-
func AuthSecretStrategyForURL(url string, gitImplementation string) (common.AuthSecretStrategy, error) {
40-
switch gitImplementation {
41-
case sourcev1.GoGitImplementation:
42-
return gogit.AuthSecretStrategyForURL(url)
43-
case sourcev1.LibGit2Implementation:
44-
return libgit2.AuthSecretStrategyForURL(url)
45-
default:
46-
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
47-
}
51+
type AuthSecretStrategy interface {
52+
Method(secret corev1.Secret) (*Auth, error)
4853
}

pkg/git/gogit/checkout.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,52 @@ import (
2323
"time"
2424

2525
"github.com/Masterminds/semver/v3"
26-
"github.com/go-git/go-git/v5"
26+
extgogit "github.com/go-git/go-git/v5"
2727
"github.com/go-git/go-git/v5/plumbing"
2828

2929
"github.com/fluxcd/pkg/version"
3030

3131
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
32-
"github.com/fluxcd/source-controller/pkg/git/common"
32+
"github.com/fluxcd/source-controller/pkg/git"
3333
)
3434

35-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) common.CheckoutStrategy {
35+
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git.CheckoutStrategy {
3636
switch {
3737
case ref == nil:
38-
return &CheckoutBranch{branch: common.DefaultBranch}
38+
return &CheckoutBranch{branch: git.DefaultBranch}
3939
case ref.SemVer != "":
4040
return &CheckoutSemVer{semVer: ref.SemVer}
4141
case ref.Tag != "":
4242
return &CheckoutTag{tag: ref.Tag}
4343
case ref.Commit != "":
4444
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit}
4545
if strategy.branch == "" {
46-
strategy.branch = common.DefaultBranch
46+
strategy.branch = git.DefaultBranch
4747
}
4848
return strategy
4949
case ref.Branch != "":
5050
return &CheckoutBranch{branch: ref.Branch}
5151
default:
52-
return &CheckoutBranch{branch: common.DefaultBranch}
52+
return &CheckoutBranch{branch: git.DefaultBranch}
5353
}
5454
}
5555

5656
type CheckoutBranch struct {
5757
branch string
5858
}
5959

60-
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
61-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
60+
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
61+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
6262
URL: url,
6363
Auth: auth.AuthMethod,
64-
RemoteName: common.DefaultOrigin,
64+
RemoteName: git.DefaultOrigin,
6565
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
6666
SingleBranch: true,
6767
NoCheckout: false,
6868
Depth: 1,
6969
RecurseSubmodules: 0,
7070
Progress: nil,
71-
Tags: git.NoTags,
71+
Tags: extgogit.NoTags,
7272
})
7373
if err != nil {
7474
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -88,18 +88,18 @@ type CheckoutTag struct {
8888
tag string
8989
}
9090

91-
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
92-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
91+
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
92+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
9393
URL: url,
9494
Auth: auth.AuthMethod,
95-
RemoteName: common.DefaultOrigin,
95+
RemoteName: git.DefaultOrigin,
9696
ReferenceName: plumbing.NewTagReferenceName(c.tag),
9797
SingleBranch: true,
9898
NoCheckout: false,
9999
Depth: 1,
100100
RecurseSubmodules: 0,
101101
Progress: nil,
102-
Tags: git.NoTags,
102+
Tags: extgogit.NoTags,
103103
})
104104
if err != nil {
105105
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -120,17 +120,17 @@ type CheckoutCommit struct {
120120
commit string
121121
}
122122

123-
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
124-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
123+
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
124+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
125125
URL: url,
126126
Auth: auth.AuthMethod,
127-
RemoteName: common.DefaultOrigin,
127+
RemoteName: git.DefaultOrigin,
128128
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
129129
SingleBranch: true,
130130
NoCheckout: false,
131131
RecurseSubmodules: 0,
132132
Progress: nil,
133-
Tags: git.NoTags,
133+
Tags: extgogit.NoTags,
134134
})
135135
if err != nil {
136136
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -143,7 +143,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *c
143143
if err != nil {
144144
return nil, "", fmt.Errorf("git commit '%s' not found: %w", c.commit, err)
145145
}
146-
err = w.Checkout(&git.CheckoutOptions{
146+
err = w.Checkout(&extgogit.CheckoutOptions{
147147
Hash: commit.Hash,
148148
Force: true,
149149
})
@@ -157,21 +157,21 @@ type CheckoutSemVer struct {
157157
semVer string
158158
}
159159

160-
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
160+
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
161161
verConstraint, err := semver.NewConstraint(c.semVer)
162162
if err != nil {
163163
return nil, "", fmt.Errorf("semver parse range error: %w", err)
164164
}
165165

166-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
166+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
167167
URL: url,
168168
Auth: auth.AuthMethod,
169-
RemoteName: common.DefaultOrigin,
169+
RemoteName: git.DefaultOrigin,
170170
NoCheckout: false,
171171
Depth: 1,
172172
RecurseSubmodules: 0,
173173
Progress: nil,
174-
Tags: git.AllTags,
174+
Tags: extgogit.AllTags,
175175
})
176176
if err != nil {
177177
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -238,7 +238,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *c
238238
return nil, "", fmt.Errorf("git worktree error: %w", err)
239239
}
240240

241-
err = w.Checkout(&git.CheckoutOptions{
241+
err = w.Checkout(&extgogit.CheckoutOptions{
242242
Branch: plumbing.NewTagReferenceName(t),
243243
})
244244
if err != nil {

pkg/git/gogit/checkout_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222
"os"
2323
"testing"
2424

25-
"github.com/fluxcd/source-controller/pkg/git/common"
25+
"github.com/fluxcd/source-controller/pkg/git"
2626
)
2727

2828
func TestCheckoutTagSemVer_Checkout(t *testing.T) {
29-
auth := &common.Auth{}
29+
auth := &git.Auth{}
3030
tag := CheckoutTag{
3131
tag: "v1.7.0",
3232
}

pkg/git/gogit/transport.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626

2727
"github.com/fluxcd/pkg/ssh/knownhosts"
2828

29-
"github.com/fluxcd/source-controller/pkg/git/common"
29+
"github.com/fluxcd/source-controller/pkg/git"
3030
)
3131

32-
func AuthSecretStrategyForURL(URL string) (common.AuthSecretStrategy, error) {
32+
func AuthSecretStrategyForURL(URL string) (git.AuthSecretStrategy, error) {
3333
u, err := url.Parse(URL)
3434
if err != nil {
3535
return nil, fmt.Errorf("failed to parse URL to determine auth strategy: %w", err)
@@ -47,8 +47,8 @@ func AuthSecretStrategyForURL(URL string) (common.AuthSecretStrategy, error) {
4747

4848
type BasicAuth struct{}
4949

50-
func (s *BasicAuth) Method(secret corev1.Secret) (*common.Auth, error) {
51-
if _, ok := secret.Data[common.CAFile]; ok {
50+
func (s *BasicAuth) Method(secret corev1.Secret) (*git.Auth, error) {
51+
if _, ok := secret.Data[git.CAFile]; ok {
5252
return nil, fmt.Errorf("found caFile key in secret '%s' but go-git HTTP transport does not support custom certificates", secret.Name)
5353
}
5454

@@ -62,18 +62,17 @@ func (s *BasicAuth) Method(secret corev1.Secret) (*common.Auth, error) {
6262
if auth.Username == "" || auth.Password == "" {
6363
return nil, fmt.Errorf("invalid '%s' secret data: required fields 'username' and 'password'", secret.Name)
6464
}
65-
return &common.Auth{AuthMethod: auth}, nil
65+
return &git.Auth{AuthMethod: auth}, nil
6666
}
6767

6868
type PublicKeyAuth struct {
6969
user string
7070
}
7171

72-
func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
73-
if _, ok := secret.Data[common.CAFile]; ok {
72+
func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
73+
if _, ok := secret.Data[git.CAFile]; ok {
7474
return nil, fmt.Errorf("found caFile key in secret '%s' but go-git SSH transport does not support custom certificates", secret.Name)
7575
}
76-
7776
identity := secret.Data["identity"]
7877
knownHosts := secret.Data["known_hosts"]
7978
if len(identity) == 0 || len(knownHosts) == 0 {
@@ -82,7 +81,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
8281

8382
user := s.user
8483
if user == "" {
85-
user = common.DefaultPublicKeyAuthUser
84+
user = git.DefaultPublicKeyAuthUser
8685
}
8786

8887
pk, err := ssh.NewPublicKeys(user, identity, "")
@@ -95,5 +94,5 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
9594
return nil, err
9695
}
9796
pk.HostKeyCallback = callback
98-
return &common.Auth{AuthMethod: pk}, nil
97+
return &git.Auth{AuthMethod: pk}, nil
9998
}

0 commit comments

Comments
 (0)