diff --git a/k8s/deployment/build_context b/k8s/deployment/build_context index 96b3b0bb..43681d2c 100755 --- a/k8s/deployment/build_context +++ b/k8s/deployment/build_context @@ -38,6 +38,10 @@ IMAGE_PULL_SECRETS=$(echo "$IMAGE_PULL_SECRETS" | jq .) TRAFFIC_CONTAINER_IMAGE=${TRAFFIC_CONTAINER_IMAGE:-"public.ecr.aws/nullplatform/k8s-traffic-manager:latest"} +# Pod Disruption Budget configuration +PDB_ENABLED=${POD_DISRUPTION_BUDGET_ENABLED:-"false"} +PDB_MAX_UNAVAILABLE=${POD_DISRUPTION_BUDGET_MAX_UNAVAILABLE:-"25%"} + CONTEXT=$(echo "$CONTEXT" | jq \ --arg blue_deployment_id "$BLUE_DEPLOYMENT_ID" \ --arg blue_replicas "$BLUE_REPLICAS" \ @@ -45,12 +49,16 @@ CONTEXT=$(echo "$CONTEXT" | jq \ --arg total_replicas "$REPLICAS" \ --arg traffic_image "$TRAFFIC_CONTAINER_IMAGE" \ --argjson pull_secrets "$IMAGE_PULL_SECRETS" \ + --arg pdb_enabled "$PDB_ENABLED" \ + --arg pdb_max_unavailable "$PDB_MAX_UNAVAILABLE" \ '. + {blue_deployment_id: $blue_deployment_id, blue_replicas: $blue_replicas, green_replicas: $green_replicas, total_replicas: $total_replicas, pull_secrets: $pull_secrets, - traffic_image: $traffic_image + traffic_image: $traffic_image, + pdb_enabled: $pdb_enabled, + pdb_max_unavailable: $pdb_max_unavailable }') DEPLOYMENT_ID=$(echo "$CONTEXT" | jq -r '.deployment.id') diff --git a/k8s/deployment/build_deployment b/k8s/deployment/build_deployment index bcd167da..2312ec34 100755 --- a/k8s/deployment/build_deployment +++ b/k8s/deployment/build_deployment @@ -42,6 +42,7 @@ DEPLOYMENT_PATH="$OUTPUT_DIR/deployment-$SCOPE_ID-$DEPLOYMENT_ID.yaml" SECRET_PATH="$OUTPUT_DIR/secret-$SCOPE_ID-$DEPLOYMENT_ID.yaml" SCALING_PATH="$OUTPUT_DIR/scaling-$SCOPE_ID-$DEPLOYMENT_ID.yaml" SERVICE_TEMPLATE_PATH="$OUTPUT_DIR/service-$SCOPE_ID-$DEPLOYMENT_ID.yaml" +PDB_PATH="$OUTPUT_DIR/pdb-$SCOPE_ID-$DEPLOYMENT_ID.yaml" CONTEXT_PATH="$OUTPUT_DIR/context-$SCOPE_ID.json" echo "$CONTEXT" | jq --arg replicas "$REPLICAS" --arg service_account_name "$SERVICE_ACCOUNT_NAME" '. + {replicas: $replicas, service_account_name: $service_account_name}' > "$CONTEXT_PATH" @@ -94,7 +95,20 @@ gomplate -c .="$CONTEXT_PATH" \ TEMPLATE_GENERATION_STATUS=$? if [[ $TEMPLATE_GENERATION_STATUS -ne 0 ]]; then - echo "Error building scaling template" + echo "Error building service template" + exit 1 +fi + +echo "Building Template: $PDB_TEMPLATE to $PDB_PATH" + +gomplate -c .="$CONTEXT_PATH" \ + --file "$PDB_TEMPLATE" \ + --out "$PDB_PATH" + +TEMPLATE_GENERATION_STATUS=$? + +if [[ $TEMPLATE_GENERATION_STATUS -ne 0 ]]; then + echo "Error building PDB template" exit 1 fi diff --git a/k8s/deployment/templates/pdb.yaml.tpl b/k8s/deployment/templates/pdb.yaml.tpl new file mode 100644 index 00000000..dc5c4a49 --- /dev/null +++ b/k8s/deployment/templates/pdb.yaml.tpl @@ -0,0 +1,53 @@ +{{if eq .pdb_enabled "true"}} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: pdb-d-{{ .scope.id }}-{{ .deployment.id }} + namespace: {{ .k8s_namespace }} + labels: + name: pdb-d-{{ .scope.id }}-{{ .deployment.id }} + app.kubernetes.io/part-of: {{ .namespace.slug }}-{{ .application.slug }} + app.kubernetes.io/component: application + app.kubernetes.io/instance: {{ .scope.slug }} + app.kubernetes.io/name: {{ .scope.slug }} +{{- $global := index .k8s_modifiers "global" }} +{{- if $global }} + {{- $labels := index $global "labels" }} + {{- if $labels }} +{{ data.ToYAML $labels | indent 4 }} + {{- end }} +{{- end }} +{{- $pdb := index .k8s_modifiers "pdb" }} +{{- if $pdb }} + {{- $labels := index $pdb "labels" }} + {{- if $labels }} +{{ data.ToYAML $labels | indent 4 }} + {{- end }} +{{- end }} + annotations: + nullplatform.com/managed-by: nullplatform + nullplatform.com/account: {{ .account.slug }} + nullplatform.com/namespace: {{ .namespace.slug }} + nullplatform.com/application: {{ .application.slug }} + nullplatform.com/scope: {{ .scope.slug }} + nullplatform.com/deployment-id: "{{ .deployment.id }}" +{{- $global := index .k8s_modifiers "global" }} +{{- if $global }} + {{- $annotations := index $global "annotations" }} + {{- if $annotations }} +{{ data.ToYAML $annotations | indent 4 }} + {{- end }} +{{- end }} +{{- $pdb := index .k8s_modifiers "pdb" }} +{{- if $pdb }} + {{- $annotations := index $pdb "annotations" }} + {{- if $annotations }} +{{ data.ToYAML $annotations | indent 4 }} + {{- end }} +{{- end }} +spec: + maxUnavailable: {{ .pdb_max_unavailable }} + selector: + matchLabels: + app: d-{{ .scope.id }}-{{ .deployment.id }} +{{- end }} \ No newline at end of file diff --git a/k8s/values.yaml b/k8s/values.yaml index 46351563..74806d30 100644 --- a/k8s/values.yaml +++ b/k8s/values.yaml @@ -11,6 +11,7 @@ configuration: SECRET_TEMPLATE: "$SERVICE_PATH/deployment/templates/secret.yaml.tpl" SCALING_TEMPLATE: "$SERVICE_PATH/deployment/templates/scaling.yaml.tpl" SERVICE_TEMPLATE: "$SERVICE_PATH/deployment/templates/service.yaml.tpl" + PDB_TEMPLATE: "$SERVICE_PATH/deployment/templates/pdb.yaml.tpl" INITIAL_INGRESS_PATH: "$SERVICE_PATH/deployment/templates/initial-ingress.yaml.tpl" BLUE_GREEN_INGRESS_PATH: "$SERVICE_PATH/deployment/templates/blue-green-ingress.yaml.tpl" SERVICE_ACCOUNT_TEMPLATE: "$SERVICE_PATH/scope/templates/service-account.yaml.tpl" @@ -53,6 +54,9 @@ configuration: # BUCKET: bucket-name # PREFIX: path-prefix # DEPLOY_STRATEGY: rolling +# POD_DISRUPTION_BUDGET: +# ENABLED: false +# MAX_UNAVAILABLE: "25%" # Can be percentage or number (e.g., "2") # K8S_MODIFIERS: # global: # annotations: