Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.
Open
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
60 changes: 46 additions & 14 deletions src/components/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

// helpers for generating the
// objects that Octant can render to components.
// helpers for generating the objects that Octant can render to components.
import * as h from "@project-octant/plugin/helpers";

import ctx from "../context";
import { V1ObjectMeta, V1ObjectReference } from "@kubernetes/client-node"
import { Component } from "@project-octant/plugin/components/component"
import { ComponentFactory, FactoryMetadata } from "@project-octant/plugin/components/component-factory"
import { TextFactory } from "@project-octant/plugin/components/text";
import { V1CustomResourceDefinition, V1CustomResourceDefinitionVersion, V1ObjectMeta, V1ObjectReference } from "@kubernetes/client-node";
import { Component } from "@project-octant/plugin/components/component";
import { ComponentFactory, FactoryMetadata } from "@project-octant/plugin/components/component-factory";
import { LinkFactory } from "@project-octant/plugin/components/link";
import { TextFactory } from "@project-octant/plugin/components/text";

export const DiscoveryV1Alpha = "discovery.knative.dev/v1alpha1"

Expand Down Expand Up @@ -64,7 +63,7 @@ export class ClusterDuckTypeTableFactory implements ComponentFactory<any> {
const { versions } = spec
const columns = {
type: 'Type',
version: 'API Version',
version: 'Stored API Version',
}
const table = new h.TableFactoryBuilder([], [], void 0, void 0, void 0, void 0, this.factoryMetadata);
table.columns = [
Expand All @@ -74,11 +73,17 @@ export class ClusterDuckTypeTableFactory implements ComponentFactory<any> {

table.emptyContent = `There are no ${spec.names.plural}!`;
// TODO: this should be handled externally
const duckVersions = versions.reduce((acc: ResourceMeta[], cur) => acc.concat(status.ducks[cur.name]), [])
const latestVersions = latestDuckTypeVersions(duckVersions)
latestVersions.sort((a, b) => a.kind.localeCompare(b.kind))
const duckMetas = versions.reduce((acc: ResourceMeta[], cur) => acc.concat(status.ducks[cur.name]), [])
const stored = StoredVersions(duckMetas)

const storedMetas = stored.reduce((acc: ResourceMeta[], cur) => {
const storedMeta = duckMetas.find(meta => meta.kind === cur[0] && meta.apiVersion.endsWith(cur[1].name))
return storedMeta ? acc.concat(storedMeta) : acc
}, [])
// const latestVersions = latestDuckTypeVersions(duckVersions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this comment for?
Also looks like this was the only reference to latestDuckTypeVersions I think

storedMetas.sort((a, b) => a.kind.localeCompare(b.kind))

for (const duck of latestVersions) {
for (const duck of storedMetas) {
const { apiVersion, kind } = duck
// TODO consider using the builtin octant CRD to link to
const ref: V1ObjectReference = { apiVersion: this.ref?.apiVersion, name: kind }
Expand All @@ -98,10 +103,37 @@ export class ClusterDuckTypeTableFactory implements ComponentFactory<any> {
}
}

export function StoredVersions(sourceMetas: ResourceMeta[]): [string, V1CustomResourceDefinitionVersion][] {
const uniqueMetas: ResourceMeta[] = sourceMetas.filter(
(meta, idx, arr) => idx === arr.findIndex(first => meta.kind === first.kind))
const crdNames: string[] = uniqueMetas.map((meta) => {
const group: string = meta?.apiVersion.split("/")[0] || "nogroup"
// TODO: this is currently a guess and potentially risky
const plural: string = meta?.kind.toLocaleLowerCase() + 's' || "noname"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to get the CRD? Or are you trying to avoid more API calls?


return `${plural}.${group}`
})

const crds: V1CustomResourceDefinition[] = crdNames.map((name) => ctx.dashboardClient.Get({
apiVersion: 'apiextensions.k8s.io/v1',
kind: 'CustomResourceDefinition',
name: `${name}`,
}))

const stored = crds.reduce((acc: [string, V1CustomResourceDefinitionVersion][], cur) => {
const kind = cur.status?.acceptedNames?.kind
const version = cur.spec.versions.find(ver => ver.storage)

if (kind && version) { acc.push([kind, version])}
return acc
}, [])
return stored
}

function latestDuckTypeVersions(duckVersions: ResourceMeta[]): ResourceMeta[] {
duckVersions.sort((a, b) => apiVersionCompare(a.apiVersion, b.apiVersion))
duckVersions.reverse()
return duckVersions.filter((val, idx, arr) => idx === arr.findIndex(v => val.kind === v.kind))
duckVersions.sort((a, b) => apiVersionCompare(a.apiVersion, b.apiVersion))
duckVersions.reverse()
return duckVersions.filter((val, idx, arr) => idx === arr.findIndex(v => val.kind === v.kind))
}

function apiVersionCompare(a: string, b: string): number {
Expand Down
22 changes: 10 additions & 12 deletions src/eventing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
DiscoveryV1AlphaClusterDuckType,
ResourceMeta,
SourcesDuck,
StoredVersions,
} from "./components/discovery";
import { MetadataSummaryFactory } from "./components/metadata";
import { EditorFactory } from "@project-octant/plugin/components/editor";
Expand Down Expand Up @@ -224,20 +225,17 @@ function sourceListing(clientID: string, factoryMetadata?: FactoryMetadata, sour
[])
sourceMetas.sort((a, b) => (a.kind.localeCompare(b.kind)))

const allSources: Source[] = sourceMetas.reduce((acc: Source[], cur) => {
if (!sourceType || cur.kind === sourceType) {
return acc.concat(ctx.dashboardClient.List({
apiVersion: cur.apiVersion,
kind: cur.kind,
namespace: ctx.namespace,
}))
}
return acc
const stored = StoredVersions(sourceMetas)
const storedMetas: ResourceMeta[] = stored.reduce((acc: ResourceMeta[], cur) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull out this reduce as a function from this and discovery?

const storedMeta = sourceMetas.find(meta => meta.kind === cur[0] && meta.apiVersion.endsWith(cur[1].name))
return storedMeta ? acc.concat(storedMeta) : acc
}, [])

const sources: Source[] = allSources.reduce((acc: Source[], cur) =>
acc.find((s) => s.metadata.uid === cur.metadata.uid) ? acc : acc.concat(cur),
[])
const sources: Source[] = storedMetas.reduce((acc: Source[], cur) => acc.concat(ctx.dashboardClient.List({
apiVersion: cur.apiVersion,
kind: cur.kind,
namespace: ctx.namespace,
})), [])
sources.sort((a, b) => (a.metadata.name || '').localeCompare(b.metadata.name || ''));

return new SourceListFactory({ sources, factoryMetadata })
Expand Down