Skip to content

Commit

Permalink
feat(consul support): consul instance store
Browse files Browse the repository at this point in the history
add context to getInstanceGroup call
  • Loading branch information
aaronkvanmeerten committed Dec 6, 2024
1 parent 7088058 commit 2a03961
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ app.put(
body('maxDesired').optional().isInt({ min: 0 }).withMessage('Value must be positive'),
body('desiredCount').optional().isInt({ min: 0 }).withMessage('Value must be positive'),
body().custom(async (value, { req }) => {
if (!(await validator.groupHasValidDesiredInput(req.params.name, value))) {
if (!(await validator.groupHasValidDesiredInput(req.context, req.params.name, value))) {
throw new Error('Desired count must be between min and max; min cannot be grater than max');
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class AutoscaleProcessor {
}

try {
const group = await this.instanceGroupManager.getInstanceGroup(groupName);
const group = await this.instanceGroupManager.getInstanceGroup(ctx, groupName);
if (!group) {
throw new Error(`Group ${groupName} not found, failed to process autoscaling`);
}
Expand Down
16 changes: 8 additions & 8 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class Handlers {
const request: InstanceGroupDesiredValuesRequest = req.body;
const lock: AutoscalerLock = await this.lockManager.lockGroup(req.context, req.params.name);
try {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, req.params.name);
if (instanceGroup) {
if (request.desiredCount != null) {
instanceGroup.scalingOptions.desiredCount = request.desiredCount;
Expand Down Expand Up @@ -268,7 +268,7 @@ class Handlers {

const lock: AutoscalerLock = await this.lockManager.lockGroup(req.context, req.params.name);
try {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context,req.params.name);
if (instanceGroup) {
if (scalingActivitiesRequest.enableAutoScale != null) {
instanceGroup.enableAutoScale = scalingActivitiesRequest.enableAutoScale;
Expand Down Expand Up @@ -298,7 +298,7 @@ class Handlers {
}

async reconfigureInstanceGroup(req: Request, res: Response): Promise<void> {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, req.params.name);
if (instanceGroup) {
if (instanceGroup.enableReconfiguration) {
// add audit item recording the request
Expand Down Expand Up @@ -330,7 +330,7 @@ class Handlers {
const instanceConfigurationUpdateRequest: InstanceConfigurationUpdateRequest = req.body;
const lock: AutoscalerLock = await this.lockManager.lockGroup(req.context, req.params.name);
try {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, req.params.name);
if (instanceGroup) {
instanceGroup.instanceConfigurationId = instanceConfigurationUpdateRequest.instanceConfigurationId;
await this.instanceGroupManager.upsertInstanceGroup(req.context, instanceGroup);
Expand Down Expand Up @@ -390,7 +390,7 @@ class Handlers {
}

async getInstanceGroup(req: Request, res: Response): Promise<void> {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, req.params.name);

if (instanceGroup) {
res.status(200);
Expand All @@ -415,7 +415,7 @@ class Handlers {
async getGroupReport(req: Request, res: Response): Promise<void> {
const groupName = req.params.name;
const ctx = req.context;
const group: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(groupName);
const group: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, groupName);
if (group) {
const groupReport = await this.groupReportGenerator.generateReport(ctx, group, null);
res.status(200);
Expand Down Expand Up @@ -490,7 +490,7 @@ class Handlers {
scaleDownProtectedTTL,
});

const group = await this.instanceGroupManager.getInstanceGroup(groupName);
const group = await this.instanceGroupManager.getInstanceGroup(req.context, groupName);
if (group) {
if (requestBody.instanceConfigurationId != null) {
group.instanceConfigurationId = requestBody.instanceConfigurationId;
Expand Down Expand Up @@ -539,7 +539,7 @@ class Handlers {
const scalingOptionsRequest: InstanceGroupScalingOptionsRequest = req.body;
const lock: AutoscalerLock = await this.lockManager.lockGroup(req.context, req.params.name);
try {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.params.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(req.context, req.params.name);
if (instanceGroup) {
if (scalingOptionsRequest.scaleUpQuantity != null) {
instanceGroup.scalingOptions.scaleUpQuantity = scalingOptionsRequest.scaleUpQuantity;
Expand Down
4 changes: 2 additions & 2 deletions src/instance_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export default class InstanceGroupManager {
return this.instanceStore.upsertInstanceGroup(ctx, group);
}

async getInstanceGroup(groupName: string): Promise<InstanceGroup> {
return this.instanceStore.getInstanceGroup(groupName);
async getInstanceGroup(ctx: Context, groupName: string): Promise<InstanceGroup> {
return this.instanceStore.getInstanceGroup(ctx, groupName);
}

async getAllInstanceGroupsAsMap(ctx: Context): Promise<Map<string, InstanceGroup>> {
Expand Down
2 changes: 1 addition & 1 deletion src/instance_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class InstanceLauncher {
}

async launchOrShutdownInstancesByGroup(ctx: Context, groupName: string): Promise<boolean> {
const group = await this.instanceGroupManager.getInstanceGroup(groupName);
const group = await this.instanceGroupManager.getInstanceGroup(ctx, groupName);
if (!group) {
throw new Error(`Group ${groupName} not found, failed to make launch decisions.`);
}
Expand Down
6 changes: 3 additions & 3 deletions src/instance_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export interface InstanceState {
lastReconfigured?: string;
}

interface InstanceStore {
export interface InstanceStore {
// instance related methods
fetchInstanceGroups: { (): Promise<string[]> };
fetchInstanceGroups: { (ctx: Context): Promise<string[]> };
fetchInstanceStates: { (ctx: Context, group: string): Promise<InstanceState[]> };
saveInstanceStatus: { (ctx: Context, group: string, state: InstanceState): Promise<boolean> };
filterOutAndTrimExpiredStates: { (ctx: Context, group: string, states: InstanceState[]): Promise<InstanceState[]> };
Expand Down Expand Up @@ -165,7 +165,7 @@ interface InstanceStore {
// group related methods
existsAtLeastOneGroup: { (): Promise<boolean> };
upsertInstanceGroup: { (ctx: Context, group: InstanceGroup): Promise<boolean> };
getInstanceGroup: { (groupName: string): Promise<InstanceGroup> };
getInstanceGroup: { (ctx: Context, groupName: string): Promise<InstanceGroup> };
getAllInstanceGroups: { (ctx: Context): Promise<InstanceGroup[]> };
getAllInstanceGroupNames: { (ctx: Context): Promise<string[]> };
deleteInstanceGroup: { (ctx: Context, groupName: string): Promise<void> };
Expand Down
4 changes: 2 additions & 2 deletions src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default class RedisStore implements MetricsStore, InstanceStore {
return instanceStatesResponse;
}

async fetchInstanceGroups(): Promise<string[]> {
async fetchInstanceGroups(_ctx: Context): Promise<string[]> {
const groups = await this.redisClient.keys('instances:status:*');
return groups.map((group) => group.split(':')[2]);
}
Expand Down Expand Up @@ -435,7 +435,7 @@ export default class RedisStore implements MetricsStore, InstanceStore {
return true;
}

async getInstanceGroup(groupName: string): Promise<InstanceGroup> {
async getInstanceGroup(_ctx: Context, groupName: string): Promise<InstanceGroup> {
const result = await this.redisClient.hget(this.GROUPS_HASH_NAME, groupName);
if (result !== null && result.length > 0) {
return JSON.parse(result);
Expand Down
2 changes: 1 addition & 1 deletion src/sanity_loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class SanityLoop {
}

async reportUntrackedInstances(ctx: Context, groupName: string): Promise<boolean> {
const group: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(groupName);
const group: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(ctx, groupName);
if (group) {
const cloudStart = process.hrtime();
ctx.logger.info(`Retrieving ${group.cloud} instances for ${groupName}`);
Expand Down
2 changes: 1 addition & 1 deletion src/scaling_options_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class ScalingManager {
}

try {
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(group.name);
const instanceGroup = await this.instanceGroupManager.getInstanceGroup(ctx, group.name);
if (instanceGroup) {
ScalingManager.setNewScalingOptions(ctx, request.options, instanceGroup, request.direction);
await this.instanceGroupManager.upsertInstanceGroup(ctx, instanceGroup);
Expand Down
8 changes: 6 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export default class Validator {
return desiredCount >= minDesired && desiredCount <= maxDesired && minDesired <= maxDesired;
}

async groupHasValidDesiredInput(name: string, request: InstanceGroupDesiredValuesRequest): Promise<boolean> {
const instanceGroup: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(name);
async groupHasValidDesiredInput(
ctx: Context,
name: string,
request: InstanceGroupDesiredValuesRequest,
): Promise<boolean> {
const instanceGroup: InstanceGroup = await this.instanceGroupManager.getInstanceGroup(ctx, name);

const minDesired = request.minDesired != null ? request.minDesired : instanceGroup.scalingOptions.minDesired;
const maxDesired = request.maxDesired != null ? request.maxDesired : instanceGroup.scalingOptions.maxDesired;
Expand Down

0 comments on commit 2a03961

Please sign in to comment.