forked from GoogleCloudPlatform/ml-auto-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(tpu_observability): Add GKE cluster version manager to automate master and node pool upgrades #248
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
Open
yuna-tzeng
wants to merge
9
commits into
dev
Choose a base branch
from
tpu-obs/user/yuna/gke_version_update
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(tpu_observability): Add GKE cluster version manager to automate master and node pool upgrades #248
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8eb5556
[cienet-private] Enable workflow checks for the primary working branches
alfredyu-cienet 2bc9cad
feat(tpu_observability): implement GKE cluster version manager to aut…
yuna-tzeng d57ea5b
format
yuna-tzeng 23e02c2
scheduling_helper update
yuna-tzeng 95ce4db
fix
yuna-tzeng 6dc157d
Move function describe_cluster, upgrade_cluster_master and upgrade_cl…
yuna-tzeng a70a50f
format fix
yuna-tzeng 5eb9215
add dag timeout
yuna-tzeng f3794a8
fix
yuna-tzeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ name: DAG Check | |
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
| types: [opened, synchronize, edited] | ||
|
|
||
| push: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ name: Unit Test | |
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
| types: [opened, synchronize, edited] | ||
|
|
||
| push: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """A DAG to upgrade GKE cluster to latest available version.""" | ||
|
|
||
| import datetime | ||
| import json | ||
| import logging | ||
| import re | ||
|
|
||
| from airflow import models | ||
| from airflow.decorators import task | ||
| from airflow.models.baseoperator import chain | ||
| from airflow.utils.task_group import TaskGroup | ||
| from dags import composer_env | ||
| from dags.tpu_observability.configs.common import MachineConfigMap, GCS_CONFIG_PATH | ||
| from dags.tpu_observability.utils import node_pool_util as node_pool | ||
| from dags.tpu_observability.utils import subprocess_util as subprocess | ||
| from dags.common.scheduling_helper.scheduling_helper import SchedulingHelper, get_dag_timeout | ||
|
|
||
| DAG_ID = "gke_cluster_version_manager" | ||
| DAGRUN_TIMEOUT = get_dag_timeout(DAG_ID) | ||
| SCHEDULE = SchedulingHelper.arrange_schedule_time(DAG_ID) | ||
|
|
||
|
|
||
| def describe_cluster(node_pool_info: node_pool.Info) -> str: | ||
| """Describes the GKE cluster using gcloud command.""" | ||
| command = ( | ||
| f"gcloud container clusters describe {node_pool_info.cluster_name} " | ||
| f"--project={node_pool_info.project_id} " | ||
| f"--region={node_pool_info.region} " | ||
| "--format='json'" | ||
| ) | ||
| return subprocess.run_exec(command) | ||
|
|
||
|
|
||
| def upgrade_cluster_master( | ||
| node_pool_info: node_pool.Info, latest_version: str | ||
| ) -> str: | ||
| """Upgrades the master of the GKE cluster.""" | ||
| command = ( | ||
| f"gcloud container clusters upgrade {node_pool_info.cluster_name} " | ||
| "--master " | ||
| f"--cluster-version={latest_version} " | ||
| f"--project={node_pool_info.project_id} " | ||
| f"--region={node_pool_info.region} --quiet" | ||
| ) | ||
| return subprocess.run_exec(command) | ||
|
|
||
|
|
||
| def upgrade_cluster_node_pool( | ||
| node_pool_info: node_pool.Info, | ||
| latest_version: str, | ||
| node_pool_name: str = "default-pool", | ||
| ) -> str: | ||
| """Upgrades a specific node pool of the GKE cluster.""" | ||
| command = ( | ||
| f"gcloud container clusters upgrade {node_pool_info.cluster_name} " | ||
| f"--project={node_pool_info.project_id} " | ||
| f"--region={node_pool_info.region} " | ||
| f"--cluster-version={latest_version} " | ||
| f"--node-pool={node_pool_name} " | ||
| "--quiet" | ||
| ) | ||
| return subprocess.run_exec(command) | ||
|
|
||
|
|
||
| @task | ||
| def find_available_version(node_pool_info: node_pool.Info) -> str: | ||
| """Finds the latest available GKE version.""" | ||
|
|
||
| command = ( | ||
| f"gcloud container get-server-config --region={node_pool_info.region} " | ||
| f"--project={node_pool_info.project_id} " | ||
| "--format='json'" | ||
| ) | ||
| logging.info("Running command: %s", command) | ||
| stdout = subprocess.run_exec(command) | ||
|
|
||
| output_json = json.loads(stdout) | ||
| valid_versions = output_json.get("validMasterVersions", []) | ||
|
|
||
| pattern = re.compile(r"^1\.(3[2-9]|[4-9][0-9])") | ||
| matching_versions = [v for v in valid_versions if pattern.match(v)] | ||
|
|
||
| if not matching_versions: | ||
| raise ValueError("No matching GKE versions found") | ||
|
|
||
| latest_version = matching_versions[0] | ||
| logging.info("Found latest available version: %s", latest_version) | ||
| return latest_version | ||
|
|
||
|
|
||
| @task | ||
| def find_current_cluster_version(node_pool_info: node_pool.Info) -> dict: | ||
| """Finds the current version of the cluster.""" | ||
|
|
||
| stdout = describe_cluster(node_pool_info) | ||
|
|
||
| output_json = json.loads(stdout) | ||
| current_master_version = output_json.get("currentMasterVersion") | ||
| current_node_version = output_json.get("currentNodeVersion") | ||
|
|
||
| logging.info("Current Master Version: %s", current_master_version) | ||
| logging.info("Current Node Version: %s", current_node_version) | ||
|
|
||
| return { | ||
| "currentMasterVersion": current_master_version, | ||
| "currentNodeVersion": current_node_version, | ||
| } | ||
|
|
||
|
|
||
| @task | ||
| def upgrade_master( | ||
| latest_version: str, current_versions: dict, node_pool_info: node_pool.Info | ||
| ): | ||
| """Upgrades the master to the target version if needed.""" | ||
| current_master = current_versions.get("currentMasterVersion") | ||
|
|
||
| if current_master != latest_version: | ||
| logging.info( | ||
| "Master version (%s) != Target (%s). Upgrading.", | ||
| current_master, | ||
| latest_version, | ||
| ) | ||
| upgrade_cluster_master(node_pool_info, latest_version) | ||
| else: | ||
| logging.info("Master is already at target version. Skipping.") | ||
|
|
||
|
|
||
| @task | ||
| def upgrade_nodes( | ||
| latest_version: str, current_versions: dict, node_pool_info: node_pool.Info | ||
| ): | ||
| """Upgrades all node pools to the target version if needed.""" | ||
| current_node = current_versions.get("currentNodeVersion") | ||
|
|
||
| if current_node != latest_version: | ||
| logging.info( | ||
| "Node version (%s) != Target (%s). Upgrading.", | ||
| current_node, | ||
| latest_version, | ||
| ) | ||
| upgrade_cluster_node_pool(node_pool_info, latest_version) | ||
| else: | ||
| logging.info("Nodes are already at target version. Skipping.") | ||
|
|
||
|
|
||
| @task | ||
| def verify_upgrade(target_version: str, node_pool_info: node_pool.Info): | ||
| """Verifies that the upgrade was successful.""" | ||
|
|
||
| stdout = describe_cluster(node_pool_info) | ||
|
|
||
| output_json = json.loads(stdout) | ||
| current_master_version = output_json.get("currentMasterVersion") | ||
| current_node_version = output_json.get("currentNodeVersion") | ||
|
|
||
| logging.info("Verifying versions against target: %s", target_version) | ||
| logging.info("Post-upgrade Master Version: %s", current_master_version) | ||
| logging.info("Post-upgrade Node Version: %s", current_node_version) | ||
|
|
||
| if ( | ||
| current_master_version != target_version | ||
| or current_node_version != target_version | ||
| ): | ||
| raise ValueError( | ||
| f"Verification failed! Master: {current_master_version}, " | ||
| f"Node: {current_node_version}, Target: {target_version}" | ||
| ) | ||
| logging.info("Upgrade verified successfully!") | ||
|
|
||
|
|
||
| with models.DAG( | ||
| dag_id=DAG_ID, | ||
| start_date=datetime.datetime(2026, 5, 20), | ||
| schedule=SCHEDULE if composer_env.is_prod_env() else None, | ||
| dagrun_timeout=DAGRUN_TIMEOUT, | ||
| tags=["gke", "upgrade"], | ||
| description="DAG to upgrade GKE cluster to latest available version", | ||
| ) as dag: | ||
| for machine in MachineConfigMap: | ||
| config = machine.value | ||
|
|
||
| with TaskGroup(group_id=f"v{config.tpu_version.value}"): | ||
| cluster_info = node_pool.build_node_pool_info_from_gcs_yaml.override( | ||
| task_id="build_node_pool_info_from_gcs_yaml" | ||
| )( | ||
| gcs_path=GCS_CONFIG_PATH, | ||
| dag_name=DAG_ID, | ||
| is_prod=composer_env.is_prod_env(), | ||
| machine_type=config.machine_version.value, | ||
| tpu_topology=config.tpu_topology, | ||
| ) | ||
|
|
||
| avail_ver = find_available_version(cluster_info) | ||
| curr_vers = find_current_cluster_version(cluster_info) | ||
|
|
||
| master_up = upgrade_master(avail_ver, curr_vers, cluster_info) | ||
| node_up = upgrade_nodes(avail_ver, curr_vers, cluster_info) | ||
| verify = verify_upgrade(avail_ver, cluster_info) | ||
|
|
||
| chain( | ||
| master_up, | ||
| node_up, | ||
| verify, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.