From 88e9d64a7d0411c5394ad0822ed717b9f6107f10 Mon Sep 17 00:00:00 2001 From: Nima Kaviani Date: Tue, 14 Nov 2023 23:00:18 -0800 Subject: [PATCH] add gitea server installation and config --- globals/project.go | 10 ++++++++ pkg/build/build.go | 3 ++- pkg/controllers/localbuild/controller.go | 31 ++++++++++++++++++------ pkg/controllers/localbuild/gitea.go | 18 +++++++------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/globals/project.go b/globals/project.go index 2c375823..4a63c761 100644 --- a/globals/project.go +++ b/globals/project.go @@ -3,7 +3,17 @@ package globals import "fmt" const ProjectName string = "idpbuilder" +const giteaResourceName string = "gitea" +const gitServerResourceName string = "gitserver" func GetProjectNamespace(name string) string { return fmt.Sprintf("%s-%s", ProjectName, name) } + +func GiteaResourceName() string { + return giteaResourceName +} + +func GitServerResourcename() string { + return gitServerResourceName +} diff --git a/pkg/build/build.go b/pkg/build/build.go index 7044792b..2b65b549 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -4,6 +4,7 @@ import ( "context" "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" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -155,7 +156,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { Enabled: true, }, GitConfig: v1alpha1.GitConfigSpec{ - Type: "gitea", + Type: globals.GiteaResourceName(), }, }, } diff --git a/pkg/controllers/localbuild/controller.go b/pkg/controllers/localbuild/controller.go index 2c74f008..1e38cc1e 100644 --- a/pkg/controllers/localbuild/controller.go +++ b/pkg/controllers/localbuild/controller.go @@ -18,12 +18,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" ) const ( defaultArgoCDProjectName string = "default" EmbeddedGitServerName string = "embedded" - gitServerResourceName string = "gitserver" gitServerDeploymentContainerName string = "httpd" gitServerIngressHostnameBase string = ".cnoe.localtest.me" repoUrlFmt string = "http://%s.%s.svc/idpbuilder-resources.git" @@ -40,7 +40,7 @@ func ingressHostname(resource *v1alpha1.GitServer) string { } func managedResourceName(resource *v1alpha1.GitServer) string { - return fmt.Sprintf("%s-%s", gitServerResourceName, resource.Name) + return fmt.Sprintf("%s-%s", globals.GitServerResourcename(), resource.Name) } type LocalbuildReconciler struct { @@ -127,7 +127,7 @@ func (r *LocalbuildReconciler) ReconcileProjectNamespace(ctx context.Context, re func (r *LocalbuildReconciler) ReconcileEmbeddedGitServer(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) { log := log.FromContext(ctx) - if resource.Spec.PackageConfigs.GitConfig.Type != gitServerResourceName { + if resource.Spec.PackageConfigs.GitConfig.Type != globals.GitServerResourcename() { log.Info("GitServer installation disabled, skipping") return ctrl.Result{}, nil } @@ -203,6 +203,26 @@ func (r *LocalbuildReconciler) ReconcileArgoApps(ctx context.Context, req ctrl.R } } + switch resource.Spec.PackageConfigs.GitConfig.Type { + // do the GitServer stuff where localbuild's GitConfig is set to gitserver + case globals.GitServerResourcename(): + result, err := r.installArgoApps(ctx, resource) + r.shouldShutdown = true + return result, err + + case globals.GiteaResourceName(): + println("do Gitea Stuff") + + default: + log.Error(fmt.Errorf("bad GitConfig value"), resource.Spec.PackageConfigs.GitConfig.Type) + } + + return ctrl.Result{}, nil +} + +func (r *LocalbuildReconciler) installArgoApps(ctx context.Context, resource *v1alpha1.Localbuild) (reconcile.Result, error) { + log := log.FromContext(ctx) + foundGitServer := &v1alpha1.GitServer{ ObjectMeta: metav1.ObjectMeta{ Name: EmbeddedGitServerName, @@ -221,7 +241,6 @@ func (r *LocalbuildReconciler) ReconcileArgoApps(ctx context.Context, req ctrl.R return ctrl.Result{}, nil } - // Install Argo Apps for _, embedApp := range apps.EmbedApps { log.Info("Ensuring Argo Application", "name", embedApp.Name) app := &argov1alpha1.Application{ @@ -257,9 +276,7 @@ func (r *LocalbuildReconciler) ReconcileArgoApps(ctx context.Context, req ctrl.R } resource.Status.ArgoAppsCreated = true - r.shouldShutdown = true - - return ctrl.Result{}, nil + return reconcile.Result{}, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/pkg/controllers/localbuild/gitea.go b/pkg/controllers/localbuild/gitea.go index d9342c26..82281283 100644 --- a/pkg/controllers/localbuild/gitea.go +++ b/pkg/controllers/localbuild/gitea.go @@ -7,6 +7,7 @@ import ( "time" "github.com/cnoe-io/idpbuilder/api/v1alpha1" + "github.com/cnoe-io/idpbuilder/globals" "github.com/cnoe-io/idpbuilder/pkg/k8s" "github.com/cnoe-io/idpbuilder/pkg/util" appsv1 "k8s.io/api/apps/v1" @@ -25,11 +26,10 @@ var installGiteaFS embed.FS var timeout = time.After(5 * time.Minute) const ( - giteaServerName string = "my-gitea" + giteaServerName = "my-gitea" + giteaNamespace = "gitea" ) -const giteaResourceName = "gitea" - func GetRawGiteaInstallResources() ([][]byte, error) { return util.ConvertFSToBytes(installGiteaFS, "resources/gitea/k8s") } @@ -46,7 +46,7 @@ func GetK8sGiteaInstallResources(scheme *runtime.Scheme) ([]client.Object, error func newGiteaNamespace() *corev1.Namespace { return &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: "gitea", + Name: giteaNamespace, }, } } @@ -54,13 +54,13 @@ func newGiteaNamespace() *corev1.Namespace { func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) { log := log.FromContext(ctx) - if resource.Spec.PackageConfigs.GitConfig.Type != giteaResourceName { + if resource.Spec.PackageConfigs.GitConfig.Type != globals.GiteaResourceName() { log.Info("Gitea installation disabled, skipping") return ctrl.Result{}, nil } // Install Gitea - giteansClient := client.NewNamespacedClient(r.Client, "gitea") + giteansClient := client.NewNamespacedClient(r.Client, giteaNamespace) installObjs, err := GetK8sGiteaInstallResources(r.Scheme) if err != nil { return ctrl.Result{}, err @@ -68,7 +68,7 @@ func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Requ // Ensure namespace exists giteaNS := newGiteaNamespace() - if err = r.Client.Get(ctx, types.NamespacedName{Name: "gitea"}, giteaNS); err != nil { + if err = r.Client.Get(ctx, types.NamespacedName{Name: giteaNamespace}, giteaNS); err != nil { // We got an error so try creating the NS if err = r.Client.Create(ctx, giteaNS); err != nil { return ctrl.Result{}, err @@ -91,7 +91,7 @@ func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Requ } // Create object - if err = k8s.EnsureObject(ctx, giteansClient, obj, "gitea"); err != nil { + if err = k8s.EnsureObject(ctx, giteansClient, obj, giteaNamespace); err != nil { return ctrl.Result{}, err } } @@ -104,7 +104,7 @@ func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Requ if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" { switch obj.GetName() { case giteaServerName: - gotObj, err := k8s.GetObject(ctx, giteansClient, obj, "gitea") + gotObj, err := k8s.GetObject(ctx, giteansClient, obj, giteaNamespace) if err != nil { ready <- err return