Skip to content

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

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

[minor] Implement Redis-Based Distributed Locking for GitOps Operations#2189
Hardik-Prajapati-10 wants to merge 38 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 Outdated
Comment thread docs/redis-locking-setup.md Outdated
### 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?

Copy link
Copy Markdown
Contributor Author

@Hardik-Prajapati-10 Hardik-Prajapati-10 Apr 17, 2026

Choose a reason for hiding this comment

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

The env var acts as a "safety check" - even though Redis is required, the explicit flag prevents accidental misconfiguration and provides clear error messages when Redis setup is incomplete.

Originally, GITOPS_USE_REDIS_LOCKING served two purposes:

Feature switch during development
Redis locking was the preferred mechanism
Git branch locking was a fallback when Redis wasn’t available

Safety / explicit opt‑in
Prevent accidental startup with partial or missing Redis configuration
Force developers/operators to make an intentional choice

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.

Thinking about this more we could keep the GITOPS_USE_REDIS_LOCKING flag and have it use the branch locking mechanism when this is false. This would allow external customers using gitops to continue with the branch locking and not force a redis instance on them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we are going with following approach :
If Redis is available → Uses Redis locking
If Redis is unavailable → Automatically falls back to Git locking

Comment thread docs/redis-locking-setup.md Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

doc updated

Comment thread docs/redis-locking-setup.md Outdated
clarify that Redis env vars must be set whenever migrated GitOps functions are executed, not only in IBM Toolchain
note that IBM Toolchain is optional and document pipeline environment properties as one way to provide runtime variables
remove the custom CLI image/container setup section because custom CLI images are not supported
Comment thread docs/redis-locking-setup.md Outdated
### 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.

Thinking about this more we could keep the GITOPS_USE_REDIS_LOCKING flag and have it use the branch locking mechanism when this is false. This would allow external customers using gitops to continue with the branch locking and not force a redis instance on them.

Comment thread image/cli/mascli/functions/gitops_utils Outdated
Comment thread image/cli/mascli/functions/gitops_utils Outdated
… updated behavior to error when Redis is unavailable with explicit Redis locking enabled.
@whitfiea whitfiea changed the title [major] Implement Redis-Based Distributed Locking for GitOps Operations [minor] Implement Redis-Based Distributed Locking for GitOps Operations Apr 21, 2026
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