Network Key Translation Feature and test cases (#1195)

This commit is contained in:
Mudit Verma
2019-12-01 10:19:23 +08:00
committed by Hang Yan
parent c13d50ee17
commit ac2b852955
18 changed files with 606 additions and 40 deletions
+4 -10
View File
@@ -30,7 +30,7 @@ import (
log "github.com/sirupsen/logrus"
)
// StdinData is data bytes read from stdin
//StdinData is data bytes read from stdin
var StdinData []byte
// Compose is docker compose file loader, implements Loader interface
@@ -72,20 +72,16 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
"Ulimits": false,
"Net": false,
"Sysctls": false,
"Networks": false, // there are special checks for Network in checkUnsupportedKey function
"Links": false,
//"Networks": false, // We shall be spporting network now. There are special checks for Network in checkUnsupportedKey function
"Links": false,
}
// collect all keys found in project
var keysFound []string
// Root level keys are not yet supported
// Root level keys are not yet supported except Network
// Check to see if the default network is available and length is only equal to one.
// Else, warn the user that root level networks are not supported (yet)
if _, ok := composeProject.NetworkConfigs["default"]; ok && len(composeProject.NetworkConfigs) == 1 {
log.Debug("Default network found")
} else if len(composeProject.NetworkConfigs) > 0 {
keysFound = append(keysFound, "root level networks")
}
// Root level volumes are not yet supported
@@ -117,8 +113,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
if len(serviceConfig.Networks.Networks) == 1 && serviceConfig.Networks.Networks[0].Name == "default" {
// this is empty Network definition, skip it
continue
} else {
yamlTagName = "networks"
}
}
+30 -10
View File
@@ -260,7 +260,7 @@ func TestUnsupportedKeys(t *testing.T) {
Ports: []string{}, // test empty array
Networks: &yaml.Networks{
Networks: []*yaml.Network{
&yaml.Network{
{
Name: "net1",
},
},
@@ -275,19 +275,19 @@ func TestUnsupportedKeys(t *testing.T) {
Ports: []string{}, // test empty array
Networks: &yaml.Networks{
Networks: []*yaml.Network{
&yaml.Network{
{
Name: "net1",
},
},
},
})
projectWithNetworks.VolumeConfigs = map[string]*config.VolumeConfig{
"foo": &config.VolumeConfig{
"foo": {
Driver: "storage",
},
}
projectWithNetworks.NetworkConfigs = map[string]*config.NetworkConfig{
"foo": &config.NetworkConfig{
"foo": {
Driver: "bridge",
},
}
@@ -304,7 +304,7 @@ func TestUnsupportedKeys(t *testing.T) {
projectWithDefaultNetwork.ServiceConfigs.Add("foo", &config.ServiceConfig{
Networks: &yaml.Networks{
Networks: []*yaml.Network{
&yaml.Network{
{
Name: "default",
},
},
@@ -318,11 +318,8 @@ func TestUnsupportedKeys(t *testing.T) {
}{
"With Networks (service and root level)": {
projectWithNetworks,
[]string{"root level networks", "root level volumes", "networks"},
},
"Empty Networks on Service level": {
projectWithEmptyNetwork,
[]string{"networks"},
//root level network and network are now supported"
[]string{"root level volumes"},
},
"Default root level Network": {
projectWithDefaultNetwork,
@@ -359,6 +356,29 @@ func TestNormalizeServiceNames(t *testing.T) {
}
}
func TestNormalizeNetworkNames(t *testing.T) {
testCases := []struct {
composeNetworkName string
normalizedNetworkName string
}{
{"foo_bar", "foobar"},
{"foo", "foo"},
{"FOO", "foo"},
{"foo.bar", "foo.bar"},
//{"", ""},
}
for _, testCase := range testCases {
returnValue, err := normalizeNetworkNames(testCase.composeNetworkName)
if err != nil {
t.Log("Unxpected error, got ", err)
}
if returnValue != testCase.normalizedNetworkName {
t.Logf("Expected %q, got %q", testCase.normalizedNetworkName, returnValue)
}
}
}
func TestCheckLabelsPorts(t *testing.T) {
testCases := []struct {
name string
+11
View File
@@ -131,6 +131,17 @@ func normalizeVolumes(svcName string) string {
return strings.Replace(svcName, "_", "-", -1)
}
func normalizeNetworkNames(netName string) (string, error) {
netval := strings.ToLower(netName)
regString := ("[^A-Za-z0-9.-]+")
reg, err := regexp.Compile(regString)
if err != nil {
return "", err
}
netval = reg.ReplaceAllString(netval, "")
return netval, nil
}
// ReadFile read data from file or stdin
func ReadFile(fileName string) ([]byte, error) {
if fileName == "-" {
+10 -2
View File
@@ -24,8 +24,6 @@ import (
"strconv"
"strings"
"k8s.io/kubernetes/pkg/api"
"github.com/docker/libcompose/config"
"github.com/docker/libcompose/lookup"
"github.com/docker/libcompose/project"
@@ -33,6 +31,7 @@ import (
"github.com/kubernetes/kompose/pkg/transformer"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/kubernetes/pkg/api"
)
// Parse Docker Compose with libcompose (only supports v1 and v2). Eventually we will
@@ -279,6 +278,15 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
serviceConfig.TmpFs = composeServiceConfig.Tmpfs
serviceConfig.StopGracePeriod = composeServiceConfig.StopGracePeriod
if composeServiceConfig.Networks != nil {
if len(composeServiceConfig.Networks.Networks) > 0 {
for _, value := range composeServiceConfig.Networks.Networks {
if value.Name != "default" {
serviceConfig.Network = append(serviceConfig.Network, value.RealName)
}
}
}
}
// Get GroupAdd, group should be mentioned in gid format but not the group name
groupAdd, err := getGroupAdd(composeServiceConfig.GroupAdd)
if err != nil {
+18
View File
@@ -281,6 +281,24 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
serviceConfig.HostName = composeServiceConfig.Hostname
serviceConfig.DomainName = composeServiceConfig.DomainName
//Adding network key related info
if len(composeServiceConfig.Networks) == 0 {
if defaultNetwork, ok := composeObject.Networks["default"]; ok {
serviceConfig.Network = append(serviceConfig.Network, defaultNetwork.Name)
}
} else {
var alias = ""
for key := range composeServiceConfig.Networks {
alias = key
netName := composeObject.Networks[alias].Name
// if Network Name Field is empty in the docker-compose definition
// we will use the alias name defined in service config file
if netName == "" {
netName = alias
}
serviceConfig.Network = append(serviceConfig.Network, netName)
}
}
//
// Deploy keys
//