Skip to content

[major] Implement Redis-Based Distributed Locking for GitOps Operations#2189

Open
Hardik-Prajapati-10 wants to merge 29 commits intomasterfrom
mascore-13109-branch-lock
Open

[major] Implement Redis-Based Distributed Locking for GitOps Operations#2189
Hardik-Prajapati-10 wants to merge 29 commits intomasterfrom
mascore-13109-branch-lock

Conversation

@Hardik-Prajapati-10
Copy link
Copy Markdown
Contributor

@Hardik-Prajapati-10 Hardik-Prajapati-10 commented Apr 16, 2026

Issue

https://jsw.ibm.com/browse/MASCORE-13109

Description

This PR implements a comprehensive Redis-based distributed locking mechanism to replace the legacy Git branch-based locking system for GitOps operations. The change addresses critical issues with concurrent pipeline executions that previously caused race conditions, merge conflicts, and branch pollution.

Key Changes

1. Redis Locking Infrastructure (image/cli/mascli/functions/gitops_utils)

  • Added Redis connection management with TLS support
  • Implemented atomic lock acquisition using Redis SET NX operations
  • Added lock release with ownership verification using Lua scripts
  • Included automatic lock expiry (TTL) to prevent orphaned locks
  • Added comprehensive retry logic with configurable parameters

2. Container Image Updates

  • Added redis-cli installation script (image/cli/install/install-redis-cli.sh)
  • Updated Dockerfile to include redis-cli in the CLI container image (v19.6.1+)
  • Supports multiple package managers (microdnf, dnf, yum, apt-get, apk)

3. Migrated GitOps Functions (10 functions)

All functions now use gitops_lock_and_modify with Redis locking:

  • gitops_cp4d_service
  • gitops_suite_app_config
  • gitops_deprovision_app_config
  • gitops_mas_config
  • gitops_suite_workspace
  • gitops_deprovision_suite_workspace
  • gitops_deprovision_cp4d_service
  • gitops_db2u_database
  • gitops_rds_db2_database
  • gitops_deprovision_db2u_database

4. Documentation

  • Comprehensive setup guide (docs/redis-locking-setup.md)
  • IBM Toolchain configuration script (docs/ibm-toolchain-redis-setup.sh)
  • Migration examples and troubleshooting guide

Impact

Reliability Improvements:

  • Eliminates race conditions through atomic Redis operations
  • Prevents merge conflicts in concurrent updates
  • Removes temporary Git branch pollution
  • Automatic lock expiry prevents orphaned locks
  • Fail-fast behavior when Redis is unavailable

Breaking Change:

  • Redis is now required for migrated GitOps flows
  • No fallback to Git branch locking
  • Operations fail immediately if Redis is unavailable or misconfigured
  • GITOPS_USE_REDIS_LOCKING=true must be set

Test Results

Manual Testing

  • ✅ Redis connection validation with TLS
  • ✅ Lock acquisition and release operations
  • ✅ Concurrent pipeline execution without conflicts
  • ✅ Lock TTL expiry behavior
  • ✅ Retry logic under contention
  • ✅ All 10 migrated GitOps functions tested

Integration Testing

  • ✅ IBM Cloud Redis instance connectivity
  • ✅ IBM Toolchain pipeline execution
  • ✅ Multi-pipeline concurrent operations
  • ✅ Error handling for Redis unavailability

Configuration Testing

  • ✅ TLS certificate handling
  • ✅ Authentication (username/password)
  • ✅ Environment variable configuration
  • ✅ Setup script validation (ibm-toolchain-redis-setup.sh)

Backporting

Related Pull Requests

https://github.ibm.com/maximoappsuite/saas-deploy-py/pull/262


⚠️ Notes for Reviewers

  • Ensure you have understood the guidelines before proceeding with a review.
  • Ensure all sections in the PR template are appropriately completed.
  • Critical: This is a breaking change - Redis is now mandatory for migrated GitOps operations
  • Review the migration strategy in docs/redis-locking-setup.md
  • Verify the setup script docs/ibm-toolchain-redis-setup.sh for IBM Cloud integration
  • Check that all 10 migrated functions follow the new locking pattern consistently
  • Validate error handling for Redis unavailability scenarios
  • Review security aspects: TLS configuration, credential handling, and password masking

@Hardik-Prajapati-10 Hardik-Prajapati-10 requested a review from a team as a code owner April 16, 2026 09:28
@whitfiea whitfiea changed the title Implement Redis-Based Distributed Locking for GitOps Operations [major] Implement Redis-Based Distributed Locking for GitOps Operations Apr 17, 2026
Comment thread image/cli/Dockerfile
--mount=type=secret,id=GITHUB_REF_TYPE,env=GITHUB_REF_TYPE \
umask 0002 && \
ls /tmp/install && \
bash /tmp/install/install-redis-cli.sh && \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally the third party cli packages are installed as part of the cli-base image at https://github.com/ibm-mas/cli-base so we should move this redis-cli install to that

### Required Runtime Requirement

For migrated GitOps flows:
- `GITOPS_USE_REDIS_LOCKING` must be `true`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that GITOPS_USE_REDIS_LOCKING has to be true, so i am not sure what the value is of having an env var anyway?

Comment on lines +89 to +112
### Step 2: Configure IBM Toolchain Environment Variables

Add these environment variables to your IBM Toolchain pipeline.

#### In IBM Toolchain → Pipeline → Environment Properties:

| Variable Name | Type | Value | Description |
|--------------|------|-------|-------------|
| `REDIS_USERNAME` | Secure | `ibm_cloud_user` | Redis username from credentials |
| `REDIS_HOST` | Text | `c-abc123.databases.appdomain.cloud` | Redis hostname |
| `REDIS_PORT` | Text | `31234` | Redis port |
| `REDIS_PASSWORD` | Secure | `your-redis-password` | Redis password |
| `REDIS_TLS_CA_CERT_B64` | Secure | `LS0tLS1CRUdJTi...` | Base64-encoded TLS certificate |
| `GITOPS_USE_REDIS_LOCKING` | Text | `true` | Required for migrated locking flows |
| `REDIS_TLS` | Text | `true` | Enable TLS (required for IBM Cloud) |
| `REDIS_DB` | Text | `0` | Redis database number |

#### Optional Tuning Parameters

| Variable Name | Default | Description |
|--------------|---------|-------------|
| `GITOPS_LOCK_TTL` | `300` | Lock expires after N seconds |
| `GITOPS_LOCK_RETRY_MAX` | `100` | Maximum retry attempts |
| `GITOPS_LOCK_RETRY_DELAY` | `20` | Seconds between retry attempts |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the IBM toolchain to call the functions is optional. We should update the docs in https://ibm-mas.github.io/gitops/main/ so state these env vars should be set when executing the functions, and also add that to this file.

Comment on lines +400 to +434
### Adding redis-cli to Custom Images

If you're building a custom CLI image, add redis-cli installation.

**1. Create installation script** (`image/cli/install/install-redis-cli.sh`):
```bash
#!/bin/bash
set -e

echo "Installing redis-cli..."

if command -v microdnf &> /dev/null; then
microdnf install -y redis && microdnf clean all
elif command -v dnf &> /dev/null; then
dnf install -y redis && dnf clean all
elif command -v yum &> /dev/null; then
yum install -y redis && yum clean all
elif command -v apt-get &> /dev/null; then
apt-get update && apt-get install -y redis-tools && rm -rf /var/lib/apt/lists/*
elif command -v apk &> /dev/null; then
apk add --no-cache redis
else
echo "ERROR: No supported package manager found"
exit 1
fi

redis-cli --version
```

**2. Update Dockerfile** (`image/cli/Dockerfile`):
```dockerfile
COPY install /tmp/install
RUN bash /tmp/install/install-redis-cli.sh && \
bash /tmp/install/install-python-packages.sh
```
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need this section as we don't allow a custom cli image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants