Example #1
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:
      Cluster message for the successfully created cluster.

    Raises:
      util.Error, if creation failed.
    """
        util.CheckKubectlInstalled()

        adapter = self.context['api_adapter']

        if not args.scopes:
            args.scopes = []
        cluster_ref = adapter.ParseCluster(args.name)
        options = self.ParseCreateOptions(args)

        if options.enable_kubernetes_alpha:
            console_io.PromptContinue(
                message=constants.KUBERNETES_ALPHA_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if options.enable_autorepair is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autorepair, 'autorepair'))

        if options.enable_autoupgrade is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autoupgrade, 'autoupgrade'))

        operation = None
        try:
            operation_ref = adapter.CreateCluster(cluster_ref, options)
            if args. async:
                return adapter.GetCluster(cluster_ref)

            operation = adapter.WaitForOperation(operation_ref,
                                                 'Creating cluster {0}'.format(
                                                     cluster_ref.clusterId),
                                                 timeout_s=args.timeout)
            cluster = adapter.GetCluster(cluster_ref)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        log.CreatedResource(cluster_ref)
        if operation.detail:
            # Non-empty detail on a DONE create operation should be surfaced as
            # a warning to end user.
            log.warning(operation.detail)

        try:
            util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
        except kconfig.MissingEnvVarError as error:
            log.warning(error.message)

        return cluster
Example #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:
      Cluster message for the successfully created cluster.

    Raises:
      util.Error, if creation failed.
    """
        if args.async_ and not args.IsSpecified('format'):
            args.format = util.OPERATIONS_FORMAT

        util.CheckKubectlInstalled()

        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)

        cluster_ref = adapter.ParseCluster(args.name, location)
        options = self.ParseCreateOptions(args)

        if options.private_cluster and not (
                options.enable_master_authorized_networks
                or options.master_authorized_networks):
            log.warning(
                '`--private-cluster` makes the master inaccessible from '
                'cluster-external IP addresses, by design. To allow limited '
                'access to the master, see the `--master-authorized-networks` flags '
                'and our documentation on setting up private clusters: '
                'https://cloud.google.com'
                '/kubernetes-engine/docs/how-to/private-clusters')

        if not options.enable_shielded_nodes:
            log.warning(
                'Starting with version 1.18, clusters will have shielded GKE nodes by default.'
            )

        if options.enable_ip_alias:
            log.warning(
                'The Pod address range limits the maximum size of the cluster. '
                'Please refer to https://cloud.google.com/kubernetes-engine/docs/how-to/flexible-pod-cidr to learn how to optimize IP address allocation.'
            )
        else:
            max_node_number = util.CalculateMaxNodeNumberByPodRange(
                options.cluster_ipv4_cidr)
            if max_node_number > 0:
                log.warning(
                    'Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most %d node(s). '
                    % max_node_number)

        if options.enable_kubernetes_alpha:
            console_io.PromptContinue(
                message=constants.KUBERNETES_ALPHA_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if options.enable_autorepair is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autorepair, 'autorepair'))

        if options.accelerators is not None:
            log.status.Print(constants.KUBERNETES_GPU_LIMITATION_MSG)

        operation = None
        try:
            operation_ref = adapter.CreateCluster(cluster_ref, options)
            if args.async_:
                return adapter.GetCluster(cluster_ref)

            operation = adapter.WaitForOperation(
                operation_ref,
                'Creating cluster {0} in {1}'.format(cluster_ref.clusterId,
                                                     cluster_ref.zone),
                timeout_s=args.timeout)
            cluster = adapter.GetCluster(cluster_ref)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        log.CreatedResource(cluster_ref)
        cluster_url = util.GenerateClusterUrl(cluster_ref)
        log.status.Print('To inspect the contents of your cluster, go to: ' +
                         cluster_url)
        if operation.detail:
            # Non-empty detail on a DONE create operation should be surfaced as
            # a warning to end user.
            log.warning(operation.detail)

        try:
            util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
        except kconfig.MissingEnvVarError as error:
            log.warning(error)

        return [cluster]
Example #3
0
class Create(base.CreateCommand):
    """Create a cluster for running containers."""
    @staticmethod
    def Args(parser):
        _Args(parser)
        _AddAdditionalZonesFlag(parser, deprecated=True)
        flags.AddNodeLocationsFlag(parser)
        flags.AddAddonsFlags(parser)
        flags.AddClusterAutoscalingFlags(parser)
        flags.AddDiskTypeFlag(parser, suppressed=True)
        flags.AddEnableAutoRepairFlag(parser, for_create=True)
        flags.AddEnableKubernetesAlphaFlag(parser)
        flags.AddEnableLegacyAuthorizationFlag(parser)
        flags.AddIPAliasFlags(parser)
        flags.AddLabelsFlag(parser)
        flags.AddLocalSSDFlag(parser)
        flags.AddMaintenanceWindowFlag(parser)
        flags.AddMasterAuthorizedNetworksFlags(parser)
        flags.AddMinCpuPlatformFlag(parser)
        flags.AddNetworkPolicyFlags(parser)
        flags.AddNodeTaintsFlag(parser)
        flags.AddPreemptibleFlag(parser)
        flags.AddDeprecatedClusterNodeIdentityFlags(parser)

    def ParseCreateOptions(self, args):
        return ParseCreateOptionsBase(args)

    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:
      Cluster message for the successfully created cluster.

    Raises:
      util.Error, if creation failed.
    """
        if args. async and not args.IsSpecified('format'):
            args.format = util.OPERATIONS_FORMAT

        util.CheckKubectlInstalled()

        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)

        cluster_ref = adapter.ParseCluster(args.name, location)
        options = self.ParseCreateOptions(args)

        if options.enable_kubernetes_alpha:
            console_io.PromptContinue(
                message=constants.KUBERNETES_ALPHA_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if getattr(args, 'region', None):
            # TODO(b/68496825): Remove this completely after regional clusters beta
            # launch.
            if self._release_track == base.ReleaseTrack.ALPHA:
                console_io.PromptContinue(
                    message=constants.KUBERNETES_REGIONAL_CHARGES_PROMPT,
                    throw_if_unattended=True,
                    cancel_on_no=True)

        if options.enable_autorepair is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autorepair, 'autorepair'))

        if options.enable_autoupgrade is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autoupgrade, 'autoupgrade'))

        if options.accelerators is not None:
            log.status.Print(constants.KUBERNETES_GPU_LIMITATION_MSG)

        operation = None
        try:
            operation_ref = adapter.CreateCluster(cluster_ref, options)
            if args. async:
                return adapter.GetCluster(cluster_ref)

            operation = adapter.WaitForOperation(operation_ref,
                                                 'Creating cluster {0}'.format(
                                                     cluster_ref.clusterId),
                                                 timeout_s=args.timeout)
            cluster = adapter.GetCluster(cluster_ref)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        log.CreatedResource(cluster_ref)
        cluster_url = util.GenerateClusterUrl(cluster_ref)
        log.status.Print('To inspect the contents of your cluster, go to: ' +
                         cluster_url)
        if operation.detail:
            # Non-empty detail on a DONE create operation should be surfaced as
            # a warning to end user.
            log.warning(operation.detail)

        try:
            util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
        except kconfig.MissingEnvVarError as error:
            log.warning(error)

        return [cluster]
Example #4
0
class Create(base.CreateCommand):
    """Create a cluster for running containers."""
    @staticmethod
    def Args(parser):
        _Args(parser)
        _AddAdditionalZonesFlag(parser, deprecated=True)
        flags.AddNodeLocationsFlag(parser)
        flags.AddAddonsFlags(parser)
        flags.AddClusterAutoscalingFlags(parser)
        flags.AddEnableAutoRepairFlag(parser, for_create=True)
        flags.AddEnableKubernetesAlphaFlag(parser)
        flags.AddEnableLegacyAuthorizationFlag(parser)
        flags.AddIPAliasFlags(parser)
        flags.AddLabelsFlag(parser)
        flags.AddLocalSSDFlag(parser)
        flags.AddMaintenanceWindowFlag(parser)
        flags.AddMasterAuthorizedNetworksFlags(parser)
        flags.AddMinCpuPlatformFlag(parser)
        flags.AddNetworkPolicyFlags(parser)
        flags.AddNodeTaintsFlag(parser)
        flags.AddPreemptibleFlag(parser)
        flags.AddDeprecatedClusterNodeIdentityFlags(parser)
        flags.AddPrivateClusterFlags(parser, with_deprecated=False)

    def ParseCreateOptions(self, args):
        return ParseCreateOptionsBase(args)

    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:
      Cluster message for the successfully created cluster.

    Raises:
      util.Error, if creation failed.
    """
        if args. async and not args.IsSpecified('format'):
            args.format = util.OPERATIONS_FORMAT

        util.CheckKubectlInstalled()

        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)

        cluster_ref = adapter.ParseCluster(args.name, location)
        options = self.ParseCreateOptions(args)

        if options.private_cluster and not (
                options.enable_master_authorized_networks
                or options.master_authorized_networks):
            log.warning(
                '`--private-cluster` makes the master inaccessible from '
                'cluster-external IP addresses, by design. To allow limited '
                'access to the master, see the `--master-authorized-networks` flags '
                'and our documentation on setting up private clusters: '
                'https://cloud.google.com'
                '/kubernetes-engine/docs/how-to/private-clusters')

        if not (options.metadata
                and 'disable-legacy-endpoints' in options.metadata):
            log.warning(
                'Starting in 1.12, default node pools in new clusters '
                'will have their legacy Compute Engine instance metadata '
                'endpoints disabled by default. To create a cluster with '
                'legacy instance metadata endpoints disabled in the default '
                'node pool, run `clusters create` with the flag '
                '`--metadata disable-legacy-endpoints=true`.')

        if options.enable_kubernetes_alpha:
            console_io.PromptContinue(
                message=constants.KUBERNETES_ALPHA_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if options.enable_autorepair is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autorepair, 'autorepair'))

        if options.enable_autoupgrade is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autoupgrade, 'autoupgrade'))

        if options.accelerators is not None:
            log.status.Print(constants.KUBERNETES_GPU_LIMITATION_MSG)

        operation = None
        try:
            operation_ref = adapter.CreateCluster(cluster_ref, options)
            if args. async:
                return adapter.GetCluster(cluster_ref)

            operation = adapter.WaitForOperation(
                operation_ref,
                'Creating cluster {0} in {1}'.format(cluster_ref.clusterId,
                                                     cluster_ref.zone),
                timeout_s=args.timeout)
            cluster = adapter.GetCluster(cluster_ref)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        log.CreatedResource(cluster_ref)
        cluster_url = util.GenerateClusterUrl(cluster_ref)
        log.status.Print('To inspect the contents of your cluster, go to: ' +
                         cluster_url)
        if operation.detail:
            # Non-empty detail on a DONE create operation should be surfaced as
            # a warning to end user.
            log.warning(operation.detail)

        try:
            util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
        except kconfig.MissingEnvVarError as error:
            log.warning(error)

        return [cluster]
Example #5
0
class Create(base.CreateCommand):
    """Create a cluster for running containers."""
    @staticmethod
    def Args(parser):
        _Args(parser)
        _AddAdditionalZonesFlag(parser)
        flags.AddClusterAutoscalingFlags(parser, hidden=True)
        flags.AddEnableAutoRepairFlag(parser, suppressed=True)
        flags.AddEnableAutoUpgradeFlag(parser, suppressed=True)
        flags.AddEnableKubernetesAlphaFlag(parser, suppressed=True)
        flags.AddEnableLegacyAuthorizationFlag(parser, hidden=True)
        flags.AddIPAliasFlags(parser, hidden=True)
        flags.AddLabelsFlag(parser, suppressed=True)
        flags.AddLocalSSDFlag(parser, suppressed=True)
        flags.AddMaintenanceWindowFlag(parser, hidden=True)
        flags.AddMasterAuthorizedNetworksFlags(parser, hidden=True)
        flags.AddNetworkPolicyFlags(parser, hidden=True)
        flags.AddNodeTaintsFlag(parser, hidden=True)
        flags.AddOldClusterScopesFlag(parser)
        flags.AddPreemptibleFlag(parser, suppressed=True)
        flags.AddServiceAccountFlag(parser, suppressed=True)

    def ParseCreateOptions(self, args):
        return ParseCreateOptionsBase(args)

    def Collection(self):
        return 'container.projects.zones.clusters'

    def DeprecatedFormat(self, args):
        return self.ListFormat(args)

    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:
      Cluster message for the successfully created cluster.

    Raises:
      util.Error, if creation failed.
    """
        if args. async and not args.IsSpecified('format'):
            args.format = util.OPERATIONS_FORMAT

        util.CheckKubectlInstalled()

        adapter = self.context['api_adapter']
        location_get = self.context['location_get']
        location = location_get(args)

        if not args.scopes:
            args.scopes = []
        cluster_ref = adapter.ParseCluster(args.name, location)
        options = self.ParseCreateOptions(args)

        if options.enable_kubernetes_alpha:
            console_io.PromptContinue(
                message=constants.KUBERNETES_ALPHA_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if getattr(args, 'region', None):
            console_io.PromptContinue(
                message=constants.KUBERNETES_REGIONAL_CHARGES_PROMPT,
                throw_if_unattended=True,
                cancel_on_no=True)

        if options.enable_autorepair is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autorepair, 'autorepair'))

        if options.enable_autoupgrade is not None:
            log.status.Print(
                messages.AutoUpdateUpgradeRepairMessage(
                    options.enable_autoupgrade, 'autoupgrade'))

        operation = None
        try:
            operation_ref = adapter.CreateCluster(cluster_ref, options)
            if args. async:
                return adapter.GetCluster(cluster_ref)

            operation = adapter.WaitForOperation(operation_ref,
                                                 'Creating cluster {0}'.format(
                                                     cluster_ref.clusterId),
                                                 timeout_s=args.timeout)
            cluster = adapter.GetCluster(cluster_ref)
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(error, util.HTTP_ERROR_FORMAT)

        log.CreatedResource(cluster_ref)
        if operation.detail:
            # Non-empty detail on a DONE create operation should be surfaced as
            # a warning to end user.
            log.warning(operation.detail)

        try:
            util.ClusterConfig.Persist(cluster, cluster_ref.projectId)
        except kconfig.MissingEnvVarError as error:
            log.warning(error.message)

        return [cluster]