Skip to content

Commit fb97105

Browse files
authored
Merge pull request #57 from PRO-Robotech/feature/dev
working events socket streaming with new k8s client
2 parents 0dcea1b + 93b9a20 commit fb97105

File tree

2 files changed

+213
-108
lines changed

2 files changed

+213
-108
lines changed

src/constants/kubeClients.ts

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ type TDiscoveryHelpers = {
7878
getResourcesFor: (group: string, version: string, signal?: AbortSignal) => Promise<V1APIResourceList>
7979
}
8080

81+
type TRequestOptionsLike = {
82+
headers?: Record<string, string>
83+
// allow extra fields the client sets internally
84+
[key: string]: any
85+
}
86+
87+
type THasAddInterceptor = {
88+
addInterceptor: (fn: (opts: TRequestOptionsLike) => void | Promise<void>) => void
89+
}
90+
8191
/**
8292
* Preserve original in-cluster file logic + logs
8393
*/
@@ -176,40 +186,39 @@ export const allApis: TKubeClientsSurface = {
176186
* ---- API discovery helpers (v1 everything) ----
177187
* Lightweight wrappers using the underlying client's raw `request` method.
178188
*/
179-
const rawGet = async <T>(client: any, path: string, signal?: AbortSignal): Promise<T> => {
180-
const opts: any = { method: 'GET', uri: `${baseUrl}${path}` }
181-
if (DEVELOPMENT) opts.rejectUnauthorized = false
182-
if (signal) opts.signal = signal
183-
return client.request(opts).then((res: any) => (res.body ? JSON.parse(res.body) : res))
184-
}
185-
186-
/** List non-core API groups (GET /apis) */
187-
export const getApiGroups = async (signal?: AbortSignal): Promise<V1APIGroupList> => {
188-
return rawGet(allApis.core, '/apis', signal)
189-
}
190-
191-
/** List core API versions (GET /api) */
192-
export const getCoreApiVersions = async (signal?: AbortSignal): Promise<V1APIVersions> => {
193-
return rawGet(allApis.core, '/api', signal)
194-
}
195-
196-
/** List resources for a given group/version (GET /apis/{group}/{version}) */
197-
export const getResourcesFor = async (
198-
group: string,
199-
version: string,
200-
signal?: AbortSignal,
201-
): Promise<V1APIResourceList> => {
202-
const path = `/apis/${group}/${version}`
203-
return rawGet(allApis.core, path, signal)
204-
}
189+
// const rawGet = async <T>(client: any, path: string, signal?: AbortSignal): Promise<T> => {
190+
// const opts: any = { method: 'GET', uri: `${baseUrl}${path}` }
191+
// if (DEVELOPMENT) opts.rejectUnauthorized = false
192+
// if (signal) opts.signal = signal
193+
// return client.request(opts).then((res: any) => (res.body ? JSON.parse(res.body) : res))
194+
// }
195+
196+
// /** List non-core API groups (GET /apis) */
197+
// export const getApiGroups = async (signal?: AbortSignal): Promise<V1APIGroupList> => {
198+
// return rawGet(allApis.core, '/apis', signal)
199+
// }
200+
201+
// /** List core API versions (GET /api) */
202+
// export const getCoreApiVersions = async (signal?: AbortSignal): Promise<V1APIVersions> => {
203+
// return rawGet(allApis.core, '/api', signal)
204+
// }
205+
206+
// /** List resources for a given group/version (GET /apis/{group}/{version}) */
207+
// export const getResourcesFor = async (
208+
// group: string,
209+
// version: string,
210+
// signal?: AbortSignal,
211+
// ): Promise<V1APIResourceList> => {
212+
// const path = `/apis/${group}/${version}`
213+
// return rawGet(allApis.core, path, signal)
214+
// }
205215

206216
/** ---------- User-proxied client: STRICTLY no SA auth ---------- */
207217
export const createUserKubeClient = (
208218
userHeaders: Record<string, string | string[] | undefined>,
209219
): TKubeClientsSurface &
210220
TDiscoveryHelpers & {
211221
kubeConfig: KubeConfig // <-- expose KC for Watch
212-
request: (opts: any) => any // <-- convenience raw requester (from core client)
213222
} => {
214223
// Build a config with NO credentials at all
215224
const kc = new KubeConfig()
@@ -247,28 +256,35 @@ export const createUserKubeClient = (
247256
customObjects: kc.makeApiClient(CustomObjectsApi),
248257
}
249258

250-
const normalizeHeaders = (h: Record<string, string | string[] | undefined>): Record<string, string> => {
251-
return Object.fromEntries(
259+
const normalizeHeaders = (h: Record<string, string | string[] | undefined>): Record<string, string> =>
260+
Object.fromEntries(
252261
Object.entries(h)
253262
.filter(([, v]) => v !== undefined)
254263
.map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : (v as string)]),
255264
)
256-
}
265+
257266
const normalizedHeaders = normalizeHeaders(userHeaders)
258267

259-
const patch = (client: any): void => {
260-
const orig = client.request.bind(client)
268+
// attach an interceptor per client to forward user headers
269+
const attachForwardedHeaders = (client: object): void => {
270+
const c = client as Partial<THasAddInterceptor>
271+
if (!c || typeof c.addInterceptor !== 'function') return
272+
273+
c.addInterceptor((opts: TRequestOptionsLike) => {
274+
// merge headers
275+
const existing = opts.headers ?? {}
276+
opts.headers = { ...existing, ...normalizedHeaders }
261277

262-
client.request = (opts: any) => {
263-
opts.headers = { ...(opts.headers || {}), ...normalizedHeaders }
278+
// dev TLS parity (like your previous rejectUnauthorized=false)
264279
if (DEVELOPMENT) {
265-
opts.rejectUnauthorized = false
280+
// for request/request-promise under the hood
281+
// both of these are recognized by the generator stack
282+
;(opts as { strictSSL?: boolean }).strictSSL = false
283+
;(opts as { rejectUnauthorized?: boolean }).rejectUnauthorized = false
266284
}
267-
return orig(opts)
268-
}
285+
})
269286
}
270-
271-
Object.values(clients).forEach(patch)
287+
Object.values(clients).forEach(attachForwardedHeaders)
272288

273289
// Provide the same discovery helpers, bound to these user clients
274290
const rawGetUser = async <T>(path: string, signal?: AbortSignal): Promise<T> => {
@@ -285,26 +301,7 @@ export const createUserKubeClient = (
285301
rawGetUser(`/apis/${group}/${version}`, signal),
286302
}
287303

288-
// --- SAFE way to expose `request` ---
289-
type TKubeApiWithRequest = {
290-
request: (opts: Record<string, unknown>) => Promise<unknown>
291-
}
292-
293-
const hasRequest = (client: object): client is TKubeApiWithRequest => {
294-
return typeof (client as any).request === 'function'
295-
}
296-
297-
let requestFn: TKubeApiWithRequest['request']
298-
299-
if (hasRequest(clients.core)) {
300-
requestFn = clients.core.request.bind(clients.core)
301-
} else {
302-
console.error('CoreV1Api does not expose a request() method — check @kubernetes/client-node version')
303-
}
304-
305-
const request = (clients.core as unknown as { request: (opts: any) => any }).request.bind(clients.core)
306-
307-
return { ...clients, ...helpers, kubeConfig: kc, request }
304+
return { ...clients, ...helpers, kubeConfig: kc }
308305
}
309306

310307
/**

0 commit comments

Comments
 (0)