-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
112 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,13 @@ import ( | |
"code.gitea.io/sdk/gitea" | ||
"github.com/cnoe-io/idpbuilder/api/v1alpha1" | ||
"github.com/cnoe-io/idpbuilder/globals" | ||
"github.com/cnoe-io/idpbuilder/pkg/controllers/localbuild" | ||
"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" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
|
@@ -28,21 +30,26 @@ const ( | |
// DefaultGiteaUsername and DefaultGiteaPassword taken from gitea helm chart. https://gitea.com/gitea/helm-chart#gitea | ||
DefaultGiteaUsername = "gitea_admin" | ||
DefaultGiteaPassword = "r8sA8CPHD9!bt6d" | ||
requeueTime = time.Second * 60 | ||
requeueTime = time.Second * 30 | ||
gitCommitAuthorName = "git-reconciler" | ||
gitCommitAuthorEmail = "[email protected]" | ||
) | ||
|
||
type ClientFN func(url string, options ...gitea.ClientOption) (GiteaClient, error) | ||
|
||
func NewGiteaClient(url string, options ...gitea.ClientOption) (GiteaClient, error) { | ||
return gitea.NewClient(url, options...) | ||
} | ||
|
||
type RepositoryReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
GiteaClientFN ClientFN | ||
Recorder record.EventRecorder | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
func getRepositoryName(repo v1alpha1.GitRepository) string { | ||
return fmt.Sprintf("%s-%s", repo.Namespace, repo.Name) | ||
return fmt.Sprintf("%s-%s", repo.Name, repo.Namespace) | ||
} | ||
|
||
func getOrganizationName(repo v1alpha1.GitRepository) string { | ||
|
@@ -73,13 +80,21 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) | |
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
logger.Info("reconciling GitRepository", "name", req.Name, "namespace", req.Namespace) | ||
if !filepath.IsAbs(gitRepo.Spec.Source.Path) { | ||
if !r.shouldProcess(gitRepo) { | ||
return ctrl.Result{Requeue: false}, nil | ||
} | ||
|
||
logger.Info("reconciling GitRepository", "name", req.Name, "namespace", req.Namespace) | ||
defer r.postProcessReconcile(ctx, req, &gitRepo) | ||
return r.reconcileGitRepo(ctx, &gitRepo) | ||
|
||
result, err := r.reconcileGitRepo(ctx, &gitRepo) | ||
if err != nil { | ||
r.Recorder.Event(&gitRepo, "Warning", "reconcile error", err.Error()) | ||
} else { | ||
r.Recorder.Event(&gitRepo, "Normal", "reconcile success", "Successfully reconciled") | ||
} | ||
|
||
return result, err | ||
} | ||
|
||
func (r *RepositoryReconciler) postProcessReconcile(ctx context.Context, req ctrl.Request, repo *v1alpha1.GitRepository) { | ||
|
@@ -116,8 +131,8 @@ func (r *RepositoryReconciler) reconcileGitRepo(ctx context.Context, repo *v1alp | |
if err != nil { | ||
return ctrl.Result{Requeue: true, RequeueAfter: requeueTime}, fmt.Errorf("failed to reconcile repo content %w", err) | ||
} | ||
|
||
return ctrl.Result{Requeue: false}, nil | ||
repo.Status.Synced = true | ||
return ctrl.Result{Requeue: true, RequeueAfter: requeueTime}, nil | ||
} | ||
|
||
func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, giteaRepo *gitea.Repository) error { | ||
|
@@ -132,12 +147,12 @@ func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v | |
NoCheckout: true, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("cloing repo: %w", err) | ||
return fmt.Errorf("cloning repo: %w", err) | ||
} | ||
|
||
err = util.CopyDirectory(repo.Spec.Source.Path, tempDir) | ||
err = writeRepoContents(repo, tempDir) | ||
if err != nil { | ||
return fmt.Errorf("copying files: %w", err) | ||
return err | ||
} | ||
|
||
tree, err := clonedRepo.Worktree() | ||
|
@@ -219,6 +234,45 @@ func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager, notifyChan cha | |
Complete(r) | ||
} | ||
|
||
func NewGiteaClient(url string, options ...gitea.ClientOption) (GiteaClient, error) { | ||
return gitea.NewClient(url, options...) | ||
func (r *RepositoryReconciler) shouldProcess(repo v1alpha1.GitRepository) bool { | ||
if repo.Spec.Source.Type == "local" && !filepath.IsAbs(repo.Spec.Source.Path) { | ||
return false | ||
} | ||
// embedded fs does not change | ||
if repo.Spec.Source.Type == "embedded" && repo.Status.Synced { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func getEmbedded(name string) ([][]byte, error) { | ||
switch name { | ||
case "argocd": | ||
return localbuild.GetRawInstallResources() | ||
default: | ||
return nil, fmt.Errorf("unsupported embedded app name %s", name) | ||
} | ||
} | ||
|
||
func writeRepoContents(repo *v1alpha1.GitRepository, tempDir string) error { | ||
if repo.Spec.Source.EmbeddedAppName != "" { | ||
resources, err := getEmbedded(repo.Spec.Source.EmbeddedAppName) | ||
if err != nil { | ||
return fmt.Errorf("getting embedded resource; %w", err) | ||
} | ||
for i := range resources { | ||
filePath := filepath.Join(tempDir, fmt.Sprintf("resource%d.yaml", i)) | ||
err = os.WriteFile(filePath, resources[i], 0644) | ||
if err != nil { | ||
return fmt.Errorf("writing embedded resource; %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
err := util.CopyDirectory(repo.Spec.Source.Path, tempDir) | ||
if err != nil { | ||
return fmt.Errorf("copying files: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: idpbuilder.cnoe.io/v1alpha1 | ||
kind: GitRepository | ||
metadata: | ||
name: argocd | ||
namespace: default | ||
spec: | ||
giteaURL: "http://localhost:3000" | ||
source: | ||
embeddedAppName: "argocd" | ||
type: embedded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: idpbuilder.cnoe.io/v1alpha1 | ||
kind: GitRepository | ||
metadata: | ||
name: test | ||
namespace: default | ||
spec: | ||
giteaURL: "http://localhost:3000" | ||
source: | ||
path: "/tmp/b/" |