From 578fa72a9a8fd54595134aeee7636f37809f6b76 Mon Sep 17 00:00:00 2001 From: jose luis <2064537+sosan@users.noreply.github.com> Date: Wed, 24 Jan 2024 23:41:01 +0100 Subject: [PATCH] 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> --- pkg/transformer/kubernetes/k8sutils.go | 21 +++++++++++++++++---- pkg/transformer/kubernetes/k8sutils_test.go | 15 ++++++++++++--- pkg/transformer/kubernetes/kubernetes.go | 4 ++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go index 49510ec82..b1f06834f 100644 --- a/pkg/transformer/kubernetes/k8sutils.go +++ b/pkg/transformer/kubernetes/k8sutils.go @@ -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 + + 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) + } if len(envName) > 63 { - envName = envName[len(envName)-63:] + envName = envName[0:63] } - return strings.Replace(envName, ".", "-", -1) + return envName } // FormatFileName format file name diff --git a/pkg/transformer/kubernetes/k8sutils_test.go b/pkg/transformer/kubernetes/k8sutils_test.go index 3be2d53cb..637d7f2b0 100644 --- a/pkg/transformer/kubernetes/k8sutils_test.go +++ b/pkg/transformer/kubernetes/k8sutils_test.go @@ -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) } }) diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index f5b61971b..a939536b2 100644 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -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)