Skip to content

Commit 66e78cc

Browse files
authored
Update for compatiblity with OBaaS (#316)
* Update for compatiblity with OBaaS * Fix Image Push Tokens
1 parent 0e60a9c commit 66e78cc

File tree

8 files changed

+163
-1432
lines changed

8 files changed

+163
-1432
lines changed

opentofu/cfgmgt/apply.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
import time
1212

1313
# --- Constants ---
14-
HELM_NAME = "ai-optimizer"
15-
HELM_REPO = "https://oracle.github.io/ai-optimizer/helm"
1614
STAGE_PATH = os.path.join(os.path.dirname(__file__), "stage")
1715
os.environ["KUBECONFIG"] = os.path.join(STAGE_PATH, "kubeconfig")
1816

17+
# --- Helm Charts ---
18+
OPTIMIZER_HELM_NAME = "ai-optimizer"
19+
OPTIMIZER_HELM_REPO = "https://oracle.github.io/ai-optimizer/helm"
1920

2021
# --- Utility Functions ---
2122
def mod_kubeconfig(private_endpoint: str = None):
@@ -78,8 +79,8 @@ def retry(func, retries=5, delay=15):
7879
# --- Core Functionalities ---
7980
def helm_repo_add_if_missing():
8081
"""Add/Update Helm Repo"""
81-
print(f"➕ Adding Helm repo '{HELM_NAME}'...")
82-
_, stderr, rc = run_cmd(["helm", "repo", "add", HELM_NAME, HELM_REPO], capture_output=False)
82+
print(f"➕ Adding Helm repo '{OPTIMIZER_HELM_NAME}'...")
83+
_, stderr, rc = run_cmd(["helm", "repo", "add", OPTIMIZER_HELM_NAME, OPTIMIZER_HELM_REPO], capture_output=False)
8384
if rc != 0:
8485
print(f"❌ Failed to add repo:\n{stderr}")
8586
sys.exit(1)
@@ -89,32 +90,42 @@ def helm_repo_add_if_missing():
8990
if rc != 0:
9091
print(f"❌ Failed to update repos:\n{stderr}")
9192
sys.exit(1)
92-
print(f"✅ Repo '{HELM_NAME}' added and updated.\n")
93+
print(f"✅ Repo '{OPTIMIZER_HELM_NAME}' added and updated.\n")
9394

9495

9596
def apply_helm_chart_inner(release_name, namespace):
9697
"""Apply Helm Chart"""
97-
values_path = os.path.join(STAGE_PATH, "optimizer-helm-values.yaml")
98-
if not os.path.isfile(values_path):
99-
print(f"⚠️ Values file not found: {values_path}")
98+
# Find all *-values.yaml files in the stage directory
99+
values_files = [
100+
f for f in os.listdir(STAGE_PATH)
101+
if f.endswith("-values.yaml") and os.path.isfile(os.path.join(STAGE_PATH, f))
102+
]
103+
104+
if not values_files:
105+
print(f"⚠️ No values files (*-values.yaml) found in: {STAGE_PATH}")
100106
print("ℹ️ Skipping Helm chart application.\n")
101107
return True # Return True to indicate this is not a retriable failure
102108

103109
helm_repo_add_if_missing()
104110

111+
# Build helm command with all values files
105112
cmd = [
106113
"helm",
107114
"upgrade",
108115
"--install",
109116
release_name,
110-
f"{HELM_NAME}/{HELM_NAME}",
117+
f"{OPTIMIZER_HELM_NAME}/{OPTIMIZER_HELM_NAME}",
111118
"--namespace",
112119
namespace,
113-
"--values",
114-
values_path,
115120
]
116121

117-
print(f"🚀 Applying Helm chart '{HELM_NAME}' to namespace '{namespace}'...")
122+
# Add each values file to the command
123+
for values_file in sorted(values_files):
124+
values_path = os.path.join(STAGE_PATH, values_file)
125+
cmd.extend(["--values", values_path])
126+
print(f"📄 Using values file: {values_file}")
127+
128+
print(f"🚀 Applying Helm chart '{OPTIMIZER_HELM_NAME}' to namespace '{namespace}'...")
118129
stdout, stderr, rc = run_cmd(cmd)
119130
if rc == 0:
120131
print("✅ Helm chart applied:")

opentofu/examples/manual-test.sh

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ set -euo pipefail
77
# Navigate to opentofu root
88
cd "$(dirname "$(dirname "$0")")" || exit 1
99

10+
# Check for tofu or terraform in PATH
11+
if command -v tofu &> /dev/null; then
12+
TF_CMD="tofu"
13+
elif command -v terraform &> /dev/null; then
14+
TF_CMD="terraform"
15+
else
16+
echo "Error: Neither 'tofu' nor 'terraform' found in PATH" >&2
17+
exit 1
18+
fi
19+
20+
echo "Using command: $TF_CMD"
21+
echo ""
22+
1023
PROFILE="${1:-DEFAULT}"
1124
OCI_CONFIG="${OCI_CONFIG_FILE:-$HOME/.oci/config}"
1225

@@ -44,6 +57,51 @@ export TF_VAR_compartment_ocid="$TF_VAR_tenancy_ocid"
4457
echo "✅ OCI credentials loaded (Profile: $PROFILE, Region: $TF_VAR_region)"
4558
echo ""
4659

60+
# Pre-flight checks: format and validate
61+
echo "Running pre-flight checks..."
62+
echo ""
63+
64+
echo "1. Formatting code with '$TF_CMD fmt --recursive'..."
65+
if $TF_CMD fmt --recursive > /dev/null; then
66+
echo " ✅ Format check passed"
67+
else
68+
echo " ❌ Format check failed"
69+
exit 1
70+
fi
71+
72+
echo "2. Validating configuration with '$TF_CMD validate'..."
73+
if $TF_CMD validate > /dev/null 2>&1; then
74+
echo " ✅ Validation passed"
75+
else
76+
echo " ❌ Validation failed"
77+
echo ""
78+
echo "Re-run: $TF_CMD validate"
79+
exit 1
80+
fi
81+
82+
echo ""
83+
84+
# Check for existing deployed resources
85+
if [ -f "terraform.tfstate" ] && [ -s "terraform.tfstate" ]; then
86+
echo "Checking for deployed resources..."
87+
88+
# Use terraform state list to check if there are any managed resources
89+
if resource_count=$($TF_CMD state list 2>/dev/null | wc -l | xargs); then
90+
if [ "$resource_count" -gt 0 ]; then
91+
echo "❌ ERROR: Found $resource_count deployed resource(s) in the state"
92+
echo ""
93+
echo "This test script requires a clean state to test multiple configurations."
94+
echo "Please destroy existing resources first:"
95+
echo ""
96+
echo " $TF_CMD destroy -auto-approve"
97+
echo ""
98+
exit 1
99+
else
100+
echo " ✅ State file exists but no resources are deployed (likely from previous destroy)"
101+
fi
102+
fi
103+
fi
104+
47105
# Run tests
48106
EXAMPLES=(
49107
examples/vm-new-adb.tfvars
@@ -56,13 +114,16 @@ EXAMPLES=(
56114
for example in "${EXAMPLES[@]}"; do
57115
echo "Testing $example..."
58116

59-
if plan_output=$(tofu plan -var-file="$example" 2>&1); then
117+
if plan_output=$($TF_CMD plan -var-file="$example" 2>&1); then
60118
plan_summary=$(echo "$plan_output" | grep -i "plan:" | tail -1 | sed 's/^[[:space:]]*//')
61119
echo "${plan_summary:-PASSED}"
62120
else
63121
echo " ❌ FAILED"
64122
echo ""
65-
echo "Re-run: tofu plan -var-file=$example"
123+
echo "Error output:"
124+
echo "$plan_output" | tail -20
125+
echo ""
126+
echo "Re-run: $TF_CMD plan -var-file=$example"
66127
exit 1
67128
fi
68129
done

opentofu/modules/kubernetes/cfgmgt.tf

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,27 @@
44

55
locals {
66
k8s_manifest = templatefile("${path.module}/templates/k8s_manifest.yaml", {
7-
label = var.label_prefix
8-
repository_host = local.repository_host
9-
optimizer_repository_server = local.optimizer_repository_server
10-
optimizer_repository_client = local.optimizer_repository_client
11-
compartment_ocid = var.lb.compartment_id
12-
lb_ocid = var.lb.id
13-
lb_subnet_ocid = var.public_subnet_id
14-
lb_ip_ocid = var.lb.ip_address_details[0].ip_address
15-
lb_nsgs = var.lb_nsg_id
16-
lb_min_shape = var.lb.shape_details[0].minimum_bandwidth_in_mbps
17-
lb_max_shape = var.lb.shape_details[0].maximum_bandwidth_in_mbps
18-
db_name = lower(var.db_name)
19-
db_username = var.db_conn.username
20-
db_password = var.db_conn.password
21-
db_service = var.db_conn.service
22-
optimizer_api_key = random_string.optimizer_api_key.result
23-
deploy_buildkit = var.byo_ocir_url == ""
24-
deploy_optimizer = var.deploy_optimizer
25-
optimizer_version = var.optimizer_version
26-
})
27-
28-
helm_values = templatefile("${path.module}/templates/optimizer_helm_values.yaml", {
29-
label = var.label_prefix
30-
optimizer_repository_server = local.optimizer_repository_server
31-
optimizer_repository_client = local.optimizer_repository_client
32-
oci_tenancy = var.tenancy_id
33-
oci_region = var.region
34-
db_type = var.db_conn.db_type
35-
db_ocid = var.db_ocid
36-
db_dsn = var.db_conn.service
37-
db_name = lower(var.db_name)
38-
node_pool_gpu_deploy = var.node_pool_gpu_deploy
39-
lb_ip = var.lb.ip_address_details[0].ip_address
7+
label = var.label_prefix
8+
repository_host = local.repository_host
9+
repository_base = local.repository_base
10+
compartment_ocid = var.lb.compartment_id
11+
lb_ocid = var.lb.id
12+
lb_subnet_ocid = var.public_subnet_id
13+
lb_ip_ocid = var.lb.ip_address_details[0].ip_address
14+
lb_nsgs = var.lb_nsg_id
15+
lb_min_shape = var.lb.shape_details[0].minimum_bandwidth_in_mbps
16+
lb_max_shape = var.lb.shape_details[0].maximum_bandwidth_in_mbps
17+
db_name = lower(var.db_name)
18+
db_username = var.db_conn.username
19+
db_password = var.db_conn.password
20+
db_service = var.db_conn.service
21+
optimizer_api_key = random_string.optimizer_api_key.result
22+
deploy_buildkit = var.byo_ocir_url == ""
23+
deploy_optimizer = var.deploy_optimizer
24+
optimizer_version = var.optimizer_version
4025
})
4126
}
4227

43-
4428
resource "local_sensitive_file" "kubeconfig" {
4529
content = data.oci_containerengine_cluster_kube_config.default_cluster_kube_config.content
4630
filename = "${path.root}/cfgmgt/stage/kubeconfig"
@@ -53,13 +37,6 @@ resource "local_sensitive_file" "k8s_manifest" {
5337
file_permission = 0600
5438
}
5539

56-
resource "local_sensitive_file" "optimizer_helm_values" {
57-
count = var.deploy_optimizer ? 1 : 0
58-
content = local.helm_values
59-
filename = "${path.root}/cfgmgt/stage/optimizer-helm-values.yaml"
60-
file_permission = 0600
61-
}
62-
6340
resource "null_resource" "apply" {
6441
count = var.run_cfgmgt ? 1 : 0
6542
triggers = {
@@ -81,7 +58,7 @@ resource "null_resource" "apply" {
8158
depends_on = [
8259
local_sensitive_file.kubeconfig,
8360
local_sensitive_file.k8s_manifest,
84-
local_sensitive_file.optimizer_helm_values,
61+
local_sensitive_file.optimizer_values,
8562
oci_containerengine_node_pool.cpu_node_pool_details,
8663
oci_containerengine_node_pool.gpu_node_pool_details,
8764
oci_containerengine_addon.oraoper_addon,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2024, 2025, Oracle and/or its affiliates.
2+
# All rights reserved. The Universal Permissive License (UPL), Version 1.0 as shown at http://oss.oracle.com/licenses/upl
3+
# spell-checker: disable
4+
5+
locals {
6+
optimizer_values = templatefile("${path.module}/templates/optimizer_values.yaml", {
7+
label = var.label_prefix
8+
repository_base = local.repository_base
9+
oci_region = var.region
10+
db_type = var.db_conn.db_type
11+
db_ocid = var.db_ocid
12+
db_dsn = var.db_conn.service
13+
db_name = lower(var.db_name)
14+
node_pool_gpu_deploy = var.node_pool_gpu_deploy
15+
lb_ip = var.lb.ip_address_details[0].ip_address
16+
})
17+
}
18+
19+
resource "local_sensitive_file" "optimizer_values" {
20+
count = var.deploy_optimizer ? 1 : 0
21+
content = local.optimizer_values
22+
filename = "${path.root}/cfgmgt/stage/optimizer-values.yaml"
23+
file_permission = 0600
24+
}

opentofu/modules/kubernetes/locals.tf

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@ locals {
5858
"ai-optimizer-server",
5959
"ai-optimizer-client"
6060
]
61-
region_map = { for r in data.oci_identity_regions.identity_regions.regions : r.name => r.key }
62-
image_region = lookup(local.region_map, var.region)
63-
repository_host = lower(format("%s.ocir.io", local.image_region))
64-
repository_base = var.byo_ocir_url != "" ? var.byo_ocir_url : lower(format("%s/%s/%s", local.repository_host, data.oci_objectstorage_namespace.objectstorage_namespace.namespace, var.label_prefix))
65-
optimizer_repository_server = lower(format("%s/ai-optimizer-server", local.repository_base))
66-
optimizer_repository_client = lower(format("%s/ai-optimizer-client", local.repository_base))
61+
region_map = { for r in data.oci_identity_regions.identity_regions.regions : r.name => r.key }
62+
image_region = lookup(local.region_map, var.region)
63+
repository_host = lower(format("%s.ocir.io", local.image_region))
64+
repository_base = var.byo_ocir_url != "" ? var.byo_ocir_url : lower(format("%s/%s/%s", local.repository_host, data.oci_objectstorage_namespace.objectstorage_namespace.namespace, var.label_prefix))
6765
}
6866

6967
// Cluster Details

0 commit comments

Comments
 (0)