# This defines a service in the robot cluster, available at:
#   ros-master-<robot name>.app-ros-master.svc.cluster.local
# The user can configure what it points to by setting `values.ros_master_host`
# in the AppRollout, allowing a single-point-of-configuration for the ROS
# master. Note that this can't specify the port, so deployments should set:
#   ROS_MASTER_URI=http://ros-master.app-ros-master.svc.cluster.local:11311
apiVersion: v1
kind: Service
metadata:
  name: ros-master
spec:
  type: ExternalName
  externalName: {{ tpl (required "missing required value ros_master_host" .Values.ros_master_host) . }}

# This defines a service in the robot cluster, available at:
#   ros-master-<robot name>.app-ros-master.svc.cluster.local
# The user can configure what it points to by setting `values.ros_master_host`
# in the AppRollout, allowing a single-point-of-configuration for the ROS
# master. Note that this can't specify the port, so deployments should set:
#   ROS_MASTER_URI=http://ros-master.app-ros-master.svc.cluster.local:11311
{{ $ipv4Pattern := `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}` }}
{{ $host := tpl (required "missing required value ros_master_host" .Values.ros_master_host) . }}
{{ if regexMatch $ipv4Pattern $host }}
# Host is an IPv4 address, so use a headless Service.
apiVersion: v1
kind: Service
metadata:
  name: ros-master
spec:
  type: ClusterIP
  ports:
  - name: ros-master-api
    port: 11311
---
apiVersion: v1
kind: Endpoints
metadata:
  name: ros-master
subsets:
- addresses:
  - ip: {{ $host }}
  ports:
  - name: ros-master-api
    port: 11311
{{ else }}
# Host is a DNS name, so use an ExternalName Service.
apiVersion: v1
kind: Service
metadata:
  name: ros-master
spec:
  type: ExternalName
  externalName: {{ $host }}
{{ end }}

Use headless service for ROS master on robot
Use headless service for ROS master on robot

I36b764d70 introduced the ros-master app, which aims to provide a DNS
name for the ROS master that works for both simulated and non-simulated
robots. However, this used an ExternalName service, which is inadequate
for non-simulated robots, as it can't use an IPv4 address for the
backend.

The k8s docs[1] recommend using a "headless service" for this case,
which was the approach used in I36b764d70. It is possible to make the
ros-master chart use a headless service or ExternalName service as
appropriate, albeit with some rather complicated templating.

Alternatives considered: rollback the ros-master app and use Helm values
to specify ROS_MASTER_URI for every app that uses ROS. If we have any
further trouble with the ros-master app, we should give up on it.

[1]: https://kubernetes.io/docs/concepts/services-networking/service/#externalname

Change-Id: I2498fce111be5cf3053d1ea7d3839e7a1028abdb
GitOrigin-RevId: 61844862f0ca0bf848977eb44c218138b6bef3a6
