fix issue 1793

* fix issue 1793  https://github.com/kubernetes/kompose/issues/1793
*add tests

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

formated k8utils_test.go

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

labels formatted as name"-"envName to match fixtures when performing the gitHub action

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

removed this piece code because apply it later, and it is redundant

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

Refactor test in k8sutils_test.go to extract the last 63 characters. This addresses the impact of the removed code that previously truncated the input with if len(envName) > 63 { envName = envName[len(envName)-63:] }

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

changed to name function to getUsableNameEnvFile

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

fix issue 1793

* fix issue 1793  https://github.com/kubernetes/kompose/issues/1793
*add tests

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

labels formatted as name"-"envName to match fixtures when performing the gitHub action

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>

changed to name function to getUsableNameEnvFile

Signed-off-by: jose luis <2064537+sosan@users.noreply.github.com>
This commit is contained in:
jose luis
2024-01-24 23:41:01 +01:00
parent 3499798b5a
commit 578fa72a9a
3 changed files with 32 additions and 10 deletions
+18 -5
View File
@@ -891,17 +891,30 @@ func GetContentFromFile(file string) (string, error) {
} }
// FormatEnvName format env name // FormatEnvName format env name
func FormatEnvName(name string) string { func FormatEnvName(name string, serviceName string) string {
envName := strings.Trim(name, "./") envName := strings.Trim(name, "./")
// only take string after the last slash only if the string contains a slash // only take string after the last slash only if the string contains a slash
if strings.Contains(envName, "/") { if strings.Contains(envName, "/") {
envName = envName[strings.LastIndex(envName, "/")+1:] envName = envName[strings.LastIndex(envName, "/")+1:]
} }
// take only last chars: The ones after 63th index
if len(envName) > 63 { envName = strings.Replace(envName, ".", "-", -1)
envName = envName[len(envName)-63:] 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 // FormatFileName format file name
+12 -3
View File
@@ -661,7 +661,8 @@ func TestReadOnlyRootFS(t *testing.T) {
func TestFormatEnvName(t *testing.T) { func TestFormatEnvName(t *testing.T) {
type args struct { type args struct {
name string name string
serviceName string
} }
tests := []struct { tests := []struct {
name string name string
@@ -694,12 +695,20 @@ func TestFormatEnvName(t *testing.T) {
args: args{ args: args{
name: "abcdefghijklnmopqrstuvxyzabcdefghijklmnopqrstuvwxyzabcdejghijkl$Hereisadditional", 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("FormatEnvName() = %v, want %v", got, tt.want)
} }
}) })
+2 -2
View File
@@ -232,7 +232,7 @@ func (k *Kubernetes) InitConfigMapForEnv(name string, opt kobject.ConvertOptions
// Remove root pathing // Remove root pathing
// replace all other slashes / periods // 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 // In order to differentiate files, we append to the name and remove '.env' if applicable from the file name
configMap := &api.ConfigMap{ configMap := &api.ConfigMap{
@@ -1132,7 +1132,7 @@ func ConfigEnvs(service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]ap
if len(service.EnvFile) > 0 { if len(service.EnvFile) > 0 {
// Load each env_file // Load each env_file
for _, file := range service.EnvFile { for _, file := range service.EnvFile {
envName := FormatEnvName(file) envName := FormatEnvName(file, service.Name)
// Load environment variables from file // Load environment variables from file
workDir, err := transformer.GetComposeFileDir(opt.InputFiles) workDir, err := transformer.GetComposeFileDir(opt.InputFiles)