-
Notifications
You must be signed in to change notification settings - Fork 22
External subnets #3039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
External subnets #3039
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { useForm } from 'react-hook-form' | ||
| import { useNavigate } from 'react-router' | ||
| import { match } from 'ts-pattern' | ||
|
|
||
| import { api, q, queryClient, useApiMutation, usePrefetchedQuery } from '@oxide/api' | ||
|
|
||
| import { DescriptionField } from '~/components/form/fields/DescriptionField' | ||
| import { ListboxField } from '~/components/form/fields/ListboxField' | ||
| import { NameField } from '~/components/form/fields/NameField' | ||
| import { NumberField } from '~/components/form/fields/NumberField' | ||
| import { RadioField } from '~/components/form/fields/RadioField' | ||
| import { TextField } from '~/components/form/fields/TextField' | ||
| import { SideModalForm } from '~/components/form/SideModalForm' | ||
| import { HL } from '~/components/HL' | ||
| import { toPoolItem } from '~/components/PoolListboxItem' | ||
| import { titleCrumb } from '~/hooks/use-crumbs' | ||
| import { useProjectSelector } from '~/hooks/use-params' | ||
| import { addToast } from '~/stores/toast' | ||
| import { SideModalFormDocs } from '~/ui/lib/ModalLinks' | ||
| import { ALL_ISH } from '~/util/consts' | ||
| import { validateIpNet } from '~/util/ip' | ||
| import { docLinks } from '~/util/links' | ||
| import { pb } from '~/util/path-builder' | ||
|
|
||
| const poolList = q(api.subnetPoolList, { query: { limit: ALL_ISH } }) | ||
|
|
||
| export async function clientLoader() { | ||
| await queryClient.prefetchQuery(poolList) | ||
| return null | ||
| } | ||
|
|
||
| export const handle = titleCrumb('New External Subnet') | ||
|
|
||
| type FormValues = { | ||
| name: string | ||
| description: string | ||
| allocationType: 'auto' | 'explicit' | ||
| prefixLen: number | ||
| pool: string | ||
| subnet: string | ||
| } | ||
|
|
||
| const defaultFormValues: Omit<FormValues, 'pool'> = { | ||
| name: '', | ||
| description: '', | ||
| allocationType: 'auto', | ||
| prefixLen: 24, | ||
| subnet: '', | ||
| } | ||
|
|
||
| export default function CreateExternalSubnetSideModalForm() { | ||
| const { data: pools } = usePrefetchedQuery(poolList) | ||
|
|
||
| const defaultPool = pools.items.find((p) => p.isDefault) | ||
|
|
||
| const projectSelector = useProjectSelector() | ||
| const navigate = useNavigate() | ||
|
|
||
| const createExternalSubnet = useApiMutation(api.externalSubnetCreate, { | ||
| onSuccess(subnet) { | ||
| queryClient.invalidateEndpoint('externalSubnetList') | ||
| // prettier-ignore | ||
| addToast(<>External subnet <HL>{subnet.name}</HL> created</>) | ||
| navigate(pb.externalSubnets(projectSelector)) | ||
| }, | ||
| }) | ||
|
|
||
| const form = useForm({ | ||
| defaultValues: { ...defaultFormValues, pool: defaultPool?.name ?? '' }, | ||
| }) | ||
|
|
||
| const allocationType = form.watch('allocationType') | ||
| const selectedPoolName = form.watch('pool') | ||
| const selectedPool = pools.items.find((p) => p.name === selectedPoolName) | ||
| // In auto allocation, the requested prefix length is matched against pool | ||
| // members whose min/max prefix range includes it, then a subnet is carved | ||
| // out of the first member with a large enough gap. Reproducing this member | ||
| // resolution logic is more or less impossible, so we just enforce a max by | ||
| // IP version. | ||
| // https://github.com/oxidecomputer/omicron/blob/e7d260a/nexus/db-queries/src/db/queries/external_subnet.rs#L906-L908 | ||
| const prefixLenMax = !selectedPool || selectedPool.ipVersion === 'v6' ? 128 : 32 | ||
|
|
||
| return ( | ||
| <SideModalForm | ||
| form={form} | ||
| formType="create" | ||
| resourceName="external subnet" | ||
| onDismiss={() => navigate(pb.externalSubnets(projectSelector))} | ||
| onSubmit={({ name, description, allocationType, prefixLen, pool, subnet }) => { | ||
| const allocator = match(allocationType) | ||
| .with('explicit', () => ({ type: 'explicit' as const, subnet })) | ||
| .with('auto', () => ({ | ||
| type: 'auto' as const, | ||
| prefixLen, | ||
| poolSelector: { type: 'explicit' as const, pool }, | ||
| })) | ||
| .exhaustive() | ||
| createExternalSubnet.mutate({ | ||
| query: projectSelector, | ||
| body: { name, description, allocator }, | ||
| }) | ||
| }} | ||
| loading={createExternalSubnet.isPending} | ||
| submitError={createExternalSubnet.error} | ||
| > | ||
| <NameField name="name" control={form.control} /> | ||
| <DescriptionField name="description" control={form.control} /> | ||
| <RadioField | ||
| name="allocationType" | ||
| label="Allocation method" | ||
| control={form.control} | ||
| items={[ | ||
| { value: 'auto', label: 'Auto' }, | ||
| { value: 'explicit', label: 'Explicit' }, | ||
| ]} | ||
| /> | ||
| {allocationType === 'auto' ? ( | ||
| <> | ||
| <ListboxField | ||
| name="pool" | ||
| label="Subnet pool" | ||
| control={form.control} | ||
| placeholder="Select a pool" | ||
| noItemsPlaceholder="No pools linked to silo" | ||
| items={pools.items.map(toPoolItem)} | ||
| required | ||
| description="Subnet pool to allocate from" | ||
| /> | ||
| <NumberField | ||
| name="prefixLen" | ||
| label="Prefix length" | ||
| required | ||
| control={form.control} | ||
| min={1} | ||
| max={prefixLenMax} | ||
| description="Prefix length for the allocated subnet (e.g., 24 for a /24). Max is 32 for IPv4 pools, 128 for IPv6." | ||
| /> | ||
| </> | ||
| ) : ( | ||
| <TextField | ||
| name="subnet" | ||
| label="Subnet CIDR" | ||
| required | ||
| control={form.control} | ||
| validate={validateIpNet} | ||
| description="The subnet to reserve, e.g., 10.128.1.0/24" | ||
| /> | ||
| )} | ||
| <SideModalFormDocs docs={[docLinks.externalSubnets]} /> | ||
| </SideModalForm> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { useForm } from 'react-hook-form' | ||
| import { useNavigate, type LoaderFunctionArgs } from 'react-router' | ||
|
|
||
| import { | ||
| api, | ||
| q, | ||
| qErrorsAllowed, | ||
| queryClient, | ||
| useApiMutation, | ||
| usePrefetchedQuery, | ||
| } from '@oxide/api' | ||
|
|
||
| import { DescriptionField } from '~/components/form/fields/DescriptionField' | ||
| import { NameField } from '~/components/form/fields/NameField' | ||
| import { SideModalForm } from '~/components/form/SideModalForm' | ||
| import { HL } from '~/components/HL' | ||
| import { titleCrumb } from '~/hooks/use-crumbs' | ||
| import { getExternalSubnetSelector, useExternalSubnetSelector } from '~/hooks/use-params' | ||
| import { addToast } from '~/stores/toast' | ||
| import { InstanceLink } from '~/table/cells/InstanceLinkCell' | ||
| import { SubnetPoolCell } from '~/table/cells/SubnetPoolCell' | ||
| import { SideModalFormDocs } from '~/ui/lib/ModalLinks' | ||
| import { PropertiesTable } from '~/ui/lib/PropertiesTable' | ||
| import { docLinks } from '~/util/links' | ||
| import { pb } from '~/util/path-builder' | ||
| import type * as PP from '~/util/path-params' | ||
|
|
||
| const externalSubnetView = ({ project, externalSubnet }: PP.ExternalSubnet) => | ||
| q(api.externalSubnetView, { | ||
| path: { externalSubnet }, | ||
| query: { project }, | ||
| }) | ||
|
|
||
| export async function clientLoader({ params }: LoaderFunctionArgs) { | ||
| const selector = getExternalSubnetSelector(params) | ||
| const subnet = await queryClient.fetchQuery(externalSubnetView(selector)) | ||
| await Promise.all([ | ||
| queryClient.prefetchQuery( | ||
| // subnet pool cell uses errors allowed, so we have to do that here to match | ||
| qErrorsAllowed( | ||
| api.subnetPoolView, | ||
| { path: { pool: subnet.subnetPoolId } }, | ||
| { | ||
| errorsExpected: { | ||
| explanation: 'the referenced subnet pool may have been deleted.', | ||
| statusCode: 404, | ||
| }, | ||
| } | ||
| ) | ||
| ), | ||
| subnet.instanceId | ||
| ? queryClient.prefetchQuery( | ||
| q(api.instanceView, { path: { instance: subnet.instanceId } }) | ||
| ) | ||
| : null, | ||
| ]) | ||
| return null | ||
| } | ||
|
|
||
| export const handle = titleCrumb('Edit External Subnet') | ||
|
|
||
| export default function EditExternalSubnetSideModalForm() { | ||
| const navigate = useNavigate() | ||
|
|
||
| const subnetSelector = useExternalSubnetSelector() | ||
| const onDismiss = () => navigate(pb.externalSubnets({ project: subnetSelector.project })) | ||
|
|
||
| const { data: subnet } = usePrefetchedQuery(externalSubnetView(subnetSelector)) | ||
|
|
||
| const editExternalSubnet = useApiMutation(api.externalSubnetUpdate, { | ||
| onSuccess(updated) { | ||
| queryClient.invalidateEndpoint('externalSubnetList') | ||
| // prettier-ignore | ||
| addToast(<>External subnet <HL>{updated.name}</HL> updated</>) | ||
| onDismiss() | ||
| }, | ||
| }) | ||
|
|
||
| const form = useForm({ defaultValues: subnet }) | ||
| return ( | ||
| <SideModalForm | ||
| form={form} | ||
| formType="edit" | ||
| resourceName="external subnet" | ||
| onDismiss={onDismiss} | ||
| onSubmit={({ name, description }) => { | ||
| editExternalSubnet.mutate({ | ||
| path: { externalSubnet: subnetSelector.externalSubnet }, | ||
| query: { project: subnetSelector.project }, | ||
| body: { name, description }, | ||
| }) | ||
| }} | ||
| loading={editExternalSubnet.isPending} | ||
| submitError={editExternalSubnet.error} | ||
| > | ||
| <PropertiesTable> | ||
| <PropertiesTable.IdRow id={subnet.id} /> | ||
| <PropertiesTable.DateRow label="Created" date={subnet.timeCreated} /> | ||
| <PropertiesTable.DateRow label="Updated" date={subnet.timeModified} /> | ||
| <PropertiesTable.Row label="Subnet">{subnet.subnet}</PropertiesTable.Row> | ||
| <PropertiesTable.Row label="Subnet Pool"> | ||
| <SubnetPoolCell subnetPoolId={subnet.subnetPoolId} /> | ||
| </PropertiesTable.Row> | ||
| <PropertiesTable.Row label="Instance"> | ||
| <InstanceLink instanceId={subnet.instanceId} tab="networking" /> | ||
| </PropertiesTable.Row> | ||
| </PropertiesTable> | ||
| <NameField name="name" control={form.control} /> | ||
| <DescriptionField name="description" control={form.control} /> | ||
| <SideModalFormDocs docs={[docLinks.externalSubnets]} /> | ||
| </SideModalForm> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We love TypeScript.