Skip to content

Commit

Permalink
Add cost-tracker (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkupferer authored Jun 29, 2022
1 parent 556012e commit e454114
Show file tree
Hide file tree
Showing 24 changed files with 948 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/cost-tracker-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
name: cost-tracker-publish
on:
push:
branches-ignore:
- '*'
tags:
- 'cost-tracker-v[0-9]*'
jobs:
publish:
env:
IMAGE_NAME: babylon-cost-tracker
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Get image tags
id: image_tags
run: |
# Version is a semantic version tag or semantic version with release number
# GITHUB_REF will be of the form "refs/tags/admin-v0.1.2" or "refs/tags/admin-v0.1.2-1"
# To determine RELEASE, strip off the leading "refs/tags/"
RELEASE=${GITHUB_REF#refs/tags/cost-tracker-}
# To determine VERSION, strip off any release number suffix
VERSION=${RELEASE/-*/}
echo "::set-output name=RELEASE::${RELEASE}"
echo "::set-output name=VERSION::${VERSION}"
# Only build image if version tag without release number
# Releases indicate a change in the repository that should not trigger a new build.
if [[ "${VERSION}" == "${RELEASE}" ]]; then
# Publish to latest, minor, and patch tags
# Ex: latest,v0.1.2,v0.1
IMAGE_TAGS=(
'${{ secrets.REGISTRY_URI }}/${{ secrets.GPTE_REGISTRY_REPOSITORY }}/${{ env.IMAGE_NAME }}:latest'
"${{ secrets.REGISTRY_URI }}/${{ secrets.GPTE_REGISTRY_REPOSITORY }}/${{ env.IMAGE_NAME }}:${VERSION%.*}"
"${{ secrets.REGISTRY_URI }}/${{ secrets.GPTE_REGISTRY_REPOSITORY }}/${{ env.IMAGE_NAME }}:${VERSION}"
)
# Set IMAGE_TAGS output for use in next step
( IFS=$','; echo "::set-output name=IMAGE_TAGS::${IMAGE_TAGS[*]}" )
fi
- name: Set up buildx
uses: docker/setup-buildx-action@v1
if: steps.image_tags.outputs.IMAGE_TAGS

- name: Login to image registry
uses: docker/login-action@v1
if: steps.image_tags.outputs.IMAGE_TAGS
with:
registry: ${{ secrets.REGISTRY_URI }}
username: ${{ secrets.GPTE_REGISTRY_USERNAME }}
password: ${{ secrets.GPTE_REGISTRY_PASSWORD }}

- name: Build and publish image
uses: docker/build-push-action@v2
if: steps.image_tags.outputs.IMAGE_TAGS
with:
context: cost-tracker
file: cost-tracker/Dockerfile
push: true
tags: ${{ steps.image_tags.outputs.IMAGE_TAGS }}
3 changes: 3 additions & 0 deletions cost-tracker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

.odo/env
.odo/odo-file-index.json
134 changes: 134 additions & 0 deletions cost-tracker/Development.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Babylon Cost Tracker Development

Development can be performed in the `odo` OpenShift developer CLI or building with OpenShift build configs.
An OpenShift cluster with cluster-admin is required for `odo` development.
https://developers.redhat.com/products/codeready-containers/overview[CodeReady Containers] is recommended for local development.
An Ansible test suite is available for functional testing.

## Development with `odo`

Use of `odo` is recommended for fast iterative development.
`odo` simplifies the build/deploy process and avoids creating unnecessary build artifacts during the development process.

. Install the `odo` developer CLI as described in the OpenShift documentation:
https://docs.openshift.com/container-platform/latest/cli_reference/developer_cli_odo/installing-odo.html[Installing odo]

. Create a project for development using `odo`:
+
---------------------------------------
odo project create babylon-cost-tracker-dev
---------------------------------------

. Create Babylon cost-tracker resources from the provided helm chart:
+
-------------------------------
helm template helm \
--set deploy=false \
--set nameOverride=babylon-cost-tracker-dev \
--set namespace.name=$(oc project --short) \
| oc apply -f -
-------------------------------
+
NOTE: Password `p4ssw0rd` is the default redis password value in `devfile.yaml`.

. Grant privileges for cluster role `babylon-cost-tracker` to default service account:
+
----------------------------------------------------------------------
oc adm policy add-cluster-role-to-user babylon-cost-tracker-dev -z default
----------------------------------------------------------------------

. Setup `odo` from the provided `devfile.yaml`:
+
---------------------------------
odo create --devfile devfile.yaml
---------------------------------

. Set `AWS_SANDBOX_MANAGER_ACCESS_KEY_ID` and `AWS_SANDBOX_MANAGER_SECRET_ACCESS_KEY` values for `odo` deployment:
+
---------------------------------------------------
odo config set --env AWS_SANDBOX_MANAGER_ACCESS_KEY_ID="..."
odo config set --env AWS_SANDBOX_MANAGER_SECRET_ACCESS_KEY="..."
---------------------------------------------------
+
NOTE: Do not check the updated `devfile.yaml` into GitHub!

. Use `odo push` to push code into the odo container:
+
--------
odo push
--------

. Cleanup
+
Remove `odo` component
+
---------------------------------------
odo delete --force babylon-cost-tracker-dev
---------------------------------------
+
Remove the cluster-reader cluster role binding
+
---------------------------------------------------------------------------
oc adm policy remove-cluster-role-from-user babylon-cost-tracker-dev -z default
---------------------------------------------------------------------------
+
Remove resources created from the helm template
+
-------------------------------
helm template helm \
--set deploy=false \
--set nameOverride=babylon-cost-tracker-dev \
--set namespace.name=$(oc project --short) \
| oc delete -f -
-------------------------------


## Development OpenShift Build

The OpenShift build process is a bit slower for development but has the advantage of being a bit closer to a normal deployment of the babylon-cost-tracker.
It is often best to iterate development using `odo` and also test with an OpenShift build and deployment prior to opening a pull request.

. Create a project for development using `oc`:
+
---------------------------------------
oc new-project babylon-cost-tracker-dev
---------------------------------------

. Process OpenShift build template to create BuildConfig and ImageStream:
+
---------------------------------------------------------
oc process --local -f build-template.yaml | oc apply -f -
---------------------------------------------------------

. Build babylon-cost-tracker image from local source:
+
----------------------------------------------------------
oc start-build babylon-cost-tracker --from-dir=.. --follow
----------------------------------------------------------

. Deploy Poolboy from build image
+
--------------------------------------------------------------------------------
helm template helm \
--set nameOverride=babylon-cost-tracker-dev \
--set namespace.create=false \
--set=image.override="$(oc get imagestream babylon-cost-tracker -o jsonpath='{.status.tags[?(@.tag=="latest")].items[0].dockerImageReference}')" \
| oc apply -f -
--------------------------------------------------------------------------------
. Cleanup
+
Remove resources created from the helm template:
+
---------------------------------------------
helm template helm \
--set nameOverride=babylon-cost-tracker-dev \
--set namespace.create=false \
| oc delete -f -
---------------------------------------------
+
Remove BuildConfig and ImageStream:
+
----------------------------------------------------------
oc process --local -f build-template.yaml | oc delete -f -
----------------------------------------------------------
18 changes: 18 additions & 0 deletions cost-tracker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM quay.io/redhat-cop/python-kopf-s2i:v1.35

USER root

COPY . /tmp/src

RUN dnf install -y @ruby && \
gem install asciidoctor && \
rm -rf /tmp/src/.git* && \
chown -R 1001 /tmp/src && \
chgrp -R 0 /tmp/src && \
chmod -R g+w /tmp/src

USER 1001

RUN /usr/libexec/s2i/assemble

CMD ["/usr/libexec/s2i/run"]
51 changes: 51 additions & 0 deletions cost-tracker/build-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
apiVersion: template.openshift.io/v1
kind: Template
metadata:
annotations:
description: babylon-cost-tracker deploy
name: babylon-cost-tracker-build

parameters:
- name: NAME
value: babylon-cost-tracker
- name: GIT_REPO
value: https://github.com/redhat-cop/babylon.git
- name: GIT_REF
value: main
- name: KOPF_S2I_IMAGE
value: quay.io/redhat-cop/python-kopf-s2i:v1.35

objects:
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
name: ${NAME}
spec:
lookupPolicy:
local: false

- apiVersion: v1
kind: BuildConfig
metadata:
name: ${NAME}
spec:
output:
to:
kind: ImageStreamTag
name: ${NAME}:latest
postCommit: {}
resources: {}
runPolicy: Serial
source:
contextDir: cost-tracker
git:
uri: ${GIT_REPO}
ref: ${GIT_REF}
strategy:
type: Docker
dockerStrategy:
from:
kind: DockerImage
name: ${KOPF_S2I_IMAGE}
triggers: []
30 changes: 30 additions & 0 deletions cost-tracker/devfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
commands:
- exec:
commandLine: /usr/libexec/s2i/assemble
component: s2i-builder
group:
isDefault: true
kind: build
hotReloadCapable: false
workingDir: ${PROJECT_SOURCE}
id: s2i-assemble
- exec:
commandLine: /usr/libexec/s2i/run
component: s2i-builder
group:
isDefault: true
kind: run
hotReloadCapable: false
workingDir: ${PROJECT_SOURCE}
id: s2i-run
components:
- container:
dedicatedPod: false
image: quay.io/redhat-cop/python-kopf-s2i
mountSources: true
sourceMapping: /tmp/projects
name: s2i-builder
metadata:
name: babylon-cost-tracker
version: 1.0.0
schemaVersion: 2.0.0
6 changes: 6 additions & 0 deletions cost-tracker/helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: babylon-cost-tracker
description: A Helm chart for the babylon cost tracker component.
type: application
version: 0.1.0
appVersion: 0.1.0
Loading

0 comments on commit e454114

Please sign in to comment.