This documentation provides a step-by-step guide for expert developers on how to build and start the fluxcd/flux2-multi-tenancy project. Ensure that you follow each step carefully to set up and bootstrap the project correctly.

Prerequisites

Before you start, confirm that you have the following prerequisites configured:

  1. Install the Flux CLI - Follow the installation instructions from the Flux CLI documentation.
  2. Fork the repository on your personal GitHub account.
  3. Set environmental variables for your GitHub username and repository name:
    export GITHUB_USER=your-github-username
    export GITHUB_REPO=your-forked-repo
    

Step 1: Validate Cluster

Validating that your staging cluster meets the prerequisites is essential. Use the following command to perform the checks:

flux check --pre

Step 2: Bootstrap Flux

Once validation is successful, bootstrap Flux to integrate it with your GitHub repository. Make sure to set the --context argument to your staging cluster’s kubectl context:

flux bootstrap github \
--context=your-staging-context \
--owner=${GITHUB_USER} \
--repository=${GITHUB_REPO} \
--branch=main \
--personal \
--path=clusters/staging

During this process, the Flux CLI will request the GITHUB_TOKEN (Personal Access Token) to authenticate. Ensure you have generated this token beforehand and are ready to enter it.

Step 3: Create Tenants

To manage multiple tenants, you can create a tenant named dev-team with access to the apps namespace.

  1. Create the Tenant Base Directory:

    mkdir -p ./tenants/base/dev-team
    
  2. Generate RBAC Resources:

    Create the namespace, service account, and role binding using:

    flux create tenant dev-team --with-namespace=apps \
    --export > ./tenants/base/dev-team/rbac.yaml
    
  3. Create the Sync Manifests:

    To define the Git repository source for this tenant, execute:

    flux create source git dev-team \
    --namespace=apps \
    --url=https://github.com// \
    --branch=main \
    --export > ./tenants/base/dev-team/sync.yaml
    
  4. Create the Kustomization:

    Link the source and the kustomization for the tenant:

    flux create kustomization dev-team \
    --namespace=apps \
    --service-account=dev-team \
    --source=GitRepository/dev-team \
    --path="./" \
    --export >> ./tenants/base/dev-team/sync.yaml
    
  5. Create the Kustomization Base File:

    Navigate to the tenant’s base directory and create the base kustomization.yaml:

    cd ./tenants/base/dev-team/ && kustomize create --autodetect --namespace apps
    
  6. Set Staging Overlay:

    Create a patch file for staging:

    cat << EOF | tee ./tenants/staging/dev-team-patch.yaml
    apiVersion: kustomize.toolkit.fluxcd.io/v1
    kind: Kustomization
    metadata:
      name: dev-team
      namespace: apps
    spec:
      path: ./staging
    EOF
    

Step 4: Validate Configuration

After setting up the configuration, it is crucial to validate the kustomizations to ensure there are no issues:

echo "INFO - Validating kustomize overlays"
find . -type f -name kustomization.yaml -print0 | while IFS= read -r -d $'\0' file; do
    echo "INFO - Validating kustomization ${file/%kustomization.yaml}"
    kustomize build "${file/%kustomization.yaml}" | \
    kubeconform "your-kubeconform-config"
    if [[ ${PIPESTATUS[0]} != 0 ]]; then
      exit 1
    fi
done

This command ensures that your kustomizations are valid and ready for deployment.

Conclusion

Following these steps, you will successfully build and start the fluxcd/flux2-multi-tenancy project, setting up the necessary tenants and validating the configurations. Make sure to take care of each command to maintain a smooth setup process.

Sources: README.md (fluxcd/flux2-multi-tenancy repository).