Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial policy template to run and execute #1

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
lib
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/models.ts
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# tfy-typescript-policy
# TypeScript Policy

This example is meant to be a boilerplate for your own typescript policy for validation or mutation on truefoundry.

All the required models can be imported from {./src/models.ts}

# How to build

After extracting the example, make sure that the dependencies are installed with:
```
npm install
```

Now compile the policy and generate the kubernetes manifests:
```
npm run compile

> ts-example-policy@0.1.0 compile
> tsc && webpack --config webpack.config.js
asset bundle.js 857 bytes [compared for emit] [minimized] (name: main)
./lib/index.js 223 bytes [built] [code generated]
./lib/policy.js 726 bytes [built] [code generated]
webpack 5.36.1 compiled successfully in 229 ms
```

# Run the policy locally
```Local Run
npx ts-node local_run.ts
```
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
testPathIgnorePatterns: ["/lib/", "/node_modules/"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
collectCoverage: true,
};
58 changes: 58 additions & 0 deletions local_run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Service, Workspace } from './src/models';
import { validate } from './src/policy';
import { ValidationContext } from './src/types';

const dummyWorkspace: Workspace = {
type: 'workspace',
cluster_fqn: 'test-cluster',
name: 'test-workspace'
}

const dummyValidationContext: ValidationContext = {
entity: 'service',
action: 'apply',
filters: {
env_name: 'dev',
}
}

// Sample service with GitHub build source (should throw error)
const githubService: Service = {
type: 'service',
name: 'test-service',
image: {
type: 'build',
build_spec: {
type: 'dockerfile',
dockerfile_path: 'Dockerfile',
build_context_path: '.'
},
build_source: {
type: 'git',
repo_url: 'https://github.com/user/repo',
ref: 'main',
}
},
ports: [
{
port: 8080,
protocol: 'TCP',
expose: true
}
],
auto_shutdown: {
wait_time: 300
}
};

const validationInput = {
manifest: githubService,
context: dummyValidationContext
}

try {
const validatedService = validate(validationInput); // will throw error that auto_shutdown wait_time should be less than 300 seconds for the dev environment
console.log(validatedService);
} catch (error) {
console.error(error);
}
Loading