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

Remove dependency on libgit2 credentials callback #727

Merged
merged 8 commits into from
May 27, 2022

Conversation

aryan9600
Copy link
Member

@aryan9600 aryan9600 commented May 23, 2022

This PR removes our dependency on the inbuilt libgit2 credentials callback used in managed transport. This helps us:

  • get rid of intermittent panics related to credentials
  • get rid of the need for libgit2 to be linked with openssl and libssh2

It refactors the pre-existing logic, such that we don't call Checkout with the fake url, as it really degrades code readability (especially for newcomers). Instead, a new field is added to AuthOptions, which is then is used by the transport action to uniquely identify the correct target URL and the correct credentials, letting us move the logic from the reconciler to the libgit2 package (where it rightfully belongs).

It separates the managed transports and unmanaged transports code, to make it easier for us in the future to remove the unmanaged transport code. It also enables OptimizedGitClones, when GitManagedTransport is enabled.

It fixes proxy handling for the managed http transport and expands the tests to cover the same.

Signed-off-by: Sanskar Jaiswal jaiswalsanskar078@gmail.com

@aryan9600 aryan9600 force-pushed the improve-managed branch 2 times, most recently from 4858774 to bba2317 Compare May 23, 2022 19:58
pkg/git/options.go Outdated Show resolved Hide resolved
@aryan9600 aryan9600 force-pushed the improve-managed branch 2 times, most recently from 5945880 to 18b1802 Compare May 24, 2022 16:39
@aryan9600 aryan9600 marked this pull request as ready for review May 24, 2022 16:39
@aryan9600 aryan9600 requested a review from pjbgf May 24, 2022 16:43
pkg/git/libgit2/checkout.go Outdated Show resolved Hide resolved
pkg/git/libgit2/checkout.go Outdated Show resolved Hide resolved
pkg/git/options.go Outdated Show resolved Hide resolved
@pjbgf pjbgf added this to the GA milestone May 24, 2022
@pjbgf pjbgf added the area/git Git related issues and pull requests label May 24, 2022
Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @aryan9600! Thank you for working on this. 🙇 🙇

LGTM

aryan9600 and others added 8 commits May 27, 2022 14:01
Injects transport and auth options at the transport level directly to
bypass the inbuilt credentials callback because of it's several
shortcomings. Moves some of the pre-existing logic from the reconciler
to the checkout implementation.

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Co-authored-by: Paulo Gomes <paulo.gomes@weave.works>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
@pjbgf
Copy link
Member

pjbgf commented May 27, 2022

This has been tested against several SaaS providers, using a wide range of authentication algorithms. The average reconciliation time (sub second) has improved a lot compared to the previous released source controller version.

image

Copy link
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @aryan9600 and @pjbgf for these awesome improvements!

@pjbgf pjbgf merged commit 82cd05e into fluxcd:main May 27, 2022
// When we get rid of unmanaged transports, we can get rid of this branching as well.
if managed.Enabled() {
if opts.TransportOptionsURL == "" {
return nil, fmt.Errorf("can't use managed transport without a valid transport auth id.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This same error is used multiple times. It'd be good to have an error variable for it to prevent any mistakes in the string and easily change the message.
And it could just be errors.New(), no formatting required.

// The branching lets us establish a clear code path to help us be certain of the expected behaviour.
// When we get rid of unmanaged transports, we can get rid of this branching as well.
if managed.Enabled() {
if opts.TransportOptionsURL == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CheckoutBranch, a nil check is performed for this option. That's missing here.

"")
if err != nil {
return nil, fmt.Errorf("unable to fetch remote '%s': %w",
managed.EffectiveURL(url), gitutil.LibGit2Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the passed in URL is now the original URL, we don't need to use EffectiveURL() here. If the url variable is not overwritten above, we still have the original URL.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The passed in URL is original but it's always overwritten here: https://github.com/fluxcd/source-controller/blob/main/pkg/git/libgit2/checkout.go#L96

ProxyOptions: &git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
})
url = opts.TransportOptionsURL
remoteCallBacks = managed.RemoteCallbacks()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above code is repeated everywhere. We can have a helper function to do the same, reducing the risk of making any mistakes in any of the checkouts.

g.Expect(err).ToNot(HaveOccurred())
g.Expect(cc.String()).To(Equal(git.DefaultBranch + "/" + commit.Id().String()))
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(true))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike in TestCheckoutBranch_checkoutUnmanaged(), looks like we are no longer testing various scenarios of checkout for managed transport anymore like non default branch, and non existing branch checkout.

name string
branch string
filesCreated map[string]string
lastRevision string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filesCreated is not being used any more.
lastRevision doesn't apply to this test as unmanaged transport doesn't support no-op clone.

)

const testRepositoryPath = "../testdata/git/repo"

func TestMain(m *testing.M) {
managed.InitManagedTransport(logr.Discard())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestMain() not calling m.Run() results in none of the tests in this whole package to run.

// It's a field of AuthOptions despite not providing any kind of authentication
// info, as it's the only way to sneak it into git.Checkout, without polluting
// it's args and keeping it generic.
TransportOptionsURL string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaks implementation details into the generic options and should be configured via other means.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to the issue tracking the consolidation of the git implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/git Git related issues and pull requests
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants