From 4545d7abf1efa6b7779908566eccefd1f7d0dfbf Mon Sep 17 00:00:00 2001 From: oleg-vorobiov-suse Date: Mon, 9 Sep 2024 05:28:00 -0400 Subject: [PATCH 1/2] Feature/Replace Rancher API calls by kubectl calls (#2) * reduced the number of API calls, simplified the code * removed automation api token creation * reverted some changes, modified upadate serverurl function * removed one unneccessary parameter * awk to jsonpath * fixed waiting for nodes function * debuggind serverurl * serverurl --------- Co-authored-by: olegvorobiov Co-authored-by: Bertrand Thomas --- scripts/rancher/cluster-actions.sh | 184 ++++++++++----------------- scripts/rancher/manager-lifecycle.sh | 3 +- scripts/rancher/manager-settings.sh | 14 +- scripts/rancher/user-actions.sh | 2 +- 4 files changed, 72 insertions(+), 131 deletions(-) diff --git a/scripts/rancher/cluster-actions.sh b/scripts/rancher/cluster-actions.sh index abd866e..d3fdca6 100644 --- a/scripts/rancher/cluster-actions.sh +++ b/scripts/rancher/cluster-actions.sh @@ -3,18 +3,10 @@ ####################################### # List clusters managed by Rancher -# Arguments: -# Rancher URL -# token -# Examples: -# rancher_list_clusters rancher.random_string.geek xxxxx ####################################### rancher_list_clusters() { - local rancherUrl=$1 - local token=$2 - echo "Listing clusters registered in Rancher..." - curl -s -k "$rancherUrl/v3/clusters" -H "Authorization: Bearer $token" | jq . + kubectl get clusters.provisioning.cattle.io --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' } ####################################### @@ -22,106 +14,73 @@ rancher_list_clusters() { # Globals: # CLUSTER_ID # Arguments: -# Rancher URL -# token # name # version (Kubernetes) # Examples: -# rancher_create_customcluster rancher.random_string.geek xxxxx demo 'v1.27.16+rke2r1' +# rancher_create_customcluster demo 'v1.27.16+rke2r1' ####################################### rancher_create_customcluster() { - local rancherUrl=$1 - local token=$2 - local name=$3 - local version=$4 + local name=$1 + local version=$2 echo "Creating downstream cluster in Rancher..." - CLUSTER_CONFIG=$(cat < Date: Mon, 9 Sep 2024 16:24:51 +0200 Subject: [PATCH 2/2] Feature/Move to GitHub packages for container images (#8) * Cosmetic code changes (docs mainly) * Try with GitHub Packages * Upgrade BCI in Dockerfile of cow-demo to Go 1.23 and SLES 15 SP6 * Update application source README files * Update READMEs --- .github/workflows/pkg.yml | 32 +++++++++++++--------- CONTRIBUTING.md | 40 +++++++++++++++++++++++++--- README.md | 14 +++++++++- scripts/rancher/cluster-actions.sh | 9 ++++--- scripts/rancher/manager-lifecycle.sh | 1 - scripts/rancher/manager-settings.sh | 4 +-- scripts/rancher/user-actions.sh | 3 +-- src/README.md | 7 +++++ src/cow-demo/Dockerfile | 4 +-- src/cow-demo/README.md | 24 +++++++++++++---- src/game-2048/README.md | 34 ++++++++++++++--------- src/rancher-helloworld/README.md | 26 ++++++++++++------ 12 files changed, 144 insertions(+), 54 deletions(-) create mode 100644 src/README.md diff --git a/.github/workflows/pkg.yml b/.github/workflows/pkg.yml index b204e89..42b0521 100644 --- a/.github/workflows/pkg.yml +++ b/.github/workflows/pkg.yml @@ -2,7 +2,8 @@ name: PKG on: push: - branches: [ "main" ] + branches: + - "main" workflow_dispatch: permissions: @@ -29,6 +30,7 @@ jobs: cow-demo: src/cow-demo/** rancher-helloworld: src/rancher-helloworld/** build: + if: ${{ github.ref_name == 'main' }} runs-on: ubuntu-latest steps: - name: Check-out the repository @@ -76,8 +78,9 @@ jobs: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest needs: build + if: ${{ github.ref_name == 'main' }} + runs-on: ubuntu-latest steps: - name: Deploy to GitHub Pages id: deployment @@ -89,27 +92,32 @@ jobs: matrix: app: ${{ fromJSON(needs.changes.outputs.apps) }} runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write steps: - - name: Checkout source code + - name: Checkout repository uses: actions/checkout@v4 - name: Login to container registry uses: docker/login-action@v3 with: - registry: ${{ env.CONTAINER_REGITRY_DOMAIN }} - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} + registry: ${{ env.CONTAINER_REGISTRY_DOMAIN }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Build container image run: | cd src/${{ matrix.app }} - docker build . --tag $IMAGE_FOLDER/${{ matrix.app }}:${{ env.IMAGE_TAG }} + docker build . --tag ${{ env.CONTAINER_REGISTRY_DOMAIN }}/${{ env.IMAGE_FOLDER }}/${{ matrix.app }}:${{ env.IMAGE_TAG }} - name: Push image to container registry - run: docker push $IMAGE_FOLDER/${{ matrix.app }}:${{ env.IMAGE_TAG }} + run: docker push ${{ env.CONTAINER_REGISTRY_DOMAIN }}/${{ env.IMAGE_FOLDER }}/${{ matrix.app }}:${{ env.IMAGE_TAG }} - name: Push latest tag to container registry if: ${{ github.ref_name == 'main' }} run: | - docker tag $IMAGE_FOLDER/${{ matrix.app }}:${{ env.IMAGE_TAG }} $IMAGE_FOLDER/${{ matrix.app }}:latest - docker push $IMAGE_FOLDER/${{ matrix.app }}:latest + docker tag ${{ env.CONTAINER_REGISTRY_DOMAIN }}/${{ env.IMAGE_FOLDER }}/${{ matrix.app }}:${{ env.IMAGE_TAG }} ${{ env.CONTAINER_REGISTRY_DOMAIN }}/${{ env.IMAGE_FOLDER }}/${{ matrix.app }}:latest + docker push ${{ env.CONTAINER_REGISTRY_DOMAIN }}/${{ env.IMAGE_FOLDER }}/${{ matrix.app }}:latest env: - CONTAINER_REGITRY_DOMAIN: docker.io - IMAGE_FOLDER: ${{ vars.DOCKERHUB_NAMESPACE }} + CONTAINER_REGISTRY_DOMAIN: ghcr.io + IMAGE_FOLDER: suse IMAGE_TAG: 1.0.${GITHUB_RUN_ID} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 937a759..145e297 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,11 +1,45 @@ # Contribution guide -## Code lifecycle +## Code lifecycle management -This git repository follows the Gitflow pattern, so make sure to follow the convention. +### Gitflow -For instance, create a branch `feature/my-change-title` from `develop`, once good enough to be shared and reviewed create a Pull Request targetting `develop`. +This git repository follows the Gitflow pattern, so make sure to follow the convention: + +- clone the repository (if not already done) +- make sure you are up-to-date with git pull command +- create a branch `feature/my-change-title` from `develop` +- commit your changes on this feature branch and send them with the git push command +- once the changes are good enough for a review/discussion, create a Pull Request (PR) targetting `develop` +- make sure the checks are all green +- once the PR is validated it will be merged to `develop` (with a squash commit) and the feature branch deleted + +Maintainers will regularly create a Pull Request (merge commit) from `develop` (latest quality) to `main` (production/stable). + +### CI/CD + +Continuous Integration and Continuous Delivery are automated through CI/CD pipelines running as GitHub actions. + +The source of the pipeline-as-code are in the `.github/workflows` folder: + +- [`ci.yml`](.github/workflows/ci.yml) +- [`pkg.yml`](.github/workflows/pkg.yml) ## Code convention For bash/shell script files, follow the conventions from [Google Style Guide](https://google.github.io/styleguide/shellguide.html). + +The quality will be checked by the CI pipeline. + +## Troubleshooting + +### Container image creation + +Here is how to build and push an image to the registry: + +```bash +docker login -u $CONTAINER_REGISTRY_USER -p $CONTAINER_REGISTRY_PASSWD ghcr.io +cd src/cow-demo +docker build . -t ghcr.io/suse/cow-demo:1.0.0 +docker push ghcr.io/suse/cow-demo:1.0.0 +``` diff --git a/README.md b/README.md index 04cc6a0..78c2e0f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CI](https://github.com/SUSE/lab-setup/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/SUSE/lab-setup/actions/workflows/ci.yml) -Welcome! You'll find in this repository some open-source material to setup a lab environment. +Welcome! You'll find in this repository everything needed to setup a lab environment from open-source components. It is used internally at SUSE but is open to everyone. Feel free to [contribute](CONTRIBUTING.md) and share feedback! @@ -43,3 +43,15 @@ helm upgrade --install cow-demo suse-lab-setup/cow-demo --namespace demo ``` Browse the [catalog of Helm charts](charts/README.md). + +### Container images + +Use the container images we provide for our demonstrations, for instance: + +```bash +docker run --rm -p 8080:8080 ghcr.io/suse/cow-demo:1.0.10773428519 +``` + +Open the [web application](http://localhost:8080/) and enjoy the live display! + +Browse the [catalog of applications](src/README.md). diff --git a/scripts/rancher/cluster-actions.sh b/scripts/rancher/cluster-actions.sh index d3fdca6..6e8593d 100644 --- a/scripts/rancher/cluster-actions.sh +++ b/scripts/rancher/cluster-actions.sh @@ -3,6 +3,8 @@ ####################################### # List clusters managed by Rancher +# Examples: +# rancher_list_clusters ####################################### rancher_list_clusters() { echo "Listing clusters registered in Rancher..." @@ -77,11 +79,10 @@ spec: skipWaitForDeleteTimeoutSeconds: 0 timeout: 120 EOF - + sleep 10 rancher_get_clusterid $name - echo "DEBUG CLUSTER_ID=${CLUSTER_ID}" } ####################################### @@ -97,6 +98,7 @@ rancher_get_clusterid() { local name=$1 CLUSTER_ID=$(kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}") + echo "DEBUG CLUSTER_ID=${CLUSTER_ID}" } ####################################### @@ -111,7 +113,6 @@ rancher_get_clusterid() { rancher_get_clusterregistrationcommand() { local id=$1 - REGISTRATION_COMMAND=$(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}' -) + REGISTRATION_COMMAND=$(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}') echo "DEBUG REGISTRATION_COMMAND=${REGISTRATION_COMMAND}" } diff --git a/scripts/rancher/manager-lifecycle.sh b/scripts/rancher/manager-lifecycle.sh index 6549835..b687b53 100644 --- a/scripts/rancher/manager-lifecycle.sh +++ b/scripts/rancher/manager-lifecycle.sh @@ -19,7 +19,6 @@ rancher_install_withcertmanagerclusterissuer() { local hostname=$4 local clusterissuer=$5 - echo "Installing Rancher..." helm repo add rancher-${repository} https://releases.rancher.com/server-charts/${repository} helm repo update diff --git a/scripts/rancher/manager-settings.sh b/scripts/rancher/manager-settings.sh index 2dd9491..29de830 100644 --- a/scripts/rancher/manager-settings.sh +++ b/scripts/rancher/manager-settings.sh @@ -12,7 +12,5 @@ rancher_update_serverurl() { local rancherUrl=$1 echo "Sets Rancher URL in settings..." - kubectl patch settings.management.cattle.io server-url --type='merge' --patch '{ - "value": "'$rancherUrl'" - }' + kubectl patch settings.management.cattle.io server-url --type='merge' --patch '{ "value": "'$rancherUrl'" }' } diff --git a/scripts/rancher/user-actions.sh b/scripts/rancher/user-actions.sh index c3f00b8..3dded56 100644 --- a/scripts/rancher/user-actions.sh +++ b/scripts/rancher/user-actions.sh @@ -44,7 +44,6 @@ rancher_update_password() { local currentPassword=$3 local newPassword=$4 - # updates password echo "Updates Rancher user password..." curl -s -k -H "Authorization: Bearer $token" \ -H 'Content-Type: application/json' \ @@ -84,4 +83,4 @@ rancher_create_apikey() { echo "DEBUG API_KEY_RESPONSE=${API_KEY_RESPONSE}" API_TOKEN=$(echo $API_KEY_RESPONSE | jq -r .token) sleep 5 -} \ No newline at end of file +} diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..34fe941 --- /dev/null +++ b/src/README.md @@ -0,0 +1,7 @@ +# Applications + +## Catalog + +* [Cow Demo](cow-demo/README.md) +* [Game 2048](game-2048/README.md) +* [Rancher Helloworld](rancher-helloworld/README.md) diff --git a/src/cow-demo/Dockerfile b/src/cow-demo/Dockerfile index c07a6a3..866a1f8 100644 --- a/src/cow-demo/Dockerfile +++ b/src/cow-demo/Dockerfile @@ -9,12 +9,12 @@ #WORKDIR /usr/src/ui/semantic #RUN npx gulp build -FROM registry.suse.com/bci/golang:1.22 as buildgo +FROM registry.suse.com/bci/golang:1.23 as buildgo COPY . /go/src WORKDIR /go/src RUN go build -FROM registry.suse.com/bci/bci-minimal:15.5 as run +FROM registry.suse.com/bci/bci-minimal:15.6 as run COPY ./static /static COPY ./templates /templates COPY --from=buildgo /go/src/cow-demo /bin/cow-demo diff --git a/src/cow-demo/README.md b/src/cow-demo/README.md index dad7b2c..af8f4a4 100644 --- a/src/cow-demo/README.md +++ b/src/cow-demo/README.md @@ -1,17 +1,17 @@ # Cow Demo Application -[![Docker Image Version](https://img.shields.io/docker/v/suselabsetup/cow-demo?label=Docker)](https://hub.docker.com/r/suselabsetup/cow-demo) - Cow Demo is a nice web application, written in Go, used for demonstrating Kubernetes and load balancing. 💡 This is a hard fork of [oskapt/rancher-demo](https://github.com/oskapt/rancher-demo) who hasn't been updated since 2021. It also took the updates from [bashofmann/rancher-demo](https://github.com/bashofmann/rancher-demo). +## Presentation + It will create a colored icon for each replica within a ReplicaSet and indicate which one most recently served a response. Replicas which haven't been heard from will begin to fade out, until after a configurable number of seconds they will disappear. This is useful for demonstrating scaling up/down an upgraded application. -![screenshot](screenshot.png) +## Configuration Environment variables: @@ -39,6 +39,20 @@ Environment variables: - chameleons - cowmeleons +## Quickstart with a container + +If you have a container engine running, you can start right away with the image: + +```bash +docker run --rm -p 8080:8080 ghcr.io/suse/cow-demo:1.0.10773428519 +``` + +Open the [web application](http://localhost:8080/) and enjoy the interactive display! + +## Deploy in an environment + +The recommanded way to run the application is to deploy in a Kubernetes cluster with with the [Helm chart](https://github.com/SUSE/lab-setup/tree/main/charts/cow-demo). + ## Local development ### How to test @@ -88,13 +102,13 @@ The container image is using [SUSE BCI (Base Container Images)](https://registry Build a local image: ```bash -docker build -t cow-demo:local . +docker build -t cow-demo . ``` Start a container: ```bash -docker run --rm -p 8080:8080 -e COW_COLOR:purple cow-demo:local +docker run --rm -p 8080:8080 -e COW_COLOR:purple cow-demo ``` Open [localhost:8080](http://localhost:8080) diff --git a/src/game-2048/README.md b/src/game-2048/README.md index f4fe936..6d450fe 100644 --- a/src/game-2048/README.md +++ b/src/game-2048/README.md @@ -1,12 +1,24 @@ # 2048 game -[![Docker Image Version](https://img.shields.io/docker/v/suselabsetup/game-2048?label=Docker)](https://hub.docker.com/r/suselabsetup/game-2048) - -Let's play 2048 Game web application in a container! +Let's play 2048 game in a browser! 💡 This is a hard fork of [gabrielecirulli/2048](https://github.com/gabrielecirulli/2048) which hasn't been updated since 2017. -## How to edit +## Quickstart with a container + +If you have a container engine running, you can start right away with the image: + +```bash +docker run --rm -p 8080:80 ghcr.io/suse/game-2048:1.0.10773428519 +``` + +Open the [web application](http://localhost:8080/) and enjoy the game! + +## Deploy in an environment + +The recommanded way to run the application is to deploy in a Kubernetes cluster with with the [Helm chart](https://github.com/SUSE/lab-setup/tree/main/charts/game-2048). + +## Update the application If you want to modify the CSS, edit the SCSS files present in `style/`: `main.scss` and others. Don't edit the `main.css`, because it's supposed to be generated. @@ -18,22 +30,18 @@ sass --unix-newlines --watch style/main.scss SASS will automatically recompile your css when changed. -## How to run locally +## Run from the sources -Build a new container image: +Build a new image: ```bash -docker build . -t game2048:local --no-cache +docker build . -t game-2048 --no-cache ``` -Run a container locally: +Start a container: ```bash -docker run --rm -p 8080:80 game2048:local +docker run --rm -p 8080:80 game-2048 ``` Open [localhost:8080](http://localhost:8080) et enjoy 😁 - -## How to deploy - -The recommanded way to deploy and run is through Helm with the [associated chart](https://github.com/devpro/helm-charts/tree/main/charts/game-2048). diff --git a/src/rancher-helloworld/README.md b/src/rancher-helloworld/README.md index d948e0d..f9f140b 100644 --- a/src/rancher-helloworld/README.md +++ b/src/rancher-helloworld/README.md @@ -1,14 +1,24 @@ # Rancher Hello World -[![Docker Image Version](https://img.shields.io/docker/v/suselabsetup/rancher-helloworld?label=Docker)](https://hub.docker.com/r/suselabsetup/rancher-helloworld) - -This is the source code of a web application used for demoing and/or testing. It shows data about hostname, k8s services and request headers. +This small web application is interesting as a first workload to install in a Kubernetes cluster. It shows data about hostname, services and request headers. 💡 This is a hard fork of [rancher/hello-world](https://github.com/rancher/hello-world) which hasn't been updated since 2018. -## Local development +## Quickstart with a container + +If you have a container engine running, you can start right away with the image: + +```bash +docker run --rm -p 8080:80 ghcr.io/suse/rancher-helloworld:1.0.10773428519 +``` + +Open the [web application](http://localhost:8080/) and look at the information! + +## Deploy in an environment + +The recommanded way to run the application is to deploy in a Kubernetes cluster with with the [Helm chart](https://github.com/SUSE/lab-setup/tree/main/charts/rancher-helloworld). -### How to run locally +### Develop locally Update the dependencies (if a change has been made): @@ -30,20 +40,20 @@ HTTP_PORT=8080 ./rancher-helloworld Open [localhost:8080](http://localhost:8080) -### How to run in a container +## Run from the sources The container image is using [SUSE BCI (Base Container Images)](https://registry.suse.com/). Build a local image: ```bash -docker build -t rancher-helloworld:local . +docker build -t rancher-helloworld . ``` Start a container: ```bash -docker run --rm -p 8080:80 rancher-helloworld:local +docker run --rm -p 8080:80 rancher-helloworld ``` Open [localhost:8080](http://localhost:8080)