Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/api/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ export type UnicastIpPool = SiloIpPool & { poolType: 'unicast' }
export const isUnicastPool = (pool: SiloIpPool): pool is UnicastIpPool =>
pool.poolType === 'unicast'

export const poolHasIpVersion =
(versions: IpVersion[]) =>
(pool: { ipVersion: IpVersion }): boolean =>
versions.includes(pool.ipVersion)
export const poolHasIpVersion = (versions: Iterable<IpVersion>) => {
const versionSet = new Set(versions)
return (pool: { ipVersion: IpVersion }): boolean => versionSet.has(pool.ipVersion)
}

const instanceActions = {
// NoVmm maps to to Stopped:
Expand Down
48 changes: 23 additions & 25 deletions app/components/AttachEphemeralIpModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@ import {
queryClient,
useApiMutation,
usePrefetchedQuery,
type IpVersion,
} from '~/api'
import { IpPoolSelector } from '~/components/form/fields/IpPoolSelector'
import { HL } from '~/components/HL'
import { useInstanceSelector } from '~/hooks/use-params'
import { addToast } from '~/stores/toast'
import { Message } from '~/ui/lib/Message'
import { Modal } from '~/ui/lib/Modal'
import { ALL_ISH } from '~/util/consts'
import { getCompatibleVersionsFromNics } from '~/util/ip'

export const AttachEphemeralIpModal = ({ onDismiss }: { onDismiss: () => void }) => {
type AttachEphemeralIpModalProps = {
availableVersions: IpVersion[]
infoMessage: string | null
onDismiss: () => void
}

export const AttachEphemeralIpModal = ({
availableVersions,
infoMessage,
onDismiss,
}: AttachEphemeralIpModalProps) => {
const { project, instance } = useInstanceSelector()
const { data: siloPools } = usePrefetchedQuery(
q(api.ipPoolList, { query: { limit: ALL_ISH } })
)
const { data: nics } = usePrefetchedQuery(
q(api.instanceNetworkInterfaceList, { query: { limit: ALL_ISH, project, instance } })
)

// Determine compatible IP versions based on instance's primary network interface
// External IPs route through the primary interface, so only its IP stack matters
// https://github.com/oxidecomputer/omicron/blob/d52aad0/nexus/db-queries/src/db/datastore/external_ip.rs#L544-L661
const compatibleVersions = useMemo(
() => getCompatibleVersionsFromNics(nics.items),
[nics]
)

// Only unicast pools can be used for ephemeral IPs
// Only show unicast pools for the IP versions that still have open slots
const compatibleUnicastPools = useMemo(
() =>
siloPools.items.filter(isUnicastPool).filter(poolHasIpVersion(compatibleVersions)),
[siloPools, compatibleVersions]
() => siloPools.items.filter(isUnicastPool).filter(poolHasIpVersion(availableVersions)),
[siloPools, availableVersions]
)

const defaultPool = useMemo(() => {
Expand All @@ -73,25 +72,24 @@ export const AttachEphemeralIpModal = ({ onDismiss }: { onDismiss: () => void })
const pool = form.watch('pool')

const disabledReason =
compatibleVersions.length === 0
? 'Instance has no network interfaces with compatible IP stacks'
: compatibleUnicastPools.length === 0
? 'No compatible unicast pools available for this instance'
: !pool
? 'Select a pool to continue'
: undefined
compatibleUnicastPools.length === 0
? 'No compatible unicast pools available for this instance'
: !pool
? 'Select a pool to continue'
: undefined

return (
<Modal isOpen title="Attach ephemeral IP" onDismiss={onDismiss}>
<Modal.Body>
<Modal.Section>
{infoMessage && <Message variant="info" content={infoMessage} />}
<form>
<IpPoolSelector
control={form.control}
poolFieldName="pool"
pools={compatibleUnicastPools}
disabled={compatibleUnicastPools.length === 0}
compatibleVersions={compatibleVersions}
compatibleVersions={availableVersions}
/>
</form>
</Modal.Section>
Expand Down
50 changes: 37 additions & 13 deletions app/pages/project/instances/NetworkingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { match } from 'ts-pattern'
import {
api,
instanceCan,
isUnicastPool,
q,
qErrorsAllowed,
queryClient,
Expand Down Expand Up @@ -58,7 +59,12 @@ import { TableEmptyBox } from '~/ui/lib/Table'
import { TipIcon } from '~/ui/lib/TipIcon'
import { Tooltip } from '~/ui/lib/Tooltip'
import { ALL_ISH } from '~/util/consts'
import { getCompatibleVersionsFromNics, ipHasVersion, parseIp } from '~/util/ip'
import {
getCompatibleVersionsFromNics,
getEphemeralIpSlots,
ipHasVersion,
parseIp,
} from '~/util/ip'
import { pb } from '~/util/path-builder'

import { fancifyStates } from './common'
Expand Down Expand Up @@ -301,6 +307,11 @@ export default function NetworkingTab() {
})
).data.items

const { data: siloPools } = usePrefetchedQuery(
q(api.ipPoolList, { query: { limit: ALL_ISH } })
)
const unicastPools = useMemo(() => siloPools.items.filter(isUnicastPool), [siloPools])

// Determine compatible IP versions from the instance's primary NIC
// External IPs route through the primary interface, so only its IP stack matters
const compatibleVersions = useMemo(() => getCompatibleVersionsFromNics(nics), [nics])
Expand Down Expand Up @@ -466,11 +477,14 @@ export default function NetworkingTab() {
}

const doDetach = match(externalIp)
.with(
{ kind: 'ephemeral' },
() => () =>
ephemeralIpDetach({ path: { instance: instanceName }, query: { project } })
)
.with({ kind: 'ephemeral' }, () => () => {
const parsed = parseIp(externalIp.ip)
const ipVersion = parsed.type === 'error' ? undefined : parsed.type
return ephemeralIpDetach({
path: { instance: instanceName },
query: { project, ipVersion },
})
})
.with(
{ kind: 'floating' },
({ name }) =>
Expand Down Expand Up @@ -517,12 +531,18 @@ export default function NetworkingTab() {
getCoreRowModel: getCoreRowModel(),
})

const ephemeralDisabledReason =
nics.length === 0
? 'Instance has no network interfaces'
: eips.items.some((ip) => ip.kind === 'ephemeral')
? 'Instance already has an ephemeral IP'
: null
const attachedEphemeralIps = useMemo(
() => eips.items.filter((ip) => ip.kind === 'ephemeral'),
[eips]
)
const {
availableVersions: ephemeralAvailableVersions,
disabledReason: ephemeralDisabledReason,
infoMessage: ephemeralInfoMessage,
} = useMemo(
() => getEphemeralIpSlots(compatibleVersions, attachedEphemeralIps, unicastPools),
[compatibleVersions, attachedEphemeralIps, unicastPools]
)

const floatingDisabledReason =
eips.items.filter((ip) => ip.kind === 'floating').length >= 32
Expand Down Expand Up @@ -574,7 +594,11 @@ export default function NetworkingTab() {
</CardBlock.Body>

{attachEphemeralModalOpen && (
<AttachEphemeralIpModal onDismiss={() => setAttachEphemeralModalOpen(false)} />
<AttachEphemeralIpModal
availableVersions={ephemeralAvailableVersions}
infoMessage={ephemeralInfoMessage}
onDismiss={() => setAttachEphemeralModalOpen(false)}
/>
)}
{attachFloatingModalOpen && (
<AttachFloatingIpModal
Expand Down
11 changes: 10 additions & 1 deletion app/util/array.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { JSX, ReactElement } from 'react'
import { expect, test } from 'vitest'

import { groupBy, intersperse, isSetEqual, setDiff } from './array'
import { groupBy, intersperse, isSetEqual, setDiff, setIntersection } from './array'

test('groupBy', () => {
expect(
Expand Down Expand Up @@ -81,3 +81,12 @@ test('setDiff', () => {
new Set(['a', 'c'])
)
})

test('setIntersection', () => {
expect(setIntersection(new Set(), new Set())).toEqual(new Set())
expect(setIntersection(new Set(['a']), new Set())).toEqual(new Set())
expect(setIntersection(new Set(), new Set(['a']))).toEqual(new Set())
expect(setIntersection(new Set(['b', 'a', 'c']), new Set(['b', 'd']))).toEqual(
new Set(['b'])
)
})
7 changes: 6 additions & 1 deletion app/util/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export function isSetEqual<T>(a: Set<T>, b: Set<T>): boolean {
}

/** Set `a - b` */
export function setDiff<T>(a: Set<T>, b: Set<T>): Set<T> {
export function setDiff<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): Set<T> {
return new Set([...a].filter((x) => !b.has(x)))
}

/** Set `a ∩ b` */
export function setIntersection<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): Set<T> {
return new Set([...a].filter((x) => b.has(x)))
}
Loading
Loading