Skip to content

Commit

Permalink
Merge pull request #121 from duplocloud/DUPLO-25387
Browse files Browse the repository at this point in the history
DUPLO-25387: Configure load balancer to a service
  • Loading branch information
shantanuduplo authored Jan 16, 2025
2 parents 099077f + abd9716 commit 83dcde4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for SSM Parameter CRUD operations.
- Added support for updating the environment variables of a lambda.
- start, stop, restart for service with `--wait`
- Added support for configuring a load balancer for a service.

## [0.2.41] - 2024-12-06

Expand Down
60 changes: 60 additions & 0 deletions src/duplo_resource/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,63 @@ def wait_check():

def name_from_body(self, body):
return body["Name"]

@Command()
def expose(self,
name: args.NAME,
container_port: args.CONTAINER_PORT=None,
external_port: args.EXTERNAL_PORT=None,
lb_type: args.LOAD_BALANCER_TYPE=None,
protocol: args.PROTOCOL=None,
visibility: args.LOAD_BALANCER_VISIBILITY="public",
mode: args.LOAD_BALANCER_MODE="docker-mode",
health_check_url: args.HEALTH_CHECK_URL=None) -> dict:
"""
Expose a service.
Usage: Basic CLI Use
```sh
duploctl service expose <service-name> --lb-type applicationlb --container-port 80 --external-port 80 \
--visibility public --mode docker-mode --health-check-url / --protocol http
```
Args:
container-port (int): The internal port of the container to expose.
external-port (int): The external port exposed by the load balancer. This is not used for targetgrouponly or k8clusterip load balancer types.
lb-type (str): The load balancer type. Valid options are ['applicationlb', 'k8clusterip', 'k8nodeport', 'networklb', 'targetgrouponly'].
protocol (str): The protocol to use, based on `lb_type`
- applicationlb: http, https
- networklb: tcp, udp, tls
- targetgrouponly: http, https
- k8clusterip: tcp, udp
- k8nodeport: tcp, udp
visibility (str): The load balancer visibility. Valid options are 'public' or 'private'.
mode (str): The load balancer application mode. Valid options are 'docker-mode' or 'native-app'.
health_check_url (str): The health check URL path. This must be empty for networklb, as it does not support health check paths.
"""
service = self.find(name)
tenant_id = service["TenantId"]
lb_type_map = {
"applicationlb": 1,
"k8clusterip": 3,
"k8nodeport": 4,
"networklb": 6,
"targetgrouponly": 7
}

if lb_type not in lb_type_map:
raise DuploError(f"Invalid lb_type: {lb_type}. Must be one of {list(lb_type_map.keys())}")

payload = {
"LbType": lb_type_map[lb_type],
"Port": container_port,
"ExternalPort": external_port if external_port is not None else None,
"IsInternal": visibility == "private",
"IsNative": mode == "native-app",
"Protocol": protocol,
"HealthCheckUrl": health_check_url if lb_type != "networklb" else None,
"ReplicationControllerName": name
}

self.duplo.post(f"subscriptions/{tenant_id}/LBConfigurationUpdate", payload)
return {"message": f"Successfully exposed service '{name}'"}
27 changes: 27 additions & 0 deletions src/duplocloud/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@
S3KEY = Arg("key",
help='The s3 key to use')

HEALTH_CHECK_URL = Arg("health-check-url", "--health-check-url",
help='The health check URL')

CONTAINER_PORT = Arg("container-port", "--container-port",
help='Container port')

EXTERNAL_PORT = Arg("external-port", "--external-port",
help='The external port')

PROTOCOL = Arg("protocol", "--protocol",
help='The protocol',
choices=['http','https','tcp','udp','tls'])

LOAD_BALANCER_TYPE = Arg("lb-type", "--lb-type",
help='The type of load balancer. Valid options are [applicationlb, k8clusterip, k8nodeport, networklb, targetgrouponly].',
choices=['applicationlb', 'k8clusterip', 'k8nodeport', 'networklb', 'targetgrouponly'])

LOAD_BALANCER_VISIBILITY = Arg("visibility", "--visibility",
help='The visibility of load balancer. Valid options are \"public\" or \"private\". Default is public.',
choices=['public', 'private'],
default = 'public')

LOAD_BALANCER_MODE = Arg("mode", "--mode",
help='The mode of load balancer. Valid options are \"docker-mode\" or \"native-app\". Default is docker-mode.',
choices=['docker-mode', 'native-app'],
default = 'docker-mode')

SERVICEIMAGE = Arg("serviceimage", "-S",
help='takes two arguments, a service name and an image:tag',
action='append',
Expand Down

0 comments on commit 83dcde4

Please sign in to comment.