Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow enabling memory oversubscription in cluster #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/nomad-servers/launch_template.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ resource "aws_launch_template" "nomad_server" {
update_default_version = true

user_data = base64encode(templatefile("${path.module}/scripts/setup_server.tftpl.sh", {
nomad_acl_bootstrap_token = var.nomad_acl_bootstrap_token
nomad_acl_enable = var.nomad_acl_enable
nomad_acl_bootstrap_token = var.nomad_acl_bootstrap_token
nomad_acl_enable = var.nomad_acl_enable
enable_mem_oversubscription = var.enable_mem_oversubscription
nomad_server_cfg = templatefile("${path.module}/templates/nomad.tftpl", {
nomad_dc = var.cluster_name
aws_region = var.aws_region
Expand Down
33 changes: 33 additions & 0 deletions modules/nomad-servers/scripts/setup_server.tftpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,34 @@ bootstrap_acl() {
fi
}

enable_mem_oversubscription() {
command -v jq >/dev/null 2>&1 || { log "ERROR" "jq not found in PATH. Aborting."; exit 1; }

local nomad_config
nomad_config=$(curl -s http://0.0.0.0:4646/v1/operator/scheduler/configuration %{ if "${nomad_acl_bootstrap_token}" != "" }-H "X-Nomad-Token: ${nomad_acl_bootstrap_token}"%{ endif })
if [[ "$nomad_config" == *"Permission denied"* ]]; then
log "ERROR" "Permission denied while enabling memory oversubscription. Please check the bootstrap token."
else
log "INFO" "Checking if Memory Over subscription is already enabled."
mem_oversub_enabled=$(echo "$nomad_config" | jq '.SchedulerConfig | .MemoryOversubscriptionEnabled')
if [ "$mem_oversub_enabled" == "false" ]; then
log "INFO" "Memory Oversubscription is disabled. Enabling."
echo "$nomad_config" | \
status_code=(jq '.SchedulerConfig | .MemoryOversubscriptionEnabled=true' | \
curl -s -X PUT --write-out %%{http_code} \
%{ if "${nomad_acl_bootstrap_token}" != "" }-H "X-Nomad-Token: ${nomad_acl_bootstrap_token}"%{ endif } \
http://0.0.0.0:4646/v1/operator/scheduler/configuration -d @-)
if [ "$status_code" == "200" ]; then
log "INFO" "Successfully enabled Memory Oversubscription!"
else
log "ERROR" "Something went wrong while updating memory oversubscription. Please run it manually."
fi
else
log "INFO" "Memory Oversubscription is already enabled!"
fi
fi
}

log "INFO" "Fetching EC2 Tags from AWS"
store_tags

Expand All @@ -165,4 +193,9 @@ log "INFO" "Skipping ACL Bootstrap for Nomad as 'nomad_acl_enable' is not set to
log "INFO" "Restarting services"
restart_nomad

%{ if enable_memory_oversubscription }
log "INFO" "Enabling Memory Oversubscription for the cluster"
enable_mem_oversubscription
%{ endif }

log "INFO" "Finished server initializing process! Enjoy Nomad!"
6 changes: 6 additions & 0 deletions modules/nomad-servers/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ variable "ebs_encryption" {
default = true
}

variable "enable_mem_oversubscription" {
description = "Whether to enable Memory Oversubscription on the cluster"
type = bool
default = false
}

variable "instance_count" {
description = "Number of Nomad server instances to run"
type = number
Expand Down