⚠️ This service is currently not maintained - please reach out to the Keptn community via https://slack.keptn.sh if you want to take ownership of this repository and update it to make it work with the latest version of Keptn ⚠️
This is a POC of a Keptn Service Template written in Python. Follow the instructions below for writing your own Keptn integration.
Quick start:
- In case you want to contribute your service to keptn-sandbox or keptn-contrib, make sure you have read and understood the Contributing Guidelines.
- Click Use this template on top of the repository, or download the repo as a zip-file, extract it into a new folder named after the service you want to create (e.g., simple-service)
- Replace every occurrence of (docker) image names and tags from
keptnsandbox/keptn-service-template-python
to your docker organization and image name (e.g.,yourorganization/simple-service
) - Replace every occurrence of
keptn-service-template-python
with the name of your service (e.g.,simple-service
) - Optional (but recommended): Create a git repo (e.g., on
github.com/your-username/simple-service
) - Àdapt the go.mod file and change
example.com/
to the actual package name (e.g.,github.com/your-username/simple-service
) - Add yourself to the CODEOWNERS file
- Initialize a git repository:
git init .
git add .
git commit -m "Initial Commit"
- Optional: Push your code an upstream git repo (e.g., GitHub) and adapt all links that contain
github.com
(e.g., togithub.com/your-username/simple-service
) - Figure out whether your Kubernetes Deployment requires any RBAC rules or a different service-account, and adapt deploy/service.yaml accordingly (initial setup is
serviceAccountName: keptn-default
). - Last but not least: Remove this intro within the README file and make sure the README file properly states what this repository is about
def some_task_triggered(keptn: Keptn, shkeptncontext: str, event, data):
# do something
Keptn.on('some_task.triggered', some_task_triggered)
def deployment_triggered(keptn: Keptn, shkeptncontext: str, event, data):
print("In deployment_triggered:")
print(" ", shkeptncontext)
print(" ", event)
print(" ", data)
keptn.send_task_started_cloudevent(message="Deployment Started")
time.sleep(10)
keptn.send_task_finished_cloudevent(message="Deployment finished")
if __name__ == "__main__":
Keptn.on('deployment.triggered', deployment_triggered)
print("Running on port", PORT, "on path", PATH)
app.run(port=PORT)
def deployment_triggered(keptn: Keptn, shkeptncontext: str, event, data):
print("In deployment_triggered:")
print(" ", shkeptncontext)
print(" ", event)
print(" ", data)
keptn.send_task_started_cloudevent(message="Deployment Started")
# keptn add-resource --project=XYZ --resource=project-resource.txt
project_resource = keptn.get_project_resource('project-resource.txt')
print("project_resource=", project_resource)
# keptn add-resource --project=XYZ --stage=STAGE --resource=stage-resource.txt
stage_resource = keptn.get_stage_resource('stage-resource.txt')
print("stage_resource=", stage_resource)
# keptn add-resource --project=XYZ --stage=STAGE --service=SERVICE --resource=service-resource.txt
service_resource = keptn.get_service_resource('service-resource.txt')
print("service_resource=", service_resource)
# do something with the files
keptn.send_task_finished_cloudevent(message="Deployment finished")
This implements a keptn-service-template-python for Keptn. If you want to learn more about Keptn visit us on keptn.sh
Please fill in your versions accordingly
Keptn Version | keptn-service-template-python Docker Image |
---|---|
0.6.1 | keptnsandbox/keptn-service-template-python:0.1.0 |
0.7.1 | keptnsandbox/keptn-service-template-python:0.1.1 |
0.7.2 | keptnsandbox/keptn-service-template-python:0.1.2 |
The keptn-service-template-python can be installed as a part of Keptn's uniform.
To deploy the current version of the keptn-service-template-python in your Keptn Kubernetes cluster, apply the deploy/service.yaml
file:
kubectl apply -f deploy/service.yaml
This should install the keptn-service-template-python
together with a Keptn distributor
into the keptn
namespace, which you can verify using
kubectl -n keptn get deployment keptn-service-template-python -o wide
kubectl -n keptn get pods -l run=keptn-service-template-python
Adapt and use the following command in case you want to up- or downgrade your installed version (specified by the $VERSION
placeholder):
kubectl -n keptn set image deployment/keptn-service-template-python keptn-service-template-python=keptnsandbox/keptn-service-template-python:$VERSION --record
To delete a deployed keptn-service-template-python, use the file deploy/*.yaml
files from this repository and delete the Kubernetes resources:
kubectl delete -f deploy/service.yaml
Development can be conducted using any GoLang compatible IDE/editor (e.g., Jetbrains GoLand, VSCode with Go plugins).
It is recommended to make use of branches as follows:
master
/main
contains the latest potentially unstable versionrelease-*
contains a stable version of the service (e.g.,release-0.1.0
contains version 0.1.0)- create a new branch for any changes that you are working on, e.g.,
feature/my-cool-stuff
orbug/overflow
- once ready, create a pull request from that branch back to the
master
branch
When writing code, it is recommended to follow the coding style suggested by the Golang community.
- Install python3 and python3-venv (sometimes also called python3-virtualenv)
- Create a virtual python environment:
virtualenv -p python3 venv
- Activate the virtual environment (you need to do this everytime before you start):
source venv/bin/ativate
(Note: the command might be different on Windows) - Install dependencies:
pip3 install -r requirements.txt
- Run :)
python3 main.py
Example output:
$ python3 main.py
Subscribing to deployment.triggered
Running on port 8080 on path /
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
Note: This program is capable of running without the Keptn distributor. For this to work, you need to provide two environment variables:
export KEPTN_ENDPOINT=http://1.2.3.4.nip.io/api
export KEPTN_API_TOKEN=<...>
See https://keptn.sh/docs/0.8.x/reference/cli/#authenticate-keptn-cli on how to get those.
Example output:
$ python3 main.py
Subscribing to deployment.triggered
Found environment variables KEPTN_ENDPOINT and KEPTN_API_TOKEN, polling events from API
Starting to poll...
Exit using CTRL-C
Polling events for event type deployment.triggered
Polling events for event type deployment.triggered
Polling events for event type deployment.triggered
...
ModuleNotFoundError: No module named 'flask'
You most likely forgot to create or activate the virtual environment or install dependencies.
You can get started developing without installing Python, but using Docker:
docker build . -t keptn-service-template-python:latest
docker run --rm -it -v "$(pwd)":"/app" keptn-service-template-python:latest bash
This spawns a new bash within a Python Container.
Your first entrypoint is main.py. Within this file you can add implementation for Keptn Cloud events.
To better understand all variants of Keptn CloudEvents, please look at the Keptn Spec.
- Build the docker image:
docker build . -t keptnsandbox/keptn-service-template-python:dev
(Note: Ensure that you use the correct DockerHub account/organization) - Run the docker image locally:
docker run --rm -it -p 8080:8080 keptnsandbox/keptn-service-template-python:dev
- Push the docker image to DockerHub:
docker push keptnsandbox/keptn-service-template-python:dev
(Note: Ensure that you use the correct DockerHub account/organization) - Deploy the service using
kubectl
:kubectl apply -f deploy/
- Delete/undeploy the service using
kubectl
:kubectl delete -f deploy/
- Watch the deployment using
kubectl
:kubectl -n keptn get deployment keptn-service-template-python -o wide
- Get logs using
kubectl
:kubectl -n keptn logs deployment/keptn-service-template-python -f
- Watch the deployed pods using
kubectl
:kubectl -n keptn get pods -l run=keptn-service-template-python
- Deploy the service using Skaffold:
skaffold run --default-repo=your-docker-registry --tail
(Note: Replaceyour-docker-registry
with your DockerHub username; also make sure to adapt the image name in skaffold.yaml)
We have dummy cloud-events in the form of RFC 2616 requests in the test-events/ directory. These can be easily executed using third party plugins such as the Huachao Mao REST Client in VS Code.
This repo uses reviewdog for automated reviews of Pull Requests.
You can find the details in .github/workflows/reviewdog.yml.
This repo has automated unit tests for pull requests.
You can find the details in .github/workflows/tests.yml.
This repo uses GH Actions and Workflows to test the code and automatically build docker images.
Docker Images are automatically pushed based on the configuration done in .ci_env and the two GitHub Secrets
REGISTRY_USER
- your DockerHub usernameREGISTRY_PASSWORD
- a DockerHub access token (alternatively, your DockerHub password)
It is assumed that the current development takes place in the master branch (either via Pull Requests or directly).
To make use of the built-in automation using GH Actions for releasing a new version of this service, you should
- branch away from master to a branch called
release-x.y.z
(wherex.y.z
is your version), - write release notes in the releasenotes/ folder,
- check the output of GH Actions builds for the release branch,
- verify that your image was built and pushed to DockerHub with the right tags,
- update the image tags in [deploy/service.yaml], and
- test your service against a working Keptn installation.
If any problems occur, fix them in the release branch and test them again.
Once you have confirmed that everything works and your version is ready to go, you should
- create a new release on the release branch using the GitHub releases page, and
- merge any changes from the release branch back to the master branch.
Please find more information in the LICENSE file.