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

Feat/add operator #310

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
201 changes: 201 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
name: Build

on:
workflow_call:
inputs:
REGISTRY:
required: true
type: string
NAMESPACE:
required: true
type: string
MULTI_ARCH:
required: true
type: boolean
TAG:
required: true
type: string
MAJOR_TAG:
required: false
type: string
MINOR_TAG:
required: false
type: string
PATCH_TAG:
required: false
type: string
USE_QEMU:
required: true
type: boolean
workflow_dispatch:
inputs:
REGISTRY:
description: Target registry to push images
required: true
type: string
default: ghcr.io
NAMESPACE:
description: Target namespace to the given registry
required: true
type: string
default: cloud-pi-native/socle
MULTI_ARCH:
description: Build for both amd64 and arm64
required: true
type: boolean
default: false
USE_QEMU:
description: Use QEMU emulator for arm64
required: true
type: boolean
default: false

jobs:
matrix:
name: Generate matrix
runs-on: ubuntu-latest
outputs:
build-matrix: ${{ steps.build-matrix.outputs.BUILD_MATRIX }}
lower-branch: ${{ steps.lower-branch.outputs.LOWER_BRANCH }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4

- name: Generate matrix
id: build-matrix
run: |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix-docker.json)" >> $GITHUB_OUTPUT

- name: Get lowercase branch name
id: lower-branch
run: |
echo "LOWER_BRANCH=$(echo '${{ github.head_ref || github.ref_name }}' | sed 's/\//-/g' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

build:
name: Build application
runs-on: ${{ matrix.runners }}
needs:
- matrix
strategy:
matrix:
runners: ${{ inputs.MULTI_ARCH && !inputs.USE_QEMU && fromJson('["ubuntu-latest", "ARM64"]') || fromJson('["ubuntu-latest"]') }}
images: ${{ fromJSON(needs.matrix.outputs.build-matrix) }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4

- name: Get short SHA
id: short-sha
run: |
echo "SHORT_SHA=sha-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Set up Docker buildx
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ format('buildx-{0}-{1}-{2}-{3}', runner.os, runner.arch, matrix.images.name, hashFiles('operator/**,roles/**,filter_plugins/**,admin-tools/**,requirements.yml,install.yaml,uninstall.yaml')) }}
restore-keys: |
buildx-${{ runner.os }}-${{ runner.arch }}-${{ matrix.images.name }}-

- name: Set up QEMU (for multi platform build)
uses: docker/setup-qemu-action@v3
if: ${{ inputs.USE_QEMU }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
logout: true

- name: Build and push docker image
id: build
uses: docker/build-push-action@v6
with:
context: ${{ matrix.images.build.context }}
file: ${{ matrix.images.build.dockerfile }}
provenance: false
platforms: ${{ inputs.MULTI_ARCH && inputs.USE_QEMU && 'linux/amd64,linux/arm64' || (contains(runner.arch, 'ARM') && 'linux/arm64' || 'linux/amd64') }}
outputs: type=image,name=${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }},push-by-digest=true,name-canonical=true,push=true
build-args: |
APP_VERSION=${{ inputs.TAG || steps.short-sha.outputs.SHORT_SHA }}
# cache-from: type=gha
# cache-to: type=gha,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new

# Necessary to avoid forever growing cache
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

- name: Export digest
run: |
mkdir -p /tmp/digests/${{ matrix.images.name }}
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${{ matrix.images.name }}/${digest#sha256:}"

- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.images.name }}-${{ inputs.MULTI_ARCH && inputs.USE_QEMU && 'multiarch' || (contains(runner.arch, 'ARM') && 'arm64' || 'amd64') }}
path: /tmp/digests/${{ matrix.images.name }}/*
if-no-files-found: error
retention-days: 1

merge:
name: Merge digest
runs-on: ubuntu-latest
needs:
- matrix
- build
strategy:
matrix:
images: ${{ fromJSON(needs.matrix.outputs.build-matrix) }}
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
pattern: digests-${{ matrix.images.name }}-*
path: /tmp/digests/${{ matrix.images.name }}
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}
tags: |
type=raw,value=${{ needs.matrix.outputs.lower-branch }},enable=${{ github.head_ref != 'main' }}
type=raw,value=${{ inputs.TAG }},enable=${{ inputs.TAG != '' }}
type=raw,value=${{ inputs.MAJOR_TAG }}.${{ inputs.MINOR_TAG }},enable=${{ inputs.MAJOR_TAG != '' && inputs.MINOR_TAG != '' }}
type=raw,value=${{ inputs.MAJOR_TAG }},enable=${{ inputs.MAJOR_TAG != '' }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
logout: true

- name: Create manifest list and push
working-directory: /tmp/digests/${{ matrix.images.name }}
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}@sha256:%s ' *)

- name: Inspect image
run: |
docker buildx imagetools inspect ${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}:${{ steps.meta.outputs.version }}
64 changes: 59 additions & 5 deletions .github/workflows/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ on:
types:
- closed
workflow_dispatch:
inputs:
PR_NUMBER:
description: ID number of the pull request assiocited with the cache
required: false
type: number
BRANCH_NAME:
description: Branch name assiocited with the cache
required: false
type: string

jobs:
cleanup:
cleanup-cache:
name: Delete gituhb cache
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand All @@ -18,18 +28,62 @@ jobs:
gh extension install actions/gh-actions-cache

REPO=${{ github.repository }}
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
if [ -n "${{ inputs.BRANCH_NAME }}" ]; then
BRANCH="${{ inputs.BRANCH_NAME }}"
else
BRANCH="refs/pull/${{ github.event.pull_request.number || inputs.PR_NUMBER }}/merge"
fi

echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )

## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
for cacheKey in $cacheKeysForPR; do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

matrix:
name: Generate matrix
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.number || github.event.number }}
outputs:
build-matrix: ${{ steps.build-matrix.outputs.BUILD_MATRIX }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4

- name: Generate matrix
id: build-matrix
run: |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix-docker.json)" >> $GITHUB_OUTPUT

cleanup-image:
name: Delete image from ghcr.io
runs-on: ubuntu-latest
needs:
- matrix
strategy:
matrix:
images: ${{ fromJSON(needs.matrix.outputs.build-matrix) }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4

- name: Get repository owner and name
id: image-infos
run: |
echo "ORG_NAME=$(echo '${{ github.repository }}' | cut -d "/" -f 1)" >> $GITHUB_OUTPUT
echo "REPO_NAME=$(echo '${{ github.repository }}' | cut -d "/" -f 2)" >> $GITHUB_OUTPUT

- name: Delete ${{ matrix.images.name }} image
run: |
./ci/scripts/delete-image.sh \
-o "${{ steps.image-infos.outputs.ORG_NAME }}" \
-i "${{ steps.image-infos.outputs.REPO_NAME }}/${{ matrix.images.name }}" \
-t "pr-${{ github.event.pull_request.number || github.event.number }}" \
-g "${{ secrets.GITHUB_TOKEN }}"
30 changes: 29 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,36 @@ on:
env:
REGISTRY: ghcr.io
NAMESPACE: "${{ github.repository }}"
PLATFORM: "linux/amd64,linux/arm64"
MULTI_ARCH: true
USE_QEMU: false

jobs:
expose-vars:
runs-on: ubuntu-latest
outputs:
REGISTRY: ${{ env.REGISTRY }}
NAMESPACE: ${{ env.NAMESPACE }}
MULTI_ARCH: ${{ env.MULTI_ARCH }}
USE_QEMU: ${{ env.USE_QEMU }}
steps:
- name: Exposing env vars
run: echo "Exposing env vars"

release:
uses: ./.github/workflows/release.yml

build:
uses: ./.github/workflows/build.yml
if: ${{ needs.release.outputs.release-created == 'true' }}
needs:
- expose-vars
- release
with:
REGISTRY: ${{ needs.expose-vars.outputs.REGISTRY }}
NAMESPACE: ${{ needs.expose-vars.outputs.NAMESPACE }}
TAG: ${{ needs.release.outputs.major-tag }}.${{ needs.release.outputs.minor-tag }}.${{ needs.release.outputs.patch-tag }}
MAJOR_TAG: ${{ needs.release.outputs.major-tag }}
MINOR_TAG: ${{ needs.release.outputs.minor-tag }}
PATCH_TAG: ${{ needs.release.outputs.patch-tag }}
MULTI_ARCH: ${{ needs.expose-vars.outputs.MULTI_ARCH == 'true' }}
USE_QEMU: ${{ needs.expose-vars.outputs.USE_QEMU == 'true' }}
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Utilisation de vos propres values](#utilisation-de-vos-propres-values)
- [Installation](#installation)
- [Lancement](#lancement)
- [Lancement via l'opérateur](#lancement-via-lopérateur)
- [Déploiement de plusieurs forges DSO dans un même cluster](#déploiement-de-plusieurs-forges-dso-dans-un-même-cluster)
- [Récupération des secrets](#récupération-des-secrets)
- [Debug](#debug)
Expand Down Expand Up @@ -218,6 +219,30 @@ watch "kubectl get ns | grep 'dso-'"

Par défaut, ils sont en effet tous préfixés « dso- ».

### Lancement via l'opérateur

Le déploiement de la forge peut aussi se faire via un opérateur. Cela évite d'installer une stack ansible localement notamment mais cela permet aussi de faire des modifications de la forge en éditant l'objet DsoSocleConfig directement sans avoir à relancer Ansible.

Cette opérateur a été créé à l'aide de l'[operateur sdk ansible](https://sdk.operatorframework.io/docs/building-operators/ansible/).

Il est possible de déployer un cluster k8s local à l'aide de kind et mkcert qui vous permettra de tester le déploiement en local. Pour se faire il faut lancer le script présent dans samples :

```
./samples/start-kind.sh
```

Pour l'utiliser pour déployer votre forge, il faut déployer le crd, créer la configuration de votre forge et finalement déployer l'opérateur :

```
kubectl apply -f roles/socle-config/files/crd-conf-dso.yaml
kubectl apply -f samples/conf-dso.yaml
# Déploiement de l'opérateur avec l'image latest
make deploy
# Il est possible de customiser l'opérateur facilement
make docker-build docker-push IMG=localhost:5001/controller:demo
make deploy IMG=localhost:5001/controller:demo
```

### Déploiement de plusieurs forges DSO dans un même cluster

Suite à une première installation réussie et selon vos besoins, il est possible, selon sa capacité, d'installer dans un même cluster une ou plusieurs autres forges DSO, en parallèle de celle installée par défaut.
Expand Down
10 changes: 10 additions & 0 deletions ci/matrix-docker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"name": "operator",
"build": {
"context": "./",
"dockerfile": "./operator/Dockerfile",
"target": "prod"
}
}
]
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default antfu({
"yml/plain-scalar": "off",
},
},
ignores: ["./operator"],
ignores: ["operator/"],
})
Loading