This repository was archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Use stored versions for duck types #192
Open
vitreuz
wants to merge
1
commit into
vmware-archive:main
Choose a base branch
from
vitreuz:BUG-missing-source-sink-information
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -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" | ||
|
||
|
@@ -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 = [ | ||
|
@@ -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) | ||
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 } | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -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"; | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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