diff --git a/api/v1alpha1/custom_package_types.go b/api/v1alpha1/custom_package_types.go index 8d0bd33f..b3ef4b73 100644 --- a/api/v1alpha1/custom_package_types.go +++ b/api/v1alpha1/custom_package_types.go @@ -26,7 +26,9 @@ type CustomPackageSpec struct { // Replicate specifies whether to replicate remote or local contents to the local gitea server. Replicate bool `json:"replicate"` // GitServerURL specifies the base URL for the git server for API calls. for example, http://gitea.cnoe.localtest.me:8880 - GitServerURL string `json:"gitServerURL"` + GitServerURL string `json:"gitServerURL"` + // InternalGitServeURL specifies the base URL for the git server accessible within the cluster. for example, http://my-gitea-http.gitea.svc.cluster.local:3000 + InternalGitServeURL string `json:"internalGitServeURL"` GitServerAuthSecretRef SecretReference `json:"gitServerAuthSecretRef"` ArgoCD ArgoCDPackageSpec `json:"argoCD,omitempty"` diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index 989e540c..4cb4d60f 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -71,9 +71,20 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp } appName := app.GetName() - if app.Spec.HasMultipleSources() { - for j := range app.Spec.Sources { - s := app.Spec.Sources[j] + if resource.Spec.Replicate { + if app.Spec.HasMultipleSources() { + for j := range app.Spec.Sources { + s := app.Spec.Sources[j] + res, repo, sErr := r.reconcileArgocdSource(ctx, resource, appName, resource.Spec.ArgoCD.ApplicationFile, s.RepoURL) + if sErr != nil { + return res, sErr + } + if repo != nil { + s.RepoURL = repo.Status.InternalGitRepositoryUrl + } + } + } else { + s := app.Spec.Source res, repo, sErr := r.reconcileArgocdSource(ctx, resource, appName, resource.Spec.ArgoCD.ApplicationFile, s.RepoURL) if sErr != nil { return res, sErr @@ -82,15 +93,6 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp s.RepoURL = repo.Status.InternalGitRepositoryUrl } } - } else { - s := app.Spec.Source - res, repo, sErr := r.reconcileArgocdSource(ctx, resource, appName, resource.Spec.ArgoCD.ApplicationFile, s.RepoURL) - if sErr != nil { - return res, sErr - } - if repo != nil { - s.RepoURL = repo.Status.InternalGitRepositoryUrl - } } foundAppObj := argov1alpha1.Application{} @@ -102,7 +104,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp return ctrl.Result{}, fmt.Errorf("creating %s app CR: %w", appName, err) } - return ctrl.Result{}, nil + return ctrl.Result{RequeueAfter: requeueTime}, nil } return ctrl.Result{}, fmt.Errorf("getting argocd application object: %w", err) } @@ -148,8 +150,9 @@ func (r *Reconciler) reconcileGitRepo(ctx context.Context, resource *v1alpha1.Cu Type: "local", Path: absPath, }, - GitURL: resource.Spec.GitServerURL, - SecretRef: resource.Spec.GitServerAuthSecretRef, + GitURL: resource.Spec.GitServerURL, + InternalGitURL: resource.Spec.InternalGitServeURL, + SecretRef: resource.Spec.GitServerAuthSecretRef, }, } diff --git a/pkg/controllers/custompackage/controller_test.go b/pkg/controllers/custompackage/controller_test.go new file mode 100644 index 00000000..58158201 --- /dev/null +++ b/pkg/controllers/custompackage/controller_test.go @@ -0,0 +1,185 @@ +package custompackage + +import ( + "context" + "fmt" + argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" + "github.com/cnoe-io/idpbuilder/api/v1alpha1" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + k8sruntime "k8s.io/apimachinery/pkg/runtime" + "os" + "path/filepath" + "reflect" + "runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "strings" + "testing" +) + +func TestReconcileCustomPkg(t *testing.T) { + s := k8sruntime.NewScheme() + sb := k8sruntime.NewSchemeBuilder( + v1.AddToScheme, + argov1alpha1.AddToScheme, + v1alpha1.AddToScheme, + ) + sb.AddToScheme(s) + testEnv := &envtest.Environment{ + CRDDirectoryPaths: []string{ + filepath.Join("..", "resources"), + "../localbuild/resources/argo/install.yaml", + }, + ErrorIfCRDPathMissing: true, + Scheme: s, + BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s", + fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)), + } + + cfg, err := testEnv.Start() + if err != nil { + t.Fatalf("Starting testenv: %v", err) + } + defer testEnv.Stop() + + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: s, + }) + if err != nil { + t.Fatalf("getting manager: %v", err) + } + + ctx, ctxCancel := context.WithCancel(context.Background()) + stoppedCh := make(chan error) + go func() { + err := mgr.Start(ctx) + stoppedCh <- err + }() + + defer func() { + ctxCancel() + err := <-stoppedCh + if err != nil { + t.Errorf("Starting controller manager: %v", err) + t.FailNow() + } + }() + + r := &Reconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("test-custompkg-controller"), + } + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("getting cwd %v", err) + } + customPkgs := []v1alpha1.CustomPackage{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test1", + Namespace: "test", + UID: "abc", + }, + Spec: v1alpha1.CustomPackageSpec{ + Replicate: true, + GitServerURL: "https://cnoe.io", + InternalGitServeURL: "http://internal.cnoe.io", + ArgoCD: v1alpha1.ArgoCDPackageSpec{ + ApplicationFile: filepath.Join(cwd, "test/resources/customPackages/testDir/app.yaml"), + Name: "my-app", + Namespace: "argocd", + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test2", + Namespace: "test", + UID: "abc", + }, + Spec: v1alpha1.CustomPackageSpec{ + Replicate: false, + GitServerURL: "https://cnoe.io", + InternalGitServeURL: "http://cnoe.io/internal", + ArgoCD: v1alpha1.ArgoCDPackageSpec{ + ApplicationFile: filepath.Join(cwd, "test/resources/customPackages/testDir2/exampleApp.yaml"), + Name: "guestbook", + Namespace: "argocd", + }, + }, + }, + } + + for _, n := range []string{"argocd", "test"} { + ns := v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: n, + }, + } + err = mgr.GetClient().Create(context.Background(), &ns) + if err != nil { + t.Fatalf("creating test ns: %v", err) + } + } + + for i := range customPkgs { + _, err = r.reconcileCustomPackage(context.Background(), &customPkgs[i]) + if err != nil { + t.Fatalf("reconciling custom packages %v", err) + } + } + + // verify repo. + c := mgr.GetClient() + repo := v1alpha1.GitRepository{ + ObjectMeta: metav1.ObjectMeta{ + Name: repoName("my-app", "test/resources/customPackages/testDir/busybox"), + Namespace: "test", + }, + } + err = c.Get(context.Background(), client.ObjectKeyFromObject(&repo), &repo) + if err != nil { + t.Fatalf("getting my-app-busybox git repo %v", err) + } + + p, _ := filepath.Abs("test/resources/customPackages/testDir/busybox") + expectedRepo := v1alpha1.GitRepository{ + Spec: v1alpha1.GitRepositorySpec{ + Source: v1alpha1.GitRepositorySource{ + Type: "local", + Path: p, + }, + GitURL: "https://cnoe.io", + InternalGitURL: "http://internal.cnoe.io", + }, + } + ok := reflect.DeepEqual(repo.Spec, expectedRepo.Spec) + if !ok { + t.Fatalf("expected spec does not match") + } + + // verify argocd apps + localApp := argov1alpha1.Application{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-app", + Namespace: "argocd", + }, + } + err = c.Get(context.Background(), client.ObjectKeyFromObject(&localApp), &localApp) + if err != nil { + t.Fatalf("failed getting my-app %v", err) + } + if strings.HasPrefix(localApp.Spec.Source.RepoURL, "cnoe://") { + t.Fatalf("cnoe:// prefix should be removed") + } + + for _, n := range []string{"guestbook", "guestbook2"} { + err = c.Get(context.Background(), client.ObjectKeyFromObject(&localApp), &localApp) + if err != nil { + t.Fatalf("expected %s arogapp : %v", n, err) + } + } +} diff --git a/pkg/controllers/localbuild/test/resources/customPackages/testDir/app.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/app.yaml similarity index 100% rename from pkg/controllers/localbuild/test/resources/customPackages/testDir/app.yaml rename to pkg/controllers/custompackage/test/resources/customPackages/testDir/app.yaml diff --git a/pkg/controllers/localbuild/test/resources/customPackages/testDir/busybox/busybox.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir/busybox/busybox.yaml similarity index 100% rename from pkg/controllers/localbuild/test/resources/customPackages/testDir/busybox/busybox.yaml rename to pkg/controllers/custompackage/test/resources/customPackages/testDir/busybox/busybox.yaml diff --git a/pkg/controllers/localbuild/test/resources/customPackages/testDir2/exampleApp.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir2/exampleApp.yaml similarity index 100% rename from pkg/controllers/localbuild/test/resources/customPackages/testDir2/exampleApp.yaml rename to pkg/controllers/custompackage/test/resources/customPackages/testDir2/exampleApp.yaml diff --git a/pkg/controllers/localbuild/test/resources/customPackages/testDir2/exampleApp2.yaml b/pkg/controllers/custompackage/test/resources/customPackages/testDir2/exampleApp2.yaml similarity index 100% rename from pkg/controllers/localbuild/test/resources/customPackages/testDir2/exampleApp2.yaml rename to pkg/controllers/custompackage/test/resources/customPackages/testDir2/exampleApp2.yaml diff --git a/pkg/controllers/gitrepository/controller_test.go b/pkg/controllers/gitrepository/controller_test.go index 78c73e7c..5799e582 100644 --- a/pkg/controllers/gitrepository/controller_test.go +++ b/pkg/controllers/gitrepository/controller_test.go @@ -259,6 +259,7 @@ func TestGitRepositoryContentReconcileEmbedded(t *testing.T) { EmbeddedAppName: "nginx", Type: "embedded", }, + InternalGitURL: "http://cnoe.io", }, } @@ -315,6 +316,7 @@ func TestGitRepositoryReconcile(t *testing.T) { Path: resourcePath, Type: "local", }, + InternalGitURL: "http://cnoe.io", }, }, expect: expect{ @@ -322,6 +324,7 @@ func TestGitRepositoryReconcile(t *testing.T) { ExternalGitRepositoryUrl: dir, LatestCommit: v1alpha1.Commit{Hash: hash}, Synced: true, + InternalGitRepositoryUrl: "http://cnoe.io/giteaAdmin/test-test.git", }, }, }, @@ -340,12 +343,14 @@ func TestGitRepositoryReconcile(t *testing.T) { Path: addDir, Type: "local", }, + InternalGitURL: "http://cnoe.io", }, }, expect: expect{ resource: v1alpha1.GitRepositoryStatus{ ExternalGitRepositoryUrl: dir, Synced: true, + InternalGitRepositoryUrl: "http://cnoe.io/giteaAdmin/test-test.git", }, }, }, diff --git a/pkg/controllers/localbuild/controller.go b/pkg/controllers/localbuild/controller.go index 34ac7c0e..b8eec71a 100644 --- a/pkg/controllers/localbuild/controller.go +++ b/pkg/controllers/localbuild/controller.go @@ -427,8 +427,9 @@ func (r *LocalbuildReconciler) reconcileCustomPkg(ctx context.Context, resource Namespace: globals.GetProjectNamespace(resource.Name), }, Spec: v1alpha1.CustomPackageSpec{ - Replicate: true, - GitServerURL: resource.Status.Gitea.ExternalURL, + Replicate: true, + GitServerURL: resource.Status.Gitea.ExternalURL, + InternalGitServeURL: resource.Status.Gitea.InternalURL, GitServerAuthSecretRef: v1alpha1.SecretReference{ Name: resource.Status.Gitea.AdminUserSecretName, Namespace: resource.Status.Gitea.AdminUserSecretNamespace, diff --git a/pkg/controllers/localbuild/controller_test.go b/pkg/controllers/localbuild/controller_test.go deleted file mode 100644 index b2d87b3a..00000000 --- a/pkg/controllers/localbuild/controller_test.go +++ /dev/null @@ -1,182 +0,0 @@ -package localbuild - -// -//import ( -// "context" -// "fmt" -// argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" -// "github.com/cnoe-io/idpbuilder/api/v1alpha1" -// "github.com/cnoe-io/idpbuilder/globals" -// v1 "k8s.io/api/core/v1" -// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -// k8sruntime "k8s.io/apimachinery/pkg/runtime" -// "path/filepath" -// "reflect" -// "runtime" -// ctrl "sigs.k8s.io/controller-runtime" -// "sigs.k8s.io/controller-runtime/pkg/client" -// "sigs.k8s.io/controller-runtime/pkg/envtest" -// "strings" -// "testing" -//) -// -//func TestReconcileCustomPkg(t *testing.T) { -// s := k8sruntime.NewScheme() -// sb := k8sruntime.NewSchemeBuilder( -// v1.AddToScheme, -// argov1alpha1.AddToScheme, -// v1alpha1.AddToScheme, -// ) -// sb.AddToScheme(s) -// testEnv := &envtest.Environment{ -// CRDDirectoryPaths: []string{ -// filepath.Join("..", "resources"), -// "resources/argo/install.yaml", -// }, -// ErrorIfCRDPathMissing: true, -// Scheme: s, -// BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s", -// fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)), -// } -// -// cfg, err := testEnv.Start() -// if err != nil { -// t.Fatalf("Starting testenv: %v", err) -// } -// defer testEnv.Stop() -// -// mgr, err := ctrl.NewManager(cfg, ctrl.Options{ -// Scheme: s, -// }) -// if err != nil { -// t.Fatalf("getting manager: %v", err) -// } -// -// ctx, ctxCancel := context.WithCancel(context.Background()) -// stoppedCh := make(chan error) -// go func() { -// err := mgr.Start(ctx) -// stoppedCh <- err -// }() -// -// defer func() { -// ctxCancel() -// err := <-stoppedCh -// if err != nil { -// t.Errorf("Starting controller manager: %v", err) -// t.FailNow() -// } -// }() -// -// r := &LocalbuildReconciler{ -// Client: mgr.GetClient(), -// Scheme: mgr.GetScheme(), -// CancelFunc: nil, -// shouldShutdown: false, -// } -// customPkgs := []v1alpha1.CustomPackageSpec{ -// { -// Directory: "test/resources/customPackages/testDir", -// }, -// { -// Directory: "test/resources/customPackages/testDir2", -// }, -// } -// -// res := v1alpha1.Localbuild{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: "test", -// UID: "uid", -// }, -// Spec: v1alpha1.LocalbuildSpec{ -// PackageConfigs: v1alpha1.PackageConfigsSpec{ -// GitConfig: v1alpha1.GitConfigSpec{}, -// Argo: v1alpha1.ArgoPackageConfigSpec{}, -// EmbeddedArgoApplications: v1alpha1.EmbeddedArgoApplicationsPackageConfigSpec{}, -// CustomPackages: customPkgs, -// }, -// }, -// Status: v1alpha1.LocalbuildStatus{ -// Gitea: v1alpha1.GiteaStatus{ -// Available: true, -// ExternalURL: "https://cnoe.io", -// InternalURL: "http://internal.cnoe.io", -// AdminUserSecretName: "abc", -// AdminUserSecretNamespace: "abc", -// }, -// }, -// } -// -// for _, n := range []string{"argocd", globals.GetProjectNamespace(res.Name)} { -// ns := v1.Namespace{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: n, -// }, -// } -// err = mgr.GetClient().Create(context.Background(), &ns) -// if err != nil { -// t.Fatalf("creating test ns: %v", err) -// } -// } -// -// for i := range customPkgs { -// _, err = r.reconcileCustomPkg(context.Background(), &res, customPkgs[i]) -// if err != nil { -// t.Fatalf("reconciling custom packages %v", err) -// } -// } -// -// // verify repo. -// c := mgr.GetClient() -// repo := v1alpha1.GitRepository{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: "my-app-busybox", -// Namespace: globals.GetProjectNamespace(res.Name), -// }, -// } -// err = c.Get(context.Background(), client.ObjectKeyFromObject(&repo), &repo) -// if err != nil { -// t.Fatalf("getting my-app-busybox git repo %v", err) -// } -// -// p, _ := filepath.Abs("test/resources/customPackages/testDir/busybox") -// expectedRepo := v1alpha1.GitRepository{ -// Spec: v1alpha1.GitRepositorySpec{ -// Source: v1alpha1.GitRepositorySource{ -// Type: "local", -// Path: p, -// }, -// GitURL: "https://cnoe.io", -// SecretRef: v1alpha1.SecretReference{ -// Name: "abc", -// Namespace: "abc", -// }, -// }, -// } -// ok := reflect.DeepEqual(repo.Spec, expectedRepo.Spec) -// if !ok { -// t.Fatalf("expected spec does not match") -// } -// -// // verify argocd apps -// localApp := argov1alpha1.Application{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: "my-app", -// Namespace: "argocd", -// }, -// } -// err = c.Get(context.Background(), client.ObjectKeyFromObject(&localApp), &localApp) -// if err != nil { -// t.Fatalf("failed getting my-app %v", err) -// } -// if strings.HasPrefix(localApp.Spec.Source.RepoURL, "cnoe://") { -// t.Fatalf("cnoe:// prefix should be removed") -// } -// -// for _, n := range []string{"guestbook", "guestbook2"} { -// err = c.Get(context.Background(), client.ObjectKeyFromObject(&localApp), &localApp) -// if err != nil { -// t.Fatalf("expected %s arogapp : %v", n, err) -// } -// } -//} diff --git a/pkg/controllers/resources/idpbuilder.cnoe.io_custompackages.yaml b/pkg/controllers/resources/idpbuilder.cnoe.io_custompackages.yaml index 35e8699f..12f4c596 100644 --- a/pkg/controllers/resources/idpbuilder.cnoe.io_custompackages.yaml +++ b/pkg/controllers/resources/idpbuilder.cnoe.io_custompackages.yaml @@ -63,6 +63,10 @@ spec: description: GitServerURL specifies the base URL for the git server for API calls. for example, http://gitea.cnoe.localtest.me:8880 type: string + internalGitServeURL: + description: InternalGitServeURL specifies the base URL for the git + server accessible within the cluster. for example, http://my-gitea-http.gitea.svc.cluster.local:3000 + type: string replicate: description: // Type specifies what kind of package this is. local means local files specified in the Directory field must be synced @@ -73,6 +77,7 @@ spec: required: - gitServerAuthSecretRef - gitServerURL + - internalGitServeURL - replicate type: object status: