Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Git packages to implementations #270

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/v1beta1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
const (
// GitRepositoryKind is the string representation of a GitRepository.
GitRepositoryKind = "GitRepository"
// GoGitImplementation represents the go-git git implementation kind.

// GoGitImplementation represents the go-git Git implementation kind.
GoGitImplementation = "go-git"
// LibGit2Implementation represents the gi2go git implementation kind.
// LibGit2Implementation represents the git2go Git implementation kind.
LibGit2Implementation = "libgit2"
)

Expand Down
8 changes: 4 additions & 4 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (

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

// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=gitrepositories,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -178,9 +178,9 @@ 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)
authStrategy, err := strategy.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
if err != nil {
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
}
Expand All @@ -204,7 +204,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
}
}

checkoutStrategy, err := git.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
checkoutStrategy, err := strategy.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
if err != nil {
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
}
Expand Down
51 changes: 0 additions & 51 deletions pkg/git/common/common.go

This file was deleted.

49 changes: 25 additions & 24 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,37 @@ 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"
gitv1 "github.com/fluxcd/source-controller/pkg/git/v1"
gitv2 "github.com/fluxcd/source-controller/pkg/git/v2"
"github.com/go-git/go-git/v5/plumbing/transport"
git2go "github.com/libgit2/git2go/v31"
corev1 "k8s.io/api/core/v1"
)

const (
defaultBranch = "master"
DefaultOrigin = "origin"
DefaultBranch = "master"
DefaultPublicKeyAuthUser = "git"
CAFile = "caFile"
)

func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (common.CheckoutStrategy, error) {
switch gitImplementation {
case sourcev1.GoGitImplementation:
return gitv1.CheckoutStrategyForRef(ref), nil
case sourcev1.LibGit2Implementation:
return gitv2.CheckoutStrategyForRef(ref), nil
default:
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
}
type Commit interface {
Verify(secret corev1.Secret) error
Hash() string
}

func AuthSecretStrategyForURL(url string, gitImplementation string) (common.AuthSecretStrategy, error) {
switch gitImplementation {
case sourcev1.GoGitImplementation:
return gitv1.AuthSecretStrategyForURL(url)
case sourcev1.LibGit2Implementation:
return gitv2.AuthSecretStrategyForURL(url)
default:
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
}
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
}

type AuthSecretStrategy interface {
Method(secret corev1.Secret) (*Auth, error)
}
51 changes: 26 additions & 25 deletions pkg/git/v1/checkout.go → pkg/git/gogit/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package gogit

import (
"context"
Expand All @@ -23,51 +23,52 @@ import (
"time"

"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
extgogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"

"github.com/fluxcd/pkg/version"

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) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git.DefaultOrigin,
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 0,
Progress: nil,
Tags: git.NoTags,
Tags: extgogit.NoTags,
})
if err != nil {
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
Expand All @@ -87,18 +88,18 @@ type CheckoutTag struct {
tag string
}

func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git.DefaultOrigin,
ReferenceName: plumbing.NewTagReferenceName(c.tag),
SingleBranch: true,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 0,
Progress: nil,
Tags: git.NoTags,
Tags: extgogit.NoTags,
})
if err != nil {
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
Expand All @@ -119,17 +120,17 @@ type CheckoutCommit struct {
commit string
}

func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git.DefaultOrigin,
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
SingleBranch: true,
NoCheckout: false,
RecurseSubmodules: 0,
Progress: nil,
Tags: git.NoTags,
Tags: extgogit.NoTags,
})
if err != nil {
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
Expand All @@ -142,7 +143,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *c
if err != nil {
return nil, "", fmt.Errorf("git commit '%s' not found: %w", c.commit, err)
}
err = w.Checkout(&git.CheckoutOptions{
err = w.Checkout(&extgogit.CheckoutOptions{
Hash: commit.Hash,
Force: true,
})
Expand All @@ -156,21 +157,21 @@ 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) {
verConstraint, err := semver.NewConstraint(c.semVer)
if err != nil {
return nil, "", fmt.Errorf("semver parse range error: %w", err)
}

repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
URL: url,
Auth: auth.AuthMethod,
RemoteName: common.DefaultOrigin,
RemoteName: git.DefaultOrigin,
NoCheckout: false,
Depth: 1,
RecurseSubmodules: 0,
Progress: nil,
Tags: git.AllTags,
Tags: extgogit.AllTags,
})
if err != nil {
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
Expand Down Expand Up @@ -237,7 +238,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *c
return nil, "", fmt.Errorf("git worktree error: %w", err)
}

err = w.Checkout(&git.CheckoutOptions{
err = w.Checkout(&extgogit.CheckoutOptions{
Branch: plumbing.NewTagReferenceName(t),
})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package gogit

import (
"context"
"io/ioutil"
"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
2 changes: 1 addition & 1 deletion pkg/git/v1/commit.go → pkg/git/gogit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package gogit

import (
"fmt"
Expand Down
Loading