Skip to content

Commit

Permalink
Merge pull request #1798 from sosan/fix-issue-1793
Browse files Browse the repository at this point in the history
fix issue 1793
  • Loading branch information
k8s-ci-robot authored Feb 1, 2024
2 parents e869594 + 578fa72 commit 52e5dbd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
21 changes: 17 additions & 4 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions pkg/transformer/kubernetes/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 52e5dbd

Please sign in to comment.