-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d995d9b
commit 2724233
Showing
3 changed files
with
152 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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,103 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { describe, it, expect } from "@jest/globals"; | ||
import { MutateResponse, ValidateResponse } from "../k8s"; | ||
import * as sut from "./index.util"; | ||
import { AdmissionRequest } from "../types"; | ||
|
||
describe("karForMutate()", () => { | ||
it("returns given MutateResponse wrapped in an KubeAdmissionReview", () => { | ||
const mr: MutateResponse = { uid: "uid", allowed: true }; | ||
const kar: sut.KubeAdmissionReview = { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: mr, | ||
}; | ||
const result = sut.karForMutate(mr); | ||
expect(result).toEqual(kar); | ||
}); | ||
}); | ||
|
||
describe("karForValidate()", () => { | ||
describe("given 0 ValidationResponse[]'s", () => { | ||
it("returns KubeAdmissionReview with abbreviated success message", () => { | ||
const ar = { uid: "uid" } as unknown as AdmissionRequest; | ||
const vrs: ValidateResponse[] = []; | ||
const resp = { | ||
uid: ar.uid, | ||
allowed: true, | ||
status: { code: 200, message: "no in-scope validations -- allowed!" }, | ||
}; | ||
const kar: sut.KubeAdmissionReview = { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: resp, | ||
}; | ||
const result = sut.karForValidate(ar, vrs); | ||
expect(result).toEqual(kar); | ||
}); | ||
}); | ||
|
||
describe("given 1-or-more ValidationResponse[]'s", () => { | ||
it("returns KubeAdmissionReview with detailed success message", () => { | ||
const ar = { uid: "uid" } as unknown as AdmissionRequest; | ||
const vrs: ValidateResponse[] = [ | ||
{ | ||
uid: "uid", | ||
allowed: true, | ||
status: { | ||
code: 200, | ||
message: "msg", | ||
}, | ||
}, | ||
]; | ||
const resp = { | ||
uid: ar.uid, | ||
allowed: true, | ||
status: { code: 200, message: "" }, | ||
}; | ||
const kar: sut.KubeAdmissionReview = { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: resp, | ||
}; | ||
const result = sut.karForValidate(ar, vrs); | ||
expect(result).toEqual(kar); | ||
}); | ||
|
||
it("returns KubeAdmissionReview with detailed failure message", () => { | ||
const ar = { uid: "uid" } as unknown as AdmissionRequest; | ||
const vrs: ValidateResponse[] = [ | ||
{ | ||
uid: "uid", | ||
allowed: false, | ||
status: { | ||
code: 422, | ||
message: "mess", | ||
}, | ||
}, | ||
{ | ||
uid: "uid", | ||
allowed: false, | ||
status: { | ||
code: 422, | ||
message: "age", | ||
}, | ||
}, | ||
]; | ||
const resp = { | ||
uid: ar.uid, | ||
allowed: false, | ||
status: { code: 422, message: "mess; age" }, | ||
}; | ||
const kar: sut.KubeAdmissionReview = { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: resp, | ||
}; | ||
const result = sut.karForValidate(ar, vrs); | ||
expect(result).toEqual(kar); | ||
}); | ||
}); | ||
}); |
This file contains 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,47 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { MutateResponse, ValidateResponse } from "../k8s"; | ||
import { ResponseItem, AdmissionRequest } from "../types"; | ||
|
||
export interface KubeAdmissionReview { | ||
apiVersion: string; | ||
kind: string; | ||
response: ValidateResponse[] | MutateResponse | ResponseItem; | ||
} | ||
|
||
export function karForMutate(mr: MutateResponse): KubeAdmissionReview { | ||
return { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: mr, | ||
}; | ||
} | ||
|
||
export function karForValidate(ar: AdmissionRequest, vr: ValidateResponse[]): KubeAdmissionReview { | ||
const isAllowed = vr.filter(r => !r.allowed).length === 0; | ||
|
||
const resp: ValidateResponse = | ||
vr.length === 0 | ||
? { | ||
uid: ar.uid, | ||
allowed: true, | ||
status: { code: 200, message: "no in-scope validations -- allowed!" }, | ||
} | ||
: { | ||
uid: vr[0].uid, | ||
allowed: isAllowed, | ||
status: { | ||
code: isAllowed ? 200 : 422, | ||
message: vr | ||
.filter(rl => !rl.allowed) | ||
.map(curr => curr.status?.message) | ||
.join("; "), | ||
}, | ||
}; | ||
return { | ||
apiVersion: "admission.k8s.io/v1", | ||
kind: "AdmissionReview", | ||
response: resp, | ||
}; | ||
} |