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

Add support for AlloyDB Cluster Major Version Upgrade #12573

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
10 changes: 9 additions & 1 deletion mmv1/products/alloydb/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ virtual_fields:
Possible values: DEFAULT, FORCE
type: String
default_value: "DEFAULT"
- name: 'skip_await_major_version_upgrade'
type: Boolean
default_value: true
Copy link
Contributor

Choose a reason for hiding this comment

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

is this the API default?

Copy link
Contributor Author

@GauravJain21 GauravJain21 Jan 9, 2025

Choose a reason for hiding this comment

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

Yes, it is API default.

description: |
Set to true to skip awaiting on the major version upgrade of the cluster.
Possible values: true, false
Default value: "true"
parameters:
- name: 'clusterId'
type: String
Expand Down Expand Up @@ -256,7 +263,8 @@ properties:
- name: 'databaseVersion'
type: String
description: |
The database engine major version. This is an optional field and it's populated at the Cluster creation time. This field cannot be changed after cluster creation.
The database engine major version. This is an optional field and it's populated at the Cluster creation time.
Note: Changing this field to a higer version results in upgrading the AlloyDB cluster which is an irreversible change.
default_from_api: true
- name: 'pscConfig'
type: NestedObject
Expand Down
63 changes: 63 additions & 0 deletions mmv1/templates/terraform/pre_update/alloydb_cluster.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
//{{- if ne $.TargetVersionName "ga" }}

// Implementation for cluster upgrade
if d.HasChange("database_version") && !tpgresource.IsEmptyValue(reflect.ValueOf(d.Get("database_version"))) {
upgradeUrl := strings.Split(url, "?updateMask")[0] + ":upgrade"
patchObj := make(map[string]interface{})
patchObj["version"] = obj["databaseVersion"]
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

upgradeClusterTimeout := 58 * time.Hour
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
Project: billingProject,
RawURL: upgradeUrl,
UserAgent: userAgent,
Body: patchObj,
Timeout: upgradeClusterTimeout,
})
if err != nil {
return fmt.Errorf("Error upgrading cluster: %v", err)
}
log.Printf("[DEBUG] Started upgrading cluster %q: %#v", d.Id(), res)
// Remove databaseVersion from the object so that it is not considered again for updating the cluster
delete(obj, "databaseVersion")

index := 0
for _, label := range updateMask {
if label != "databaseVersion" {
updateMask[index] = label
index++
}
}
updateMask = updateMask[:index]

// Update url with the new updateMask
url := strings.Split(url, "?updateMask=")[0]
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}

if d.Get("skip_await_major_version_upgrade") == false {
err = AlloydbOperationWaitTime(
config, res, project, "Upgrading cluster", userAgent,
upgradeClusterTimeout)

if err != nil {
return fmt.Errorf("Error waiting to upgrade cluster: %s", err)
}

log.Printf("[DEBUG] Finished upgrading cluster %q: %#v", d.Id(), res)
}
}

//{{- end }}





// Restrict setting secondary_config if cluster_type is PRIMARY
if d.Get("cluster_type") == "PRIMARY" && !tpgresource.IsEmptyValue(reflect.ValueOf(d.Get("secondary_config"))) {
return fmt.Errorf("Can not set secondary config for primary cluster.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,108 @@ resource "google_alloydb_cluster" "default" {
network_config {
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
}
}

data "google_project" "project" {
}

resource "google_compute_network" "default" {
name = "tf-test-alloydb-cluster%{random_suffix}"
}
`, context)
}

func TestAccAlloydbCluster_upgrade(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-upgrade-1"),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbCluster_beforeUpgrade(context),
},
{
ResourceName: "google_alloydb_cluster.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location", "labels", "terraform_labels", "skip_await_major_version_upgrade"},
},
{
Config: testAccAlloydbCluster_afterUpgrade(context),
},
{
ResourceName: "google_alloydb_cluster.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location", "labels", "terraform_labels", "skip_await_major_version_upgrade"},
},
},
})
}

func testAccAlloydbCluster_beforeUpgrade(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_cluster" "default" {
skip_await_major_version_upgrade = false
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network_config {
network = data.google_compute_network.default.id
}

labels = {
foo = "bar"
test = "tf-test-alloydb-cluster%{random_suffix}"
}
database_version = "POSTGRES_14"
}

lifecycle {
prevent_destroy = true
resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"

machine_config {
cpu_count = 8
}
}

data "google_project" "project" {
data "google_compute_network" "default" {
name = "%{network_name}"
}
`, context)
}

resource "google_compute_network" "default" {
name = "tf-test-alloydb-cluster%{random_suffix}"
func testAccAlloydbCluster_afterUpgrade(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_cluster" "default" {
skip_await_major_version_upgrade = false
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network_config {
network = data.google_compute_network.default.id
}
database_version = "POSTGRES_15"
}

resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"

machine_config {
cpu_count = 8
}
}

data "google_compute_network" "default" {
name = "%{network_name}"
}
`, context)
}
Expand Down
Loading