Skip to content

Commit

Permalink
Move git/common to git
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Feb 8, 2021
1 parent b44faa4 commit 9f5a915
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 122 deletions.
3 changes: 1 addition & 2 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/source-controller/pkg/git"
"github.com/fluxcd/source-controller/pkg/git/common"
)

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

// determine auth method
auth := &common.Auth{}
auth := &git.Auth{}
if repository.Spec.SecretRef != nil {
authStrategy, err := git.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
if err != nil {
Expand Down
50 changes: 0 additions & 50 deletions pkg/git/common/common.go

This file was deleted.

50 changes: 27 additions & 23 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,36 @@ limitations under the License.
package git

import (
"fmt"
"context"

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

func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (common.CheckoutStrategy, error) {
switch gitImplementation {
case sourcev1.GoGitImplementation:
return gogit.CheckoutStrategyForRef(ref), nil
case sourcev1.LibGit2Implementation:
return libgit2.CheckoutStrategyForRef(ref), nil
default:
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
}
const (
DefaultOrigin = "origin"
DefaultBranch = "master"
DefaultPublicKeyAuthUser = "git"
)

type Commit interface {
Verify(secret corev1.Secret) error
Hash() string
}

type CheckoutStrategy interface {
Checkout(ctx context.Context, path, url string, auth *Auth) (Commit, string, error)
}

// TODO(hidde): candidate for refactoring, so that we do not directly
// depend on implementation specifics here.
type Auth struct {
AuthMethod transport.AuthMethod
CredCallback git2go.CredentialsCallback
CertCallback git2go.CertificateCheckCallback
}

func AuthSecretStrategyForURL(url string, gitImplementation string) (common.AuthSecretStrategy, error) {
switch gitImplementation {
case sourcev1.GoGitImplementation:
return gogit.AuthSecretStrategyForURL(url)
case sourcev1.LibGit2Implementation:
return libgit2.AuthSecretStrategyForURL(url)
default:
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
}
type AuthSecretStrategy interface {
Method(secret corev1.Secret) (*Auth, error)
}
26 changes: 13 additions & 13 deletions pkg/git/gogit/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ import (
"github.com/fluxcd/pkg/version"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/source-controller/pkg/git/common"
git2 "github.com/fluxcd/source-controller/pkg/git"
)

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

type CheckoutBranch struct {
branch string
}

func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git2.DefaultOrigin,
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
SingleBranch: true,
NoCheckout: false,
Expand All @@ -88,11 +88,11 @@ type CheckoutTag struct {
tag string
}

func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git2.DefaultOrigin,
ReferenceName: plumbing.NewTagReferenceName(c.tag),
SingleBranch: true,
NoCheckout: false,
Expand Down Expand Up @@ -120,11 +120,11 @@ type CheckoutCommit struct {
commit string
}

func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git2.DefaultOrigin,
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
SingleBranch: true,
NoCheckout: false,
Expand Down Expand Up @@ -157,7 +157,7 @@ type CheckoutSemVer struct {
semVer string
}

func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git2.Auth) (git2.Commit, string, error) {
verConstraint, err := semver.NewConstraint(c.semVer)
if err != nil {
return nil, "", fmt.Errorf("semver parse range error: %w", err)
Expand All @@ -166,7 +166,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *c
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git2.DefaultOrigin,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 0,
Expand Down
4 changes: 2 additions & 2 deletions pkg/git/gogit/checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"os"
"testing"

"github.com/fluxcd/source-controller/pkg/git/common"
"github.com/fluxcd/source-controller/pkg/git"
)

func TestCheckoutTagSemVer_Checkout(t *testing.T) {
auth := &common.Auth{}
auth := &git.Auth{}
tag := CheckoutTag{
tag: "v1.7.0",
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/git/gogit/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

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

"github.com/fluxcd/source-controller/pkg/git/common"
"github.com/fluxcd/source-controller/pkg/git"
)

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

type BasicAuth struct{}

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

type PublicKeyAuth struct {
user string
}

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

user := s.user
if user == "" {
user = common.DefaultPublicKeyAuthUser
user = git.DefaultPublicKeyAuthUser
}

pk, err := ssh.NewPublicKeys(user, identity, "")
Expand All @@ -87,5 +87,5 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*common.Auth, error) {
return nil, err
}
pk.HostKeyCallback = callback
return &common.Auth{AuthMethod: pk}, nil
return &git.Auth{AuthMethod: pk}, nil
}
8 changes: 4 additions & 4 deletions pkg/git/gogit/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
corev1 "k8s.io/api/core/v1"

"github.com/fluxcd/source-controller/pkg/git/common"
"github.com/fluxcd/source-controller/pkg/git"
)

const (
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestAuthSecretStrategyForURL(t *testing.T) {
tests := []struct {
name string
url string
want common.AuthSecretStrategy
want git.AuthSecretStrategy
wantErr bool
}{
{"HTTP", "http://git.example.com/org/repo.git", &BasicAuth{}, false},
Expand Down Expand Up @@ -97,10 +97,10 @@ func TestBasicAuthStrategy_Method(t *testing.T) {
name string
secret corev1.Secret
modify func(secret *corev1.Secret)
want *common.Auth
want *git.Auth
wantErr bool
}{
{"username and password", basicAuthSecretFixture, nil, &common.Auth{AuthMethod: &http.BasicAuth{Username: "git", Password: "password"}}, false},
{"username and password", basicAuthSecretFixture, nil, &git.Auth{AuthMethod: &http.BasicAuth{Username: "git", Password: "password"}}, false},
{"without username", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "username") }, nil, true},
{"without password", basicAuthSecretFixture, func(s *corev1.Secret) { delete(s.Data, "password") }, nil, true},
{"empty", corev1.Secret{}, nil, nil, true},
Expand Down
18 changes: 9 additions & 9 deletions pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ import (
git2go "github.com/libgit2/git2go/v31"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/source-controller/pkg/git/common"
"github.com/fluxcd/source-controller/pkg/git"
)

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

type CheckoutBranch struct {
branch string
}

func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsNone,
Expand Down Expand Up @@ -81,7 +81,7 @@ type CheckoutTag struct {
tag string
}

func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsAll,
Expand Down Expand Up @@ -118,7 +118,7 @@ type CheckoutCommit struct {
commit string
}

func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsNone,
Expand Down Expand Up @@ -158,7 +158,7 @@ type CheckoutSemVer struct {
semVer string
}

func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
rng, err := semver.ParseRange(c.semVer)
if err != nil {
return nil, "", fmt.Errorf("semver parse range error: %w", err)
Expand Down
Loading

0 comments on commit 9f5a915

Please sign in to comment.