Merge pull request #1126 from fluxcd/fix-optimized-clone

Re-instantiate non-optimized clone fallback
This commit is contained in:
Stefan Prodan 2023-06-23 18:12:38 +03:00 committed by GitHub
commit bade8c9ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 55 deletions

View File

@ -536,7 +536,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
// Persist the ArtifactSet. // Persist the ArtifactSet.
*includes = *artifacts *includes = *artifacts
c, err := r.gitCheckout(ctx, obj, authOpts, dir) c, err := r.gitCheckout(ctx, obj, authOpts, dir, true)
if err != nil { if err != nil {
return sreconcile.ResultEmpty, err return sreconcile.ResultEmpty, err
} }
@ -578,7 +578,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
// If we can't skip the reconciliation, checkout again without any // If we can't skip the reconciliation, checkout again without any
// optimization. // optimization.
c, err := r.gitCheckout(ctx, obj, authOpts, dir) c, err := r.gitCheckout(ctx, obj, authOpts, dir, false)
if err != nil { if err != nil {
return sreconcile.ResultEmpty, err return sreconcile.ResultEmpty, err
} }
@ -777,7 +777,7 @@ func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context, sp *patc
// gitCheckout builds checkout options with the given configurations and // gitCheckout builds checkout options with the given configurations and
// performs a git checkout. // performs a git checkout.
func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context, func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
obj *sourcev1.GitRepository, authOpts *git.AuthOptions, dir string) (*git.Commit, error) { obj *sourcev1.GitRepository, authOpts *git.AuthOptions, dir string, optimized bool) (*git.Commit, error) {
// Configure checkout strategy. // Configure checkout strategy.
cloneOpts := repository.CloneConfig{ cloneOpts := repository.CloneConfig{
RecurseSubmodules: obj.Spec.RecurseSubmodules, RecurseSubmodules: obj.Spec.RecurseSubmodules,
@ -794,7 +794,7 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
// Only if the object has an existing artifact in storage, attempt to // Only if the object has an existing artifact in storage, attempt to
// short-circuit clone operation. reconcileStorage has already verified // short-circuit clone operation. reconcileStorage has already verified
// that the artifact exists. // that the artifact exists.
if conditions.IsTrue(obj, sourcev1.ArtifactInStorageCondition) { if optimized && conditions.IsTrue(obj, sourcev1.ArtifactInStorageCondition) {
if artifact := obj.GetArtifact(); artifact != nil { if artifact := obj.GetArtifact(); artifact != nil {
cloneOpts.LastObservedCommit = artifact.Revision cloneOpts.LastObservedCommit = artifact.Revision
} }

View File

@ -219,57 +219,62 @@ func TestGitRepositoryReconciler_Reconcile(t *testing.T) {
testSuspendedObjectDeleteWithArtifact(ctx, g, obj) testSuspendedObjectDeleteWithArtifact(ctx, g, obj)
} }
func TestGitRepositoryReconciler_reconcileSource_emptyRepository(t *testing.T) { // TODO(hidde): Re-enable this test.
g := NewWithT(t) // It is currently disabled because it fails on machines with Git version
// >=v2.41.0 due to changes to commands used by the test server. Causing
server, err := gittestserver.NewTempGitServer() // the test server to return an error when cloning an empty repository,
g.Expect(err).NotTo(HaveOccurred()) // instead of yielding an empty object.
defer os.RemoveAll(server.Root()) //func TestGitRepositoryReconciler_reconcileSource_emptyRepository(t *testing.T) {
server.AutoCreate() // g := NewWithT(t)
g.Expect(server.StartHTTP()).To(Succeed()) //
defer server.StopHTTP() // server, err := gittestserver.NewTempGitServer()
// g.Expect(err).NotTo(HaveOccurred())
obj := &sourcev1.GitRepository{ // defer os.RemoveAll(server.Root())
ObjectMeta: metav1.ObjectMeta{ // server.AutoCreate()
GenerateName: "empty-", // g.Expect(server.StartHTTP()).To(Succeed())
Generation: 1, // defer server.StopHTTP()
}, //
Spec: sourcev1.GitRepositorySpec{ // obj := &sourcev1.GitRepository{
Interval: metav1.Duration{Duration: interval}, // ObjectMeta: metav1.ObjectMeta{
Timeout: &metav1.Duration{Duration: timeout}, // GenerateName: "empty-",
URL: server.HTTPAddress() + "/test.git", // Generation: 1,
}, // },
} // Spec: sourcev1.GitRepositorySpec{
// Interval: metav1.Duration{Duration: interval},
clientBuilder := fakeclient.NewClientBuilder(). // Timeout: &metav1.Duration{Duration: timeout},
WithScheme(testEnv.GetScheme()). // URL: server.HTTPAddress() + "/test.git",
WithStatusSubresource(&sourcev1.GitRepository{}) // },
// }
r := &GitRepositoryReconciler{ //
Client: clientBuilder.Build(), // clientBuilder := fakeclient.NewClientBuilder().
EventRecorder: record.NewFakeRecorder(32), // WithScheme(testEnv.GetScheme()).
Storage: testStorage, // WithStatusSubresource(&sourcev1.GitRepository{})
patchOptions: getPatchOptions(gitRepositoryReadyCondition.Owned, "sc"), //
} // r := &GitRepositoryReconciler{
// Client: clientBuilder.Build(),
g.Expect(r.Client.Create(context.TODO(), obj)).ToNot(HaveOccurred()) // EventRecorder: record.NewFakeRecorder(32),
defer func() { // Storage: testStorage,
g.Expect(r.Client.Delete(context.TODO(), obj)).ToNot(HaveOccurred()) // patchOptions: getPatchOptions(gitRepositoryReadyCondition.Owned, "sc"),
}() // }
//
var commit git.Commit // g.Expect(r.Client.Create(context.TODO(), obj)).ToNot(HaveOccurred())
var includes artifactSet // defer func() {
sp := patch.NewSerialPatcher(obj, r.Client) // g.Expect(r.Client.Delete(context.TODO(), obj)).ToNot(HaveOccurred())
// }()
got, err := r.reconcileSource(context.TODO(), sp, obj, &commit, &includes, t.TempDir()) //
assertConditions := []metav1.Condition{ // var commit git.Commit
*conditions.TrueCondition(sourcev1.FetchFailedCondition, "EmptyGitRepository", "git repository is empty"), // var includes artifactSet
} // sp := patch.NewSerialPatcher(obj, r.Client)
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(assertConditions)) //
g.Expect(err).To(HaveOccurred()) // got, err := r.reconcileSource(context.TODO(), sp, obj, &commit, &includes, t.TempDir())
g.Expect(got).To(Equal(sreconcile.ResultEmpty)) // assertConditions := []metav1.Condition{
g.Expect(commit).ToNot(BeNil()) // *conditions.TrueCondition(sourcev1.FetchFailedCondition, "EmptyGitRepository", "git repository is empty"),
} // }
// g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(assertConditions))
// g.Expect(err).To(HaveOccurred())
// g.Expect(got).To(Equal(sreconcile.ResultEmpty))
// g.Expect(commit).ToNot(BeNil())
//}
func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) { func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
type options struct { type options struct {