Skip to content

Commit

Permalink
ensure git repo is initalized
Browse files Browse the repository at this point in the history
use credentials when pushing
  • Loading branch information
nabuskey committed Nov 15, 2023
1 parent 31c79f9 commit d1feb81
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -52,6 +53,17 @@ func getCredentials(name, namespace string) (string, string, error) {
return DefaultGiteaUsername, DefaultGiteaPassword, nil
}

func getBasicAuth(name, namespace string) (http.BasicAuth, error) {
u, p, err := getCredentials(name, namespace)
if err != nil {
return http.BasicAuth{}, err
}
return http.BasicAuth{
Username: u,
Password: p,
}, nil
}

func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)

Expand All @@ -70,8 +82,12 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return r.reconcileGitRepo(ctx, &gitRepo)
}

func (r *RepositoryReconciler) postProcessReconcile(ctx context.Context, req ctrl.Request, resource *v1alpha1.GitRepository) {

func (r *RepositoryReconciler) postProcessReconcile(ctx context.Context, req ctrl.Request, repo *v1alpha1.GitRepository) {
logger := log.FromContext(ctx)
err := r.Status().Update(ctx, repo)
if err != nil {
logger.Error(err, "failed updating repo status")
}
}

func (r *RepositoryReconciler) reconcileGitRepo(ctx context.Context, repo *v1alpha1.GitRepository) (ctrl.Result, error) {
Expand Down Expand Up @@ -158,7 +174,13 @@ func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v
return fmt.Errorf("committing git files: %w", err)
}

err = clonedRepo.Push(&git.PushOptions{})
auth, err := getBasicAuth(repo.Name, repo.Namespace)
if err != nil {
return fmt.Errorf("getting basic auth: %w", err)
}
err = clonedRepo.Push(&git.PushOptions{
Auth: &auth,
})
if err != nil {
return fmt.Errorf("pushing to git: %w", err)
}
Expand All @@ -178,9 +200,10 @@ func reconcileRepo(giteaClient GiteaClient, repo *v1alpha1.GitRepository) (*gite
// we should reconsider this when targeting non-local clusters.
Private: false,
DefaultBranch: DefaultBranchName,
AutoInit: true,
})
if CErr != nil {
return &gitea.Repository{}, fmt.Errorf("failed to create git repository: %w", err)
return &gitea.Repository{}, fmt.Errorf("failed to create git repository: %w", CErr)
}
repo.Status.ExternalGitRepositoryUrl = createResp.CloneURL
return createResp, nil
Expand All @@ -195,3 +218,7 @@ func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager, notifyChan cha
Watches(&source.Channel{Source: notifyChan}, &handler.EnqueueRequestForObject{}).
Complete(r)
}

func NewGiteaClient(url string, options ...gitea.ClientOption) (GiteaClient, error) {
return gitea.NewClient(url, options...)
}

0 comments on commit d1feb81

Please sign in to comment.