mirror of
https://github.com/outbackdingo/kubernetes.git
synced 2026-01-28 02:19:27 +00:00
In a downstream test job, I've seen an e2e test failing to create a directory for Pod logs when running an e2e test. Shorten them to 255 characters. For example, consider this test: "[sig-storage] CSI Mock selinux on mount metrics and SELinuxWarningController SELinuxMount metrics [LinuxOnly] [Feature:SELinux] [Serial] error is not bumped on two Pods with a different policy RWX volume (MountOption + MountOption) [FeatureGate:SELinuxMountReadWriteOncePod] [Beta] [FeatureGate:SELinuxChangePolicy] [Beta] [FeatureGate:SELinuxMount] [Beta] [Feature:OffByDefault] [sig-storage, Feature:SELinux, Serial, FeatureGate:SELinuxMountReadWriteOncePod, Beta, FeatureGate:SELinuxChangePolicy, FeatureGate:SELinuxMount, Feature:OffByDefault, BetaOffByDefault]" During execution, the test will create a directory `error_is_not_bumped_on_two_Pods_with_Recursive_policy_and_a_different_context_on_RWX_volume_FeatureGate_SELinuxMountReadWriteOncePod_Beta_FeatureGate_SELinuxChangePolicy_Beta_FeatureGate_SELinuxMount_Beta_Feature_OffByDefault_`, which has 226 characters and it's close to the limit (256).
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
/*
|
|
Copyright 2025 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"hash/crc32"
|
|
)
|
|
|
|
// The max length for ntfs, ext4, xfs and btrfs.
|
|
const maxFileNameLength = 255
|
|
|
|
// Shorten a file name to size allowed by the most common filesystems.
|
|
// If the filename is too long, cut it + add a short hash (crc32) that makes it unique.
|
|
// Note that the input should be a single file / directory name, not a path
|
|
// composed of several directories.
|
|
func ShortenFileName(filename string) string {
|
|
if len(filename) <= maxFileNameLength {
|
|
return filename
|
|
}
|
|
|
|
hash := crc32.ChecksumIEEE([]byte(filename))
|
|
hashString := fmt.Sprintf("%x", hash)
|
|
hashLen := len(hashString)
|
|
|
|
return fmt.Sprintf("%s-%s", filename[:maxFileNameLength-1-hashLen], hashString)
|
|
}
|