From 60e74c5340c36d96c1c3ac03c791d9583b1022ca Mon Sep 17 00:00:00 2001 From: John Cowen Date: Fri, 22 Nov 2024 16:27:55 +0000 Subject: [PATCH] make some amends to the filters Signed-off-by: John Cowen --- .../ConnectionInboundSummaryXdsConfigView.vue | 2 +- .../kuma-gui/src/app/data-planes/sources.ts | 62 ++++++++----------- .../src/app/kuma/services/kuma-api/KumaApi.ts | 4 ++ .../x/components/x-code-block/XCodeBlock.vue | 7 +-- .../src/meshes/_/dataplanes/_/_overview.ts | 20 +++--- 5 files changed, 42 insertions(+), 53 deletions(-) diff --git a/packages/kuma-gui/src/app/connections/views/ConnectionInboundSummaryXdsConfigView.vue b/packages/kuma-gui/src/app/connections/views/ConnectionInboundSummaryXdsConfigView.vue index 4f1381ac1..e048c9f55 100644 --- a/packages/kuma-gui/src/app/connections/views/ConnectionInboundSummaryXdsConfigView.vue +++ b/packages/kuma-gui/src/app/connections/views/ConnectionInboundSummaryXdsConfigView.vue @@ -20,7 +20,7 @@ :src="uri(sources, '/meshes/:mesh/dataplanes/:dataplane/inbound/:inbound/xds', { mesh: route.params.mesh, dataplane: route.params.dataPlane, - inbound: `${props.data.addressPort}`, + inbound: `${props.data.port}`, })" v-slot="{ data: raw, refresh }" > diff --git a/packages/kuma-gui/src/app/data-planes/sources.ts b/packages/kuma-gui/src/app/data-planes/sources.ts index f74fe4013..09ec7357a 100644 --- a/packages/kuma-gui/src/app/data-planes/sources.ts +++ b/packages/kuma-gui/src/app/data-planes/sources.ts @@ -30,28 +30,19 @@ export type MeshGatewayDataplaneSource = DataSourceResponse(arr: T, item: string): item is T[number] => { return arr.includes(item as T[number]) } -type XdsConfig = Record<'configs', { - dynamic_active_clusters?: { - cluster: { - name: string - } - }[] - dynamic_listeners?: { - name: string - }[] - dynamic_endpoint_configs?: { - endpoint_config: { - cluster_name: string - } - }[] -}[]> -const filter = (data: XdsConfig, cb: (key: string, arr: unknown[]) => unknown[]) => { +const prop = (obj: unknown, key: K | null | undefined): obj is Record => { + return key != null && obj != null && typeof obj === 'object' && key in obj +} +const filter = (data: Record, cb: (key: string, arr: unknown[]) => unknown[]) => { const { configs } = data + if (!Array.isArray(configs)) { + return { configs: [] } + } return { configs: configs.reduce((prev, item) => { const entries = Object.entries(item) const found = entries.reduce((prev, [key, value]) => { - const found = cb(key, value) + const found = cb(key, Array.isArray(value) ? value : []) if (found.length > 0) { if (typeof prev[key] === 'undefined') { prev[key] = [] @@ -60,6 +51,7 @@ const filter = (data: XdsConfig, cb: (key: string, arr: unknown[]) => unknown[]) } return prev }, {} as typeof configs[number]) + if (Object.keys(found).length > 0) { return prev.concat(found) } @@ -128,26 +120,20 @@ export const sources = (source: Source, api: KumaApi, can: Can) => { const { mesh, dataplane, inbound } = params // we don't ask for endpoints because we don't need them for inbound filtering - const res = await api.getDataplaneData({ + const res = await api.getDataplaneXds({ mesh, dppName: dataplane, - dataPath: 'xds', }, { include_eds: false, }) - // @ts-ignore res: api.getDataplaneData() returns a string even though it doesn't always return a string - // once we get OpenAPI specs for this we should be able to remove - return filter(res, (key: string, arr: { name: string }[]) => { + return filter(res, (key: string, arr: unknown[]) => { switch (key) { case 'dynamic_listeners': - // dynamic_listeners[].name === inbound - return arr.filter(item => item.name === `inbound:${inbound}`) + // dynamic_listeners[].name === 'inbound::0000' + return arr.filter((item = {}) => prop(item, 'name') && typeof item.name === 'string' && item.name.startsWith('inbound:') && item.name?.endsWith(`:${inbound}`)) case 'dynamic_active_clusters': - case 'static_clusters': - // {dynamic_active_clusters, static_clusters}.cluster.load_assignment.endpoints[].lb_endpoints[].endpoint.address.socket_address.address.{address:port_value} === inbound - return arr.filter(item => { - return item.cluster?.load_assignment?.endpoints?.some((item) => item.lb_endpoints.some(item => `${item.endpoint?.address?.socket_address?.address}:${item.endpoint?.address?.socket_address?.port_value}` === inbound)) - }) + // dynamic_active_clusters[].cluster.name === ':0000' + return arr.filter(item => prop(item, 'cluster') && prop(item.cluster, 'name') && typeof item.cluster.name === 'string' && item.cluster?.name?.endsWith(`:${inbound}`)) } return [] }) @@ -156,23 +142,25 @@ export const sources = (source: Source, api: KumaApi, can: Can) => { const { mesh, dataplane, outbound, endpoints } = params // we don't ask for endpoints because we don't need them for inbound filtering - const res = await api.getDataplaneData({ + const res = await api.getDataplaneXds({ mesh, dppName: dataplane, - dataPath: 'xds', }, { include_eds: endpoints, }) - // @ts-ignore res: api.getDataplaneData() returns a string even though it doesn't always return a string - // once we get OpenAPI specs for this we should be able to remove - return filter(res, (key: string, arr: { endpoint_config: { cluster_name: string }, cluster: { name: string } }[]) => { + return filter(res, (key: string, arr: unknown[]) => { switch (key) { + case 'dynamic_listeners': + // this one won't work yet see + // https://github.com/kumahq/kuma/issues/12093 + // dynamic_listeners[].name === 'outbound:' + return arr.filter(item => prop(item, 'name') && item.name === `outbound:${outbound}`) case 'dynamic_active_clusters': - // dynamic_active_clusters[].name === outbound - return arr.filter(item => item.cluster.name === outbound) + // dynamic_active_clusters[].cluster.name === outbound + return arr.filter(item => prop(item, 'cluster') && prop(item.cluster, 'name') && item.cluster?.name === outbound) case 'dynamic_endpoint_configs': // dynamic_endpoint_configs[].endpoint_config.cluster_name === outbound - return arr.filter(item => item.endpoint_config.cluster_name === outbound) + return arr.filter(item => prop(item, 'endpoint_config') && prop(item.endpoint_config, 'cluster_name') && item.endpoint_config?.cluster_name === outbound) } return [] }) diff --git a/packages/kuma-gui/src/app/kuma/services/kuma-api/KumaApi.ts b/packages/kuma-gui/src/app/kuma/services/kuma-api/KumaApi.ts index a4efb28be..d32d55c54 100644 --- a/packages/kuma-gui/src/app/kuma/services/kuma-api/KumaApi.ts +++ b/packages/kuma-gui/src/app/kuma/services/kuma-api/KumaApi.ts @@ -175,6 +175,10 @@ export default class KumaApi extends Api { return this.client.get(`/meshes/${mesh}/dataplanes/${dppName}/${dataPath}`, { params }) } + getDataplaneXds({ mesh, dppName }: { mesh: string, dppName: string, params?: any }, params?: any): Promise> { + return this.client.get(`/meshes/${mesh}/dataplanes/${dppName}/xds`, { params }) + } + getAllMeshServicesFromMesh({ mesh }: { mesh: string }, params?: PaginationParameters): Promise> { return this.client.get(`/meshes/${mesh}/meshservices`, { params }) } diff --git a/packages/kuma-gui/src/app/x/components/x-code-block/XCodeBlock.vue b/packages/kuma-gui/src/app/x/components/x-code-block/XCodeBlock.vue index 56b815c6d..f243fa0d5 100644 --- a/packages/kuma-gui/src/app/x/components/x-code-block/XCodeBlock.vue +++ b/packages/kuma-gui/src/app/x/components/x-code-block/XCodeBlock.vue @@ -1,8 +1,5 @@