in ad-joining/register-computer/main.py [0:0]
def __shorten_computer_name(computer_name, gce_instance):
# Initialize hasher with a 2-byte size
hasher = blake2b(digest_size=2)
# We can shorten the name of instances if they are part of a MIG
if __get_managed_instance_group_for_instance(gce_instance):
if __is_gke_nodepool_member(gce_instance):
# For MIGs created by GKE, we will use a special naming convention
# Generate GKE node naming convention k-XXXXXXXX-YYYY
# k - Kubernetes node
# X - unique value given to the cluster's node pool
# Y - unique value given to each instance by the MIG
instance_name_parts = computer_name.rsplit('-', 2)
node_pool_hash = instance_name_parts[-2]
unique_id = instance_name_parts[-1]
new_computer_name = ("k-%s-%s" % (node_pool_hash, unique_id))
else:
# Generate MIG naming convention XXXXX-YYYY-ZZZZ
# X - partial MIG name
# Y - hashed value of MIG name
# Z - unique value given to each instance by the MIG
instance_name_parts = computer_name.rsplit('-', 1)
mig_name = instance_name_parts[-2]
unique_id = instance_name_parts[-1]
# Create a hash that produces 4 hex characters
hasher.update(mig_name.encode("utf-8"))
mig_name_hash = hasher.hexdigest()
# Get first 5 characters from MIG's name
mig_name_prefix = mig_name[:5]
new_computer_name = ("%s-%s-%s" % (mig_name_prefix, mig_name_hash, unique_id))
else:
# Not MIG - create a name using the convention XXXXXXXXXX-YYYY
# X - partial instance name
# Y - hashed value of instance name
hasher.update(computer_name.encode("utf-8"))
instance_name_hash = hasher.hexdigest()
instance_name_prefix = computer_name[:10]
new_computer_name = ("%s-%s" % (instance_name_prefix, instance_name_hash))
return new_computer_name