https://pulumi.com logo
#azure
Title
# azure
p

proud-portugal-76914

12/28/2023, 1:38 PM
Iam new to iac. Can somebody help plz. pulumi code is invoked by rest api. here is the simplified version of the code: first keycloak is deployed by calling @app.route('/kc', methods=['POST']) routes.py:
@app.route('/kc', methods=['POST'])
def deploy():
//args are coming from request.json
threading.Thread(target=deploy_kc.deploy, args=(
keycloak_namespace, aks_cluster_name, resource_group_name, keycloak_yaml, project_name, stack_name))
deploy_kc.py:
def deploy_keycloak():
release = Chart(
"keycloak",
ChartOpts(
chart="keycloakx",
version="2.3.0",
fetch_opts=FetchOpts(
repo="<https://codecentric.github.io/helm-charts>"
),
values=yaml.safe_load(get_config('keycloak_yaml')),
namespace=get_config('keycloak_namespace')
)  # , opts=pulumi.ResourceOptions(depends_on=[mysql_chart])
)
def deploy(keycloak_namespace, aks_cluster_name, resource_group_name, keycloak_yaml, project_name, stack_name):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
subprocess.run(f"az aks get-credentials -n {aks_cluster_name} -g {resource_group_name} --overwrite-existing",
shell=True, check=True)
stack = auto.create_or_select_stack(
stack_name=stack_name,
project_name=project_name,
program=deploy_keycloak
)
stack.refresh()
up_res = stack.up()
print(f"update summary: {up_res.summary.resource_changes}")
// update summary: {'create': 7, 'delete': 1, 'same': 1}
then springboot app is deployed by calling: @app.route('/deploy_img', methods=['POST']) routes.py:
@app.route('/deploy_image', methods=['POST'])
def deploy_resources():
data = request.json
threading.Thread(target=deploy_docker_img.deploy, args=(data,)).start()
deploy_docker_img.py
def deploy(request_data):
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
set_config("request_data", request_data)
subprocess.run(f"az aks get-credentials -n {aks_cluster_name} -g {resource_group_name} --overwrite-existing",
shell=True, check=True)
stack = auto.create_or_select_stack(
stack_name=request_data.get('stack_name'),
project_name=request_data.get('project_name'),
program=deploy_resources
)
stack.refresh()
up_res = stack.up()
print(f"update summary: {up_res.summary.resource_changes}")
// update summary: {'create': 3, 'delete': 7, 'same': 1}
def deploy_docker_image():
request_data = get_config('request_data')
app_labels = {"app": request_data.get('app_name')}
config_map = create_config_map(request_data)
# Deployment
deployment = k8s.apps.v1.Deployment(
request_data.get('deployment_name') or request_data.get('app_name') + "-deployment",
metadata={
"namespace": request_data.get('namespace'),
"name": request_data.get('deployment_name') or request_data.get('app_name') + "-deployment",
},
spec={
"selector": {"matchLabels": app_labels},
"replicas": request_data.get('replica_count') or 1,
"template": {
"metadata": {"labels": app_labels},
"spec": {
"containers": [{
"name": request_data.get('app_name'),
"image": request_data.get('image_name'),
"ports": [{"containerPort": request_data.get('port') or 9087}],
"volumeMounts": [{
"name": "config-volume",
"mountPath": "/config"
}],
}],
"volumes": [{
"name": "config-volume",
"configMap": {
"name": config_map.metadata.name
}
}],
"image_pull_secrets": [{"name": request_data.get('image_pull_secret')}]
}
}
}
)
service = k8s.core.v1.Service(
"kcadminapp-service",
metadata={
"namespace": request_data.get('namespace'),
"name": "kcadminapp-service"
},
spec={
"selector": app_labels,
"ports": [{
"protocol": "TCP",
"port": 80,
"targetPort": request_data.get('port') or 9087
}],
"type": "LoadBalancer"
}
)
pulumi.export('deployment_name', deployment.metadata.name)
def create_config_map(request_data):
config_data = {
"application.properties": f"""
server.servlet.context-path=/{request_data.get('app_name')}
....
keycloak.smtp.ssl=false
"""
}
config_map = k8s.core.v1.ConfigMap(
request_data.get('config_map_name') or "kc-admin-config",
metadata={"namespace": request_data.get('namespace'),
"name": request_data.get('config_map_name') or "kc-admin-config"},
data=config_data
)
return config_map