Skip to main content

The regulator of your Docker Swarm cluster

Project description

Swarm Regulator

Regulate your Docker Swarm cluster, according to YOUR rules. Use async Python functions to enforce rules like service placement and resource management on your Docker Swarm.



Each regulator is based on top of a single consumer. The consumer responds to Docker events and instruments all components of the regulator.


A regulator needs to know when to apply its rules. This is what conditions are for. Conditions help us know when should a rule be applied on a Docker Swarm entity. In brief, a condition is a Python function (or callable) that accepts a Docker Swarm entity as a single argument and returns True when the rule should run, and False when it shouldn't.

🙌 Heads up: In order to avoid infinite loops, a confition that initially returns True, should return False, when it gets the regulated entity as an argument. If this is not the case, the rule is being ignored.


Rules simply do the job. Rules are async Python functions that accept a Docker Swarm entity as an argument and return it modified, so it complies with the business logic we intend to enforce on our Docker Swarm cluster.


  • Python 3.8+
  • Docker Swarm Mode cluster



pipenv install swarm-regulator


poetry add swarm-regulator


pip install swarm-regulator


  1. Create a Python file (e.g.
  2. Import the consumer from the swarm_regulator package
  3. Register your rules that will run according to given conditions
  4. Run your regulator


from swarm_regulator import consumer

def _extract_constraints(service_spec):
    return service_spec["TaskTemplate"]["Placement"].get("Constraints", [])

def has_not_constraints(service_spec) -> bool:
    constraints = _extract_constraints(service_spec)
    return not len(constraints)

async def do_not_schedule_on_gpu(service_spec):
    constraints = _extract_constraints(service_spec) + ["node.labels.gpu!=true"]
    service_spec["TaskTemplate"]["Placement"]["Constraints"] = constraints
    return service_spec

    "service", has_not_constraints, do_not_schedule_on_gpu,



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swarm-regulator-0.2.3.tar.gz (5.2 kB view hashes)

Uploaded source

Built Distribution

swarm_regulator-0.2.3-py3-none-any.whl (6.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page