Skip to content

Commit

Permalink
fix issue 1793
Browse files Browse the repository at this point in the history
* fix issue 1793  kubernetes#1793
*add tests

Signed-off-by: jose luis <[email protected]>
  • Loading branch information
sosan committed Jan 22, 2024
1 parent 7cc1b58 commit 4bc7e4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
21 changes: 19 additions & 2 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ 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, "/") {
Expand All @@ -901,7 +901,24 @@ func FormatEnvName(name string) string {
if len(envName) > 63 {
envName = envName[len(envName)-63:]
}
return strings.Replace(envName, ".", "-", -1)

envName = strings.Replace(envName, ".", "-", -1)
envName = checkUsableNameEnvFile(envName, serviceName)
return envName
}

// checkUsableNameEnvFile 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 checkUsableNameEnvFile(envName string, serviceName string) string {
if string(envName[0]) == "-" { // -env-local....
envName = fmt.Sprintf("%s%s", serviceName, envName)
}
if len(envName) > 63 {
envName = envName[0:63]
}
return envName
}

// FormatFileName format file name
Expand Down
11 changes: 10 additions & 1 deletion pkg/transformer/kubernetes/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ func TestReadOnlyRootFS(t *testing.T) {
func TestFormatEnvName(t *testing.T) {
type args struct {
name string
serviceName string
}
tests := []struct {
name string
Expand Down Expand Up @@ -696,10 +697,18 @@ func TestFormatEnvName(t *testing.T) {
},
want: "rstuvxyzabcdefghijklmnopqrstuvwxyzabcdejghijkl$Hereisadditional",
},
{
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)
}
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,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{
Expand All @@ -241,7 +241,7 @@ func (k *Kubernetes) InitConfigMapForEnv(name string, opt kobject.ConvertOptions
},
ObjectMeta: metav1.ObjectMeta{
Name: envName,
Labels: transformer.ConfigLabels(name + "-" + envName),
Labels: transformer.ConfigLabels(envName),
},
Data: envs,
}
Expand Down Expand Up @@ -1105,7 +1105,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)
Expand Down

0 comments on commit 4bc7e4d

Please sign in to comment.