Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install gitea #39

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
nimakaviani marked this conversation as resolved.
Show resolved Hide resolved
}

type PackageConfigsSpec struct {
GitConfig GitConfigSpec `json:"gitConfig,omitempty"`
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
}
Expand All @@ -37,6 +44,7 @@ type LocalbuildStatus struct {
GitServerAvailable bool `json:"gitServerAvailable,omitempty"`
ArgoAvailable bool `json:"argoAvailable,omitempty"`
NginxAvailable bool `json:"nginxAvailable,omitempty"`
GiteaAvailable bool `json:"giteaAvailable,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.

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
}
5 changes: 5 additions & 0 deletions 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 @@ -154,6 +155,10 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
EmbeddedArgoApplications: v1alpha1.EmbeddedArgoApplicationsPackageConfigSpec{
Enabled: true,
},
GitConfig: v1alpha1.GitConfigSpec{
// hint: for the old behavior, replace Type value below with globals.GitServerResourcename()
Type: globals.GiteaResourceName(),
},
},
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/gitserver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

const (
gitServerResourceName string = "gitserver"
gitServerDeploymentContainerName string = "httpd"
gitServerIngressHostnameBase string = ".cnoe.localtest.me"
repoUrlFmt string = "http://%s.%s.svc/idpbuilder-resources.git"
Expand All @@ -38,7 +37,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 subReconciler func(ctx context.Context, req ctrl.Request, resource *v1alpha1.GitServer) (ctrl.Result, error)
Expand Down
114 changes: 28 additions & 86 deletions pkg/controllers/localbuild/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,47 @@ import (
"embed"

"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"
"k8s.io/apimachinery/pkg/runtime/schema"
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/argo/*
var installFS embed.FS
var installArgoFS embed.FS

const (
argoApplicationControllerName string = "argocd-application-controller"
argoServerName string = "argocd-server"
argoRepoServerName string = "argocd-repo-server"
argocdNamespace string = "argocd"
)

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

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

return k8s.ConvertRawResourcesToObjects(scheme, rawResources)
}

func newArgoNamespace() *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "argocd",
func (r LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
nimakaviani marked this conversation as resolved.
Show resolved Hide resolved
argocd := EmbeddedInstallation{
name: "Argo CD",
resourcePath: "resources/argo",
resourceFS: installArgoFS,
namespace: argocdNamespace,
expectedResources: map[string]schema.GroupVersionKind{
"argocd-server": {
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
"argocd-repo-server": {
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
"argocd-application-controller": {
Group: "apps",
Version: "v1",
Kind: "StatefulSet",
},
},
}
}

func (r *LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
log := log.FromContext(ctx)

if !resource.Spec.PackageConfigs.Argo.Enabled {
log.Info("Argo installation disabled, skipping")
return ctrl.Result{}, nil
}

// Install Argo
argonsClient := client.NewNamespacedClient(r.Client, "argocd")
installObjs, err := GetK8sInstallResources(r.Scheme)
if err != nil {
return ctrl.Result{}, err
skipReadinessCheck: true,
}

// Ensure namespace exists
argocdNs := newArgoNamespace()
if err = r.Client.Get(ctx, types.NamespacedName{Name: "argocd"}, argocdNs); err != nil {
// We got an error so try creating the NS
if err = r.Client.Create(ctx, argocdNs); err != nil {
return ctrl.Result{}, err
}
if result, err := argocd.Install(ctx, req, resource, r.Client, r.Scheme); err != nil {
return result, err
}

log.Info("Installing argo resources")
for _, obj := range installObjs {
// Find the objects we need to track and own for status updates
if obj.GetObjectKind().GroupVersionKind().Kind == "StatefulSet" && obj.GetName() == argoApplicationControllerName {
if err = controllerutil.SetControllerReference(resource, obj, r.Scheme); err != nil {
log.Error(err, "Setting controller reference for Argo application controller")
return ctrl.Result{}, err
}
} else if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
switch obj.GetName() {
case argoServerName:
fallthrough
case argoRepoServerName:
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 Argo deployment", "deployment", obj)
return ctrl.Result{}, err
}
}
}
}

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

// Set Argo available.
// TODO(greghaynes) This should actually wait for status of some resources
resource.Status.ArgoAvailable = true

return ctrl.Result{}, nil
}
12 changes: 10 additions & 2 deletions pkg/controllers/localbuild/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

func TestGetRawInstallResources(t *testing.T) {
resources, err := GetRawInstallResources()
e := EmbeddedInstallation{
resourceFS: installArgoFS,
resourcePath: "resources/argo",
}
resources, err := e.rawInstallResources()
if err != nil {
t.Fatalf("GetRawInstallResources() error: %v", err)
}
Expand All @@ -23,7 +27,11 @@ func TestGetRawInstallResources(t *testing.T) {
}

func TestGetK8sInstallResources(t *testing.T) {
objs, err := GetK8sInstallResources(k8s.GetScheme())
e := EmbeddedInstallation{
resourceFS: installArgoFS,
resourcePath: "resources/argo",
}
objs, err := e.installResources(k8s.GetScheme())
if err != nil {
t.Fatalf("GetK8sInstallResources() error: %v", err)
}
Expand Down
33 changes: 27 additions & 6 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
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 +39,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 All @@ -67,12 +66,26 @@ func (r *LocalbuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Make sure we post process
defer r.postProcessReconcile(ctx, req, &localBuild)

// respecting order of installation matters as there are hard dependencies
subReconcilers := []subReconciler{
r.ReconcileProjectNamespace,
r.ReconcileArgo,
r.ReconcileNginx,
r.ReconcileEmbeddedGitServer,
r.ReconcileArgoApps,
r.ReconcileArgo,
}

switch localBuild.Spec.PackageConfigs.GitConfig.Type {
case globals.GitServerResourcename():
subReconcilers = append(
subReconcilers,
[]subReconciler{r.ReconcileEmbeddedGitServer, r.ReconcileArgoAppsWithGitServer}...,
)
case globals.GiteaResourceName():
subReconcilers = append(
subReconcilers,
[]subReconciler{r.ReconcileGitea, r.ReconcileArgoAppsWithGitea}...,
)
default:
return ctrl.Result{}, fmt.Errorf("GitConfig %s is invalid for LocalBuild %s", localBuild.Spec.PackageConfigs.GitConfig.Type, localBuild.GetName())
}

for _, sub := range subReconcilers {
Expand Down Expand Up @@ -171,7 +184,7 @@ func (r *LocalbuildReconciler) ReconcileEmbeddedGitServer(ctx context.Context, r
return ctrl.Result{}, err
}

func (r *LocalbuildReconciler) ReconcileArgoApps(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitServer(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
log := log.FromContext(ctx)

// Bail if embedded argo applications not enabled
Expand Down Expand Up @@ -263,3 +276,11 @@ func (r *LocalbuildReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&v1alpha1.Localbuild{}).
Complete(r)
}

func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitea(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
log := log.FromContext(ctx)

log.Info("TODO(nimak): enable installing Argo Apps")
r.shouldShutdown = true
return ctrl.Result{}, nil
}
40 changes: 40 additions & 0 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package localbuild

import (
"context"
"embed"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
)

const (
giteaNamespace = "gitea"
)

//go:embed resources/gitea/k8s/*
var installGiteaFS embed.FS

func (r LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) {
nimakaviani marked this conversation as resolved.
Show resolved Hide resolved
gitea := EmbeddedInstallation{
name: "Gitea",
resourcePath: "resources/gitea/k8s",
resourceFS: installGiteaFS,
namespace: giteaNamespace,
expectedResources: map[string]schema.GroupVersionKind{
"my-gitea": {
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
},
}

if result, err := gitea.Install(ctx, req, resource, r.Client, r.Scheme); err != nil {
return result, err
}

resource.Status.GiteaAvailable = true
return ctrl.Result{}, nil
}
Loading