Skip to content

Commit d7cfaf5

Browse files
author
Kerkesni
committed
remove hardcoded replicationGroupId and generate from pod name
- Removed the hardcoded default value 'RG001' for replicationGroupId. - replicationGroupId is now generated from the Kubernetes pod name by taking the first 2 characters of the long hash and concatenating it with the full short hash (e.g., cloudserver-7869f99955-nr984 → 78nr984). - Ensures uniqueness across deployments within the same namespace while keeping the ID short. - Falls back to 'RG001' only if neither config nor HOSTNAME is available. Issue: CLDSRV-665
1 parent 6285a15 commit d7cfaf5

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/Config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,20 @@ class Config extends EventEmitter {
906906
assert(typeof config.replicationGroupId === 'string',
907907
'bad config: replicationGroupId must be a string');
908908
this.replicationGroupId = config.replicationGroupId;
909+
} else if (process.env.HOSTNAME) {
910+
// Extract the hash part from the pod name (e.g., cloudserver-7869f99955-nr984)
911+
const parts = process.env.HOSTNAME.split('-');
912+
// Kubernetes pods typically have two UID parts:
913+
// - A long hash (10 characters) that guarantees uniqueness within the namespace.
914+
// - A short hash (5 characters) that is unique only within the current ReplicaSet/Deployment.
915+
if (parts.length > 2) {
916+
const longId = parts[parts.length - 2];
917+
const shortId = parts[parts.length - 1];
918+
this.replicationGroupId = longId.slice(0, 2) + shortId;
919+
} else {
920+
// fallback: just use the last part if format is unexpected
921+
this.replicationGroupId = parts.pop();
922+
}
909923
} else {
910924
this.replicationGroupId = 'RG001';
911925
}

tests/unit/Config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,47 @@ describe('Config', () => {
810810
assert.deepEqual(config.internalListenOn, [{ ip: '127.0.0.1', port: 9000 }]);
811811
});
812812
});
813+
814+
describe('replicationGroupId', () => {
815+
let sandbox;
816+
let readFileStub;
817+
let previousHostname;
818+
819+
beforeEach(() => {
820+
sandbox = sinon.createSandbox();
821+
readFileStub = sandbox.stub(fs, 'readFileSync');
822+
readFileStub.callThrough();
823+
previousHostname = process.env.HOSTNAME;
824+
});
825+
826+
afterEach(() => {
827+
sandbox.restore();
828+
if (previousHostname) {
829+
process.env.HOSTNAME = previousHostname;
830+
} else {
831+
delete process.env.HOSTNAME;
832+
}
833+
});
834+
835+
it('should return the replicationGroupId from config', () => {
836+
const config = new ConfigObject();
837+
assert.strictEqual(config.replicationGroupId, 'RG001');
838+
});
839+
it('should genrate a new replicationGroupId if not set in config', () => {
840+
process.env.HOSTNAME = 'cloudserver-7869f99955-nr984';
841+
842+
const modifiedConfig = { ...defaultConfig, replicationGroupId: null };
843+
readFileStub.withArgs(sinon.match(/config.json$/)).returns(JSON.stringify(modifiedConfig));
844+
845+
const config = new ConfigObject();
846+
assert.strictEqual(config.replicationGroupId, '78nr984');
847+
});
848+
it('should use default replicationGroupId if HOSTNAME is not set', () => {
849+
const modifiedConfig = { ...defaultConfig, replicationGroupId: null };
850+
readFileStub.withArgs(sinon.match(/config.json$/)).returns(JSON.stringify(modifiedConfig));
851+
852+
const config = new ConfigObject();
853+
assert.strictEqual(config.replicationGroupId, 'RG001');
854+
});
855+
});
813856
});

0 commit comments

Comments
 (0)