Skip to content

Commit

Permalink
wip - install gitea
Browse files Browse the repository at this point in the history
fixes 32
  • Loading branch information
nimakaviani committed Oct 18, 2023
1 parent fd507f5 commit b4405df
Show file tree
Hide file tree
Showing 9 changed files with 742 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/localbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ type EmbeddedArgoApplicationsPackageConfigSpec struct {
Enabled bool `json:"enabled,omitempty"`
}

// GitConfigSpec controls what git server to use for the idpbuilder
// It can take on the values of either gitea or gitserver
type GitConfigSpec struct {
Type string `json:"type,omitempty"`
}

type PackageConfigsSpec struct {
GitConfig GitConfigSpec `json:"gitConfig,omitempty"`
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
}
Expand All @@ -35,6 +42,7 @@ type LocalbuildStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

GitServerAvailable bool `json:"gitServerAvailable,omitempty"`
GiteaAvailable bool `json:"giteaAvailable,omitempty"`
ArgoAvailable bool `json:"argoAvailable,omitempty"`
ArgoAppsCreated bool `json:"argoAppsCreated,omitempty"`
}
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
EmbeddedArgoApplications: v1alpha1.EmbeddedArgoApplicationsPackageConfigSpec{
Enabled: true,
},
GitConfig: v1alpha1.GitConfigSpec{
Type: "gitea",
},
},
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/localbuild/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

//go:embed resources/argo/*
var installFS embed.FS
var installArgoFS embed.FS

const (
argoApplicationControllerName string = "argocd-application-controller"
Expand All @@ -28,7 +28,7 @@ const (
)

func GetRawInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(installFS, "resources/argo")
return util.ConvertFSToBytes(installArgoFS, "resources/argo")
}

func GetK8sInstallResources(scheme *runtime.Scheme) ([]client.Object, error) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (r *LocalbuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)
subReconcilers := []subReconciler{
r.ReconcileProjectNamespace,
r.ReconcileArgo,
r.ReconcileGitea,
r.ReconcileEmbeddedGitServer,
r.ReconcileArgoApps,
}
Expand Down Expand Up @@ -125,6 +126,11 @@ 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 {
log.Info("GitServer installation disabled, skipping")
return ctrl.Result{}, nil
}

// Bail if argo is not yet available
if !resource.Status.ArgoAvailable {
log.Info("argo not yet available, not installing embedded git server")
Expand Down
131 changes: 131 additions & 0 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package localbuild

import (
"context"
"embed"
"errors"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
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/log"
)

//go:embed resources/gitea/k8s/*
var installGiteaFS embed.FS
var timeout = time.After(30 * time.Second)

const (
giteaServerName string = "my-gitea"
)

const giteaResourceName = "gitea"

func GetRawGiteaInstallResources() ([][]byte, error) {
return util.ConvertFSToBytes(installGiteaFS, "resources/gitea/k8s")
}

func GetK8sGiteaInstallResources(scheme *runtime.Scheme) ([]client.Object, error) {
rawResources, err := GetRawGiteaInstallResources()
if err != nil {
return nil, err
}

return k8s.ConvertRawResourcesToObjects(scheme, rawResources)
}

func newGiteaNamespace() *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "gitea",
},
}
}

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 {
log.Info("Gitea installation disabled, skipping")
return ctrl.Result{}, nil
}

// Install Gitea
giteansClient := client.NewNamespacedClient(r.Client, "gitea")
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 {
// We got an error so try creating the NS
if err = r.Client.Create(ctx, giteaNS); err != nil {
return ctrl.Result{}, err
}
}

log.Info("Installing gitea resources")
for _, obj := range installObjs {
if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
switch obj.GetName() {
case giteaServerName:
gotObj := appsv1.Deployment{}
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, &gotObj); err != nil {
if err = controllerutil.SetControllerReference(resource, obj, r.Scheme); err != nil {
log.Error(err, "Setting controller reference for Gitea deployment", "deployment", obj)
return ctrl.Result{}, err
}
}
}
}

// Create object
if err = k8s.EnsureObject(ctx, giteansClient, obj, "gitea"); err != nil {
return ctrl.Result{}, err
}
}

// Wait for Gitea to become available
ready := make(chan bool)
go func() {
for {
for _, obj := range installObjs {
if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
switch obj.GetName() {
case giteaServerName:
gotObj := appsv1.Deployment{}
if gotObj.Status.AvailableReplicas >= 1 {
ready <- true
return
}
}
}
}

time.Sleep(1 * time.Second)
}
}()

select {
case <-timeout:
err := errors.New("Timeout")
log.Error(err, "Didn't reconcile Gitea on time.")
return ctrl.Result{}, err
case <-ready:
log.Info("Gitea is ready!")
resource.Status.GiteaAvailable = true
}

return ctrl.Result{}, nil
}
Loading

0 comments on commit b4405df

Please sign in to comment.