scope($replica, $consumerReplica, $allowPublicFallback); return $replica->service->endpoints()->updateOrCreate([ 'service_replica_id' => $replica->id, 'scope' => $scope, 'port' => $replica->internal_port, ], [ 'hostname' => $this->hostname($replica, $scope), 'ip_address' => $this->ipAddress($replica, $scope), 'priority' => $this->priority($scope), 'health_status' => $replica->health_status, ]); } private function scope(ServiceReplica $replica, ?ServiceReplica $consumerReplica, bool $allowPublicFallback): ServiceEndpointScope { if ($consumerReplica && $consumerReplica->server_id === $replica->server_id) { return ServiceEndpointScope::DOCKER_NETWORK; } if ($replica->server?->private_ip) { return ServiceEndpointScope::PRIVATE_NETWORK; } if ($allowPublicFallback && $replica->server?->ipv4) { return ServiceEndpointScope::PUBLIC; } return ServiceEndpointScope::DOCKER_NETWORK; } private function hostname(ServiceReplica $replica, ServiceEndpointScope $scope): string { return match ($scope) { ServiceEndpointScope::DOCKER_NETWORK => $replica->internal_host, ServiceEndpointScope::PRIVATE_NETWORK => $replica->server->private_ip, ServiceEndpointScope::PUBLIC => $replica->server->ipv4, }; } private function ipAddress(ServiceReplica $replica, ServiceEndpointScope $scope): ?string { return match ($scope) { ServiceEndpointScope::DOCKER_NETWORK => null, ServiceEndpointScope::PRIVATE_NETWORK => $replica->server->private_ip, ServiceEndpointScope::PUBLIC => $replica->server->ipv4, }; } private function priority(ServiceEndpointScope $scope): int { return match ($scope) { ServiceEndpointScope::DOCKER_NETWORK => 10, ServiceEndpointScope::PRIVATE_NETWORK => 20, ServiceEndpointScope::PUBLIC => 100, }; } }