diff --git a/.ct/chart_schema.yaml b/.ct/chart_schema.yaml new file mode 100644 index 0000000..2a26d9b --- /dev/null +++ b/.ct/chart_schema.yaml @@ -0,0 +1,37 @@ +name: str() +home: str(required=False) +version: str() +apiVersion: str() +appVersion: any(str(), num(), required=False) +description: str(required=False) +keywords: list(str(), required=False) +sources: list(str(), required=False) +maintainers: list(include('maintainer'), required=False) +dependencies: list(include('dependency'), required=False) +icon: str(required=False) +engine: str(required=False) +condition: str(required=False) +tags: str(required=False) +deprecated: bool(required=False) +kubeVersion: str(required=False) +annotations: map(str(), str(), required=False) +type: str(required=False) +--- +maintainer: + name: str() + email: str(required=False) + url: str(required=False) +--- +dependency: + name: str() + version: str() + repository: str(required=False) + condition: str(required=False) + tags: list(str(), required=False) + enabled: bool(required=False) + import-values: any(list(str()), list(include('import-value')), required=False) + alias: str(required=False) +--- +import-value: + child: str() + parent: str() diff --git a/.ct/lintconf.yaml b/.ct/lintconf.yaml new file mode 100644 index 0000000..92a76d2 --- /dev/null +++ b/.ct/lintconf.yaml @@ -0,0 +1,42 @@ +--- +rules: + braces: + min-spaces-inside: 0 + max-spaces-inside: 0 + min-spaces-inside-empty: -1 + max-spaces-inside-empty: -1 + brackets: + min-spaces-inside: 0 + max-spaces-inside: 0 + min-spaces-inside-empty: -1 + max-spaces-inside-empty: -1 + colons: + max-spaces-before: 0 + max-spaces-after: 1 + commas: + max-spaces-before: 0 + min-spaces-after: 1 + max-spaces-after: 1 + comments: + require-starting-space: true + min-spaces-from-content: 2 + document-end: disable + document-start: disable # No --- to start a file + empty-lines: + max: 2 + max-start: 0 + max-end: 0 + hyphens: + max-spaces-after: 1 + indentation: + spaces: consistent + indent-sequences: true + check-multi-line-strings: false + key-duplicates: enable + line-length: disable # Lines can be any length + new-line-at-end-of-file: enable + new-lines: + type: unix + trailing-spaces: enable + truthy: + level: warning diff --git a/.gitignore b/.gitignore index a83bd31..81c4706 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ charts/**/charts/*.tgz values.yaml /.idea/ .DS_Store +tmp +venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 79816e2..128d381 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,6 +5,7 @@ repos: hooks: - id: trailing-whitespace - id: check-yaml + args: [--allow-multiple-documents] exclude: ^charts/s3gw/templates/.*.yaml$ - id: check-added-large-files - id: end-of-file-fixer diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bbe14ac --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +# Copyright © 2023 SUSE LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +######################################################################## +# Testing cluster + +cluster-start: + @./test-env/cluster-create.sh + k3d kubeconfig merge -ad + kubectl config use-context k3d-s3gw-charts-testing + @./test-env/cluster-prepare.sh + +cluster-delete: + k3d cluster delete s3gw-charts-testing + @if test -f /usr/local/bin/rke2-uninstall.sh; then sudo sh /usr/local/bin/rke2-uninstall.sh; fi + +######################################################################## +# Setup test environment + +setup-test-environment: + python3 -m venv venv \ + && source venv/bin/activate \ + && pip install -r requirements-dev.txt + +######################################################################## +# Test charts + +lint-test-all: + @./test-env/run-lint-test.sh diff --git a/charts/s3gw/README.md b/charts/s3gw/README.md index e89bf03..c9c54ca 100644 --- a/charts/s3gw/README.md +++ b/charts/s3gw/README.md @@ -37,6 +37,56 @@ follows: - Apps -> Charts -> Install `s3gw`. Select the `s3gw` namespace previously created. A `pvc` for `s3gw` will be created automatically during installation. +## Develop and test charts on the local machine + +We have defined a set of common tasks frequently used during the +development and the testing process of the s3gw's charts. + +### Requirements + +- make +- Python 3, pip +- Docker, Docker compose +- Helm +- k3d +- Kubectl +- [ct][ct-url] + +### Setup the test environment + +It creates a Python venv and downloads the required pip packages used +during the charts testing. + +```shell +make setup-test-environment +``` + +### Start a k3d cluster and deploy charts + +This task spawns a k3d cluster and deploys the local charts version on it. + +```shell +make cluster-start +``` + +### Delete the k3d cluster + +It deletes the cluster created with `make cluster-start`. + +```shell +make cluster-delete +``` + +### Lint and unittest the charts + +It lints and runs the all the unittest using the ct tool. +This task requires you previously created the k3d cluster with +`make cluster-start` + +```shell +make lint-test-all +``` + ## Documentation You can access our documentation [here][docs-url]. @@ -59,3 +109,4 @@ limitations under the License. [s3gw-url]: https://s3gw.io [docs-url]: https://s3gw-docs.readthedocs.io/en/latest/helm-charts/ +[ct-url]: https://github.com/helm/chart-testing diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..9ec2058 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +yamllint +yamale diff --git a/test-env/cluster-create.sh b/test-env/cluster-create.sh new file mode 100755 index 0000000..bc2b51e --- /dev/null +++ b/test-env/cluster-create.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Copyright © 2023 SUSE LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +NETWORK_NAME=s3gw-charts-testing +CLUSTER_NAME=s3gw-charts-testing +K3S_IMAGE=${K3S_IMAGE:-rancher/k3s:v1.25.10-k3s1} +export KUBECONFIG=$SCRIPT_DIR/../tmp/kubeconfig + +check_deps() { + if ! command -v k3d &> /dev/null + then + echo "k3d could not be found" + exit + fi +} + +existingCluster() { + k3d cluster list | grep ${CLUSTER_NAME} +} + +if [[ "$(existingCluster)" != "" ]]; then + echo "Cluster already exists, skipping creation." + exit 0 +fi + +echo "Ensuring a network" +docker network create $NETWORK_NAME || echo "Network already exists" + +echo "Creating a new one named $CLUSTER_NAME" +if [ -z ${EXPOSE_CLUSTER_PORTS+x} ]; then + # Without exposing ports on the host: + k3d cluster create $CLUSTER_NAME --network $NETWORK_NAME --image "$K3S_IMAGE" $S3GW_K3D_INSTALL_ARGS +else + # Exposing ports on the host: + k3d cluster create $CLUSTER_NAME --network $NETWORK_NAME --image "$K3S_IMAGE" -p '80:80@server:0' -p '443:443@server:0' $S3GW_K3D_INSTALL_ARGS +fi +k3d kubeconfig get $CLUSTER_NAME > $KUBECONFIG + +echo "Waiting for node to be ready" +nodeName=$(kubectl get nodes -o name) +kubectl wait --for=condition=Ready "$nodeName" + +date +echo "Waiting for the deployments of the foundational configurations to be ready" +# 1200s = 20 min, to handle even a horrendously slow setup. Regular is 10 to 30 seconds. +kubectl wait --for=condition=Available --namespace kube-system deployment/metrics-server --timeout=1200s +kubectl wait --for=condition=Available --namespace kube-system deployment/coredns --timeout=1200s +kubectl wait --for=condition=Available --namespace kube-system deployment/local-path-provisioner --timeout=1200s +date + +echo "The cluster is ready! ✔️" diff --git a/test-env/cluster-prepare.sh b/test-env/cluster-prepare.sh new file mode 100755 index 0000000..b25c97f --- /dev/null +++ b/test-env/cluster-prepare.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# Copyright © 2023 SUSE LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +UI_IMAGE_TAG=${UI_IMAGE_TAG:-"latest"} +BE_IMAGE_TAG=${BE_IMAGE_TAG:-"latest"} + +function prepare_system_domain { + echo -e "\e[32mGenerating a magic domain...\e[0m" + S3GW_CLUSTER_IP=$(docker inspect k3d-s3gw-charts-testing-server-0 | jq -r '.[0]["NetworkSettings"]["Networks"]["s3gw-charts-testing"]["IPAddress"]') + if [[ -z $S3GW_CLUSTER_IP ]]; then + echo "Couldn't find the cluster's IP address" + exit 1 + fi + export S3GW_CLUSTER_IP="${S3GW_CLUSTER_IP}" + export S3GW_SYSTEM_DOMAIN="${S3GW_CLUSTER_IP}.omg.howdoi.website" + echo -e "Using \e[32m${S3GW_SYSTEM_DOMAIN}\e[0m for s3gw domain" +} + +function deploy_s3gw_local_charts { + helm upgrade --debug --wait --timeout 180s --install -n s3gw-charts-testing --create-namespace s3gw-charts-testing ./charts/s3gw \ + --set publicDomain="${S3GW_SYSTEM_DOMAIN}" \ + --set ui.publicDomain="${S3GW_SYSTEM_DOMAIN}" \ + --set ui.imageTag="${UI_IMAGE_TAG}" \ + --set imageTag="${BE_IMAGE_TAG}" \ + --set logLevel="20" +} + +echo "Installing helm-unittest plugin" + +set +e +helm plugin install https://github.com/helm-unittest/helm-unittest.git +helm plugin update unittest +set -e + +echo "Preparing k3d environment" + +helm repo add jetstack https://charts.jetstack.io +helm repo add s3gw https://aquarist-labs.github.io/s3gw-charts/ +helm repo update + +#Install the cert-manager +if ! [[ -v SKIP_CM_INSTALL ]]; then +kubectl create namespace cert-manager + +helm install cert-manager --namespace cert-manager jetstack/cert-manager \ + --set installCRDs=true \ + --set extraArgs[0]=--enable-certificate-owner-ref=true \ + --version 1.10 \ + --wait +fi + +echo "Deploying s3gw" + +prepare_system_domain + +echo -e "Using UI_IMAGE_TAG: \e[32m${UI_IMAGE_TAG}\e[0m" +echo -e "Using BE_IMAGE_TAG: \e[32m${BE_IMAGE_TAG}\e[0m" + +deploy_s3gw_local_charts + +echo +echo "Done preparing k3d environment! ✔️" diff --git a/test-env/run-lint-test.sh b/test-env/run-lint-test.sh new file mode 100755 index 0000000..5a93205 --- /dev/null +++ b/test-env/run-lint-test.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Copyright © 2023 SUSE LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) +echo -e "\e[32mUsing BRANCH_NAME=$BRANCH_NAME \e[0m" + +python3 -m venv venv && source venv/bin/activate + +ct lint-and-install \ + --charts ${SCRIPT_DIR}"/../charts/s3gw" \ + --target-branch ${BRANCH_NAME} \ + --additional-commands="helm unittest --strict ${SCRIPT_DIR}/../charts/s3gw" \ + --validate-maintainers=false \ + --debug