Skip to content

Commit 113812c

Browse files
authored
Merge pull request #1206 from golemfactory/scx1332/custom-constraints
Add possibility of adding custom constraint to workload demand
2 parents 12a22e8 + 07a0dd4 commit 113812c

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

src/market/demand/directors/workload-demand-director-config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WorkloadDemandDirectorConfigOptions } from "../options";
1+
import { CustomConstraint, WorkloadDemandDirectorConfigOptions } from "../options";
22
import { GolemConfigError } from "../../../shared/error/golem-error";
33
import { BaseConfig } from "./base-config";
44

@@ -35,11 +35,15 @@ export class WorkloadDemandDirectorConfig extends BaseConfig {
3535
readonly imageTag?: string;
3636
readonly imageUrl?: string;
3737

38+
readonly customConstraints: CustomConstraint[];
39+
3840
constructor(options: Partial<WorkloadDemandDirectorConfigOptions> & RequiredWorkloadDemandConfigOptions) {
3941
super();
4042

4143
Object.assign(this, options);
4244

45+
this.customConstraints = options.customConstraints ?? [];
46+
4347
if (!options.runtime?.name) {
4448
this.runtime.name = this.engine;
4549
}

src/market/demand/directors/workload-demand-director.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DemandBodyBuilder } from "../demand-body-builder";
1+
import { ComparisonOperator, DemandBodyBuilder } from "../demand-body-builder";
22
import { WorkloadDemandDirector } from "./workload-demand-director";
33
import { WorkloadDemandDirectorConfig } from "./workload-demand-director-config";
44

@@ -142,4 +142,26 @@ describe("ActivityDemandDirector", () => {
142142

143143
expect(decorations.constraints).toEqual(expect.arrayContaining(["(golem.runtime.name=vm-test)"]));
144144
});
145+
146+
test("should add custom constraints", async () => {
147+
const builder = new DemandBodyBuilder();
148+
149+
const director = new WorkloadDemandDirector(
150+
new WorkloadDemandDirectorConfig({
151+
expirationSec: 600,
152+
imageHash: "529f7fdaf1cf46ce3126eb6bbcd3b213c314fe8fe884914f5d1106d4",
153+
customConstraints: [
154+
{ name: "golem.custom.constraint1", value: "value1", comparator: ComparisonOperator.Eq },
155+
{ name: "golem.custom.constraint2", value: 10, comparator: ComparisonOperator.GtEq },
156+
],
157+
}),
158+
);
159+
await director.apply(builder);
160+
161+
const decorations = builder.getProduct();
162+
163+
expect(decorations.constraints).toEqual(
164+
expect.arrayContaining(["(golem.custom.constraint1=value1)", "(golem.custom.constraint2>=10)"]),
165+
);
166+
});
145167
});

src/market/demand/directors/workload-demand-director.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class WorkloadDemandDirector implements IDemandDirector {
3636
}
3737

3838
this.addManifestDecorations(builder);
39+
40+
for (const constr of this.config.customConstraints) {
41+
builder.addConstraint(constr.name, constr.value, constr.comparator);
42+
}
3943
}
4044

4145
private async resolveTaskPackageFromCustomUrl(): Promise<string> {

src/market/demand/options.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RequireAtLeastOne } from "../../shared/utils/types";
2+
import { ComparisonOperator } from "./demand-body-builder";
23

34
/**
45
* Specifies a set of options related to computation resources that will be used to form the demand
@@ -14,6 +15,18 @@ export type ResourceDemandOptions = {
1415
minCpuCores: number;
1516
};
1617

18+
/**
19+
* User defined constraints for demand
20+
*/
21+
export type CustomConstraint = {
22+
/** Name of the constraint */
23+
name: string;
24+
/** Value of the constraint */
25+
value: string | number;
26+
/** Type of comparison */
27+
comparator: ComparisonOperator;
28+
};
29+
1730
/**
1831
* Specifies a set of options related to runtime configuration that will be used to form the demand
1932
*/
@@ -38,6 +51,9 @@ export type RuntimeDemandOptions = {
3851

3952
/** Required providers capabilities to run application: example: ["vpn"] */
4053
capabilities: string[];
54+
55+
/** Specify additional, custom constraints added to demand */
56+
customConstraints: CustomConstraint[];
4157
};
4258

4359
/**

src/market/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { PaymentDemandDirector } from "./demand/directors/payment-demand-directo
1313
export { WorkloadDemandDirector } from "./demand/directors/workload-demand-director";
1414
export * from "./proposal/market-proposal-event";
1515
export * from "./scan";
16+
export { ComparisonOperator } from "./demand/demand-body-builder";

0 commit comments

Comments
 (0)