castai / k8s-castai-autobot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MIT License


Cast.AI API Bot Helper πŸ€–

A mechanism intended to run in k8s as a workload (e.g. cronjob) and consume Cast.AI API.
This sub-repo is provided as a blueprint - so one can clone and design their own functions/docker-image.
Please regard this project as a framework, additional functions may be added in the future.

Getting Started

First things first - Install cluster hibernate and configure it. https://github.com/castai/hibernate

  • This project relies on an API key stored in one of the projects secrets.
  • Secret can also be created manually (instructions on how to set key in hibernate project)
  • Defined in both pod and cronjob as follows
      valueFrom:
        secretKeyRef:
          name: castai-hibernate
          key: API_KEY

Next there are several steps to achieve your end-goal of a custom CAST.AI API consuming bot.

  1. Design your function (see src/functions/example.py for an example)
    1. The function is built to digest a BotExecutionConfig (src/models/bot_execution_config.py) to make it easier to define inputs and env vars
    2. The config is passed via a config-map that needs to be deployed (see api-bot-helper-function-config-example_configmap.yaml for an example)
    3. The config can also be passed for testing purposed using src/function_config.json
  2. Build you image using helper_scripts/build-and_push.py
    1. Rename helper_scritps/secrets.env.template to helper_scritps/secrets.env for registry credentials
    2. Set your Image name/tag in helper_scritps/constants.py
    3. Run build-and-push.py to create your image that will be used in the k8s bot workload
    4. Set your image name/tag in either api-bot-helper_pod.yaml or api-bot-helper_cronjob.yaml
      1. you can also create your own type of k8s workload
    5. A configmap like api-bot-helper-function-config-example_configmap.yaml and the workload needs to be deployed in your cluster
  3. In order to test the functionality locally before deploying:
    1. Configure src/function_config.json with right settings
    2. Set the TEST_RUN in main.py to True

Green section below includes a more bare-boned approach - just create whatever script in any language you want. Reference the script as entrypoint in Dockerimage make sure to add the required variables as env_vars as in the example.

Folder Structure - And files documentation

castai-api-bot-helper
β”œβ”€β”€ bare_bones
β”‚β€ƒβ€ƒβ”œβ”€β”€ src
│  │  └── script_example.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ helper_scripts
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ build-and-push.py
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ build-and-push.sh
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ constants.py
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ requirements.txt
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ secrets.env
│  │  └── secrets.env.template
β”‚β€ƒβ€ƒβ”œβ”€β”€ api-bot-helper_cronjob.yaml
β”‚β€ƒβ€ƒβ”œβ”€β”€ api-bot-helper_pod.yaml
│  └── Dockerfile

β”œβ”€β”€ helper_scripts
β”‚β€ƒβ€ƒβ”œβ”€β”€ build-and-push.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ constants.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ requirements.txt
β”‚β€ƒβ€ƒβ”œβ”€β”€ secrets.env
│  └── secrets.env.template
β”œβ”€β”€ src
β”‚β€ƒβ€ƒβ”œβ”€ functions
│  │ └── example.py
β”‚β€ƒβ€ƒβ”œβ”€ models
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ bot.py
│  │  └── bot_execution_config.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ services
β”‚β€ƒβ€ƒβ”‚β€ƒβ€ƒβ”œβ”€β”€ api_requests_svc.py
│  │  └── request_handle_svc.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ constants.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ function_confi.json
β”‚β€ƒβ€ƒβ”œβ”€β”€ main.py
β”‚β€ƒβ€ƒβ”œβ”€β”€ requirements.txt
β”‚β€ƒβ€ƒβ”œβ”€β”€ test_config.env
│  └── test_config.env.template
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ .gitignore
β”œβ”€β”€ api-bot-helper-function-config-example_configmap.yaml
β”œβ”€β”€ api-bot-helper_cronjob.yaml
β”œβ”€β”€ api-bot-helper_pod.yaml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ LICENSE.txt
└── README.md

Item Number Item Name Documentation
1 build-and-push.py Creates docker image for the bot
2 constants.py Constants like the image name/tag
3 requirements.txt requirements for helper scripts
4 secrets.env provides credentials for image registry
5 secrets.env.template .template needs to be removed to get #4
6 example.py an example of a bot function
7 bot.py Bot Class
8 bot_execution_config.py Bot Execution Configuration
9 api_requests_svc.py Used to consume Cast.AI API
10 request_handle_svc.py Actually handles the API request
11 constants.py Bot high-level constants
12 function_config.json Used for local testing
13 main.py Entry point (can be augmented for testing)
14 requirements.txt Requirements.txt for Bot
15 test_config.env Used for testing (cluster_id/API_KEY)
16 test_config.env.template .template needs to be removed to get #15
17 .dockerignore For building the image
18 .gitignore Git ignore for repo
19 api-bot-helper-function-config-example_configmap.yaml Bot ConfigMap example
20 api-bot-helper_cronjob.yaml Bot Cronjob example
21 api-bot-helper_pod.yaml Bot Pod example
22 Dockerfile Dockerfile for building the image
23 LICENSE.txt License File
24 README.md Repo README File

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

About

License:MIT License


Languages

Language:Python 87.2%Language:Shell 7.5%Language:Dockerfile 5.3%