Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Make container port optional (#57)
Browse files Browse the repository at this point in the history
- Instead of using fixed port to communicate with each other, some frameworks randomly pick up port by worker and use auto-discovery to find entire topology.
- In that case, we only need pod-to-pod communication and don’t need to specify port in headless service.
- This give the flexibility for framework which doesn’t rely on fix port.

Signed-off-by: Jiaxin Shan <seedjeffwan@gmail.com>
  • Loading branch information
Jeffwan committed Mar 27, 2020
1 parent 7e0d50e commit 0b3a4c3
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/controller.v1/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,25 @@ func (jc *JobController) ReconcileServices(
return nil
}

// GetPortFromJob gets the port of job container.
func (jc *JobController) GetPortFromJob(spec *apiv1.ReplicaSpec) (int32, error) {
// GetPortFromJob gets the port of job container. Port could be nil depending on different distributed communication strategy
func (jc *JobController) GetPortFromJob(spec *apiv1.ReplicaSpec) (*int32, error) {
// Consider the case controller doesn't use fixed port, headless service without port will enable random pod to pod communication
if jc.Controller.GetDefaultContainerPortName() == "" {
return nil, nil
}

containers := spec.Template.Spec.Containers
for _, container := range containers {
if container.Name == jc.Controller.GetDefaultContainerName() {
ports := container.Ports
for _, port := range ports {
if port.Name == jc.Controller.GetDefaultContainerPortName() {
return port.ContainerPort, nil
return &port.ContainerPort, nil
}
}
}
}
return -1, fmt.Errorf("failed to find the port")
return nil, fmt.Errorf("failed to find the port")
}

// createNewService creates a new service for the given index and type.
Expand Down Expand Up @@ -246,15 +251,16 @@ func (jc *JobController) CreateNewService(job metav1.Object, rtype apiv1.Replica
Spec: v1.ServiceSpec{
ClusterIP: "None",
Selector: labels,
Ports: []v1.ServicePort{
{
Name: jc.Controller.GetDefaultContainerPortName(),
Port: port,
},
},
Ports: []v1.ServicePort{},
},
}

// Add service port to headless service only if port is set from controller implementation
if port != nil {
svcPort := v1.ServicePort{Name: jc.Controller.GetDefaultContainerPortName(), Port: *port}
service.Spec.Ports = append(service.Spec.Ports, svcPort)
}

service.Name = GenGeneralName(job.GetName(), rt, index)
service.Labels = labels
// Create OwnerReference.
Expand Down

0 comments on commit 0b3a4c3

Please sign in to comment.