forked from LaconicNetwork/kompose
Support port expose (#1227)
This commit is contained in:
parent
5732a555cb
commit
1089a26844
@ -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
|
||||
|
||||
@ -917,8 +917,6 @@ func (k *Kubernetes) ConfigHostPathVolumeSource(path string) (*api.VolumeSource,
|
||||
absPath = filepath.Join(dir, path)
|
||||
}
|
||||
|
||||
log.Debugf("fuck path: %s,%s", path, absPath)
|
||||
|
||||
return &api.VolumeSource{
|
||||
HostPath: &api.HostPathVolumeSource{Path: absPath},
|
||||
}, nil
|
||||
|
||||
@ -3,6 +3,44 @@
|
||||
"apiVersion": "v1",
|
||||
"metadata": {},
|
||||
"items": [
|
||||
{
|
||||
"kind": "Service",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "server",
|
||||
"creationTimestamp": null,
|
||||
"labels": {
|
||||
"io.kompose.service": "server"
|
||||
},
|
||||
"annotations": {
|
||||
"complexlabel": "override",
|
||||
"kompose.cmd": "%CMD%",
|
||||
"kompose.version": "%VERSION%",
|
||||
"simplelabel.first": "Foo",
|
||||
"simplelabel.second": "Bar"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"ports": [
|
||||
{
|
||||
"name": "3000",
|
||||
"port": 3000,
|
||||
"targetPort": 3000
|
||||
},
|
||||
{
|
||||
"name": "5000",
|
||||
"port": 5000,
|
||||
"targetPort": 5000
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
"io.kompose.service": "server"
|
||||
}
|
||||
},
|
||||
"status": {
|
||||
"loadBalancer": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
@ -64,6 +102,14 @@
|
||||
"image": "test",
|
||||
"imagePullPolicy": "",
|
||||
"name": "test-server",
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 3000
|
||||
},
|
||||
{
|
||||
"containerPort": 5000
|
||||
}
|
||||
],
|
||||
"resources": {},
|
||||
"volumeMounts": [
|
||||
{
|
||||
|
||||
@ -6,14 +6,19 @@ services:
|
||||
ports:
|
||||
- "5000:5000/tcp"
|
||||
- "5001:5000/tcp"
|
||||
# this may not be the correct usage in docker-compose
|
||||
expose:
|
||||
- "3000"
|
||||
- "5000"
|
||||
- "8000"
|
||||
links:
|
||||
- redis
|
||||
networks:
|
||||
- default
|
||||
|
||||
|
||||
redis:
|
||||
image: redis:3.0
|
||||
networks:
|
||||
networks:
|
||||
- default
|
||||
ports:
|
||||
- "6379/tcp"
|
||||
|
||||
@ -64,6 +64,16 @@
|
||||
"name": "5001",
|
||||
"port": 5001,
|
||||
"targetPort": 5000
|
||||
},
|
||||
{
|
||||
"name": "3000",
|
||||
"port": 3000,
|
||||
"targetPort": 3000
|
||||
},
|
||||
{
|
||||
"name": "8000",
|
||||
"port": 8000,
|
||||
"targetPort": 8000
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
@ -175,6 +185,12 @@
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 5000
|
||||
},
|
||||
{
|
||||
"containerPort": 3000
|
||||
},
|
||||
{
|
||||
"containerPort": 8000
|
||||
}
|
||||
],
|
||||
"resources": {}
|
||||
|
||||
@ -64,6 +64,16 @@
|
||||
"name": "5001",
|
||||
"port": 5001,
|
||||
"targetPort": 5000
|
||||
},
|
||||
{
|
||||
"name": "3000",
|
||||
"port": 3000,
|
||||
"targetPort": 3000
|
||||
},
|
||||
{
|
||||
"name": "8000",
|
||||
"port": 8000,
|
||||
"targetPort": 8000
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
@ -229,6 +239,12 @@
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 5000
|
||||
},
|
||||
{
|
||||
"containerPort": 3000
|
||||
},
|
||||
{
|
||||
"containerPort": 8000
|
||||
}
|
||||
],
|
||||
"resources": {}
|
||||
|
||||
@ -6,6 +6,9 @@ services:
|
||||
image: tutum/hello-world
|
||||
ports:
|
||||
- 80:80
|
||||
expose: # this may be mis-usage, only to test expose with ports.
|
||||
- "3000"
|
||||
- "80"
|
||||
networks:
|
||||
- helloworld-network
|
||||
|
||||
|
||||
8
script/test/fixtures/v3/output-k8s-3.5.json
vendored
8
script/test/fixtures/v3/output-k8s-3.5.json
vendored
@ -23,6 +23,11 @@
|
||||
"name": "80",
|
||||
"port": 80,
|
||||
"targetPort": 80
|
||||
},
|
||||
{
|
||||
"name": "3000",
|
||||
"port": 3000,
|
||||
"targetPort": 3000
|
||||
}
|
||||
],
|
||||
"selector": {
|
||||
@ -76,6 +81,9 @@
|
||||
"ports": [
|
||||
{
|
||||
"containerPort": 80
|
||||
},
|
||||
{
|
||||
"containerPort": 3000
|
||||
}
|
||||
],
|
||||
"resources": {}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user