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!: Enable Helm Release to be Optional #5

Merged
merged 12 commits into from
Dec 18, 2024
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_operator_image_tag"></a> [operator\_image\_tag](#input\_operator\_image\_tag) | wandb/controller image tag | `string` | `"1.2.13"` | no |
| <a name="input_operator_namespace"></a> [operator\_namespace](#input\_operator\_namespace) | Kubernetes namespace where the operator CRD's will be deployed. By default, it uses the `default` namespace. | `string` | `"wandb"` | no |
| <a name="input_operator_version"></a> [operator\_version](#input\_operator\_version) | https://github.com/wandb/helm-charts/tree/main/charts/operator helm chart version | `string` | `"0.1.6"` | no |
| <a name="input_controller_image_tag"></a> [controller\_image\_tag](#input\_controller\_image\_tag) | wandb/controller image tag | `string` | `"latest"` | no |
| <a name="input_enable_helm_release"></a> [enable\_helm\_release](#input\_enable\_helm\_release) | Enable or disable applying and releasing Helm chart | `bool` | `true` | no |
| <a name="input_operator_chart_namespace"></a> [operator\_chart\_namespace](#input\_operator\_chart\_namespace) | Kubernetes namespace where the operator CRD's will be deployed. By default, it uses the `default` namespace. | `string` | `"wandb"` | no |
| <a name="input_operator_chart_version"></a> [operator\_chart\_version](#input\_operator\_chart\_version) | https://github.com/wandb/helm-charts/tree/main/charts/operator helm chart version | `string` | `"latest"` | no |
| <a name="input_spec"></a> [spec](#input\_spec) | Specification used in the helm release for the instance. see https://github.com/wandb/cdk8s/blob/main/config-schema.json for details. | `any` | n/a | yes |
| <a name="input_wandb_cloud"></a> [wandb\_cloud](#input\_wandb\_cloud) | The cloud provider to use. | `string` | n/a | yes |
| <a name="input_wandb_fqdn"></a> [wandb\_fqdn](#input\_wandb\_fqdn) | The FQDN to the W&B application | `string` | n/a | yes |
| <a name="input_wandb_namespace"></a> [wandb\_namespace](#input\_wandb\_namespace) | Kubernetes namespace where the operator will be deployed. By default, it uses the `default` namespace. | `string` | `"default"` | no |

## Outputs
Expand Down
22 changes: 20 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ resource "helm_release" "operator" {
cleanup_on_fail = false
disable_webhooks = true
verify = false
count = var.enable_helm_release ? 1 : 0

set {
name = "image.tag"
Expand All @@ -21,6 +22,8 @@ resource "helm_release" "wandb" {
name = "wandb-cr"
chart = "wandb-cr"

count = var.enable_helm_release ? 1 : 0

force_update = true

repository = path.module
Expand All @@ -40,5 +43,20 @@ resource "helm_release" "wandb" {
type = "string"
}

depends_on = [helm_release.operator]
}
depends_on = [local.helm_release_operator]
}

locals {
helm_release_wandb = one(helm_release.wandb[*])
helm_release_operator = one(helm_release.operator[*])
}

moved {
from = helm_release.operator
to = helm_release.operator[0]
}

moved {
from = helm_release.wandb
to = helm_release.wandb[0]
}
43 changes: 43 additions & 0 deletions migrate-state.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
set -e

echo "Starting Terraform state migration..."

# Function to check if a resource exists in terraform state
check_resource() {
terraform state list | grep -q "$1" && return 0 || return 1
}

# Function to migrate a single resource
migrate_resource() {
local OLD_RESOURCE="$1"
local NEW_RESOURCE="$2"

if check_resource "$OLD_RESOURCE"; then
echo "Migrating ${OLD_RESOURCE} to ${NEW_RESOURCE}"
terraform state mv -lock=false "$OLD_RESOURCE" "$NEW_RESOURCE" || {
echo "Failed to migrate ${OLD_RESOURCE}"
return 1
}
else
echo "Resource ${OLD_RESOURCE} not found in state, skipping..."
fi
}

# Backup the current state
echo "Creating state backup..."
terraform state pull > terraform.tfstate.backup.$(date +%Y%m%d-%H%M%S)

# Migrate each resource
echo "Migrating resources..."

# Find all helm_release resources and migrate them
terraform state list | grep "helm_release" | while read -r resource; do
# Skip if resource already has count index
if [[ "$resource" != *"[0]"* ]]; then
migrate_resource "$resource" "${resource}[0]"
fi
done

echo "Migration completed!"
echo "Please review the state and run 'terraform plan' to verify the migration"
Comment on lines +1 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this checked in?

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ variable "operator_chart_version" {
default = "latest"
description = "https://github.com/wandb/helm-charts/tree/main/charts/operator helm chart version"
}

variable "enable_helm_release" {
type = bool
default = true
description = "Enable or disable applying and releasing Helm chart"
}
Loading