Skip to content

Commit 64cdfad

Browse files
authored
Merge pull request #1 from truefoundry/np-add-initial-policy
Initial policy template to run and execute
2 parents efe30e5 + 77ea8cb commit 64cdfad

13 files changed

+10257
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
lib

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/models.ts

README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
# tfy-typescript-policy
1+
# TypeScript Policy
2+
3+
This example is meant to be a boilerplate for your own typescript policy for validation or mutation on truefoundry.
4+
5+
All the required models can be imported from {./src/models.ts}
6+
7+
# How to build
8+
9+
After extracting the example, make sure that the dependencies are installed with:
10+
```
11+
npm install
12+
```
13+
14+
Now compile the policy and generate the kubernetes manifests:
15+
```
16+
npm run compile
17+
18+
> ts-example-policy@0.1.0 compile
19+
> tsc && webpack --config webpack.config.js
20+
asset bundle.js 857 bytes [compared for emit] [minimized] (name: main)
21+
./lib/index.js 223 bytes [built] [code generated]
22+
./lib/policy.js 726 bytes [built] [code generated]
23+
webpack 5.36.1 compiled successfully in 229 ms
24+
```
25+
26+
# Run the policy locally
27+
```Local Run
28+
npx ts-node local_run.ts
29+
```

jest.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
transform: {
3+
"^.+\\.tsx?$": "ts-jest",
4+
},
5+
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6+
testPathIgnorePatterns: ["/lib/", "/node_modules/"],
7+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
8+
collectCoverage: true,
9+
};

local_run.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Service, Workspace } from './src/models';
2+
import { validate } from './src/policy';
3+
import { ValidationContext } from './src/types';
4+
5+
const dummyWorkspace: Workspace = {
6+
type: 'workspace',
7+
cluster_fqn: 'test-cluster',
8+
name: 'test-workspace'
9+
}
10+
11+
const dummyValidationContext: ValidationContext = {
12+
entity: 'service',
13+
action: 'apply',
14+
filters: {
15+
env_name: 'dev',
16+
}
17+
}
18+
19+
// Sample service with GitHub build source (should throw error)
20+
const githubService: Service = {
21+
type: 'service',
22+
name: 'test-service',
23+
image: {
24+
type: 'build',
25+
build_spec: {
26+
type: 'dockerfile',
27+
dockerfile_path: 'Dockerfile',
28+
build_context_path: '.'
29+
},
30+
build_source: {
31+
type: 'git',
32+
repo_url: 'https://github.com/user/repo',
33+
ref: 'main',
34+
}
35+
},
36+
ports: [
37+
{
38+
port: 8080,
39+
protocol: 'TCP',
40+
expose: true
41+
}
42+
],
43+
auto_shutdown: {
44+
wait_time: 300
45+
}
46+
};
47+
48+
const validationInput = {
49+
manifest: githubService,
50+
context: dummyValidationContext
51+
}
52+
53+
try {
54+
const validatedService = validate(validationInput); // will throw error that auto_shutdown wait_time should be less than 300 seconds for the dev environment
55+
console.log(validatedService);
56+
} catch (error) {
57+
console.error(error);
58+
}

0 commit comments

Comments
 (0)