feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 Google LLC. All Rights Reserved.
#
# 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.
"""Command group for cluster upgrade feature."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class ClusterUpgrades(base.Group):
"""Configure the Fleet clusterupgrade feature.
This fleet feature is used to configure fleet-based rollout sequencing.
"""
category = base.COMPUTE_CATEGORY

View File

@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 Google LLC. All Rights Reserved.
#
# 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.
"""Command to create Cluster Ugprade Feature information for a Fleet."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.container.fleet.clusterupgrade import flags as clusterupgrade_flags
from surface.container.fleet.clusterupgrade import update as clusterupgrade_update
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Create(clusterupgrade_update.Update):
"""Create the clusterupgrade feature for a fleet within a given project."""
detailed_help = {"EXAMPLES": """\
To create the clusterupgrade feature for the current fleet, run:
$ {command} --default-upgrade-soaking=DEFAULT_UPGRADE_SOAKING
"""}
@staticmethod
def Args(parser):
flags = clusterupgrade_flags.ClusterUpgradeFlags(parser)
flags.AddDefaultUpgradeSoakingFlag()
flags.AddUpgradeSoakingOverrideFlags(with_destructive=False)
flags.AddUpstreamFleetFlags(with_destructive=False)

View File

@@ -0,0 +1,199 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 Google LLC. All Rights Reserved.
#
# 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.
"""Command to show Cluster Ugprade Feature information for a Fleet."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import re
import frozendict
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.container.fleet.clusterupgrade import flags as clusterupgrade_flags
from googlecloudsdk.command_lib.container.fleet.features import base as feature_base
from googlecloudsdk.command_lib.util.apis import arg_utils
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.util import times
CLUSTER_UPGRADE_FEATURE = 'clusterupgrade'
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Describe(feature_base.DescribeCommand):
"""Describe the clusterupgrade feature for a fleet within a given project."""
detailed_help = frozendict.frozendict({
'DESCRIPTION': """\
Describe the Fleet clusterupgrade feature used for configuring
fleet-based rollout sequencing.
""",
'EXAMPLES': """\
To view the cluster upgrade feature information for the current fleet, run:
$ {command}
""",
})
feature_name = CLUSTER_UPGRADE_FEATURE
@staticmethod
def Args(parser):
flags = clusterupgrade_flags.ClusterUpgradeFlags(parser)
flags.AddShowLinkedClusterUpgrade()
def Run(self, args):
project = arg_utils.GetFromNamespace(args, '--project', use_defaults=True)
feature = self.GetFeature(project=project)
return self.GetFleetClusterUpgradeInfo(project, feature, args)
@staticmethod
def GetProjectIDFromFleet(fleet):
"""Extracts the project ID from the fleet."""
return fleet
@staticmethod
def FormatDurations(cluster_upgrade_spec):
"""Formats display strings for all cluster upgrade duration fields."""
if cluster_upgrade_spec.postConditions is not None:
default_soaking = cluster_upgrade_spec.postConditions.soaking
if default_soaking is not None:
cluster_upgrade_spec.postConditions.soaking = Describe.DisplayDuration(
default_soaking
)
for override in cluster_upgrade_spec.gkeUpgradeOverrides:
if override.postConditions is not None:
override_soaking = override.postConditions.soaking
if override_soaking is not None:
override.postConditions.soaking = Describe.DisplayDuration(
override_soaking
)
return cluster_upgrade_spec
@staticmethod
def DisplayDuration(proto_duration_string):
"""Returns the display string for a duration value."""
duration = times.ParseDuration(proto_duration_string)
iso_duration = times.FormatDuration(duration)
return re.sub('[-PT]', '', iso_duration).lower()
def GetFleetClusterUpgradeInfo(self, fleet, feature, args):
"""Gets Cluster Upgrade Feature information for the provided Fleet."""
if (
args.IsKnownAndSpecified('show_linked_cluster_upgrade')
and args.show_linked_cluster_upgrade
):
return self.GetLinkedClusterUpgrades(fleet, feature)
return Describe.GetClusterUpgradeInfo(fleet, feature)
@staticmethod
def GetClusterUpgradeInfo(fleet, feature):
"""Gets Cluster Upgrade Feature information for the provided Fleet."""
fleet_spec = feature.spec.clusterupgrade
if not fleet_spec:
msg = ('Cluster Upgrade feature is not configured for Fleet: {}.').format(
fleet
)
raise exceptions.Error(msg)
res = {
'fleet': fleet,
'spec': Describe.FormatDurations(fleet_spec),
}
if feature.state is not None and feature.state.clusterupgrade is not None:
res['state'] = feature.state.clusterupgrade
return res
def GetLinkedClusterUpgrades(self, fleet, feature):
"""Gets Cluster Upgrade Feature information for the entire sequence."""
current_project = Describe.GetProjectIDFromFleet(fleet)
visited = set([fleet])
def _UpTheStream(cluster_upgrade):
"""Recursively gets information for the upstream Fleets."""
upstream_spec = cluster_upgrade.get('spec', None)
upstream_fleets = upstream_spec.upstreamFleets if upstream_spec else None
if not upstream_fleets:
return [cluster_upgrade]
# Currently, we only process the first upstream Fleet in the
# Cluster Upgrade Feature, forming a linked-list of Fleets. If the API
# ever supports multiple upstream Fleets (i.e., graph of Fleets), this
# will need to be modified to recurse on every Fleet.
upstream_fleet = upstream_fleets[0]
if upstream_fleet in visited:
return [cluster_upgrade] # Detected a cycle.
visited.add(upstream_fleet)
upstream_fleet_project = Describe.GetProjectIDFromFleet(upstream_fleet)
upstream_feature = (
feature
if upstream_fleet_project == current_project
else self.GetFeature(project=upstream_fleet_project)
)
try:
upstream_cluster_upgrade = Describe.GetClusterUpgradeInfo(
upstream_fleet, upstream_feature
)
except exceptions.Error as e:
log.warning(e)
return [cluster_upgrade]
return _UpTheStream(upstream_cluster_upgrade) + [cluster_upgrade]
def _DownTheStream(cluster_upgrade):
"""Recursively gets information for the downstream Fleets."""
downstream_state = cluster_upgrade.get('state', None)
downstream_fleets = (
downstream_state.downstreamFleets if downstream_state else None
)
if not downstream_fleets:
return [cluster_upgrade]
# Currently, we only process the first downstream Fleet in the
# Cluster Upgrade Feature, forming a linked-list of Fleet. If the API
# ever supports multiple downstream Fleets (i.e., graph of Fleets), this
# will need to be modified to recurse on every Scope.
downstream_fleet = downstream_fleets[0]
if downstream_fleet in visited:
return [cluster_upgrade] # Detected a cycle.
visited.add(downstream_fleet)
downstream_scope_project = Describe.GetProjectIDFromFleet(
downstream_fleet
)
downstream_feature = (
feature
if downstream_scope_project == current_project
else self.GetFeature(project=downstream_scope_project)
)
downstream_cluster_upgrade = Describe.GetClusterUpgradeInfo(
downstream_fleet, downstream_feature
)
return [cluster_upgrade] + _DownTheStream(downstream_cluster_upgrade)
current_cluster_upgrade = Describe.GetClusterUpgradeInfo(fleet, feature)
upstream_cluster_upgrades = _UpTheStream(current_cluster_upgrade)[:-1]
downstream_cluster_upgrades = _DownTheStream(current_cluster_upgrade)[1:]
return (
upstream_cluster_upgrades
+ [current_cluster_upgrade]
+ downstream_cluster_upgrades
)

View File

@@ -0,0 +1,173 @@
# -*- coding: utf-8 -*- #
# Copyright 2023 Google LLC. All Rights Reserved.
#
# 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.
"""Command to update Cluster Ugprade Feature information for a Fleet."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from apitools.base.py import exceptions as apitools_exceptions
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.container.fleet.clusterupgrade import flags as clusterupgrade_flags
from googlecloudsdk.command_lib.container.fleet.features import base as feature_base
from googlecloudsdk.command_lib.util.apis import arg_utils
from googlecloudsdk.core.util import iso_duration
from googlecloudsdk.core.util import times
CLUSTER_UPGRADE_FEATURE = 'clusterupgrade'
@base.ReleaseTracks(
base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA, base.ReleaseTrack.GA
)
class Update(feature_base.UpdateCommand):
"""Update the clusterupgrade feature for a fleet within a given project."""
detailed_help = {'EXAMPLES': """\
To update the clusterupgrade feature for the current fleet, run:
$ {command} --default-upgrade-soaking=DEFAULT_UPGRADE_SOAKING
"""}
feature_name = CLUSTER_UPGRADE_FEATURE
@staticmethod
def Args(parser):
flags = clusterupgrade_flags.ClusterUpgradeFlags(parser)
flags.AddDefaultUpgradeSoakingFlag()
flags.AddUpgradeSoakingOverrideFlags(with_destructive=True)
flags.AddUpstreamFleetFlags(with_destructive=True)
def Run(self, args):
project = arg_utils.GetFromNamespace(args, '--project', use_defaults=True)
enable_cmd = _EnableCommand(args)
feature = enable_cmd.GetWithForceEnable(project)
self.Update(feature, args)
def Update(self, feature, args):
"""Updates Cluster Upgrade Feature information for a fleet."""
cluster_upgrade_spec = (
feature.spec.clusterupgrade or self.messages.ClusterUpgradeFleetSpec()
)
Update._HandleUpstreamFleets(args, cluster_upgrade_spec)
self._HandleDefaultSoakTime(args, cluster_upgrade_spec)
self._HandleUpgradeSoakingOverrides(args, cluster_upgrade_spec)
patch = self.messages.Feature(
spec=self.messages.CommonFeatureSpec(
clusterupgrade=cluster_upgrade_spec
)
)
path = (
'spec.clusterupgrade'
if feature.spec.clusterupgrade is not None
else 'spec'
)
return super(Update, self).Update([path], patch)
@staticmethod
def _HandleUpstreamFleets(args, cluster_upgrade_spec):
"""Updates the Cluster Upgrade Feature's upstreamFleets field."""
if (
args.IsKnownAndSpecified('reset_upstream_fleet')
and args.reset_upstream_fleet
):
cluster_upgrade_spec.upstreamFleets = []
elif (
args.IsKnownAndSpecified('upstream_fleet')
and args.upstream_fleet is not None
):
cluster_upgrade_spec.upstreamFleets = [args.upstream_fleet]
def _HandleDefaultSoakTime(self, args, cluster_upgrade_spec):
"""Updates the Cluster Upgrade Feature's postConditions.soaking field."""
if (
not args.IsKnownAndSpecified('default_upgrade_soaking')
or args.default_upgrade_soaking is None
):
return
default_soaking = times.FormatDurationForJson(
iso_duration.Duration(seconds=args.default_upgrade_soaking)
)
post_conditions = (
cluster_upgrade_spec.postConditions
or self.messages.ClusterUpgradePostConditions()
)
post_conditions.soaking = default_soaking
cluster_upgrade_spec.postConditions = post_conditions
def _HandleUpgradeSoakingOverrides(self, args, cluster_upgrade_spec):
"""Updates the ClusterUpgrade Feature's gkeUpgradeOverrides field."""
if (
args.IsKnownAndSpecified('remove_upgrade_soaking_overrides')
and args.remove_upgrade_soaking_overrides
):
cluster_upgrade_spec.gkeUpgradeOverrides = []
elif (
args.IsKnownAndSpecified('add_upgrade_soaking_override')
and args.IsKnownAndSpecified('upgrade_selector')
and args.add_upgrade_soaking_override is not None
and args.upgrade_selector is not None
):
soaking = times.FormatDurationForJson(
iso_duration.Duration(seconds=args.add_upgrade_soaking_override)
)
existing_gke_upgrade_overrides = (
cluster_upgrade_spec.gkeUpgradeOverrides or []
)
new_gke_upgrade_override = (
self.messages.ClusterUpgradeGKEUpgradeOverride()
)
new_gke_upgrade_override.postConditions = (
self.messages.ClusterUpgradePostConditions(soaking=soaking)
)
upgrade_name = args.upgrade_selector['name']
upgrade_version = args.upgrade_selector['version']
new_gke_upgrade_override.upgrade = self.messages.ClusterUpgradeGKEUpgrade(
name=upgrade_name, version=upgrade_version
)
new_gke_upgrade_overrides = existing_gke_upgrade_overrides + [
new_gke_upgrade_override
]
cluster_upgrade_spec.gkeUpgradeOverrides = new_gke_upgrade_overrides
class _EnableCommand(feature_base.EnableCommandMixin):
"""Base class for enabling the Cluster Upgrade Feature."""
def __init__(self, args):
self.feature_name = CLUSTER_UPGRADE_FEATURE
self.args = args
def ReleaseTrack(self):
"""Required to initialize HubClient. See calliope base class."""
return self.args.calliope_command.ReleaseTrack()
def GetWithForceEnable(self, project):
"""Gets the project's Cluster Upgrade Feature, enabling if necessary."""
try:
# Get the feature without transforming HTTP errors.
return self.hubclient.GetFeature(
self.FeatureResourceName(project=project)
)
except apitools_exceptions.HttpNotFoundError:
# It is expected for self.GetFeature to raise an exception when the
# feature is not enabled. If that is the case, we enable it on behalf of
# the caller.
self.Enable(self.messages.Feature())
return self.GetFeature()