Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey committed May 16, 2024
1 parent b64b627 commit 275c99a
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 268 deletions.
15 changes: 6 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ module github.com/cnoe-io/idpbuilder

go 1.21.3

toolchain go1.21.5

require (
code.gitea.io/sdk/gitea v0.16.0
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d
github.com/docker/docker v24.0.7+incompatible
github.com/go-git/go-git/v5 v5.10.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
github.com/go-logr/logr v1.4.1
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v61 v61.0.0
Expand All @@ -29,12 +28,11 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidmz/go-pageant v1.0.2 // indirect
Expand All @@ -49,7 +47,6 @@ require (
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-fed/httpsig v1.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
Expand Down Expand Up @@ -90,9 +87,9 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
Expand Down
27 changes: 12 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
Expand All @@ -25,8 +23,9 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d h1:cmcSrS0OYTLlGsBFshaAG29qC+PC5LBtKRhnEknlzgU=
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d/go.mod h1:IXG3LiEAeckMfjdwJnt6qC0ee4J4U5bleMuk1HN82ZA=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -59,8 +58,8 @@ github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1
github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
Expand All @@ -71,8 +70,8 @@ github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
github.com/go-git/go-git/v5 v5.10.0 h1:F0x3xXrAWmhwtzoCokU4IMPcBdncG+HAAqi9FcOOjbQ=
github.com/go-git/go-git/v5 v5.10.0/go.mod h1:1FOZ/pQnqw24ghP2n7cunVl0ON55BsjPYvhWHvZGhoo=
github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -140,8 +139,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -190,13 +187,13 @@ github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3c
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
github.com/skeema/knownhosts v1.2.0/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand Down
15 changes: 12 additions & 3 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package build
import (
"context"
"fmt"
"os"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
"github.com/cnoe-io/idpbuilder/pkg/controllers"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/cnoe-io/idpbuilder/pkg/util"
Expand Down Expand Up @@ -120,8 +122,8 @@ func (b *Build) ReconcileCRDs(ctx context.Context, kubeClient client.Client) err
return nil
}

func (b *Build) RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error) error {
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc, b.exitOnSync, b.cfg)
func (b *Build) RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, tmpDir string) error {
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc, b.exitOnSync, b.cfg, tmpDir)
}

func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
Expand Down Expand Up @@ -162,8 +164,15 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
return err
}

dir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-", globals.ProjectName, b.name))
if err != nil {
setupLog.Error(err, "creating temp dir")
return err
}
defer os.RemoveAll(dir)

setupLog.V(1).Info("Running controllers")
if err := b.RunControllers(ctx, mgr, managerExit); err != nil {
if err := b.RunControllers(ctx, mgr, managerExit, dir); err != nil {
setupLog.Error(err, "Error running controllers")
return err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Reconciler struct {
Recorder record.EventRecorder
Scheme *runtime.Scheme
Config util.CorePackageTemplateConfig
TempDir string
RepoMap *util.RepoMap
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -70,7 +72,7 @@ func (r *Reconciler) postProcessReconcile(ctx context.Context, req ctrl.Request,

// create an in-cluster repository CR, update the application spec, then apply
func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alpha1.CustomPackage) (ctrl.Result, error) {
b, err := getArgoCDAppFile(ctx, resource)
b, err := r.getArgoCDAppFile(ctx, resource)
if err != nil {
return ctrl.Result{}, fmt.Errorf("reading file %s: %w", resource.Spec.ArgoCD.ApplicationFile, err)
}
Expand Down Expand Up @@ -278,9 +280,13 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func getArgoCDAppFile(ctx context.Context, resource *v1alpha1.CustomPackage) ([]byte, error) {
func (r *Reconciler) getArgoCDAppFile(ctx context.Context, resource *v1alpha1.CustomPackage) ([]byte, error) {
if resource.Spec.RemoteRepository.Url != "" {
wt, _, err := util.CloneRemoteRepoToMemory(ctx, resource.Spec.RemoteRepository, 1, false)
cloneDir := util.RepoDir(resource.Spec.RemoteRepository.Url, r.TempDir)
st := r.RepoMap.Get(resource.Spec.RemoteRepository.Url, cloneDir)
st.MU.Lock()
wt, _, err := util.CloneRemoteRepoToDir(ctx, resource.Spec.RemoteRepository, 1, false, cloneDir, "")
defer st.MU.Unlock()
if err != nil {
return nil, fmt.Errorf("cloning repo, %s: %w", resource.Spec.RemoteRepository.Url, err)
}
Expand Down
47 changes: 31 additions & 16 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type RepositoryReconciler struct {
Scheme *runtime.Scheme
Config util.CorePackageTemplateConfig
GitProviderFunc gitProviderFunc
TempDir string
RepoMap *util.RepoMap
}

type gitProviderFunc func(context.Context, *v1alpha1.GitRepository, client.Client, *runtime.Scheme, util.CorePackageTemplateConfig) (gitProvider, error)
Expand Down Expand Up @@ -167,7 +169,7 @@ func (r *RepositoryReconciler) reconcileGitRepo(ctx context.Context, repo *v1alp
providerRepo = p
}

err = provider.updateRepoContent(ctx, repo, providerRepo, creds)
err = provider.updateRepoContent(ctx, repo, providerRepo, creds, r.TempDir, r.RepoMap)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating repository contents: %w", err)
}
Expand Down Expand Up @@ -229,7 +231,7 @@ func pushToRemote(ctx context.Context, remoteRepo *git.Repository, creds gitProv
})
}

func reconcileLocalRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials, scheme *runtime.Scheme, tmplConfig util.CorePackageTemplateConfig) error {
func reconcileLocalRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials, scheme *runtime.Scheme, tmplConfig util.CorePackageTemplateConfig, repoMap *util.RepoMap) error {
logger := log.FromContext(ctx)

tempDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s", repo.Name, repo.Namespace))
Expand Down Expand Up @@ -285,37 +287,50 @@ func reconcileLocalRepoContent(ctx context.Context, repo *v1alpha1.GitRepository
return nil
}

func reconcileRemoteRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials) error {
func reconcileRemoteRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, tgtRepo repoInfo, creds gitProviderCredentials, tmpDir string, repoMap *util.RepoMap) error {
logger := log.FromContext(ctx)

remoteWT, _, err := util.CloneRemoteRepoToMemory(ctx, repo.Spec.Source.RemoteRepository, 1, false)
cloneDir := util.RepoDir(repo.Spec.Source.RemoteRepository.Url, tmpDir)
st := repoMap.Get(repo.Spec.Source.RemoteRepository.Url, cloneDir)
st.MU.Lock()
defer st.MU.Unlock()
remoteWT, _, err := util.CloneRemoteRepoToDir(ctx, repo.Spec.Source.RemoteRepository, 1, false, cloneDir, "")
if err != nil {
logger.V(1).Info("cloning repo.", "err", err, "url", repo.Spec.Source.RemoteRepository, "dir", cloneDir)
return fmt.Errorf("cloning repo, %s: %w", repo.Spec.Source.RemoteRepository.Url, err)
}

localRepoSpec := v1alpha1.RemoteRepositorySpec{
CloneSubmodules: false,
Path: ".",
Url: repoInfo.cloneUrl,
Url: tgtRepo.cloneUrl,
Ref: "",
}

var localRepo *git.Repository
tgtCloneDir := util.RepoDir(tgtRepo.cloneUrl, tmpDir)
lst := repoMap.Get(localRepoSpec.Url, tgtCloneDir)

localWT, lr, err := util.CloneRemoteRepoToMemory(ctx, localRepoSpec, 1, true)
lst.MU.Lock()
defer lst.MU.Unlock()
tgtRepoWT, localRepo, err := util.CloneRemoteRepoToDir(ctx, localRepoSpec, 1, true, tgtCloneDir, getFallbackRepositoryURL(repo, tgtRepo))
if err != nil {
logger.V(1).Info("failed cloning repo. trying fallback url", "err", err, "url", repoInfo.cloneUrl)
localRepoSpec.Url = getFallbackRepositoryURL(repo, repoInfo)
_, lr, err = util.CloneRemoteRepoToMemory(ctx, localRepoSpec, 1, true)
if err != nil {
return fmt.Errorf("cloning repo, %s: %w", repoInfo.cloneUrl, err)
}
return fmt.Errorf("cloning repo %s: %w", repo.Spec.Source.RemoteRepository.Url, err)
}

localRepo = lr
err = util.CopyTreeToTree(remoteWT, localWT, fmt.Sprintf("/%s", repo.Spec.Source.Path), ".")
//localWT, lr, err := util.CloneRemoteRepoToMemory(ctx, localRepoSpec, 1, true)
//if err != nil {
// logger.V(1).Info("failed cloning repo. trying fallback url", "err", err, "url", repoInfo.cloneUrl)
// localRepoSpec.Url = getFallbackRepositoryURL(repo, repoInfo)
// _, lr, err = util.CloneRemoteRepoToMemory(ctx, localRepoSpec, 1, true)
// if err != nil {
// return fmt.Errorf("cloning repo, %s: %w", repoInfo.cloneUrl, err)
// }
//}

//localRepo = lr
err = util.CopyTreeToTree(remoteWT, tgtRepoWT, fmt.Sprintf("/%s", repo.Spec.Source.Path), ".")
if err != nil {
return fmt.Errorf("copying contents, %s: %w", repoInfo.cloneUrl, err)
return fmt.Errorf("copying contents, %s: %w", tgtRepo.cloneUrl, err)
}

hash, push, err := addAllAndCommit(repo.Spec.Source.Path, localRepo)
Expand Down
14 changes: 8 additions & 6 deletions pkg/controllers/gitrepository/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestGitRepositoryContentReconcile(t *testing.T) {
giteaClient: mockGitea{},
}
// add file to source directory, reconcile, clone the repo and check if the added file exists
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{})
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{}, "", util.NewRepoLock())
if err != nil {
t.Fatalf("failed adding %v", err)
}
Expand All @@ -250,7 +250,7 @@ func TestGitRepositoryContentReconcile(t *testing.T) {
if err != nil {
t.Fatalf("failed to remove added file %v", err)
}
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{})
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{}, "", util.NewRepoLock())
if err != nil {
t.Fatalf("failed removing %v", err)
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestGitRepositoryContentReconcileEmbedded(t *testing.T) {
Client: &fakeClient{},
giteaClient: mockGitea{},
}
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{})
err = p.updateRepoContent(ctx, &resource, repoInfo{cloneUrl: dir}, gitProviderCredentials{}, "", util.NewRepoLock())
if err != nil {
t.Fatalf("failed adding %v", err)
}
Expand All @@ -316,8 +316,11 @@ func TestGitRepositoryReconcile(t *testing.T) {
if err != nil {
t.Fatalf("failed to get absolute path: %v", err)
}
updateDir, _, _ := setUpLocalRepo()
defer os.RemoveAll(updateDir)

addDir, err := setupDir()
fmt.Println(addDir)
defer os.RemoveAll(addDir)
if err != nil {
t.Fatalf("failed to set up dirs: %v", err)
Expand Down Expand Up @@ -360,7 +363,7 @@ func TestGitRepositoryReconcile(t *testing.T) {
"update": {
giteaClient: mockGitea{
getRepo: func() (*gitea.Repository, *gitea.Response, error) {
return &gitea.Repository{CloneURL: dir}, nil, nil
return &gitea.Repository{CloneURL: updateDir}, nil, nil
},
},
input: v1alpha1.GitRepository{
Expand All @@ -378,7 +381,7 @@ func TestGitRepositoryReconcile(t *testing.T) {
},
expect: expect{
resource: v1alpha1.GitRepositoryStatus{
ExternalGitRepositoryUrl: dir,
ExternalGitRepositoryUrl: updateDir,
Synced: true,
InternalGitRepositoryUrl: "http://cnoe.io/giteaAdmin/test-test.git",
},
Expand All @@ -403,7 +406,6 @@ func TestGitRepositoryReconcile(t *testing.T) {
if v.expect.resource.LatestCommit.Hash == "" {
v.expect.resource.LatestCommit.Hash = v.input.Status.LatestCommit.Hash
}
time.Sleep(100 * time.Millisecond)
assert.Equal(t, v.input.Status, v.expect.resource)
}
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/gitrepository/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"code.gitea.io/sdk/gitea"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/google/go-github/v61/github"
)

Expand Down Expand Up @@ -44,5 +45,5 @@ type gitProvider interface {
getProviderCredentials(ctx context.Context, repo *v1alpha1.GitRepository) (gitProviderCredentials, error)
getRepository(ctx context.Context, repo *v1alpha1.GitRepository) (repoInfo, error)
setProviderCredentials(ctx context.Context, repo *v1alpha1.GitRepository, creds gitProviderCredentials) error
updateRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials) error
updateRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials, tmpDir string, repoMap *util.RepoMap) error
}
Loading

0 comments on commit 275c99a

Please sign in to comment.