Skip to content

Commit 38b7314

Browse files
committed
feat: parse Team Service status
1 parent 998f20e commit 38b7314

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

src/k8s-operations.test.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,28 @@ const makeServiceResponse = (name: string, teamName: string, overrides: Record<s
184184
...overrides,
185185
}) as unknown as AplServiceResponse
186186

187-
const makeHTTPRoute = (conditions: { type: string; status: string }[]) => ({
187+
const makeHTTPRoute = (
188+
conditions: { type: string; status: string }[],
189+
options: { parentGatewayName?: string; visibility?: string } = {},
190+
) => ({
191+
metadata: {
192+
labels: {
193+
'networking.knative.dev/visibility': options.visibility ?? '',
194+
},
195+
},
196+
spec: {
197+
parentRefs: [{ name: options.parentGatewayName ?? 'platform' }],
198+
},
188199
status: {
189200
parents: [
190201
{
191202
conditions,
192203
controllerName: 'istio.io/gateway-controller',
193-
parentRef: { group: 'gateway.networking.k8s.io', kind: 'Gateway', name: 'knative-local-gateway' },
204+
parentRef: {
205+
group: 'gateway.networking.k8s.io',
206+
kind: 'Gateway',
207+
name: options.parentGatewayName ?? 'platform',
208+
},
194209
},
195210
],
196211
},
@@ -252,6 +267,20 @@ describe('getServiceStatus', () => {
252267
expect(result).toBe('NotFound')
253268
})
254269

270+
test('returns NotFound when only local HTTPRoutes exist', async () => {
271+
mockList([
272+
makeHTTPRoute(
273+
[
274+
{ type: 'Accepted', status: 'True' },
275+
{ type: 'ResolvedRefs', status: 'True' },
276+
],
277+
{ parentGatewayName: 'knative-local-gateway', visibility: 'cluster-local' },
278+
),
279+
])
280+
const result = await getServiceStatus(makeServiceResponse('web', 'alpha'))
281+
expect(result).toBe('NotFound')
282+
})
283+
255284
test('returns Unknown when multiple HTTPRoutes are found', async () => {
256285
mockList([makeHTTPRoute([]), makeHTTPRoute([])])
257286
const result = await getServiceStatus(makeServiceResponse('web', 'alpha'))
@@ -269,6 +298,24 @@ describe('getServiceStatus', () => {
269298
expect(result).toBe('Succeeded')
270299
})
271300

301+
test('returns Succeeded when local and public routes exist and public is accepted', async () => {
302+
mockList([
303+
makeHTTPRoute(
304+
[
305+
{ type: 'Accepted', status: 'True' },
306+
{ type: 'ResolvedRefs', status: 'True' },
307+
],
308+
{ parentGatewayName: 'knative-local-gateway', visibility: 'cluster-local' },
309+
),
310+
makeHTTPRoute([
311+
{ type: 'Accepted', status: 'True' },
312+
{ type: 'ResolvedRefs', status: 'True' },
313+
]),
314+
])
315+
const result = await getServiceStatus(makeServiceResponse('web', 'alpha'))
316+
expect(result).toBe('Succeeded')
317+
})
318+
272319
test('returns Unknown when single HTTPRoute is not accepted', async () => {
273320
mockList([
274321
makeHTTPRoute([

src/k8s-operations.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ export function parseHTTPRouteStatus(httpRoute: any): boolean {
243243
return isAccepted
244244
}
245245

246+
function isPublicHTTPRoute(httpRoute: any): boolean {
247+
const visibility = httpRoute?.metadata?.labels?.['networking.knative.dev/visibility']
248+
if (visibility === 'cluster-local') return false
249+
250+
const parentRefs: any[] = Array.isArray(httpRoute?.spec?.parentRefs) ? httpRoute.spec.parentRefs : []
251+
const hasLocalGateway = parentRefs.some((parentRef) => parentRef?.name === 'knative-local-gateway')
252+
return !hasLocalGateway
253+
}
254+
246255
export async function getServiceStatus(service: AplServiceResponse): Promise<'Succeeded' | 'Unknown' | 'NotFound'> {
247256
const { name, labels } = service.metadata
248257
const teamName = labels['apl.io/teamId']
@@ -257,18 +266,19 @@ export async function getServiceStatus(service: AplServiceResponse): Promise<'Su
257266
)
258267

259268
const httpRoutes = Array.isArray(res?.items) ? res.items : []
269+
const publicHttpRoutes = httpRoutes.filter((httpRoute) => isPublicHTTPRoute(httpRoute))
260270

261-
if (httpRoutes.length === 0) {
262-
debug(`No HTTPRoutes found for service ${name} in namespace ${namespace}.`)
271+
if (publicHttpRoutes.length === 0) {
272+
debug(`No public HTTPRoutes found for service ${name} in namespace ${namespace}.`)
263273
return 'NotFound'
264-
} else if (httpRoutes.length > 1) {
274+
} else if (publicHttpRoutes.length > 1) {
265275
debug(
266-
`Multiple HTTPRoutes found for service ${name} in namespace ${namespace}. This may indicate an issue with the service configuration.`,
276+
`Multiple public HTTPRoutes found for service ${name} in namespace ${namespace}. This may indicate an issue with the service configuration.`,
267277
)
268278
return 'Unknown'
269279
}
270280

271-
const [httpRoute] = httpRoutes
281+
const [httpRoute] = publicHttpRoutes
272282
const isAccepted = parseHTTPRouteStatus(httpRoute)
273283
return isAccepted ? 'Succeeded' : 'Unknown'
274284
}

0 commit comments

Comments
 (0)