wkulhanek / gogs-operator

Gogs Operator (based on Ansible Operators)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gogs Operator

Overview

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.

Building the Operator Image

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.

Installation

Common Installation Steps

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:

  1. Deploy the Custom Resource Definition:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/crds/gpte_v1alpha1_gogs_crd.yaml
  2. Deploy the gogs-admin-rules Cluster Role (which will be granted automatically to everyone with admin 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

Clusterwide Installation

Perform the following tasks as cluster-admin:

  1. Create the Cluster Role for the Operator

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/clusterrole.yaml
  2. 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"
  3. Deploy the gogs-operator service account:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/service_account.yaml
  4. 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
  5. And finally create the Gogs Operator:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/cluster_operator.yaml
  6. Once the Operator pod is running the Operator is ready to start creating Gogs Servers.

Local Installation in a Project

The next steps work either as cluster-admin or as a regular user.

  1. Create a new project in which to deploy Gogs:

    oc new-project gogs --display-name "Gogs Server"
  2. Deploy the gogs-operator service account:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/service_account.yaml
  3. Deploy the gogs-operator role:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/role.yaml
  4. 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
  5. And finally create the Gogs Operator:

    oc create -f https://raw.githubusercontent.com/wkulhanek/gogs-operator/master/deploy/operator.yaml
  6. Once the Operator pod is running the Operator is ready to start creating Gogs Servers.

Deploying a Gogs Server using the Operator

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.

Example
apiVersion: gpte.opentlc.com/v1alpha1
kind: Gogs
metadata:
  name: gogs-server
spec:
  postgresqlVolumeSize: 4Gi
  gogsVolumeSize: 4Gi
  gogsSsl: True
  gogsServiceName: mygogs
  1. Write the definition to a file (e.g. gogs-server.yaml) and then create the Gogs instance:

    oc create -f ./gogs-server.yaml
  2. The operator will first create the PostgreSQL database, wait until it is up and running and then create the Gogs Server.

  3. Validate that both pods (postgresql and gogs) are running before proceeding.

  4. You can validate the existence of your Gogs Server by querying for gogs objects:

    oc get gogs
  5. Get the Route for the Gogs Server (the PostgreSQL database is not accessible outside of the project):

    oc get route
  6. Use the hostname returned in your Web Browser to open the Gogs UI.

Deleting a Gogs Server

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.

Uninstalling the Gogs 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

About

Gogs Operator (based on Ansible Operators)


Languages

Language:Shell 72.2%Language:Dockerfile 27.8%