Created
April 20, 2023 20:12
-
-
Save deemp/a2236a42e6aba1cc981c2368baa8b856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Creating a Kubernetes Deployment | |
""" | |
import pulumi | |
from pulumi_kubernetes.apps.v1 import Deployment, DeploymentSpecArgs | |
import pulumi_kubernetes.core.v1 as k8s | |
from pulumi_kubernetes.meta.v1 import ObjectMetaArgs, LabelSelectorArgs | |
from pulumi_kubernetes.provider import Provider | |
from pulumi import ResourceOptions | |
import pulumi | |
import pulumi_kubernetes as kubernetes | |
import yaml | |
def mk_back(back_config, environment, render_provider): | |
name = back_config["name"] | |
full_name = f"{name}-{environment}" | |
deployment_config = back_config["deployment"] | |
container_config = deployment_config["container"] | |
service_config = back_config["service"] | |
config_file = back_config["config_file"] | |
# TODO maybe need to share app? | |
labels = {"environment": environment, "app": name} | |
back_config_map = ( | |
lambda name=f"{full_name}-configMap": k8s.ConfigMap( | |
resource_name=name, | |
api_version="apps/v1", | |
kind="ConfigMap", | |
metadata=ObjectMetaArgs( | |
name=name, | |
labels=labels, | |
), | |
data={container_config["config"]["file"]: str(yaml.dump(config_file))}, | |
opts=ResourceOptions(provider=render_provider), | |
) | |
)() | |
port = config_file["web"]["port"] | |
port_name = f"{full_name}-container-port" | |
back_deployment = ( | |
lambda name=f"{full_name}-deployment": Deployment( | |
resource_name=name, | |
api_version="apps/v1", | |
kind="Deployment", | |
metadata=ObjectMetaArgs( | |
name=name, | |
), | |
spec=DeploymentSpecArgs( | |
replicas=1, | |
selector=LabelSelectorArgs( | |
match_labels=labels, | |
), | |
template=k8s.PodTemplateSpecArgs( | |
metadata=ObjectMetaArgs( | |
labels=labels, | |
), | |
spec=( | |
lambda config_volume=f"{full_name}-config-volume": k8s.PodSpecArgs( | |
containers=[ | |
( | |
lambda container_name=container_config[ | |
"name" | |
], docker_hub_image=container_config[ | |
"dockerHubImage" | |
]: k8s.ContainerArgs( | |
name=container_name, | |
image=docker_hub_image, | |
image_pull_policy="Always", | |
ports=[ | |
k8s.ContainerPortArgs( | |
container_port=port, | |
name=port_name, | |
) | |
], | |
volume_mounts=[ | |
k8s.VolumeMountArgs( | |
mount_path=container_config["config"][ | |
"mountPath" | |
], | |
name=config_volume, | |
) | |
], | |
env=[ | |
k8s.EnvVarArgs( | |
name=container_config["config"][ | |
"varName" | |
], | |
value=container_config["config"][ | |
"mountPath" | |
], | |
) | |
], | |
resources=k8s.ResourceRequirementsArgs( | |
requests={ | |
"memory": "64Mi", | |
"cpu": "250m", | |
}, | |
limits={ | |
"memory": "128Mi", | |
"cpu": "500m", | |
}, | |
), | |
) | |
)() | |
], | |
volumes=[ | |
k8s.VolumeArgs( | |
name=config_volume, | |
config_map=k8s.ConfigMapVolumeSourceArgs( | |
name=back_config_map.metadata.name | |
), | |
) | |
], | |
) | |
)(), | |
), | |
), | |
opts=ResourceOptions(provider=render_provider), | |
) | |
)() | |
back_service = ( | |
lambda name=f"{full_name}-service": k8s.Service( | |
resource_name=name, | |
api_version="v1", | |
kind="Service", | |
metadata=ObjectMetaArgs( | |
name=name, | |
labels=labels, | |
), | |
spec=k8s.ServiceSpecArgs( | |
type=service_config["type"], | |
ports=[ | |
k8s.ServicePortArgs( | |
port=service_config["port"], | |
name=port_name, | |
target_port=port, | |
node_port=service_config["nodePort"], | |
) | |
], | |
selector=labels, | |
), | |
opts=ResourceOptions(provider=render_provider), | |
) | |
)() | |
render_provider = Provider("k8s-yaml-rendered", render_yaml_to_directory="yaml") | |
config = pulumi.Config("dev") | |
environment = config.require("environment") | |
mk_back( | |
back_config=config.require_object("back"), | |
environment=environment, | |
render_provider=render_provider, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ $environment := print .Values.environment }} | |
{{ $appName := .Values.app }} | |
{{ $app := print $appName "-" .Values.environment }} | |
{{ $configMapName := print $app "-configmap" }} | |
{{ $container := .Values.container }} | |
{{ $service := .Values.service }} | |
{{ $containerConfig := $container.config }} | |
{{ $containerPortName := print $app "-port" }} | |
{{ $volumeName := print $app "-volume" }} | |
{{ $pvcName := print $app "-pvc" }} | |
{{ $db := .Values.db }} | |
{{ $labels := (dict "environment" $environment "app" $appName) | toYaml }} | |
{{ $labelsBlock := (dict "labels" ($labels | fromYaml)) | toYaml }} | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: {{ $configMapName }} | |
{{ $labelsBlock | indent 2 }} | |
data: | |
POSTGRES_DB: {{ $db.POSTGRES_DB }} | |
POSTGRES_USER: {{ $db.POSTGRES_USER }} | |
POSTGRES_PASSWORD: {{ $db.POSTGRES_PASSWORD }} | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: {{ $app }}-deployment | |
spec: | |
replicas: {{ .Values.replicaCount }} | |
selector: | |
matchLabels: | |
{{ $labels | indent 6 }} | |
template: | |
metadata: | |
{{ $labelsBlock | indent 6 }} | |
spec: | |
containers: | |
- name: postgres | |
image: "{{ $container.image }}:{{ $container.tag }}" | |
imagePullPolicy: "IfNotPresent" | |
ports: | |
- containerPort: {{ $db.port }} | |
name: {{ $containerPortName }} | |
envFrom: | |
- configMapRef: | |
name: {{ $configMapName }} | |
volumeMounts: | |
- mountPath: {{ $container.volumeMounts.mountPath }} | |
name: {{ $volumeName }} | |
resources: | |
requests: | |
memory: "64Mi" | |
cpu: "250m" | |
limits: | |
memory: "128Mi" | |
cpu: "500m" | |
volumes: | |
- name: {{ $volumeName }} | |
persistentVolumeClaim: | |
claimName: {{ $pvcName }} | |
--- | |
kind: PersistentVolume | |
apiVersion: v1 | |
metadata: | |
name: {{ $pvName }} | |
labels: | |
type: local | |
{{ $labels | indent 4 }} | |
spec: | |
storageClassName: manual | |
capacity: | |
storage: 5Gi | |
accessModes: | |
- ReadWriteMany | |
hostPath: | |
path: "/mnt/data" | |
--- | |
kind: PersistentVolumeClaim | |
apiVersion: v1 | |
metadata: | |
name: {{ $pvcName }} | |
{{ $labelsBlock | indent 2 }} | |
spec: | |
storageClassName: manual | |
accessModes: | |
- ReadWriteMany | |
resources: | |
requests: | |
storage: 5Gi | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: {{ $app }}-service | |
{{ $labelsBlock | indent 2 }} | |
spec: | |
type: NodePort | |
ports: | |
- port: {{ $service.port }} | |
name: {{ $containerPortName }} | |
targetPort: {{ $db.port }} | |
nodePort: {{ $service.nodePort }} | |
selector: | |
{{ $labels | indent 4 }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment