Skip to content

Commit

Permalink
Improve resource kind typing (#1251)
Browse files Browse the repository at this point in the history
With these changes, `Kube.resourcePlural("Dummy")` is a type error, so
the non-existing resource kind test should not be needed anymore.
  • Loading branch information
SimonAlling authored and Andres Martinez Gotor committed Oct 25, 2019
1 parent 37ba9a2 commit eaf12bf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
19 changes: 0 additions & 19 deletions dashboard/src/shared/Kube.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,4 @@ describe("App", () => {
socket.close();
});
});

describe("resourcePlural", () => {
const tests = [
{ kind: "Service", expected: "services" },
{ kind: "Ingress", expected: "ingresses" },
{ kind: "Deployment", expected: "deployments" },
];
tests.forEach(t => {
it(`returns the correct plural for ${t.kind}`, () => {
expect(Kube.resourcePlural(t.kind)).toBe(t.expected);
});
});

it("throws an error if the resource kind isn't registered", () => {
expect(() => Kube.resourcePlural("ThisKindWillNeverExist")).toThrow(
"Don't know plural for ThisKindWillNeverExist, register it in Kube",
);
});
});
});
22 changes: 3 additions & 19 deletions dashboard/src/shared/Kube.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Auth } from "./Auth";
import { axiosWithAuth } from "./AxiosInstance";
import { ResourceKind, ResourceKindsWithPlurals } from "./ResourceKinds";
import { IResource } from "./types";

export const APIBase = "api/kube";
Expand All @@ -10,19 +11,6 @@ if (location.protocol === "https:") {
WebSocketAPIBase = `ws://${window.location.host}${window.location.pathname}`;
}

// We explicitly define the plurals here, just in case a generic pluralizer
// isn't sufficient. Note that CRDs can explicitly define pluralized forms,
// which might not match with the Kind. If this becomes difficult to
// maintain we can add a generic pluralizer and a way to override.
const ResourceKindToPlural = {
Secret: "secrets",
Service: "services",
Ingress: "ingresses",
Deployment: "deployments",
StatefulSet: "statefulsets",
DaemonSet: "daemonsets",
};

// Kube is a lower-level class for interacting with the Kubernetes API. Use
// ResourceRef to interact with a single API resource rather than using Kube
// directly.
Expand Down Expand Up @@ -97,11 +85,7 @@ export class Kube {
}

// Gets the plural form of the resource Kind for use in the resource path
public static resourcePlural(kind: string) {
const plural = ResourceKindToPlural[kind];
if (!plural) {
throw new Error(`Don't know plural for ${kind}, register it in Kube`);
}
return plural;
public static resourcePlural(kind: ResourceKind) {
return ResourceKindsWithPlurals[kind];
}
}
16 changes: 16 additions & 0 deletions dashboard/src/shared/ResourceKinds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// We explicitly define the plurals here, just in case a generic pluralizer
// isn't sufficient. Note that CRDs can explicitly define pluralized forms,
// which might not match with the Kind. If this becomes difficult to
// maintain we can add a generic pluralizer and a way to override.
export const ResourceKindsWithPlurals = {
ClusterRole: "clusterroles",
ConfigMap: "configmaps",
DaemonSet: "daemonsets",
Deployment: "deployments",
Ingress: "ingresses",
Secret: "secrets",
Service: "services",
StatefulSet: "statefulsets",
} as const;

export type ResourceKind = keyof typeof ResourceKindsWithPlurals;
3 changes: 2 additions & 1 deletion dashboard/src/shared/ResourceRef.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Kube } from "./Kube";
import { ResourceKind } from "./ResourceKinds";
import { IResource } from "./types";

// ResourceRef defines a reference to a namespaced Kubernetes API Object and
// provides helpers to retrieve the resource URL
class ResourceRef {
public apiVersion: string;
public kind: string;
public kind: ResourceKind;
public name: string;
public namespace: string;

Expand Down
3 changes: 2 additions & 1 deletion dashboard/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IConfigState } from "../reducers/config";
import { INamespaceState } from "../reducers/namespace";
import { IAppRepositoryState } from "../reducers/repos";
import { hapi } from "./hapi/release";
import { ResourceKind } from "./ResourceKinds";

// Allow defining multiple error classes
// tslint:disable:max-classes-per-file
Expand Down Expand Up @@ -141,7 +142,7 @@ export interface IIngressSpec {

export interface IResource {
apiVersion: string;
kind: string;
kind: ResourceKind;
type: string;
spec: any;
status: any;
Expand Down

0 comments on commit eaf12bf

Please sign in to comment.