This repository contains the code to build a Gogs Operator for Red Hat OpenShift Container Platform. It will not run on Kubernetes because it uses the Route
object to provide access to Gogs.
The Operator will create a PostgreSQL database with persistent storage and a Gogs Server also with persistent storage connected to the PostgreSQL database.
It is implemented on top of the Red Hat Operator SDK - in particular the Ansible Operator.
There is a script build.sh
which will download the prerequisite Ansible roles from https://github.com/redhat-gpte-devopsautomation/ansible-operator-roles and install the required roles. The script will then build the container image.
Before running the script make sure to update the location of the container image to a repository you have access to. If you decide to build your own also make sure to update deploy/operator.yaml with the updated container image location.
The installation of the Custom Resource Definition and Cluster Role requires cluster-admin privileges. After that regular users with admin
privileges on their projects (which is automatically granted to the user who creates a project) can provision the Gogs Operator in their projects and deploy instances of Gogs using the gogs.gpte.opentlc.com Custom Resource.
Perform the following tasks as cluster-admin:
-
Deploy the Custom Resource Definition:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/crds/gpte_v1alpha1_gogs_crd.yaml
-
Deploy the
gogs-admin-rules
Cluster Role (which will be granted automatically to everyone withadmin
privileges). This role allows every project admin to create Gogs custom resources:oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/clusterrole_admin.yaml
Perform the following tasks as cluster-admin:
-
Create the Cluster Role for the Operator
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/clusterrole.yaml
-
Create a project for the operator to run in (different from where the application will be running)
oc new-project gpte-operators --display-name="GPTE Operators"
-
Deploy the gogs-operator service account:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/service_account.yaml
-
Grant the gogs-cluster-operator role to the gogs-operator service account (if your project is not
gpte-operators
change the project name in the command):oc adm policy add-cluster-role-to-user gogs-cluster-operator system:serviceaccount:gpte-operators:gogs-operator
-
And finally create the Gogs Operator:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/cluster_operator.yaml
-
Once the Operator pod is running the Operator is ready to start creating Gogs Servers.
The next steps work either as cluster-admin or as a regular user.
-
Create a new project in which to deploy Gogs:
oc new-project gogs --display-name "Gogs Server"
-
Deploy the gogs-operator service account:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/service_account.yaml
-
Deploy the gogs-operator role:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/role.yaml
-
Grant the gogs-operator role to the gogs-operator service account:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/rolebinding.yaml
-
And finally create the Gogs Operator:
oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/operator.yaml
-
Once the Operator pod is running the Operator is ready to start creating Gogs Servers.
The Gogs Server is deployed by creating a Custom Resource based on the gogs Custom Resource Definition. There is an example of a Gogs CR at https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/crds/gpte_v1alpha1_gogs_cr.yaml.
The Gogs Operator understands the following settings under it’s spec setting:
-
postgresqlVolumeSize: The size of the Persistent Volume to be created for the PostgreSQL database (e.g. 4Gi)
-
gogsVolumeSize: The size of the Persistent Volume to be created for the Gogs database (e.g. 4Gi)
-
gogsSsl: Weather the created route should be a HTTP (false) or HTTPS (true) route.
-
gogsServiceName: Optional. The name of the gogs service. The default is "gogs-metadata.name" where metadata.name is the name of the CR object (in the example below
gogs-server
). When specifying this parameter ensure that the service name does not already exist in the project/namespace.
apiVersion: gpte.opentlc.com/v1alpha1
kind: Gogs
metadata:
name: gogs-server
spec:
postgresqlVolumeSize: 4Gi
gogsVolumeSize: 4Gi
gogsSsl: True
gogsServiceName: mygogs
-
Write the definition to a file (e.g. gogs-server.yaml) and then create the Gogs instance:
oc create -f ./gogs-server.yaml
-
The operator will first create the PostgreSQL database, wait until it is up and running and then create the Gogs Server.
-
Validate that both pods (postgresql and gogs) are running before proceeding.
-
You can validate the existence of your Gogs Server by querying for gogs objects:
oc get gogs
-
Get the Route for the Gogs Server (the PostgreSQL database is not accessible outside of the project):
oc get route
-
Use the hostname returned in your Web Browser to open the Gogs UI.
Deleting a gogs server and its associated resources is as simple as deleting the gogs object. If you created a gogs server called gogs-server
as in the example above it suffices to run the delete command on that resource:
oc delete gogs gogs-server
The Operator adds ownerReference fields to all created objects - which means that deleting the Gogs object also deletes all objects that have been created by the Operator.
In case you wish to uninstall the Gogs Operator make sure that there are no more Gogs instances running. Once all Gogs instances have been deleted simply delete the project the operator is running in.
oc delete project gogs
Then as cluster-admin delete the ClusterRole and Custom Resource:
oc delete clusterrole gogs-admin-rules
oc delete crd gogs.gogs.opentlc.com