Fix CRD.getPreferedVersion() to work based on apiVersion (#4553)

* Fix CRD.getPreferedVersion() to work based on apiVersion

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Sebastian Malton authored and Jim Ehrismann committed 2022-01-19 11:30:56 -05:00
1 parent 35c8b76a95
commit 745c9d2622
2 files changed
+27 -15

No files matched your search

+10 -3
View File
@@ -19,10 +19,10 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { CustomResourceDefinition } from "../endpoints"; import { CustomResourceDefinition, CustomResourceDefinitionSpec } from "../endpoints";
describe("Crds", () => { describe("Crds", () => {
describe("getVersion", () => { describe("getVersion()", () => {
it("should throw if none of the versions are served", () => { it("should throw if none of the versions are served", () => {
const crd = new CustomResourceDefinition({ const crd = new CustomResourceDefinition({
apiVersion: "apiextensions.k8s.io/v1", apiVersion: "apiextensions.k8s.io/v1",
@@ -136,7 +136,7 @@ describe("Crds", () => {
expect(crd.getVersion()).toBe("123"); expect(crd.getVersion()).toBe("123");
}); });
it("should get the version name from the version field", () => { it("should get the version name from the version field, ignoring versions on v1beta", () => {
const crd = new CustomResourceDefinition({ const crd = new CustomResourceDefinition({
apiVersion: "apiextensions.k8s.io/v1beta1", apiVersion: "apiextensions.k8s.io/v1beta1",
kind: "CustomResourceDefinition", kind: "CustomResourceDefinition",
@@ -147,7 +147,14 @@ describe("Crds", () => {
}, },
spec: { spec: {
version: "abc", version: "abc",
versions: [
{
name: "foobar",
served: true,
storage: true,
}, },
],
} as CustomResourceDefinitionSpec,
}); });
expect(crd.getVersion()).toBe("abc"); expect(crd.getVersion()).toBe("abc");
+17 -12
View File
@@ -48,11 +48,10 @@ export interface CRDVersion {
additionalPrinterColumns?: AdditionalPrinterColumnsV1[]; additionalPrinterColumns?: AdditionalPrinterColumnsV1[];
} }
export interface CustomResourceDefinition { export interface CustomResourceDefinitionSpec {
spec: {
group: string; group: string;
/** /**
* @deprecated for apiextensions.k8s.io/v1 but used previously * @deprecated for apiextensions.k8s.io/v1 but used in v1beta1
*/ */
version?: string; version?: string;
names: { names: {
@@ -61,9 +60,9 @@ export interface CustomResourceDefinition {
kind: string; kind: string;
listKind: string; listKind: string;
}; };
scope: "Namespaced" | "Cluster" | string; scope: "Namespaced" | "Cluster";
/** /**
* @deprecated for apiextensions.k8s.io/v1 but used previously * @deprecated for apiextensions.k8s.io/v1 but used in v1beta1
*/ */
validation?: object; validation?: object;
versions?: CRDVersion[]; versions?: CRDVersion[];
@@ -72,10 +71,13 @@ export interface CustomResourceDefinition {
webhook?: any; webhook?: any;
}; };
/** /**
* @deprecated for apiextensions.k8s.io/v1 but used previously * @deprecated for apiextensions.k8s.io/v1 but used in v1beta1
*/ */
additionalPrinterColumns?: AdditionalPrinterColumnsV1Beta[]; additionalPrinterColumns?: AdditionalPrinterColumnsV1Beta[];
}; }
export interface CustomResourceDefinition {
spec: CustomResourceDefinitionSpec;
status: { status: {
conditions: { conditions: {
lastTransitionTime: string; lastTransitionTime: string;
@@ -150,14 +152,17 @@ export class CustomResourceDefinition extends KubeObject {
} }
getPreferedVersion(): CRDVersion { getPreferedVersion(): CRDVersion {
// Prefer the modern `versions` over the legacy `version` const { apiVersion } = this;
if (this.spec.versions) {
switch (apiVersion) {
case "apiextensions.k8s.io/v1":
for (const version of this.spec.versions) { for (const version of this.spec.versions) {
if (version.storage) { if (version.storage) {
return version; return version;
} }
} }
} else if (this.spec.version) { break;
case "apiextensions.k8s.io/v1beta1":
const { additionalPrinterColumns: apc } = this.spec; const { additionalPrinterColumns: apc } = this.spec;
const additionalPrinterColumns = apc?.map(({ JSONPath, ...apc }) => ({ ...apc, jsonPath: JSONPath })); const additionalPrinterColumns = apc?.map(({ JSONPath, ...apc }) => ({ ...apc, jsonPath: JSONPath }));
@@ -170,7 +175,7 @@ export class CustomResourceDefinition extends KubeObject {
}; };
} }
throw new Error(`Failed to find a version for CustomResourceDefinition ${this.metadata.name}`); throw new Error(`Unknown apiVersion=${apiVersion}: Failed to find a version for CustomResourceDefinition ${this.metadata.name}`);
} }
getVersion() { getVersion() {
@@ -197,7 +202,7 @@ export class CustomResourceDefinition extends KubeObject {
const columns = this.getPreferedVersion().additionalPrinterColumns ?? []; const columns = this.getPreferedVersion().additionalPrinterColumns ?? [];
return columns return columns
.filter(column => column.name != "Age" && (ignorePriority || !column.priority)); .filter(column => column.name.toLowerCase() != "age" && (ignorePriority || !column.priority));
} }
getValidation() { getValidation() {