Skip to content

Commit 491cabe

Browse files
committed
Move git/common to git
Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent b44faa4 commit 491cabe

12 files changed

+127
-124
lines changed

controllers/gitrepository_controller.go

+4-4
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

-50
This file was deleted.

pkg/git/git.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,36 @@ 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+
)
32+
33+
type Commit interface {
34+
Verify(secret corev1.Secret) error
35+
Hash() string
36+
}
37+
38+
type CheckoutStrategy interface {
39+
Checkout(ctx context.Context, path, url string, auth *Auth) (Commit, string, error)
40+
}
41+
42+
// TODO(hidde): candidate for refactoring, so that we do not directly
43+
// depend on implementation specifics here.
44+
type Auth struct {
45+
AuthMethod transport.AuthMethod
46+
CredCallback git2go.CredentialsCallback
47+
CertCallback git2go.CertificateCheckCallback
3748
}
3849

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-
}
50+
type AuthSecretStrategy interface {
51+
Method(secret corev1.Secret) (*Auth, error)
4852
}

pkg/git/gogit/checkout.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,39 @@ import (
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+
git2 "github.com/fluxcd/source-controller/pkg/git"
3333
)
3434

35-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) common.CheckoutStrategy {
35+
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git2.CheckoutStrategy {
3636
switch {
3737
case ref == nil:
38-
return &CheckoutBranch{branch: common.DefaultBranch}
38+
return &CheckoutBranch{branch: git2.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 = git2.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: git2.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) {
60+
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
6161
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
6262
URL: url,
6363
Auth: auth.AuthMethod,
64-
RemoteName: common.DefaultOrigin,
64+
RemoteName: git2.DefaultOrigin,
6565
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
6666
SingleBranch: true,
6767
NoCheckout: false,
@@ -88,11 +88,11 @@ 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) {
91+
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
9292
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
9393
URL: url,
9494
Auth: auth.AuthMethod,
95-
RemoteName: common.DefaultOrigin,
95+
RemoteName: git2.DefaultOrigin,
9696
ReferenceName: plumbing.NewTagReferenceName(c.tag),
9797
SingleBranch: true,
9898
NoCheckout: false,
@@ -120,11 +120,11 @@ 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) {
123+
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
124124
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
125125
URL: url,
126126
Auth: auth.AuthMethod,
127-
RemoteName: common.DefaultOrigin,
127+
RemoteName: git2.DefaultOrigin,
128128
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
129129
SingleBranch: true,
130130
NoCheckout: false,
@@ -157,7 +157,7 @@ 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 *git2.Auth) (git2.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)
@@ -166,7 +166,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *c
166166
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
167167
URL: url,
168168
Auth: auth.AuthMethod,
169-
RemoteName: common.DefaultOrigin,
169+
RemoteName: git2.DefaultOrigin,
170170
NoCheckout: false,
171171
Depth: 1,
172172
RecurseSubmodules: 0,

pkg/git/gogit/checkout_test.go

+2-2
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

+7-7
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,7 +47,7 @@ func AuthSecretStrategyForURL(URL string) (common.AuthSecretStrategy, error) {
4747

4848
type BasicAuth struct{}
4949

50-
func (s *BasicAuth) Method(secret corev1.Secret) (*common.Auth, error) {
50+
func (s *BasicAuth) Method(secret corev1.Secret) (*git.Auth, error) {
5151
auth := &http.BasicAuth{}
5252
if username, ok := secret.Data["username"]; ok {
5353
auth.Username = string(username)
@@ -58,14 +58,14 @@ func (s *BasicAuth) Method(secret corev1.Secret) (*common.Auth, error) {
5858
if auth.Username == "" || auth.Password == "" {
5959
return nil, fmt.Errorf("invalid '%s' secret data: required fields 'username' and 'password'", secret.Name)
6060
}
61-
return &common.Auth{AuthMethod: auth}, nil
61+
return &git.Auth{AuthMethod: auth}, nil
6262
}
6363

6464
type PublicKeyAuth struct {
6565
user string
6666
}
6767

68-
func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
68+
func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
6969
identity := secret.Data["identity"]
7070
knownHosts := secret.Data["known_hosts"]
7171
if len(identity) == 0 || len(knownHosts) == 0 {
@@ -74,7 +74,7 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
7474

7575
user := s.user
7676
if user == "" {
77-
user = common.DefaultPublicKeyAuthUser
77+
user = git.DefaultPublicKeyAuthUser
7878
}
7979

8080
pk, err := ssh.NewPublicKeys(user, identity, "")
@@ -87,5 +87,5 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
8787
return nil, err
8888
}
8989
pk.HostKeyCallback = callback
90-
return &common.Auth{AuthMethod: pk}, nil
90+
return &git.Auth{AuthMethod: pk}, nil
9191
}

pkg/git/gogit/transport_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/go-git/go-git/v5/plumbing/transport/http"
2424
corev1 "k8s.io/api/core/v1"
2525

26-
"github.com/fluxcd/source-controller/pkg/git/common"
26+
"github.com/fluxcd/source-controller/pkg/git"
2727
)
2828

2929
const (
@@ -69,7 +69,7 @@ func TestAuthSecretStrategyForURL(t *testing.T) {
6969
tests := []struct {
7070
name string
7171
url string
72-
want common.AuthSecretStrategy
72+
want git.AuthSecretStrategy
7373
wantErr bool
7474
}{
7575
{"HTTP", "http://git.example.com/org/repo.git", &BasicAuth{}, false},
@@ -97,10 +97,10 @@ func TestBasicAuthStrategy_Method(t *testing.T) {
9797
name string
9898
secret corev1.Secret
9999
modify func(secret *corev1.Secret)
100-
want *common.Auth
100+
want *git.Auth
101101
wantErr bool
102102
}{
103-
{"username and password", basicAuthSecretFixture, nil, &common.Auth{AuthMethod: &http.BasicAuth{Username: "git", Password: "password"}}, false},
103+
{"username and password", basicAuthSecretFixture, nil, &git.Auth{AuthMethod: &http.BasicAuth{Username: "git", Password: "password"}}, false},
104104
{"without username", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "username") }, nil, true},
105105
{"without password", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "password") }, nil, true},
106106
{"empty", corev1.Secret{}, nil, nil, true},

0 commit comments

Comments
 (0)