Repository: kubernetes/kompose
Pull request #1798 (2024-01-09 19:56:39)

fix issue 1793
What type of PR is this?
/kind bug

Which issue(s) this PR fixes:
* Fixes  https://github.com/kubernetes/kompose/issues/1793  
* add tests

Special notes for your reviewer:
* Created new function checkUsableNameEnvFile


linux-foundation-easycla[bot]:  <a href="https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/18706487/62088377/1798/#/?version=2"><img src="https://s3.amazonaws.com/cla-project-logo-prod/cla-not-signed.svg" alt="CLA Not Signed" align="left" height="28" width="328"></a><br/><br /><ul><li><a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/18706487/62088377/1798/#/?version=2' target='_blank'>:x:</a> - login: @sosan / name: Jose Luis . The commit (4d2b26be3eb7531178c9f92670fc033a3abf0beb) is not authorized under a signed CLA. <a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/18706487/62088377/1798/#/?version=2' target='_blank'>Please click here to be authorized</a>. For further assistance with EasyCLA, <a href='https://jira.linuxfoundation.org/servicedesk/customer/portal/4' target='_blank'>please submit a support request ticket</a>.</li></ul>
k8s-ci-robot: Welcome @sosan! <br><br>It looks like this is your first PR to <a href='https://github.com/kubernetes/kompose'>kubernetes/kompose</a> 🎉. Please refer to our [pull request process documentation](https://git.k8s.io/community/contributors/guide/pull-requests.md) to help your PR have a smooth ride to approval. <br><br>You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. [Here is the bot commands documentation](https://go.k8s.io/bot-commands). <br><br>You can also check if kubernetes/kompose has [its own contribution guidelines](https://github.com/kubernetes/kompose/tree/master/CONTRIBUTING.md). <br><br>You may want to refer to our [testing guide](https://git.k8s.io/community/contributors/devel/sig-testing/testing.md) if you run into trouble with your tests not passing. <br><br>If you are having difficulty getting your pull request seen, please follow the [recommended escalation practices](https://github.com/kubernetes/community/blob/master/contributors/guide/pull-requests.md#why-is-my-pull-request-not-getting-reviewed). Also, for tips and tricks in the contribution process you may want to read the [Kubernetes contributor cheat sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet/README.md). We want to make sure your contribution gets all the attention it needs! <br><br>Thank you, and welcome to Kubernetes. :smiley:
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes/kompose/pull/1798#" title="Author self-approved">sosan</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign [kadel](https://github.com/kadel) for approval. For more information see [the Kubernetes Code Review Process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process).

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes%2Fkompose).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes/kompose/blob/main/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["kadel"]} -->
AhmedGrati: @sosan thanks for working on this. You should update files in `script/test/fixtures` to make the e2e tests pass.
cdrage: The code LGTM. Are you able to add some fixture tests?
sosan: @cdrage rebased
AhmedGrati: @sosan For some reason your rebase went wrong.
sosan: @AhmedGrati done :)
cdrage: > @AhmedGrati done :)

looks like something still went wrong.. over 30 commits in this PR? Maybe your main branch isn't synced to the upstream kubernetes/kompose branch?

Patch:
diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go
index 49510ec8..b1f06834 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/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go
index f5b61971..a939536b 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)


Test patch:
diff --git a/pkg/transformer/kubernetes/k8sutils_test.go b/pkg/transformer/kubernetes/k8sutils_test.go
index 3be2d53c..637d7f2b 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)
 			}
 		})