Skip to content

Commit

Permalink
fix tests, ensure internal repo urls are passed
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu Mccloskey <[email protected]>
  • Loading branch information
nabuskey committed Dec 8, 2023
1 parent 9f83544 commit 9054d09
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 200 deletions.
4 changes: 3 additions & 1 deletion api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
33 changes: 18 additions & 15 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
},
}

Expand Down
185 changes: 185 additions & 0 deletions pkg/controllers/custompackage/controller_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
5 changes: 5 additions & 0 deletions pkg/controllers/gitrepository/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func TestGitRepositoryContentReconcileEmbedded(t *testing.T) {
EmbeddedAppName: "nginx",
Type: "embedded",
},
InternalGitURL: "http://cnoe.io",
},
}

Expand Down Expand Up @@ -315,13 +316,15 @@ func TestGitRepositoryReconcile(t *testing.T) {
Path: resourcePath,
Type: "local",
},
InternalGitURL: "http://cnoe.io",
},
},
expect: expect{
resource: v1alpha1.GitRepositoryStatus{
ExternalGitRepositoryUrl: dir,
LatestCommit: v1alpha1.Commit{Hash: hash},
Synced: true,
InternalGitRepositoryUrl: "http://cnoe.io/giteaAdmin/test-test.git",
},
},
},
Expand All @@ -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",
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 9054d09

Please sign in to comment.