mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix: Add checks to KubeObject constructor to ensure shape
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
4634643f93
commit
bf6af58d80
@ -548,14 +548,30 @@ export class KubeObject<
|
||||
}
|
||||
|
||||
constructor(data: KubeJsonApiData<Metadata, Status, Spec>) {
|
||||
if (typeof data !== "object") {
|
||||
if (!isObject(data)) {
|
||||
throw new TypeError(`Cannot create a KubeObject from ${typeof data}`);
|
||||
}
|
||||
|
||||
if (!data.metadata || typeof data.metadata !== "object") {
|
||||
if (!isObject(data.metadata)) {
|
||||
throw new KubeCreationError(`Cannot create a KubeObject from an object without metadata`, data);
|
||||
}
|
||||
|
||||
if (!isString(data.metadata.name)) {
|
||||
throw new KubeCreationError(`Cannot create a KubeObject from an object without metadata.name being a string`, data);
|
||||
}
|
||||
|
||||
if (!isString(data.metadata.uid)) {
|
||||
throw new KubeCreationError(`Cannot create a KubeObject from an object without metadata.uid being a string`, data);
|
||||
}
|
||||
|
||||
if (!isString(data.metadata.resourceVersion)) {
|
||||
throw new KubeCreationError(`Cannot create a KubeObject from an object without metadata.resourceVersion being a string`, data);
|
||||
}
|
||||
|
||||
if (!isString(data.metadata.selfLink)) {
|
||||
throw new KubeCreationError(`Cannot create a KubeObject from an object without metadata.selfLink being a string`, data);
|
||||
}
|
||||
|
||||
Object.assign(this, data);
|
||||
autoBind(this);
|
||||
}
|
||||
|
||||
@ -22,8 +22,16 @@ describe("NetworkPolicyDetails", () => {
|
||||
|
||||
it("should render w/o errors", () => {
|
||||
const policy = new NetworkPolicy({
|
||||
metadata: {} as never,
|
||||
spec: {} as never,
|
||||
metadata: {
|
||||
name: "some-network-policy-name",
|
||||
namespace: "some-namespace",
|
||||
resourceVersion: "1",
|
||||
selfLink: "/apis/networking.k8s.io/v1/namespace/some-namespace/some-network-policy-name",
|
||||
uid: "1",
|
||||
},
|
||||
spec: {
|
||||
podSelector: {},
|
||||
},
|
||||
apiVersion: "networking.k8s.io/v1",
|
||||
kind: "NetworkPolicy",
|
||||
});
|
||||
@ -34,7 +42,13 @@ describe("NetworkPolicyDetails", () => {
|
||||
|
||||
it("should render egress nodeSelector", async () => {
|
||||
const policy = new NetworkPolicy({
|
||||
metadata: {} as never,
|
||||
metadata: {
|
||||
name: "some-network-policy-name",
|
||||
namespace: "some-namespace",
|
||||
resourceVersion: "1",
|
||||
selfLink: "/apis/networking.k8s.io/v1/namespace/some-namespace/some-network-policy-name",
|
||||
uid: "1",
|
||||
},
|
||||
spec: {
|
||||
egress: [{
|
||||
to: [{
|
||||
@ -58,7 +72,13 @@ describe("NetworkPolicyDetails", () => {
|
||||
|
||||
it("should not crash if egress nodeSelector doesn't have matchLabels", async () => {
|
||||
const policy = new NetworkPolicy({
|
||||
metadata: {} as never,
|
||||
metadata: {
|
||||
name: "some-network-policy-name",
|
||||
namespace: "some-namespace",
|
||||
resourceVersion: "1",
|
||||
selfLink: "/apis/networking.k8s.io/v1/namespace/some-namespace/some-network-policy-name",
|
||||
uid: "1",
|
||||
},
|
||||
spec: {
|
||||
egress: [{
|
||||
to: [{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user