Skip to content

Commit

Permalink
make some amends to the filters
Browse files Browse the repository at this point in the history
Signed-off-by: John Cowen <[email protected]>
  • Loading branch information
johncowen committed Nov 22, 2024
1 parent 2f6aa7b commit 268c360
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"
>
Expand Down
62 changes: 25 additions & 37 deletions packages/kuma-gui/src/app/data-planes/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,19 @@ export type MeshGatewayDataplaneSource = DataSourceResponse<MeshGatewayDataplane
const includes = <T extends readonly string[]>(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 = <K extends PropertyKey>(obj: unknown, key: K | null | undefined): obj is Record<K, unknown> => {
return key != null && obj != null && typeof obj === 'object' && key in obj
}
const filter = (data: Record<string, unknown>, 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] = []
Expand All @@ -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)
}
Expand Down Expand Up @@ -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:<ignored>: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 === '<ignored>:0000'
return arr.filter(item => prop(item, 'cluster') && prop(item.cluster, 'name') && typeof item.cluster.name === 'string' && item.cluster?.name?.endsWith(`:${inbound}`))
}
return []
})
Expand All @@ -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:<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 []
})
Expand Down
4 changes: 4 additions & 0 deletions packages/kuma-gui/src/app/kuma/services/kuma-api/KumaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, unknown>> {
return this.client.get(`/meshes/${mesh}/dataplanes/${dppName}/xds`, { params })
}

getAllMeshServicesFromMesh({ mesh }: { mesh: string }, params?: PaginationParameters): Promise<PaginatedApiListResponse<MeshService>> {
return this.client.get(`/meshes/${mesh}/meshservices`, { params })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export default ({ env, fake }: EndpointDependencies): MockResponder => (req) =>
// temporarily overwrite the result of dataplaneNetworking as it doesn't
// currently accept port plus we need to keep our ports synced.
; (networking.inbound ?? []).forEach((inbound, i) => {
// if (fake.datatype.boolean()) {
inbound.address = address
inbound.port = ports[i].port
inbound.servicePort = undefined
// } else {
// inbound.port = fake.number.int({ min: 1, max: 65535 })
// inbound.servicePort = ports[i].port
// }
inbound.tags['kuma.io/protocol'] = ports[i].protocol
})
// if (fake.datatype.boolean()) {
inbound.address = address
inbound.port = ports[i].port
inbound.servicePort = undefined
// } else {
// inbound.port = fake.number.int({ min: 1, max: 65535 })
// inbound.servicePort = ports[i].port
// }
inbound.tags['kuma.io/protocol'] = ports[i].protocol
})

const parts = String(name).split('.')
const displayName = parts.slice(0, -1).join('.')
Expand Down

0 comments on commit 268c360

Please sign in to comment.