diff --git a/README.md b/README.md index eda7b1e..1bbf3de 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,28 @@ exposed as an mDNS service. If a service is deleted or updated to not be exposed, the daemon will remove the mDNS service. +## Kubernetes services with multiple ports + +If a service has multiple ports, the port to be exposed as an mDNS service +has to be annotated with the same name as the mDNS service name, e.g.: + +```yaml +metadata: + annotations: + kubelish/service-name: Example + kubelish/service-type: _example._tcp + kubelish/txt: Optional TXT record to be exposed along with the service on mDNS + ... +spec: + ... + ports: + - name: Example + nodePort: 10000 + port: 9090 + protocol: TCP + targetPort: 9090 +``` + ## Running the daemon The daemon must be run natively on a node of the cluster, outside of Kubernetes. diff --git a/pkg/k8s-watcher/watcher.go b/pkg/k8s-watcher/watcher.go index b8c04ed..eb1311f 100644 --- a/pkg/k8s-watcher/watcher.go +++ b/pkg/k8s-watcher/watcher.go @@ -57,13 +57,21 @@ func (w *Watcher) meshDetails(svc *corev1.Service) *ServiceMDNS { } } - for _, port := range svc.Spec.Ports { - if port.Name == service.Annotations.ServiceName { - service.Port = int(port.Port) - break + if len(svc.Spec.Ports) == 1 { + service.Port = int(svc.Spec.Ports[0].Port) + } else { + for _, port := range svc.Spec.Ports { + if port.Name == service.Annotations.ServiceName { + service.Port = int(port.Port) + break + } } } + if service.Port == 0 { + return nil + } + return service }