Skip to content

Commit

Permalink
add gitea server installation and config
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakaviani committed Nov 15, 2023
1 parent 5061a75 commit 88e9d64
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
10 changes: 10 additions & 0 deletions globals/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -155,7 +156,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
Enabled: true,
},
GitConfig: v1alpha1.GitConfigSpec{
Type: "gitea",
Type: globals.GiteaResourceName(),
},
},
}
Expand Down
31 changes: 24 additions & 7 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand All @@ -46,29 +46,29 @@ func GetK8sGiteaInstallResources(scheme *runtime.Scheme) ([]client.Object, error
func newGiteaNamespace() *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "gitea",
Name: giteaNamespace,
},
}
}

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
}

// 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
Expand All @@ -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
}
}
Expand All @@ -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
Expand Down

0 comments on commit 88e9d64

Please sign in to comment.