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

GitRepo Initial Checkout when DisablePolling is True #2469

Merged
merged 28 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
36 changes: 36 additions & 0 deletions integrationtests/gitjob/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ var _ = Describe("GitJob controller", func() {
}).Should(BeTrue())
})
})

When("a new GitRepo is created with DisablePolling set to true", func() {
var (
gitRepo v1alpha1.GitRepo
gitRepoName string
)

BeforeEach(func() {
gitRepoName = "disable-polling"
})

It("should update the commit from the actual repo", func() {
gitRepo = createGitRepoWithDisablePolling(gitRepoName)
Expect(k8sClient.Create(ctx, &gitRepo)).To(Succeed())

By("verifying the commit is updated")
Eventually(func() string {
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: gitRepoName, Namespace: gitRepoNamespace}, &gitRepo)).To(Succeed())
return gitRepo.Status.Commit
}, "30s", "1s").ShouldNot(Equal(""))
})
})
})

func simulateIncreaseForceSyncGeneration(gitRepo v1alpha1.GitRepo) error {
Expand Down Expand Up @@ -268,3 +290,17 @@ func createGitRepo(gitRepoName string) v1alpha1.GitRepo {
},
}
}

func createGitRepoWithDisablePolling(gitRepoName string) v1alpha1.GitRepo {
return v1alpha1.GitRepo{
ObjectMeta: metav1.ObjectMeta{
Name: gitRepoName,
Namespace: gitRepoNamespace,
},
Spec: v1alpha1.GitRepoSpec{
Repo: repo,
DisablePolling: true,
Branch: "main",
},
}
}
25 changes: 22 additions & 3 deletions internal/cmd/controller/gitops/reconciler/gitjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

grutil "github.com/rancher/fleet/internal/cmd/controller/gitrepo"
v1alpha1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/git"

"github.com/rancher/wrangler/v2/pkg/condition"
"github.com/rancher/wrangler/v2/pkg/kstatus"
Expand Down Expand Up @@ -103,9 +104,27 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, fmt.Errorf("error retrieving git job: %v", err)
}

if errors.IsNotFound(err) && gitRepo.Status.Commit != "" {
if err := r.createJob(ctx, &gitRepo); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating git job: %v", err)
if errors.IsNotFound(err) {
if gitRepo.Status.Commit != "" {
if err := r.createJob(ctx, &gitRepo); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating git job: %v", err)
}
} else {
if gitRepo.Status.GitJobStatus == status.FailedStatus.String() {
return ctrl.Result{}, fmt.Errorf("error creating git job: %v", err)
}

fetcher := git.NewFetcher()
commit, err := fetcher.LatestCommit(ctx, &gitRepo, r.Client)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error fetching commit: %v", err)
}
gitRepo.Status.Commit = commit
logger.V(1).Info("Updating GitRepo status", "commit", gitRepo.Status.Commit)
if err := r.Status().Update(ctx, &gitRepo); err != nil {
return ctrl.Result{}, fmt.Errorf("error updating git repo status: %v", err)
}
return ctrl.Result{}, nil
}
} else if gitRepo.Status.Commit != "" {
if err = r.deleteJobIfNeeded(ctx, &gitRepo, &job); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/git/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (

type Fetch struct{}

func NewFetcher() *Fetch {
return &Fetch{}
}

func (f *Fetch) LatestCommit(ctx context.Context, gitrepo *v1alpha1.GitRepo, client client.Client) (string, error) {
secretName := DefaultSecretName
if gitrepo.Spec.ClientSecretName != "" {
Expand Down
Loading