Skip to content

[BUG] The resource name of generated k8s service may not conform to the DNS-1035 format specification #2007

@rudyares

Description

@rudyares

Expected Behavior

If the name of the compose service does not conform to the DNS-1035 format specification, for example, it contains the character '_', when generating k8s service, perform a special character conversion on the name of k8s service.

Actual Behavior

The name of the generated k8s service contains the special character '_' that does not conform to the DNS-1035 format specification.

Steps To Reproduce

kompose convert -f docker-compose.yaml --stdout

Kompose Version

1.35.0

Docker-Compose file

version: '3'
services:
  webDemo:
    image: nginx:latest
    ports:
    - "80:80"
  web_demo:
    image: nginx:latest
    ports:
    - "80:80"

Anything else?

I saw the following source code in pkg/transformer/kubernetes/kubernetes.go.
I don't understand why "name" cannot be used instead of "service.Name" as the name of resource when initializing a k8s service.

// InitSvc initializes Kubernetes Service object
// The created service name will = ServiceConfig.Name, but the selector may be not.
// If this service is grouped, the selector may be another name = name
func (k *Kubernetes) InitSvc(name string, service kobject.ServiceConfig) *api.Service {
	svc := &api.Service{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Service",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:   service.Name,
			Labels: transformer.ConfigLabels(name),
		},
		// The selector uses the service.Name, which must be consistent with workloads label
		Spec: api.ServiceSpec{
			Selector: transformer.ConfigLabels(name),
		},
	}
	return svc
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions