Repository: kubernetes/kompose
Pull request #1515 (2022-08-16 10:56:41)

fix: change the port ID definition in kubernetes.go file
# Problem
Fix this [issue](https://github.com/kubernetes/kompose/issues/1514). The problem was mainly, in the ID of each port.
In the previous implementation, the ID was composed of the ASCII code of the port number and its protocol. However, such implementation led to some inconveniences for the large port numbers. In fact, If you try this command `fmt.Println(string(55546) == string(55547))` it will print `true`, which is not the desired behavior and it's the main cause behind the issue.
# Solution
To overcome this issue, I just replaced the `string()` function with `strconv.Itoa()`. I added large close numbers in the tests suite, to ensure that we get the desired behavior.

linux-foundation-easycla[bot]:  <a href="https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/18706487/62088377/1515/#/?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/1515/#/?version=2' target='_blank'>:x:</a> - login: @AhmedGrati . The commit (86e08c2d93237944cd23b919d1b32c65a32b1cca) is not authorized under a signed CLA. <a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/18706487/62088377/1515/#/?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 @AhmedGrati! <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/1515#" title="Author self-approved">AhmedGrati</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign surajssd for approval by writing `/assign @surajssd` in a comment. 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/master/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":["surajssd"]} -->
cdrage: LGTM!

Patch:
diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go
index f2cf67c2..94a2df34 100644
--- a/pkg/kobject/kobject.go
+++ b/pkg/kobject/kobject.go
@@ -18,6 +18,7 @@ package kobject
 
 import (
 	"path/filepath"
+	"strconv"
 	"time"
 
 	dockerCliTypes "github.com/docker/cli/cli/compose/types"
@@ -196,7 +197,7 @@ type Ports struct {
 
 // ID returns an unique id for this port settings, to avoid conflict
 func (port *Ports) ID() string {
-	return string(port.ContainerPort) + port.Protocol
+	return strconv.Itoa(int(port.ContainerPort)) + port.Protocol
 }
 
 // Volumes holds the volume struct of container
diff --git a/pkg/loader/compose/v1v2.go b/pkg/loader/compose/v1v2.go
index 5d800e55..aff7c53f 100644
--- a/pkg/loader/compose/v1v2.go
+++ b/pkg/loader/compose/v1v2.go
@@ -90,7 +90,6 @@ func parseV1V2(files []string) (kobject.KomposeObject, error) {
 func loadPorts(composePorts []string, expose []string) ([]kobject.Ports, error) {
 	kp := []kobject.Ports{}
 	exist := map[string]bool{}
-
 	for _, cp := range composePorts {
 		var hostIP string
 
diff --git a/pkg/loader/compose/v3.go b/pkg/loader/compose/v3.go
index e58925f7..2e218e76 100644
--- a/pkg/loader/compose/v3.go
+++ b/pkg/loader/compose/v3.go
@@ -222,7 +222,6 @@ func loadV3Volumes(volumes []types.ServiceVolumeConfig) []string {
 // expose ports will be treated as TCP ports
 func loadV3Ports(ports []types.ServicePortConfig, expose []string) []kobject.Ports {
 	komposePorts := []kobject.Ports{}
-
 	exist := map[string]bool{}
 
 	for _, port := range ports {
@@ -235,7 +234,6 @@ func loadV3Ports(ports []types.ServicePortConfig, expose []string) []kobject.Por
 			HostIP:        "",
 			Protocol:      strings.ToUpper(port.Protocol),
 		})
-
 		exist[cast.ToString(port.Target)+port.Protocol] = true
 	}
 
diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go
index f2e6317f..fb530d5d 100644
--- a/pkg/transformer/kubernetes/k8sutils.go
+++ b/pkg/transformer/kubernetes/k8sutils.go
@@ -513,7 +513,6 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
 
 	// Configure the container ports.
 	ports := ConfigPorts(service)
-
 	// Configure capabilities
 	capabilities := ConfigCapabilities(service)
 
diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go
index 6d2dd1ac..b412e456 100644
--- a/pkg/transformer/kubernetes/kubernetes.go
+++ b/pkg/transformer/kubernetes/kubernetes.go
@@ -1436,7 +1436,6 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
 						objects = append(objects, c)
 					}
 				}
-
 				podSpec.Append(
 					SetPorts(service),
 					ImagePullPolicy(name, service),
@@ -1492,17 +1491,14 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
 		} else {
 			objects = k.CreateWorkloadAndConfigMapObjects(name, service, opt)
 		}
-
 		if opt.Controller == StatefulStateController {
 			service.ServiceType = "Headless"
 		}
 		k.configKubeServiceAndIngressForService(service, name, &objects)
-
 		err := k.UpdateKubernetesObjects(name, service, opt, &objects)
 		if err != nil {
 			return nil, errors.Wrap(err, "Error transforming Kubernetes objects")
 		}
-
 		if err := k.configNetworkPolicyForService(service, name, &objects); err != nil {
 			return nil, err
 		}
@@ -1513,7 +1509,6 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
 	k.SortServicesFirst(&allobjects)
 	k.RemoveDupObjects(&allobjects)
 	// k.FixWorkloadVersion(&allobjects)
-
 	return allobjects, nil
 }
 
diff --git a/pkg/transformer/kubernetes/podspec.go b/pkg/transformer/kubernetes/podspec.go
index 040a95ef..7531b3eb 100644
--- a/pkg/transformer/kubernetes/podspec.go
+++ b/pkg/transformer/kubernetes/podspec.go
@@ -210,7 +210,6 @@ func SetPorts(service kobject.ServiceConfig) PodSpecOption {
 	return func(podSpec *PodSpec) {
 		// Configure the container ports.
 		ports := ConfigPorts(service)
-
 		for i := range podSpec.Containers {
 			if GetContainerName(service) == podSpec.Containers[i].Name {
 				podSpec.Containers[i].Ports = ports


Test patch:
diff --git a/pkg/transformer/kubernetes/kubernetes_test.go b/pkg/transformer/kubernetes/kubernetes_test.go
index 6134899d..b732bff3 100644
--- a/pkg/transformer/kubernetes/kubernetes_test.go
+++ b/pkg/transformer/kubernetes/kubernetes_test.go
@@ -44,7 +44,7 @@ func newServiceConfig() kobject.ServiceConfig {
 		ContainerName:   "name",
 		Image:           "image",
 		Environment:     []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}},
-		Port:            []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456}, kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: string(api.ProtocolUDP)}},
+		Port:            []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456}, kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: string(api.ProtocolUDP)}, kobject.Ports{HostPort: 55564, ContainerPort: 55564}, kobject.Ports{HostPort: 55563, ContainerPort: 55563}},
 		Command:         []string{"cmd"},
 		WorkingDir:      "dir",
 		Args:            []string{"arg1", "arg2"},
@@ -119,6 +119,7 @@ func equalPorts(kobjectPorts []kobject.Ports, k8sPorts []api.ContainerPort) bool
 		return false
 	}
 	for _, port := range kobjectPorts {
+		fmt.Println(port)
 		found := false
 		for _, k8sPort := range k8sPorts {
 			// FIXME: HostPort should be copied to container port