import {z} from 'zod' export type Kind = {apiGroups?: Array | undefined, kinds?: Array | undefined}; export type LabelSelectorOperator = string; export type LabelSelectorRequirement = {key?: string | undefined, operator?: LabelSelectorOperator | undefined, values?: Array | undefined}; export type LabelSelector = {matchExpressions?: Array | undefined, matchLabels?: {[key: string]: string} | undefined}; export type Match = {excludedNamespaces?: Array | undefined, kinds?: Array | undefined, labelSelector?: LabelSelector | undefined, namespaceSelector?: LabelSelector | undefined, namespaces?: Array | undefined, scope?: string | undefined}; export type Parameters = {[key: string]: Record}; export type ConstraintSelector = {labelSelector?: LabelSelector | undefined, providers?: Array | undefined}; export type ConstraintSpec = {constraintType?: string | undefined, disabled?: boolean | undefined, match?: Match | undefined, parameters?: Parameters | undefined, selector?: ConstraintSelector | undefined}; export type ConstraintBody = {Spec?: ConstraintSpec | undefined, name?: string | undefined}; export type CreateApiApiV2ConstraintsArgs = {body: ConstraintBody}; export type CreateApiApiV2ConstraintsOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const kind = z.object({apiGroups: z.array(z.string()).optional(), kinds: z.array(z.string()).optional()}); export const labelSelectorOperator = z.string(); export const labelSelectorRequirement = z.object({key: z.string().optional(), operator: labelSelectorOperator.optional(), values: z.array(z.string()).optional()}); export const labelSelector = z.object({matchExpressions: z.array(labelSelectorRequirement).optional(), matchLabels: z.record(z.string(), z.string()).optional()}); export const match = z.object({excludedNamespaces: z.array(z.string()).optional(), kinds: z.array(kind).optional(), labelSelector: labelSelector.optional(), namespaceSelector: labelSelector.optional(), namespaces: z.array(z.string()).optional(), scope: z.string().optional()}); export const parameters = z.record(z.string(), z.object({})); export const constraintSelector = z.object({labelSelector: labelSelector.optional(), providers: z.array(z.string()).optional()}); export const constraintSpec = z.object({constraintType: z.string().optional(), disabled: z.boolean().optional(), match: match.optional(), parameters: parameters.optional(), selector: constraintSelector.optional()}); export const violation = z.object({enforcementAction: z.string().optional(), kind: z.string().optional(), message: z.string().optional(), name: z.string().optional(), namespace: z.string().optional()}); export const constraintStatus = z.object({auditTimestamp: z.string().optional(), enforcement: z.string().optional(), synced: z.boolean().optional(), violations: z.array(violation).optional()}); export const constraint = z.object({labels: z.record(z.string(), z.string()).optional(), name: z.string().optional(), spec: constraintSpec.optional(), status: constraintStatus.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Creates default constraint * * POST /api/v2/constraints */ export const createApiApiV2Constraints = async (args: CreateApiApiV2ConstraintsArgs, options: CreateApiApiV2ConstraintsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/constraints${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(constraint, await res.json()) };