forked from LaconicNetwork/kompose
Support port expose (#1227)
This commit is contained in:
@@ -89,8 +89,9 @@ func TestLoadV3Ports(t *testing.T) {
|
||||
Published: 80,
|
||||
Protocol: "TCP",
|
||||
}
|
||||
expose := []string{"80", "8080"}
|
||||
ports := []types.ServicePortConfig{port}
|
||||
output := loadV3Ports(ports)
|
||||
output := loadV3Ports(ports, expose)
|
||||
expected := kobject.Ports{
|
||||
HostPort: 80,
|
||||
ContainerPort: 80,
|
||||
@@ -101,6 +102,16 @@ func TestLoadV3Ports(t *testing.T) {
|
||||
t.Errorf("Expected %v, got %v", expected, output[0])
|
||||
}
|
||||
|
||||
ep2 := kobject.Ports{
|
||||
HostPort: 8080,
|
||||
ContainerPort: 8080,
|
||||
Protocol: api.ProtocolTCP,
|
||||
}
|
||||
|
||||
if output[1] != ep2 {
|
||||
t.Errorf("Expected %v, got %v", ep2, output[1])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Test if service types are parsed properly on user input
|
||||
@@ -169,7 +180,7 @@ func TestLoadPorts(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
result, err := loadPorts(tt.ports)
|
||||
result, err := loadPorts(tt.ports, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error with loading ports %v", err)
|
||||
}
|
||||
|
||||
@@ -84,9 +84,11 @@ func parseV1V2(files []string) (kobject.KomposeObject, error) {
|
||||
}
|
||||
|
||||
// Load ports from compose file
|
||||
func loadPorts(composePorts []string) ([]kobject.Ports, error) {
|
||||
// also load `expose` here
|
||||
func loadPorts(composePorts []string, expose []string) ([]kobject.Ports, error) {
|
||||
ports := []kobject.Ports{}
|
||||
character := ":"
|
||||
exist := map[string]bool{}
|
||||
|
||||
// For each port listed
|
||||
for _, port := range composePorts {
|
||||
@@ -175,6 +177,32 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// load remain expose ports
|
||||
for _, port := range ports {
|
||||
// must use cast...
|
||||
exist[cast.ToString(port.ContainerPort)+string(port.Protocol)] = true
|
||||
}
|
||||
|
||||
if expose != nil {
|
||||
for _, port := range expose {
|
||||
portValue := port
|
||||
protocol := api.ProtocolTCP
|
||||
if strings.Contains(portValue, "/") {
|
||||
splits := strings.Split(port, "/")
|
||||
portValue = splits[0]
|
||||
protocol = api.Protocol(strings.ToUpper(splits[1]))
|
||||
}
|
||||
|
||||
if !exist[portValue+string(protocol)] {
|
||||
ports = append(ports, kobject.Ports{
|
||||
ContainerPort: cast.ToInt32(portValue),
|
||||
Protocol: protocol,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ports, nil
|
||||
}
|
||||
|
||||
@@ -204,6 +232,7 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
|
||||
serviceConfig.Args = composeServiceConfig.Command
|
||||
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
|
||||
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
|
||||
serviceConfig.Expose = composeServiceConfig.Expose
|
||||
|
||||
envs := loadEnvVars(composeServiceConfig.Environment)
|
||||
serviceConfig.Environment = envs
|
||||
@@ -213,8 +242,8 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
|
||||
log.Fatalf("%q defined in service %q is an absolute path, it must be a relative path.", serviceConfig.Dockerfile, name)
|
||||
}
|
||||
|
||||
// load ports
|
||||
ports, err := loadPorts(composeServiceConfig.Ports)
|
||||
// load ports, same as v3, we also load `expose`
|
||||
ports, err := loadPorts(composeServiceConfig.Ports, serviceConfig.Expose)
|
||||
if err != nil {
|
||||
return kobject.KomposeObject{}, errors.Wrap(err, "loadPorts failed. "+name+" failed to load ports from compose file")
|
||||
}
|
||||
@@ -279,7 +308,7 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
|
||||
serviceConfig.CapAdd = composeServiceConfig.CapAdd
|
||||
serviceConfig.CapDrop = composeServiceConfig.CapDrop
|
||||
serviceConfig.Pid = composeServiceConfig.Pid
|
||||
serviceConfig.Expose = composeServiceConfig.Expose
|
||||
|
||||
serviceConfig.Privileged = composeServiceConfig.Privileged
|
||||
serviceConfig.Restart = composeServiceConfig.Restart
|
||||
serviceConfig.User = composeServiceConfig.User
|
||||
|
||||
@@ -181,9 +181,12 @@ func loadV3Volumes(volumes []types.ServiceVolumeConfig) []string {
|
||||
}
|
||||
|
||||
// Convert Docker Compose v3 ports to kobject.Ports
|
||||
func loadV3Ports(ports []types.ServicePortConfig) []kobject.Ports {
|
||||
// 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 {
|
||||
|
||||
// Convert to a kobject struct with ports
|
||||
@@ -196,6 +199,30 @@ func loadV3Ports(ports []types.ServicePortConfig) []kobject.Ports {
|
||||
Protocol: api.Protocol(strings.ToUpper(string(port.Protocol))),
|
||||
})
|
||||
|
||||
exist[cast.ToString(port.Target)+strings.ToUpper(string(port.Protocol))] = true
|
||||
|
||||
}
|
||||
|
||||
if expose != nil {
|
||||
for _, port := range expose {
|
||||
portValue := port
|
||||
protocol := api.ProtocolTCP
|
||||
if strings.Contains(portValue, "/") {
|
||||
splits := strings.Split(port, "/")
|
||||
portValue = splits[0]
|
||||
protocol = api.Protocol(strings.ToUpper(splits[1]))
|
||||
}
|
||||
|
||||
if exist[portValue+string(protocol)] {
|
||||
continue
|
||||
}
|
||||
komposePorts = append(komposePorts, kobject.Ports{
|
||||
HostPort: cast.ToInt32(portValue),
|
||||
ContainerPort: cast.ToInt32(portValue),
|
||||
HostIP: "",
|
||||
Protocol: protocol,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return komposePorts
|
||||
@@ -404,7 +431,9 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
||||
// Parse the ports
|
||||
// v3 uses a new format called "long syntax" starting in 3.2
|
||||
// https://docs.docker.com/compose/compose-file/#ports
|
||||
serviceConfig.Port = loadV3Ports(composeServiceConfig.Ports)
|
||||
|
||||
// here we will translate `expose` too, they basically means the same thing in kubernetes
|
||||
serviceConfig.Port = loadV3Ports(composeServiceConfig.Ports, serviceConfig.Expose)
|
||||
|
||||
// Parse the volumes
|
||||
// Again, in v3, we use the "long syntax" for volumes in terms of parsing
|
||||
|
||||
Reference in New Issue
Block a user