def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        adapter = self.context['api_adapter']

        cluster_ref = adapter.ParseCluster(args.name)

        # Make sure it exists (will raise appropriate error if not)
        cluster = adapter.GetCluster(cluster_ref)

        options = api_adapter.UpdateClusterOptions(
            version=args.cluster_version,
            update_master=args.master,
            update_nodes=(not args.master),
            node_pool=args.node_pool,
            image_family=args.image_family)

        if options.version:
            new_version = options.version
        else:
            new_version = 'latest'

        if args.master:
            node_message = 'Master'
            current_version = cluster.currentMasterVersion
        else:
            node_message = 'All {node_count} nodes'.format(
                node_count=cluster.currentNodeCount)
            current_version = cluster.currentNodeVersion

        console_io.PromptContinue(
            message=
            '{node_message} of cluster [{cluster_name}] will be upgraded '
            'from version [{current_version}] to version [{new_version}]. '
            'This operation is long-running and will block other operations '
            'on the cluster (including delete) until it has run to completion.'
            .format(node_message=node_message,
                    cluster_name=cluster.name,
                    current_version=current_version,
                    new_version=new_version),
            throw_if_unattended=True,
            cancel_on_no=True)

        try:
            op_ref = adapter.UpdateCluster(cluster_ref, options)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))

        if args.wait:
            adapter.WaitForOperation(
                op_ref, 'Upgrading {0}'.format(cluster_ref.clusterId))

            log.UpdatedResource(cluster_ref)
Beispiel #2
0
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
    adapter = self.context['api_adapter']

    cluster_ref = adapter.ParseCluster(args.name)
    # Make sure it exists (will raise appropriate error if not)
    adapter.GetCluster(cluster_ref)

    options = api_adapter.UpdateClusterOptions(
        monitoring_service=args.monitoring_service,
        disable_addons=args.disable_addons)

    try:
      op_ref = adapter.UpdateCluster(cluster_ref, options)
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(util.GetError(error))

    if args.wait:
      adapter.WaitForOperation(
          op_ref, 'Updating {0}'.format(cluster_ref.clusterId))

      log.UpdatedResource(cluster_ref)
def ParseUpdateOptionsBase(args, locations):
    """Helper function to build ClusterUpdateOptions object from args.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
        command invocation.
    locations: list of strings. Zones in which cluster has nodes.

  Returns:
    ClusterUpdateOptions, object with data used to update cluster.
  """
    return api_adapter.UpdateClusterOptions(
        monitoring_service=args.monitoring_service,
        logging_service=args.logging_service,
        enable_stackdriver_kubernetes=args.enable_stackdriver_kubernetes,
        disable_addons=args.disable_addons,
        enable_autoscaling=args.enable_autoscaling,
        enable_binauthz=args.enable_binauthz,
        min_nodes=args.min_nodes,
        max_nodes=args.max_nodes,
        node_pool=args.node_pool,
        locations=locations,
        enable_master_authorized_networks=args.
        enable_master_authorized_networks,
        master_authorized_networks=args.master_authorized_networks,
        enable_vertical_pod_autoscaling=args.enable_vertical_pod_autoscaling)
Beispiel #4
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)
        cluster_ref = adapter.ParseCluster(args.name, location)
        concurrent_node_count = getattr(args, 'concurrent_node_count', None)

        try:
            cluster = adapter.GetCluster(cluster_ref)
        except (exceptions.HttpException,
                apitools_exceptions.HttpForbiddenError, util.Error) as error:
            log.warning(
                ('Problem loading details of cluster to upgrade:\n\n{}\n\n' +
                 'You can still attempt to upgrade the cluster.\n').format(
                     console_attr.SafeText(error)))
            cluster = None

        upgrade_message = container_command_util.ClusterUpgradeMessage(
            name=args.name,
            cluster=cluster,
            master=args.master,
            node_pool_name=args.node_pool,
            new_version=args.cluster_version,
            concurrent_node_count=concurrent_node_count)

        console_io.PromptContinue(message=upgrade_message,
                                  throw_if_unattended=True,
                                  cancel_on_no=True)

        options = api_adapter.UpdateClusterOptions(
            version=args.cluster_version,
            update_master=args.master,
            update_nodes=(not args.master),
            node_pool=args.node_pool,
            image_type=args.image_type,
            image=args.image,
            image_project=args.image_project,
            concurrent_node_count=concurrent_node_count)

        try:
            op_ref = adapter.UpdateCluster(cluster_ref, options)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        if not args. async:
            adapter.WaitForOperation(
                op_ref, 'Upgrading {0}'.format(cluster_ref.clusterId))

            log.UpdatedResource(cluster_ref)
Beispiel #5
0
def ParseUpgradeOptionsBase(args):
    """Parses the flags provided with the cluster upgrade command."""
    return api_adapter.UpdateClusterOptions(version=args.cluster_version,
                                            update_master=args.master,
                                            update_nodes=(not args.master),
                                            node_pool=args.node_pool,
                                            image_type=args.image_type,
                                            image=args.image,
                                            image_project=args.image_project)
Beispiel #6
0
def ParseUpgradeOptionsBase(args):
  """Parses the flags provided with the cluster upgrade command."""
  return api_adapter.UpdateClusterOptions(
      version=args.cluster_version,
      update_main=args.main,
      update_nodes=(not args.main),
      node_pool=args.node_pool,
      image_type=args.image_type,
      image=args.image,
      image_project=args.image_project,
      concurrent_node_count=getattr(args, 'concurrent_node_count', None))
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        adapter = self.context['api_adapter']

        cluster_ref = adapter.ParseCluster(args.name)
        # Make sure it exists (will raise appropriate error if not)
        adapter.GetCluster(cluster_ref)

        # locations will be None if additional-zones was specified, an empty list
        # if it was specified with no argument, or a populated list if zones were
        # provided. We want to distinguish between the case where it isn't
        # specified (and thus shouldn't be passed on to the API) and the case where
        # it's specified as wanting no additional zones, in which case we must pass
        # the cluster's primary zone to the API.
        # TODO(b/29578401): Remove the hasattr once the flag is GA.
        locations = None
        if hasattr(args,
                   'additional_zones') and args.additional_zones is not None:
            locations = sorted([cluster_ref.zone] + args.additional_zones)

        enable_master_authorized_networks = args.enable_master_authorized_networks
        options = api_adapter.UpdateClusterOptions(
            monitoring_service=args.monitoring_service,
            disable_addons=args.disable_addons,
            enable_autoscaling=args.enable_autoscaling,
            min_nodes=args.min_nodes,
            max_nodes=args.max_nodes,
            node_pool=args.node_pool,
            locations=locations,
            enable_master_authorized_networks=enable_master_authorized_networks,
            master_authorized_networks=args.master_authorized_networks)

        try:
            op_ref = adapter.UpdateCluster(cluster_ref, options)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        if not flags.GetAsyncValueFromAsyncAndWaitFlags(
                args. async, args.wait):
            adapter.WaitForOperation(
                op_ref, 'Updating {0}'.format(cluster_ref.clusterId))

            log.UpdatedResource(cluster_ref)
def ParseUpdateOptionsBase(args, locations):
    """Helper function to build ClusterUpdateOptions object from args.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.
    locations: list of strings. Zones in which cluster has nodes.

  Returns:
    ClusterUpdateOptions, object with data used to update cluster.
  """
    return api_adapter.UpdateClusterOptions(
        monitoring_service=args.monitoring_service,
        logging_service=args.logging_service,
        enable_stackdriver_kubernetes=args.enable_stackdriver_kubernetes,
        disable_addons=args.disable_addons,
        enable_autoscaling=args.enable_autoscaling,
        enable_binauthz=args.enable_binauthz,
        min_nodes=args.min_nodes,
        max_nodes=args.max_nodes,
        node_pool=args.node_pool,
        locations=locations,
        enable_master_authorized_networks=args.
        enable_master_authorized_networks,
        master_authorized_networks=args.master_authorized_networks,
        workload_pool=args.workload_pool,
        disable_workload_identity=args.disable_workload_identity,
        database_encryption_key=args.database_encryption_key,
        disable_database_encryption=args.disable_database_encryption,
        enable_vertical_pod_autoscaling=args.enable_vertical_pod_autoscaling,
        enable_autoprovisioning=args.enable_autoprovisioning,
        autoprovisioning_config_file=args.autoprovisioning_config_file,
        autoprovisioning_service_account=args.autoprovisioning_service_account,
        autoprovisioning_scopes=args.autoprovisioning_scopes,
        autoprovisioning_locations=args.autoprovisioning_locations,
        autoprovisioning_max_surge_upgrade=getattr(
            args, 'autoprovisioning_max_surge_upgrade', None),
        autoprovisioning_max_unavailable_upgrade=getattr(
            args, 'autoprovisioning_max_unavailable_upgrade', None),
        enable_autoprovisioning_autorepair=getattr(
            args, 'enable_autoprovisioning_autorepair', None),
        enable_autoprovisioning_autoupgrade=getattr(
            args, 'enable_autoprovisioning_autoupgrade', None),
        autoprovisioning_min_cpu_platform=getattr(
            args, 'autoprovisioning_min_cpu_platform', None),
        min_cpu=args.min_cpu,
        max_cpu=args.max_cpu,
        min_memory=args.min_memory,
        max_memory=args.max_memory,
        min_accelerator=args.min_accelerator,
        max_accelerator=args.max_accelerator)
Beispiel #9
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)
        cluster_ref = adapter.ParseCluster(args.name, location)
        concurrent_node_count = getattr(args, 'concurrent_node_count', None)

        # Make sure it exists (will raise appropriate error if not)
        cluster = adapter.GetCluster(cluster_ref)

        options = api_adapter.UpdateClusterOptions(
            version=args.cluster_version,
            update_master=args.master,
            update_nodes=(not args.master),
            node_pool=args.node_pool,
            image_type=args.image_type,
            image=args.image,
            image_project=args.image_project,
            concurrent_node_count=concurrent_node_count)

        upgrade_message = container_command_util.ClusterUpgradeMessage(
            cluster,
            master=args.master,
            node_pool=args.node_pool,
            new_version=options.version,
            concurrent_node_count=concurrent_node_count)

        console_io.PromptContinue(message=upgrade_message,
                                  throw_if_unattended=True,
                                  cancel_on_no=True)

        try:
            op_ref = adapter.UpdateCluster(cluster_ref, options)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        if not args. async:
            adapter.WaitForOperation(
                op_ref, 'Upgrading {0}'.format(cluster_ref.clusterId))

            log.UpdatedResource(cluster_ref)
def ParseUpdateOptionsBase(args, locations):
    """Helper function to build ClusterUpdateOptions object from args.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.
    locations: list of strings. Zones in which cluster has nodes.

  Returns:
    ClusterUpdateOptions, object with data used to update cluster.
  """
    opts = api_adapter.UpdateClusterOptions(
        monitoring_service=args.monitoring_service,
        logging_service=args.logging_service,
        monitoring=args.monitoring,
        logging=args.logging,
        enable_stackdriver_kubernetes=args.enable_stackdriver_kubernetes,
        disable_addons=args.disable_addons,
        enable_autoscaling=args.enable_autoscaling,
        enable_binauthz=args.enable_binauthz,
        min_nodes=args.min_nodes,
        max_nodes=args.max_nodes,
        node_pool=args.node_pool,
        locations=locations,
        enable_master_authorized_networks=args.
        enable_master_authorized_networks,
        master_authorized_networks=args.master_authorized_networks,
        private_ipv6_google_access_type=args.private_ipv6_google_access_type,
        workload_pool=args.workload_pool,
        disable_workload_identity=args.disable_workload_identity,
        database_encryption_key=args.database_encryption_key,
        disable_database_encryption=args.disable_database_encryption,
        enable_vertical_pod_autoscaling=args.enable_vertical_pod_autoscaling,
        enable_autoprovisioning=args.enable_autoprovisioning,
        enable_mesh_certificates=args.enable_mesh_certificates,
        autoprovisioning_config_file=args.autoprovisioning_config_file,
        autoprovisioning_service_account=args.autoprovisioning_service_account,
        autoprovisioning_scopes=args.autoprovisioning_scopes,
        autoprovisioning_locations=args.autoprovisioning_locations,
        autoprovisioning_max_surge_upgrade=getattr(
            args, 'autoprovisioning_max_surge_upgrade', None),
        autoprovisioning_max_unavailable_upgrade=getattr(
            args, 'autoprovisioning_max_unavailable_upgrade', None),
        enable_autoprovisioning_rolling_update=getattr(
            args, 'enable_autoprovisioning_rolling_update', None),
        enable_autoprovisioning_blue_green_update=getattr(
            args, 'enable_autoprovisioning_blue_green_update', None),
        autoprovisioning_standard_rollout_policy=getattr(
            args, 'autoprovisioning_standard_rollout_policy', None),
        autoprovisioning_node_pool_soak_duration=getattr(
            args, 'autoprovisioning_node_pool_soak_duration', None),
        enable_autoprovisioning_autorepair=getattr(
            args, 'enable_autoprovisioning_autorepair', None),
        enable_autoprovisioning_autoupgrade=getattr(
            args, 'enable_autoprovisioning_autoupgrade', None),
        autoprovisioning_min_cpu_platform=getattr(
            args, 'autoprovisioning_min_cpu_platform', None),
        autoprovisioning_image_type=getattr(args,
                                            'autoprovisioning_image_type',
                                            None),
        min_cpu=args.min_cpu,
        max_cpu=args.max_cpu,
        min_memory=args.min_memory,
        max_memory=args.max_memory,
        min_accelerator=args.min_accelerator,
        max_accelerator=args.max_accelerator)

    if (args.disable_addons
            and api_adapter.GCEPDCSIDRIVER in args.disable_addons):
        pdcsi_disabled = args.disable_addons[api_adapter.GCEPDCSIDRIVER]
        if pdcsi_disabled:
            console_io.PromptContinue(
                message=
                'If the GCE Persistent Disk CSI Driver is disabled, then any '
                'pods currently using PersistentVolumes owned by the driver '
                'will fail to terminate. Any new pods that try to use those '
                'PersistentVolumes will also fail to start.',
                cancel_on_no=True)

    if (args.disable_addons
            and api_adapter.GCPFILESTORECSIDRIVER in args.disable_addons):
        filestorecsi_disabled = args.disable_addons[
            api_adapter.GCPFILESTORECSIDRIVER]
        if filestorecsi_disabled:
            console_io.PromptContinue(
                message='If the GCP Filestore CSI Driver is disabled, then any '
                'pods currently using PersistentVolumes owned by the driver '
                'will fail to terminate. Any new pods that try to use those '
                'PersistentVolumes will also fail to start.',
                cancel_on_no=True)

    return opts
Beispiel #11
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """
        adapter = self.context['api_adapter']

        cluster_ref = adapter.ParseCluster(args.name,
                                           getattr(args, 'region', None))
        # Make sure it exists (will raise appropriate error if not)
        cluster = adapter.GetCluster(cluster_ref)

        # locations will be None if additional-zones was specified, an empty list
        # if it was specified with no argument, or a populated list if zones were
        # provided. We want to distinguish between the case where it isn't
        # specified (and thus shouldn't be passed on to the API) and the case where
        # it's specified as wanting no additional zones, in which case we must pass
        # the cluster's primary zone to the API.
        # TODO(b/29578401): Remove the hasattr once the flag is GA.
        locations = None
        if hasattr(args,
                   'additional_zones') and args.additional_zones is not None:
            locations = sorted([cluster_ref.zone] + args.additional_zones)

        if args.generate_password or args.set_password:
            if args.generate_password:
                password = ''
                options = api_adapter.SetMasterAuthOptions(
                    action=api_adapter.SetMasterAuthOptions.GENERATE_PASSWORD,
                    password=password)
            else:
                password = raw_input('Please enter the new password:'******'Enabling/Disabling Network Policy causes a rolling '
                'update of all cluster nodes, similar to performing a cluster '
                'upgrade.  This operation is long-running and will block other '
                'operations on the cluster (including delete) until it has run '
                'to completion.',
                cancel_on_no=True)
            options = api_adapter.SetNetworkPolicyOptions(
                enabled=args.enable_network_policy)
            try:
                op_ref = adapter.SetNetworkPolicy(cluster_ref, options)
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)
        elif args.start_ip_rotation:
            console_io.PromptContinue(
                message=
                'This will start an IP Rotation on cluster [{name}]. The '
                'master will be updated to serve on a new IP address in addition to '
                'the current IP address. Container Engine will then recreate all '
                'nodes ({num_nodes} nodes) to point to the new IP address. This '
                'operation is long-running and will block other operations on the '
                'cluster (including delete) until it has run to completion.'.
                format(name=cluster.name, num_nodes=cluster.currentNodeCount),
                cancel_on_no=True)
            try:
                op_ref = adapter.StartIpRotation(cluster_ref)
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)
        elif args.complete_ip_rotation:
            console_io.PromptContinue(
                message=
                'This will complete the in-progress IP Rotation on cluster '
                '[{name}]. The master will be updated to stop serving on the old IP '
                'address and only serve on the new IP address. Make sure all API '
                'clients have been updated to communicate with the new IP address '
                '(e.g. by running `gcloud container clusters get-credentials '
                '--project {project} --zone {zone} {name}`). This operation is long-'
                'running and will block other operations on the cluster (including '
                'delete) until it has run to completion.'.format(
                    name=cluster.name,
                    project=cluster_ref.projectId,
                    zone=cluster.zone),
                cancel_on_no=True)
            try:
                op_ref = adapter.CompleteIpRotation(cluster_ref)
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)
        elif args.update_labels is not None:
            try:
                op_ref = adapter.UpdateLabels(cluster_ref, args.update_labels)
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)
        elif args.remove_labels is not None:
            try:
                op_ref = adapter.RemoveLabels(cluster_ref, args.remove_labels)
            except apitools_exceptions.HttpError as error:
                raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)
        else:
            enable_master_authorized_networks = args.enable_master_authorized_networks

            if args.enable_legacy_authorization is not None:
                op_ref = adapter.SetLegacyAuthorization(
                    cluster_ref, args.enable_legacy_authorization)
            else:
                options = api_adapter.UpdateClusterOptions(
                    monitoring_service=args.monitoring_service,
                    disable_addons=args.disable_addons,
                    enable_autoscaling=args.enable_autoscaling,
                    min_nodes=args.min_nodes,
                    max_nodes=args.max_nodes,
                    node_pool=args.node_pool,
                    locations=locations,
                    enable_master_authorized_networks=
                    enable_master_authorized_networks,
                    master_authorized_networks=args.master_authorized_networks)
                op_ref = adapter.UpdateCluster(cluster_ref, options)

        if not args. async:
            adapter.WaitForOperation(
                op_ref, 'Updating {0}'.format(cluster_ref.clusterId))

            log.UpdatedResource(cluster_ref)

            if args.start_ip_rotation or args.complete_ip_rotation:
                cluster = adapter.GetCluster(cluster_ref)
                try:
                    util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
                except kconfig.MissingEnvVarError as error:
                    log.warning(error.message)