Merge pull request #1798 from sosan/fix-issue-1793

fix issue 1793
This commit is contained in:
Kubernetes Prow Robot 2024-02-01 06:35:27 -08:00 committed by GitHub
commit 52e5dbda55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 10 deletions

View File

@ -891,17 +891,30 @@ func GetContentFromFile(file string) (string, error) {
}
// FormatEnvName format env name
func FormatEnvName(name string) string {
func FormatEnvName(name string, serviceName string) string {
envName := strings.Trim(name, "./")
// only take string after the last slash only if the string contains a slash
if strings.Contains(envName, "/") {
envName = envName[strings.LastIndex(envName, "/")+1:]
}
// take only last chars: The ones after 63th index
if len(envName) > 63 {
envName = envName[len(envName)-63:]
envName = strings.Replace(envName, ".", "-", -1)
envName = getUsableNameEnvFile(envName, serviceName)
return envName
}
// getUsableNameEnvFile checks and adjusts the environment file name to make it usable.
// If the first character of envName is a hyphen "-", it is concatenated with nameService.
// If the length of envName is greater than 63, it is truncated to 63 characters.
// Returns the adjusted environment file name.
func getUsableNameEnvFile(envName string, serviceName string) string {
if string(envName[0]) == "-" { // -env-local....
envName = fmt.Sprintf("%s%s", serviceName, envName)
}
return strings.Replace(envName, ".", "-", -1)
if len(envName) > 63 {
envName = envName[0:63]
}
return envName
}
// FormatFileName format file name

View File

@ -661,7 +661,8 @@ func TestReadOnlyRootFS(t *testing.T) {
func TestFormatEnvName(t *testing.T) {
type args struct {
name string
name string
serviceName string
}
tests := []struct {
name string
@ -694,12 +695,20 @@ func TestFormatEnvName(t *testing.T) {
args: args{
name: "abcdefghijklnmopqrstuvxyzabcdefghijklmnopqrstuvwxyzabcdejghijkl$Hereisadditional",
},
want: "rstuvxyzabcdefghijklmnopqrstuvwxyzabcdejghijkl$Hereisadditional",
want: "abcdefghijklnmopqrstuvxyzabcdefghijklmnopqrstuvwxyzabcdejghijkl",
},
{
name: "check that not begins with -",
args: args{
name: "src/app/.env",
serviceName: "app",
},
want: "app-env",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatEnvName(tt.args.name); got != tt.want {
if got := FormatEnvName(tt.args.name, tt.args.serviceName); got != tt.want {
t.Errorf("FormatEnvName() = %v, want %v", got, tt.want)
}
})

View File

@ -232,7 +232,7 @@ func (k *Kubernetes) InitConfigMapForEnv(name string, opt kobject.ConvertOptions
// Remove root pathing
// replace all other slashes / periods
envName := FormatEnvName(envFile)
envName := FormatEnvName(envFile, name)
// In order to differentiate files, we append to the name and remove '.env' if applicable from the file name
configMap := &api.ConfigMap{
@ -1132,7 +1132,7 @@ func ConfigEnvs(service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]ap
if len(service.EnvFile) > 0 {
// Load each env_file
for _, file := range service.EnvFile {
envName := FormatEnvName(file)
envName := FormatEnvName(file, service.Name)
// Load environment variables from file
workDir, err := transformer.GetComposeFileDir(opt.InputFiles)