Limit GCE tag for role to 63 chars

This commit is contained in:
Ciprian Hacman 2022-06-24 17:49:34 +03:00
parent 3310db3447
commit ccf31f7491
2 changed files with 19 additions and 1 deletions

View File

@ -67,5 +67,5 @@ func DecodeGCELabel(s string) (string, error) {
// TagForRole return the instance (network) tag used for instances with the given role.
func TagForRole(clusterName string, role kops.InstanceGroupRole) string {
return SafeClusterName(clusterName) + "-" + GceLabelNameRolePrefix + strings.ToLower(string(role))
return ClusterPrefixedName(GceLabelNameRolePrefix+strings.ToLower(string(role)), clusterName, 63)
}

View File

@ -49,6 +49,24 @@ func IsNotReady(err error) bool {
return false
}
// ClusterPrefixedName returns a cluster-prefixed name, with a maxLength
func ClusterPrefixedName(objectName string, clusterName string, maxLength int) string {
suffix := "-" + objectName
suffixLength := maxLength - len(suffix)
// GCE does not support . in tags / names
safeClusterName := strings.Replace(clusterName, ".", "-", -1)
opt := truncate.TruncateStringOptions{
MaxLength: suffixLength,
AlwaysAddHash: false,
HashLength: 6,
}
prefix := truncate.TruncateString(safeClusterName, opt)
return prefix + suffix
}
// ClusterSuffixedName returns a cluster-suffixed name, with a maxLength
func ClusterSuffixedName(objectName string, clusterName string, maxLength int) (string, error) {
prefix := objectName + "-"