|
1 |
| -import { K8s, Log } from "pepr"; |
2 |
| - |
3 | 1 | import { UDSConfig } from "../../../config";
|
4 |
| -import { Expose, Gateway, Istio, UDSPackage } from "../../crd"; |
5 |
| -import { getOwnerRef, sanitizeResourceName } from "../utils"; |
| 2 | +import { V1OwnerReference } from "@kubernetes/client-node"; |
| 3 | +import { Expose, Gateway, IstioVirtualService, IstioHTTP, IstioHTTPRoute } from "../../crd"; |
| 4 | +import { sanitizeResourceName } from "../utils"; |
6 | 5 |
|
7 | 6 | /**
|
8 | 7 | * Creates a VirtualService for each exposed service in the package
|
9 | 8 | *
|
10 | 9 | * @param pkg
|
11 | 10 | * @param namespace
|
12 | 11 | */
|
13 |
| -export async function virtualService(pkg: UDSPackage, namespace: string) { |
14 |
| - const pkgName = pkg.metadata!.name!; |
15 |
| - const generation = (pkg.metadata?.generation ?? 0).toString(); |
16 |
| - |
17 |
| - // Get the list of exposed services |
18 |
| - const exposeList = pkg.spec?.network?.expose ?? []; |
19 |
| - |
20 |
| - // Create a list of generated VirtualServices |
21 |
| - const payloads: Istio.VirtualService[] = []; |
22 |
| - |
23 |
| - // Iterate over each exposed service |
24 |
| - for (const expose of exposeList) { |
25 |
| - const { gateway = Gateway.Tenant, host, port, service, advancedHTTP = {} } = expose; |
26 |
| - |
27 |
| - const name = generateVSName(pkg, expose); |
28 |
| - |
29 |
| - // For the admin gateway, we need to add the path prefix |
30 |
| - const domain = (gateway === Gateway.Admin ? "admin." : "") + UDSConfig.domain; |
31 |
| - |
32 |
| - // Append the domain to the host |
33 |
| - const fqdn = `${host}.${domain}`; |
34 |
| - |
35 |
| - const http: Istio.HTTP = { ...advancedHTTP }; |
36 |
| - |
37 |
| - // Create the route to the service |
38 |
| - const route: Istio.HTTPRoute[] = [ |
39 |
| - { |
40 |
| - destination: { |
41 |
| - // Use the service name as the host |
42 |
| - host: `${service}.${namespace}.svc.cluster.local`, |
43 |
| - // The CRD only uses numeric ports |
44 |
| - port: { number: port }, |
45 |
| - }, |
| 12 | +export function generateVirtualService( |
| 13 | + expose: Expose, |
| 14 | + namespace: string, |
| 15 | + pkgName: string, |
| 16 | + generation: string, |
| 17 | + ownerRefs: V1OwnerReference[], |
| 18 | +) { |
| 19 | + const { gateway = Gateway.Tenant, host, port, service, advancedHTTP = {} } = expose; |
| 20 | + |
| 21 | + const name = generateVSName(pkgName, expose); |
| 22 | + |
| 23 | + // For the admin gateway, we need to add the path prefix |
| 24 | + const domain = (gateway === Gateway.Admin ? "admin." : "") + UDSConfig.domain; |
| 25 | + |
| 26 | + // Append the domain to the host |
| 27 | + const fqdn = `${host}.${domain}`; |
| 28 | + |
| 29 | + const http: IstioHTTP = { ...advancedHTTP }; |
| 30 | + |
| 31 | + // Create the route to the service |
| 32 | + const route: IstioHTTPRoute[] = [ |
| 33 | + { |
| 34 | + destination: { |
| 35 | + // Use the service name as the host |
| 36 | + host: `${service}.${namespace}.svc.cluster.local`, |
| 37 | + // The CRD only uses numeric ports |
| 38 | + port: { number: port }, |
46 | 39 | },
|
47 |
| - ]; |
| 40 | + }, |
| 41 | + ]; |
48 | 42 |
|
49 |
| - if (!advancedHTTP.directResponse) { |
50 |
| - // Create the route to the service if not using advancedHTTP.directResponse |
51 |
| - http.route = route; |
52 |
| - } |
| 43 | + if (!advancedHTTP.directResponse) { |
| 44 | + // Create the route to the service if not using advancedHTTP.directResponse |
| 45 | + http.route = route; |
| 46 | + } |
53 | 47 |
|
54 |
| - const payload: Istio.VirtualService = { |
55 |
| - metadata: { |
56 |
| - name, |
57 |
| - namespace, |
58 |
| - labels: { |
59 |
| - "uds/package": pkgName, |
60 |
| - "uds/generation": generation, |
61 |
| - }, |
62 |
| - // Use the CR as the owner ref for each VirtualService |
63 |
| - ownerReferences: getOwnerRef(pkg), |
| 48 | + const payload: IstioVirtualService = { |
| 49 | + metadata: { |
| 50 | + name, |
| 51 | + namespace, |
| 52 | + labels: { |
| 53 | + "uds/package": pkgName, |
| 54 | + "uds/generation": generation, |
64 | 55 | },
|
65 |
| - spec: { |
66 |
| - // Append the UDS Domain to the host |
67 |
| - hosts: [fqdn], |
68 |
| - // Map the gateway (admin, passthrough or tenant) to the VirtualService |
69 |
| - gateways: [`istio-${gateway}-gateway/${gateway}-gateway`], |
70 |
| - // Apply the route to the VirtualService |
71 |
| - http: [http], |
| 56 | + // Use the CR as the owner ref for each VirtualService |
| 57 | + ownerReferences: ownerRefs, |
| 58 | + }, |
| 59 | + spec: { |
| 60 | + // Append the UDS Domain to the host |
| 61 | + hosts: [fqdn], |
| 62 | + // Map the gateway (admin, passthrough or tenant) to the VirtualService |
| 63 | + gateways: [`istio-${gateway}-gateway/${gateway}-gateway`], |
| 64 | + // Apply the route to the VirtualService |
| 65 | + http: [http], |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + // If the gateway is the passthrough gateway, apply the TLS match |
| 70 | + if (gateway === Gateway.Passthrough) { |
| 71 | + payload.spec!.tls = [ |
| 72 | + { |
| 73 | + match: [{ port: 443, sniHosts: [fqdn] }], |
| 74 | + route, |
72 | 75 | },
|
73 |
| - }; |
74 |
| - |
75 |
| - // If the gateway is the passthrough gateway, apply the TLS match |
76 |
| - if (gateway === Gateway.Passthrough) { |
77 |
| - payload.spec!.tls = [ |
78 |
| - { |
79 |
| - match: [{ port: 443, sniHosts: [fqdn] }], |
80 |
| - route, |
81 |
| - }, |
82 |
| - ]; |
83 |
| - } |
84 |
| - |
85 |
| - Log.debug(payload, `Applying VirtualService ${name}`); |
86 |
| - |
87 |
| - // Apply the VirtualService and force overwrite any existing policy |
88 |
| - await K8s(Istio.VirtualService).Apply(payload, { force: true }); |
89 |
| - |
90 |
| - payloads.push(payload); |
91 |
| - } |
92 |
| - |
93 |
| - // Get all related VirtualServices in the namespace |
94 |
| - const virtualServices = await K8s(Istio.VirtualService) |
95 |
| - .InNamespace(namespace) |
96 |
| - .WithLabel("uds/package", pkgName) |
97 |
| - .Get(); |
98 |
| - |
99 |
| - // Find any orphaned VirtualServices (not matching the current generation) |
100 |
| - const orphanedVS = virtualServices.items.filter( |
101 |
| - vs => vs.metadata?.labels?.["uds/generation"] !== generation, |
102 |
| - ); |
103 |
| - |
104 |
| - // Delete any orphaned VirtualServices |
105 |
| - for (const vs of orphanedVS) { |
106 |
| - Log.debug(vs, `Deleting orphaned VirtualService ${vs.metadata!.name}`); |
107 |
| - await K8s(Istio.VirtualService).Delete(vs); |
| 76 | + ]; |
108 | 77 | }
|
109 |
| - |
110 |
| - // Return the list of unique hostnames |
111 |
| - return [...new Set(payloads.map(v => v.spec!.hosts!).flat())]; |
| 78 | + return payload; |
112 | 79 | }
|
113 | 80 |
|
114 |
| -export function generateVSName(pkg: UDSPackage, expose: Expose) { |
| 81 | +export function generateVSName(pkgName: string, expose: Expose) { |
115 | 82 | const { gateway = Gateway.Tenant, host, port, service, description, advancedHTTP } = expose;
|
116 | 83 |
|
117 | 84 | // Ensure the resource name is valid
|
118 | 85 | const matchHash = advancedHTTP?.match?.flatMap(m => m.name).join("-") || "";
|
119 | 86 | const nameSuffix = description || `${host}-${port}-${service}-${matchHash}`;
|
120 |
| - const name = sanitizeResourceName(`${pkg.metadata!.name}-${gateway}-${nameSuffix}`); |
| 87 | + const name = sanitizeResourceName(`${pkgName}-${gateway}-${nameSuffix}`); |
121 | 88 |
|
122 | 89 | return name;
|
123 | 90 | }
|
0 commit comments