Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey committed Nov 16, 2023
1 parent ab7a85c commit cd01ed4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 58 deletions.
53 changes: 25 additions & 28 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package build
import (
"context"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/controllers"
"github.com/cnoe-io/idpbuilder/pkg/kind"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

Expand Down Expand Up @@ -98,10 +95,10 @@ func (b *Build) RunControllers(ctx context.Context, mgr manager.Manager, exitCh
func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
managerExit := make(chan error)

setupLog.Info("Creating kind cluster")
if err := b.ReconcileKindCluster(ctx, recreateCluster); err != nil {
return err
}
//setupLog.Info("Creating kind cluster")
//if err := b.ReconcileKindCluster(ctx, recreateCluster); err != nil {
// return err
//}

setupLog.Info("Getting Kube config")
kubeConfig, err := b.GetKubeConfig()
Expand Down Expand Up @@ -137,27 +134,27 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
}

// Create localbuild resource
localBuild := v1alpha1.Localbuild{
ObjectMeta: metav1.ObjectMeta{
Name: b.name,
Namespace: "default",
},
}

setupLog.Info("Creating localbuild resource")
controllerutil.CreateOrUpdate(ctx, kubeClient, &localBuild, func() error {
localBuild.Spec = v1alpha1.LocalbuildSpec{
PackageConfigs: v1alpha1.PackageConfigsSpec{
Argo: v1alpha1.ArgoPackageConfigSpec{
Enabled: true,
},
EmbeddedArgoApplications: v1alpha1.EmbeddedArgoApplicationsPackageConfigSpec{
Enabled: true,
},
},
}
return nil
})
//localBuild := v1alpha1.Localbuild{
// ObjectMeta: metav1.ObjectMeta{
// Name: b.name,
// Namespace: "default",
// },
//}
//
//setupLog.Info("Creating localbuild resource")
//controllerutil.CreateOrUpdate(ctx, kubeClient, &localBuild, func() error {
// localBuild.Spec = v1alpha1.LocalbuildSpec{
// PackageConfigs: v1alpha1.PackageConfigsSpec{
// Argo: v1alpha1.ArgoPackageConfigSpec{
// Enabled: true,
// },
// EmbeddedArgoApplications: v1alpha1.EmbeddedArgoApplicationsPackageConfigSpec{
// Enabled: true,
// },
// },
// }
// return nil
//})

err = <-managerExit
close(managerExit)
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const (
gitCommitAuthorEmail = "[email protected]"
)

type ClientFN func(url string, options ...gitea.ClientOption) (GiteaClient, error)
type GiteaClientFunc 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
GiteaClientFN ClientFN
Recorder record.EventRecorder
Scheme *runtime.Scheme
GiteaClientFunc GiteaClientFunc
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

func getRepositoryName(repo v1alpha1.GitRepository) string {
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *RepositoryReconciler) postProcessReconcile(ctx context.Context, req ctr
func (r *RepositoryReconciler) reconcileGitRepo(ctx context.Context, repo *v1alpha1.GitRepository) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("reconciling", "name", repo.Name, "dir", repo.Spec.Source)
giteaClient, err := r.GiteaClientFN(repo.Spec.GiteaURL)
giteaClient, err := r.GiteaClientFunc(repo.Spec.GiteaURL)
if err != nil {
return ctrl.Result{Requeue: true, RequeueAfter: requeueTime}, fmt.Errorf("failed to get gitea client: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/gitrepository/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestGitRepositoryContentReconcile(t *testing.T) {
t.Run("files modified", func(t *testing.T) {
reconciler := RepositoryReconciler{
Client: fake.NewClientBuilder().Build(),
GiteaClientFN: func(url string, options ...gitea.ClientOption) (GiteaClient, error) {
GiteaClientFunc: func(url string, options ...gitea.ClientOption) (GiteaClient, error) {
return mockGitea{}, nil
},
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestGitRepositoryContentReconcileEmbedded(t *testing.T) {
t.Run("should sync embedded", func(t *testing.T) {
reconciler := RepositoryReconciler{
Client: fake.NewClientBuilder().Build(),
GiteaClientFN: func(url string, options ...gitea.ClientOption) (GiteaClient, error) {
GiteaClientFunc: func(url string, options ...gitea.ClientOption) (GiteaClient, error) {
return mockGitea{}, nil
},
}
Expand Down Expand Up @@ -343,8 +343,8 @@ func TestGitRepositoryReconcile(t *testing.T) {
v := cases[k]
t.Run(k, func(t *testing.T) {
reconciler := RepositoryReconciler{
Client: fake.NewClientBuilder().Build(),
GiteaClientFN: v.giteaClient,
Client: fake.NewClientBuilder().Build(),
GiteaClientFunc: v.giteaClient,
}
_, err := reconciler.reconcileGitRepo(ctx, &v.input)
if v.expect.err == nil && err != nil {
Expand Down
55 changes: 34 additions & 21 deletions pkg/controllers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package controllers
import (
"context"

"github.com/cnoe-io/idpbuilder/pkg/apps"
"github.com/cnoe-io/idpbuilder/pkg/controllers/gitserver"
"github.com/cnoe-io/idpbuilder/pkg/controllers/localbuild"
"github.com/cnoe-io/idpbuilder/pkg/controllers/gitrepository"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
Expand All @@ -14,27 +13,41 @@ func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error,
log := log.FromContext(ctx)

// Run Localbuild controller
if err := (&localbuild.LocalbuildReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CancelFunc: ctxCancel,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create localbuild controller")
return err
}
//if err := (&localbuild.LocalbuildReconciler{
// Client: mgr.GetClient(),
// Scheme: mgr.GetScheme(),
// CancelFunc: ctxCancel,
//}).SetupWithManager(mgr); err != nil {
// log.Error(err, "unable to create localbuild controller")
// return err
//}

// Run GitServer controller
appsFS, err := apps.GetAppsFS()
//appsFS, err := apps.GetAppsFS()
//if err != nil {
// log.Error(err, "unable to find srv dir in apps fs")
// return err
//}
//if err := (&gitserver.GitServerReconciler{
// Client: mgr.GetClient(),
// Scheme: mgr.GetScheme(),
// Content: appsFS,
//}).SetupWithManager(mgr); err != nil {
// log.Error(err, "unable to create gitserver controller")
// return err
//}

repoEventCh := make(chan event.GenericEvent, 1)
defer close(repoEventCh)

err := (&gitrepository.RepositoryReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
GiteaClientFunc: gitrepository.NewGiteaClient,
Recorder: mgr.GetEventRecorderFor("gitrepo-controller"),
}).SetupWithManager(mgr, repoEventCh)
if err != nil {
log.Error(err, "unable to find srv dir in apps fs")
return err
}
if err := (&gitserver.GitServerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Content: appsFS,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create gitserver controller")
log.Error(err, "unable to create git repository controller")
return err
}

Expand Down

0 comments on commit cd01ed4

Please sign in to comment.