forked from LaconicNetwork/kompose
Fix container name (#1528)
* fix: update get container name function * test: add a test for the introduced fix
This commit is contained in:
@@ -680,15 +680,15 @@ func getServiceGroupID(service kobject.ServiceConfig, mode string) string {
|
|||||||
|
|
||||||
// KomposeObjectToServiceConfigGroupMapping returns the service config group by name or by volume
|
// KomposeObjectToServiceConfigGroupMapping returns the service config group by name or by volume
|
||||||
// This group function works as following
|
// This group function works as following
|
||||||
// 1. Support two mode
|
// 1. Support two mode
|
||||||
// (1): label: use a custom label, the service that contains it will be merged to one workload.
|
// (1): label: use a custom label, the service that contains it will be merged to one workload.
|
||||||
// (2): volume: the service that share to exactly same volume config will be merged to one workload. If use pvc, only
|
// (2): volume: the service that share to exactly same volume config will be merged to one workload. If use pvc, only
|
||||||
// create one for this group.
|
// create one for this group.
|
||||||
// 2. If service containers restart policy and no workload argument provide and it's restart policy looks like a pod, then
|
// 2. If service containers restart policy and no workload argument provide and it's restart policy looks like a pod, then
|
||||||
// this service should generate a pod. If group mode specified, it should be grouped and ignore the restart policy.
|
// this service should generate a pod. If group mode specified, it should be grouped and ignore the restart policy.
|
||||||
// 3. If group mode specified, port conflict between services in one group will be ignored, and multiple service should be created.
|
// 3. If group mode specified, port conflict between services in one group will be ignored, and multiple service should be created.
|
||||||
// 4. If `volume` group mode specified, we don't have an appropriate name for this combined service, use the first one for now.
|
// 4. If `volume` group mode specified, we don't have an appropriate name for this combined service, use the first one for now.
|
||||||
// A warn/info message should be printed to let the user know.
|
// A warn/info message should be printed to let the user know.
|
||||||
func KomposeObjectToServiceConfigGroupMapping(komposeObject *kobject.KomposeObject, opt kobject.ConvertOptions) map[string]kobject.ServiceConfigGroup {
|
func KomposeObjectToServiceConfigGroupMapping(komposeObject *kobject.KomposeObject, opt kobject.ConvertOptions) map[string]kobject.ServiceConfigGroup {
|
||||||
serviceConfigGroup := make(map[string]kobject.ServiceConfigGroup)
|
serviceConfigGroup := make(map[string]kobject.ServiceConfigGroup)
|
||||||
|
|
||||||
@@ -881,7 +881,7 @@ func FormatFileName(name string) string {
|
|||||||
return strings.Replace(file, "_", "-", -1)
|
return strings.Replace(file, "_", "-", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormatContainerName format Container name
|
// FormatContainerName format Container name
|
||||||
func FormatContainerName(name string) string {
|
func FormatContainerName(name string) string {
|
||||||
name = strings.Replace(name, "_", "-", -1)
|
name = strings.Replace(name, "_", "-", -1)
|
||||||
return name
|
return name
|
||||||
@@ -890,9 +890,9 @@ func FormatContainerName(name string) string {
|
|||||||
func GetContainerName(service kobject.ServiceConfig) string {
|
func GetContainerName(service kobject.ServiceConfig) string {
|
||||||
name := service.Name
|
name := service.Name
|
||||||
if len(service.ContainerName) > 0 {
|
if len(service.ContainerName) > 0 {
|
||||||
name = FormatContainerName(service.ContainerName)
|
name = service.ContainerName
|
||||||
}
|
}
|
||||||
return name
|
return FormatContainerName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatResourceName generate a valid k8s resource name
|
// FormatResourceName generate a valid k8s resource name
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Test the creation of a service
|
Test the creation of a service
|
||||||
*/
|
*/
|
||||||
func TestCreateService(t *testing.T) {
|
func TestCreateService(t *testing.T) {
|
||||||
// An example service
|
// An example service
|
||||||
@@ -179,8 +179,8 @@ func TestCreateServiceWithCPULimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Test the creation of a service with a specified user.
|
Test the creation of a service with a specified user.
|
||||||
The expected result is that Kompose will set user in PodSpec
|
The expected result is that Kompose will set user in PodSpec
|
||||||
*/
|
*/
|
||||||
func TestCreateServiceWithServiceUser(t *testing.T) {
|
func TestCreateServiceWithServiceUser(t *testing.T) {
|
||||||
// An example service
|
// An example service
|
||||||
@@ -517,7 +517,7 @@ func TestSortedKeys(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//test conversion from duration string to seconds *int64
|
// test conversion from duration string to seconds *int64
|
||||||
func TestDurationStrToSecondsInt(t *testing.T) {
|
func TestDurationStrToSecondsInt(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
in string
|
in string
|
||||||
@@ -571,3 +571,29 @@ func TestServiceWithServiceAccount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateServiceWithSpecialName(t *testing.T) {
|
||||||
|
service := kobject.ServiceConfig{
|
||||||
|
ContainerName: "front_end",
|
||||||
|
Image: "nginx",
|
||||||
|
}
|
||||||
|
|
||||||
|
// An example object generated via k8s runtime.Objects()
|
||||||
|
komposeObject := kobject.KomposeObject{
|
||||||
|
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
|
||||||
|
}
|
||||||
|
k := Kubernetes{}
|
||||||
|
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 3})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(errors.Wrap(err, "k.Transform failed"))
|
||||||
|
}
|
||||||
|
expectedContainerName := "front-end"
|
||||||
|
for _, obj := range objects {
|
||||||
|
if deploy, ok := obj.(*appsv1.Deployment); ok {
|
||||||
|
containerName := deploy.Spec.Template.Spec.Containers[0].Name
|
||||||
|
if containerName != "front-end" {
|
||||||
|
t.Errorf("Error while transforming container name. Expected %s Got %s", expectedContainerName, containerName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ func (k *Kubernetes) InitPodSpec(name string, image string, pullSecret string) a
|
|||||||
return pod
|
return pod
|
||||||
}
|
}
|
||||||
|
|
||||||
//InitPodSpecWithConfigMap creates the pod specification
|
// InitPodSpecWithConfigMap creates the pod specification
|
||||||
func (k *Kubernetes) InitPodSpecWithConfigMap(name string, image string, service kobject.ServiceConfig) api.PodSpec {
|
func (k *Kubernetes) InitPodSpecWithConfigMap(name string, image string, service kobject.ServiceConfig) api.PodSpec {
|
||||||
var volumeMounts []api.VolumeMount
|
var volumeMounts []api.VolumeMount
|
||||||
var volumes []api.Volume
|
var volumes []api.Volume
|
||||||
@@ -245,7 +245,7 @@ func (k *Kubernetes) InitConfigMapForEnv(name string, opt kobject.ConvertOptions
|
|||||||
|
|
||||||
// IntiConfigMapFromFileOrDir will create a configmap from dir or file
|
// IntiConfigMapFromFileOrDir will create a configmap from dir or file
|
||||||
// usage:
|
// usage:
|
||||||
// 1. volume
|
// 1. volume
|
||||||
func (k *Kubernetes) IntiConfigMapFromFileOrDir(name, cmName, filePath string, service kobject.ServiceConfig) (*api.ConfigMap, error) {
|
func (k *Kubernetes) IntiConfigMapFromFileOrDir(name, cmName, filePath string, service kobject.ServiceConfig) (*api.ConfigMap, error) {
|
||||||
configMap := &api.ConfigMap{
|
configMap := &api.ConfigMap{
|
||||||
TypeMeta: metav1.TypeMeta{
|
TypeMeta: metav1.TypeMeta{
|
||||||
@@ -324,7 +324,7 @@ func initConfigMapData(configMap *api.ConfigMap, data map[string]string) {
|
|||||||
configMap.BinaryData = binData
|
configMap.BinaryData = binData
|
||||||
}
|
}
|
||||||
|
|
||||||
//InitConfigMapFromFile initializes a ConfigMap object
|
// InitConfigMapFromFile initializes a ConfigMap object
|
||||||
func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceConfig, fileName string) *api.ConfigMap {
|
func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceConfig, fileName string) *api.ConfigMap {
|
||||||
content, err := GetContentFromFile(fileName)
|
content, err := GetContentFromFile(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -708,7 +708,7 @@ func (k *Kubernetes) ConfigServicePorts(service kobject.ServiceConfig) []api.Ser
|
|||||||
return servicePorts
|
return servicePorts
|
||||||
}
|
}
|
||||||
|
|
||||||
//ConfigCapabilities configure POSIX capabilities that can be added or removed to a container
|
// ConfigCapabilities configure POSIX capabilities that can be added or removed to a container
|
||||||
func ConfigCapabilities(service kobject.ServiceConfig) *api.Capabilities {
|
func ConfigCapabilities(service kobject.ServiceConfig) *api.Capabilities {
|
||||||
capsAdd := []api.Capability{}
|
capsAdd := []api.Capability{}
|
||||||
capsDrop := []api.Capability{}
|
capsDrop := []api.Capability{}
|
||||||
@@ -1012,7 +1012,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConfigEmptyVolumeSource is helper function to create an EmptyDir api.VolumeSource
|
// ConfigEmptyVolumeSource is helper function to create an EmptyDir api.VolumeSource
|
||||||
//either for Tmpfs or for emptyvolumes
|
// either for Tmpfs or for emptyvolumes
|
||||||
func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
|
func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
|
||||||
//if key is tmpfs
|
//if key is tmpfs
|
||||||
if key == "tmpfs" {
|
if key == "tmpfs" {
|
||||||
|
|||||||
Reference in New Issue
Block a user