Feat: Add hostPort and protocol to containers (#1558)

* feat: add the host port to containers

* test(unit): add unit tests for host port and protocol

* test(functional): add functional tests of host port and protocol

* test(functional): add functional tests of host port and protocol for openshift
This commit is contained in:
AhmedGrati
2023-02-08 15:46:13 -05:00
committed by GitHub
parent d43aefd882
commit 9ab4ef3a9c
6 changed files with 224 additions and 4 deletions
+2 -4
View File
@@ -623,10 +623,8 @@ func ConfigPorts(service kobject.ServiceConfig) []api.ContainerPort {
containerPort := api.ContainerPort{
ContainerPort: port.ContainerPort,
HostIP: port.HostIP,
}
// If the default is already TCP, no need to include protocol.
if protocol := api.Protocol(port.Protocol); protocol != api.ProtocolTCP {
containerPort.Protocol = protocol
HostPort: port.HostPort,
Protocol: api.Protocol(port.Protocol),
}
ports = append(ports, containerPort)
exist[port.ID()] = true
@@ -84,6 +84,15 @@ func newKomposeObject() kobject.KomposeObject {
}
}
func newKomposeObjectHostPortProtocolConfig() kobject.ServiceConfig {
return kobject.ServiceConfig{
Name: "nginx",
ContainerName: "nginx",
Image: "nginx",
Port: []kobject.Ports{{HostPort: 80, Protocol: string(api.ProtocolTCP), ContainerPort: 80}},
}
}
func equalStringSlice(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
@@ -938,3 +947,41 @@ func TestCreatePVC(t *testing.T) {
t.Errorf("Expected %s returned, got %s", storageClassName, *result.Spec.StorageClassName)
}
}
func TestCreateHostPortAndProtocol(t *testing.T) {
groupName := "pod_group"
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": newKomposeObjectHostPortProtocolConfig()},
}
k := Kubernetes{}
objs, err := k.Transform(komposeObject, kobject.ConvertOptions{ServiceGroupMode: groupName})
if err != nil {
t.Error(errors.Wrap(err, "k.Transform failed"))
}
for _, obj := range objs {
if deployment, ok := obj.(*appsv1.Deployment); ok {
container := deployment.Spec.Template.Spec.Containers[0]
port := container.Ports[0]
containerPort := port.ContainerPort
hostPort := port.HostPort
protocol := port.Protocol
expectedPort := komposeObject.ServiceConfigs["app"].Port[0]
expectedContainerPort := expectedPort.ContainerPort
expectedHostPort := expectedPort.HostPort
expectedProtocol := expectedPort.Protocol
if containerPort != expectedContainerPort {
t.Errorf("Expected container port %v, got %v", expectedContainerPort, containerPort)
}
if hostPort != expectedHostPort {
t.Errorf("Expected host port %v, got %v", expectedHostPort, hostPort)
}
if protocol != api.Protocol(expectedProtocol) {
t.Errorf("Expected protocol %v, got %v", expectedProtocol, protocol)
}
}
}
}