Skip to content
Merged
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
14 changes: 7 additions & 7 deletions cmapi-portal-ui/src/api/schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2698,7 +2698,7 @@ export interface components {
state: "permanent" | "active" | "expiring" | "grace" | "pending_delete";
/**
* Team
* @description Team (`cmapi.net/team`)
* @description Team from the label named by metadata-keys.yaml `teamKey` (`cmapi.net/team` by default)
* @example payments
*/
team: string | null;
Expand Down Expand Up @@ -3459,7 +3459,7 @@ export interface components {
onBehalfOf?: string | null;
/**
* Placementid
* @description The `placementId` of POST /api/v1/capacity:place: its hold becomes this request's own. The placement must be active and made for the path's cluster, its environment and the `cmapi.net/team` label sent here
* @description The `placementId` of POST /api/v1/capacity:place: its hold becomes this request's own. The placement must be active and made for the path's cluster, its environment and the team label named by metadata-keys.yaml `teamKey` (`cmapi.net/team` by default)
* @example null
*/
placementId?: string | null;
Expand Down Expand Up @@ -3725,7 +3725,7 @@ export interface components {
sync: components["schemas"]["NamespaceSync"];
/**
* Team
* @description cmapi.net/team
* @description The team: the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default
* @example payments
*/
team: string | null;
Expand Down Expand Up @@ -4360,7 +4360,7 @@ export interface components {
} | null;
/**
* Team
* @description The team the namespace is for: the create's `cmapi.net/team` label must be the same. Left out on an instance whose metadata keys have no team label; the cap of 3 active placements is then the caller's own
* @description The team the namespace is for: the create's team label (metadata-keys.yaml `teamKey`, `cmapi.net/team` by default) must be the same. Left out on an instance whose metadata keys have no team label; the cap of 3 active placements is then the caller's own
* @example payments
*/
team?: string | null;
Expand Down Expand Up @@ -11980,7 +11980,7 @@ export interface operations {
env?: string | null;
/** @description Only the namespaces on this cluster. */
clusterId?: string | null;
/** @description Only the namespaces of this team (`cmapi.net/team`). */
/** @description Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default). */
team?: string | null;
/** @description Only the namespaces in one state: `permanent`, `active`, `expiring`, `grace` or `pending_delete`, or `attention` for the three that need action (expiring, grace and pending_delete). Another value returns 422 `validation-failed`. */
state?: string | null;
Expand Down Expand Up @@ -14458,7 +14458,7 @@ export interface operations {
env?: string | null;
/** @description Only the namespaces on this cluster. */
clusterId?: string | null;
/** @description Only the namespaces of this team (`cmapi.net/team`). */
/** @description Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default). */
team?: string | null;
/** @description Only the namespaces of this tier (`cmapi.net/tier`). */
tier?: string | null;
Expand Down Expand Up @@ -14639,7 +14639,7 @@ export interface operations {
env?: string | null;
/** @description Only the namespaces on this cluster. */
clusterId?: string | null;
/** @description Only the namespaces of this team (`cmapi.net/team`). */
/** @description Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default). */
team?: string | null;
/** @description Only the namespaces of this tier (`cmapi.net/tier`). */
tier?: string | null;
Expand Down
6 changes: 3 additions & 3 deletions cmapi-portal-ui/src/portal/NewNamespaceTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { isoDay } from "./format";
import { Field, fieldName, MetadataSection } from "./MetadataFields";
import {
bodyOf, checkAll, checkExtras, checkName, defaultSource, endValue, errorsByField, expiryInfo, expiryRulesOf, joinName, nameEnds, nameSuggestion,
shownValues, valueKey
shownValues, teamFieldOf, valueKey
} from "./metadataForm";
import type { ExpiryInfo, ExtraCheck, ExtraRow, Kind, MetaField, MetaForm, Source } from "./metadataForm";
import { NamespaceNameField } from "./NamespaceNameField";
Expand Down Expand Up @@ -81,7 +81,6 @@ const CHECK_AFTER_MS = 150;
/** How long the team, tier and region stay still before the Placement preview ranks them: each ranking spends the caller's
* :place bucket (10 a minute), so typing a team name does not rank each prefix. */
const RANK_AFTER_MS = 600;
const TEAM_KEY = "cmapi.net/team";
const LABEL_VALUE = /^[A-Za-z0-9]([-A-Za-z0-9_.]{0,61}[A-Za-z0-9])?$/;
const GI = 2 ** 30;

Expand Down Expand Up @@ -314,7 +313,8 @@ export function NewNamespaceTab({ config, open }: TabProps) {
const activeRegion = inEnv.some((c) => c.labels.region === region) ? region : ANY_REGION;
const instance = me.state === "ok" ? me.data.instance : "";
// The team the namespace is for, as the form shows it: placement ranks for it, and the create must carry the same label.
const teamField = form?.fields.find((f) => f.kind === "label" && f.key === TEAM_KEY) ?? null;
// Its key is the instance's team label (metadata-keys.yaml teamKey, #258), which the answer of GET /api/v1/metadata-keys names.
const teamField = form ? teamFieldOf(form) : null;
const team = form && teamField ? shownValues(form, typed, { namespace: "", environment: env, cluster: "", instance })[valueKey(teamField)] ?? "" : "";
// A label value (PlacementRequest.team), or the preview waits: the form's own check names what is wrong with it. An instance
// whose keys have no team label places with no team.
Expand Down
13 changes: 11 additions & 2 deletions cmapi-portal-ui/src/portal/metadataForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export interface ExpiryRules {

/** What the form reads from one answer of GET /api/v1/metadata-keys. */
export interface MetaForm {
/** The label that names a namespace's team (metadata-keys.yaml teamKey, #258): labels.x-cmapi-team-key, which the answer
* leaves out when it is cmapi.net/team. POST /api/v1/capacity:place takes its value as `team`. */
teamKey: string;
/** Inputs, then read-only fields, then optional inputs, for each kind; operator-only keys are not offered. */
fields: MetaField[];
/** A label value's syntax (labels.additionalProperties): Kubernetes' IsValidLabelValue, which cmapi checks first. */
Expand Down Expand Up @@ -124,7 +127,7 @@ type Json = Record<string, unknown>;
export const KEYWORDS = {
/** The members of the answer itself. */
answer: { read: ["labels", "annotations", "x-cmapi-expiration"], ignored: {} },
map: { read: ["type", "properties", "required", "additionalProperties", "propertyNames", "x-cmapi-other-keys"], ignored: {} },
map: { read: ["type", "properties", "required", "additionalProperties", "propertyNames", "x-cmapi-other-keys", "x-cmapi-team-key"], ignored: {} },
property: {
read: ["type", "pattern", "allOf", "maxLength", "minLength", "enum", "title", "description", "examples", "readOnly", "x-cmapi-rule",
"x-cmapi-derived-from", "x-cmapi-format", "x-cmapi-namespace-prefix", "x-cmapi-namespace-suffix", "x-cmapi-default-from",
Expand Down Expand Up @@ -295,7 +298,13 @@ export function fieldsOf(view: unknown): MetaForm {
];
ordered.forEach((f, i) => fields.push({ ...f, id: `ns-${kind}-${i}` }));
}
return { fields, labelValue, operatorOnlyLabels, operatorOnly, otherKeys, expiration: expirationOf(view), edits };
const teamKey = isObject(view.labels) ? textOf(view.labels["x-cmapi-team-key"]) : null;
return { fields, teamKey: teamKey ?? "cmapi.net/team", labelValue, operatorOnlyLabels, operatorOnly, otherKeys, expiration: expirationOf(view), edits };
}

/** The field of the team label, whose value placement ranks for and the create must carry; null when the form offers none. */
export function teamFieldOf(form: MetaForm): MetaField | null {
return form.fields.find((f) => f.kind === "label" && f.key === form.teamKey) ?? null;
}

/** The key of a field's value in the form's state: a key can be a label and an annotation, each with its own value. */
Expand Down
1 change: 1 addition & 0 deletions cmapi-portal-ui/tests/api/metadata-keys.company.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
}
}
},
"x-cmapi-team-key": "company.net/mnemonic",
"required": [
"name",
"company.net/app-environment",
Expand Down
4 changes: 2 additions & 2 deletions cmapi-portal-ui/tests/api/standin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ export function missingKeysOf(keys, maps, sources) {
/** NamespaceView of a record: the list's (no Git, no etag) or the detail's (router.py _namespace_view and get_namespace). */
export function namespaceViewOf(keys, rec, withGit, environment = rec.env) {
const live = rec.live ?? { labels: {}, annotations: {} };
return { clusterId: rec.clusterId, name: rec.name, env: live.labels["cmapi.net/env"] ?? null, team: live.labels["cmapi.net/team"] ?? null, tier: live.labels["cmapi.net/tier"] ?? null,
return { clusterId: rec.clusterId, name: rec.name, env: live.labels["cmapi.net/env"] ?? null, team: live.labels[keys.labels["x-cmapi-team-key"] ?? "cmapi.net/team"] ?? null, tier: live.labels["cmapi.net/tier"] ?? null,
labels: live.labels, annotations: live.annotations, managed: live.labels["cmapi.net/managed"] === "true", expiresAt: live.annotations["cmapi.net/expires-at"] ?? null,
etag: withGit ? recordEtag(rec) : null, openRequest: rec.openRequest?.requestId ?? null, mnemonic: prefixOf(keys, rec.live),
git: withGit ? { ...rec.git, edits: gitEditsOf(keys, rec.git, environment),
Expand Down Expand Up @@ -1172,7 +1172,7 @@ export function apiHandler({ issuer, portalOrigin }) {
const p = state.placements.get(body.placementId);
if (!p || p.used) return problem(res, refuse(422, "placement-not-found", `No active placement ${body.placementId}: it expired, was used by another create, or never existed. Place again.`), cors);
// The team that the namespace will carry: the request's, or the one cmapi fills in (a default), as facts["team"].
if (p.clusterId !== clusterId || p.env !== env || p.team !== (made.filled.label["cmapi.net/team"] ?? null)) {
if (p.clusterId !== clusterId || p.env !== env || p.team !== (made.filled.label[state.metadataKeys.labels["x-cmapi-team-key"] ?? "cmapi.net/team"] ?? null)) {
return problem(res, refuse(422, "placement-mismatch", `Placement ${body.placementId} was made for another target`), cors);
}
}
Expand Down
16 changes: 15 additions & 1 deletion cmapi-portal-ui/tests/contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ console.log("ok - contract");
// #336: the New namespace form evaluates or shows each keyword that GET /api/v1/metadata-keys sends, or lists it as
// ignored with its reason (src/portal/metadataForm.ts, KEYWORDS). A keyword in neither list fails here: the generator
// (MetadataKeys.json_schema) gained a rule that the form would silently check less than cmapi does.
const { KEYWORDS } = await import("../src/portal/metadataForm.ts");
const { fieldsOf, KEYWORDS, teamFieldOf } = await import("../src/portal/metadataForm.ts");
const samples = JSON.parse(readFileSync(join(app, "..", "cmapi", "src", "cmapi", "contract", "samples.json"), "utf8"));
const answers = { reference: samples.operations.core_metadata_keys.response.body,
company: JSON.parse(readFileSync(join(app, "tests", "api", "metadata-keys.company.json"), "utf8")),
Expand Down Expand Up @@ -64,3 +64,17 @@ for (const [name, answer] of Object.entries(answers)) {
}
assert.deepEqual(unknown, [], "keywords of GET /api/v1/metadata-keys that the form neither evaluates nor lists as ignored");
console.log("ok - the New namespace form reads each keyword of GET /api/v1/metadata-keys or lists it as ignored");

// #258: the New namespace form places for the value of the instance's team label, which the answer names when it is not
// cmapi.net/team. A form that looked for cmapi.net/team on the company answer placed with no team, and the create, whose
// team is the mnemonic, was refused (422 placement-mismatch).
for (const [name, key] of [["reference", "cmapi.net/team"], ["company", "company.net/mnemonic"], ["company-479", "cmapi.net/team"]]) {
const form = fieldsOf(answers[name]);
assert.equal(form.teamKey, key, `${name}: the team label`);
const field = teamFieldOf(form);
assert.deepEqual(field && [field.kind, field.key, field.required, field.derivedFrom], ["label", key, true, null], `${name}: its field`);
}
// An answer with no field of its team label places with no team (PlacementRequest.team: "Left out on an instance whose
// metadata keys have no team label").
assert.equal(teamFieldOf({ ...fieldsOf(answers.company), teamKey: "cmapi.net/team" }), null);
console.log("ok - the New namespace form finds the team that placement ranks for by the answer's team label");
20 changes: 10 additions & 10 deletions cmapi/contract/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3067,7 +3067,7 @@
"type": "null"
}
],
"description": "Team (`cmapi.net/team`)",
"description": "Team from the label named by metadata-keys.yaml `teamKey` (`cmapi.net/team` by default)",
"examples": [
"payments"
],
Expand Down Expand Up @@ -4701,7 +4701,7 @@
"type": "null"
}
],
"description": "The `placementId` of POST /api/v1/capacity:place: its hold becomes this request's own. The placement must be active and made for the path's cluster, its environment and the `cmapi.net/team` label sent here",
"description": "The `placementId` of POST /api/v1/capacity:place: its hold becomes this request's own. The placement must be active and made for the path's cluster, its environment and the team label named by metadata-keys.yaml `teamKey` (`cmapi.net/team` by default)",
"examples": [
null
],
Expand Down Expand Up @@ -5449,7 +5449,7 @@
"type": "null"
}
],
"description": "cmapi.net/team",
"description": "The team: the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default",
"examples": [
"payments"
],
Expand Down Expand Up @@ -6734,7 +6734,7 @@
"type": "null"
}
],
"description": "The team the namespace is for: the create's `cmapi.net/team` label must be the same. Left out on an instance whose metadata keys have no team label; the cap of 3 active placements is then the caller's own",
"description": "The team the namespace is for: the create's team label (metadata-keys.yaml `teamKey`, `cmapi.net/team` by default) must be the same. Left out on an instance whose metadata keys have no team label; the cap of 3 active placements is then the caller's own",
"examples": [
"payments"
],
Expand Down Expand Up @@ -20924,7 +20924,7 @@
}
},
{
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"in": "query",
"name": "team",
"required": false,
Expand All @@ -20937,7 +20937,7 @@
"type": "null"
}
],
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"examples": [
"payments"
],
Expand Down Expand Up @@ -24874,7 +24874,7 @@
}
},
{
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"in": "query",
"name": "team",
"required": false,
Expand All @@ -24887,7 +24887,7 @@
"type": "null"
}
],
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"examples": [
"payments"
],
Expand Down Expand Up @@ -25347,7 +25347,7 @@
}
},
{
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"in": "query",
"name": "team",
"required": false,
Expand All @@ -25360,7 +25360,7 @@
"type": "null"
}
],
"description": "Only the namespaces of this team (`cmapi.net/team`).",
"description": "Only the namespaces of this team (the label that metadata-keys.yaml names in `teamKey`, `cmapi.net/team` by default).",
"examples": [
"payments"
],
Expand Down
11 changes: 10 additions & 1 deletion cmapi/src/cmapi/core/approvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def _added(self, facts: Mapping[str, Any]) -> list[tuple[Slot, str, frozenset[st

A request beyond its tier always gets an answer from a rule. When no threshold that reads a beyond-tier fact is true,
one approver of defaults.group is added: an instance that removes the rule does not open the gate by accident. A
rule that means no extra approval says so with `add: 0`.
rule that means no extra approval says so with `add: 0`. A quota lowered below what the pods request, or lowered
while their use cannot be read (quotaBelowRequested, #256), gets its answer the same way: docs/01, "A decrease
below what pods already request is refused unless approver-platform approve".
"""
out = []
for expr, t in self.thresholds:
Expand All @@ -154,6 +156,13 @@ def _added(self, facts: Mapping[str, Any]) -> list[tuple[Slot, str, frozenset[st
# A site may still mount the policy shipped before live usage existed.
out.append((Slot("approver-platform", 1, "Live usage unavailable"),
"live usage unavailable: +1 approver-platform", frozenset({"usage"})))
if facts.get("quotaBelowRequested") and not any("quotaBelowRequested" in names for _, _, names in out):
# The fact was always false before #256, so a file without the rule never answered it. A file that answers it,
# with any group or with `add: 0`, is the site's policy and stands, also for a use that could not be read.
g = self.defaults["group"]
out.append((Slot(g, 1, "Quota below what pods request"),
f"quota below what pods request, and no rule reads quotaBelowRequested: +1 {g}",
frozenset({"quotaBelowRequested"})))
if facts.get("beyondTier") and not any(names & BEYOND_FACTS for _, _, names in out):
g = self.defaults["group"]
out.append((Slot(g, 1, "Value beyond the tier"), f"beyond the tier, and no rule reads beyondTier: +1 {g}",
Expand Down
Loading
Loading