def testCalculateMaxNodeNumberByPodRange(self): testcases = [ # cluster_ipv4_cidr, expected return ('bad', -1), (None, 1008), ('10.0.0.0/20', -1), ('10.0.0.0/19', 16), ('10.0.0.0/-1', -1), ('10.0.0.0/bad', -1), ('10.0.0.0/10', 16368), ] for (cluster_ipv4_cidr, want_return) in testcases: got_return = c_util.CalculateMaxNodeNumberByPodRange( cluster_ipv4_cidr) self.assertEqual( got_return, want_return, 'CalculateMaxNodeNumberByPodRange(%s) = %d; want %d' % ('None' if cluster_ipv4_cidr is None else cluster_ipv4_cidr, got_return, want_return))
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.AddMaxPodsPerNodeFlag(parser) flags.AddEnableAutoRepairFlag(parser, for_create=True) flags.AddEnableKubernetesAlphaFlag(parser) flags.AddEnableStackdriverKubernetesFlag(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.AddClusterNodeIdentityFlags(parser) flags.AddPrivateClusterFlags(parser, with_deprecated=False, with_alpha=False) flags.AddClusterVersionFlag(parser) flags.AddNodeVersionFlag(parser) flags.AddEnableAutoUpgradeFlag(parser) flags.AddTpuFlags(parser, hidden=False) flags.AddResourceUsageExportFlags(parser) def ParseCreateOptions(self, args): flags.WarnGAForFutureAutoUpgradeChange() 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_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]
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.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]