|
| 1 | +import { K8s, Log } from "pepr"; |
| 2 | + |
| 3 | +import { IstioVirtualService, IstioServiceEntry, UDSPackage } from "../../crd"; |
| 4 | +import { getOwnerRef } from "../utils"; |
| 5 | +import { generateVirtualService } from "./virtual-service"; |
| 6 | +import { generateServiceEntry } from "./service-entry"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Creates a VirtualService and ServiceEntry for each exposed service in the package |
| 10 | + * |
| 11 | + * @param pkg |
| 12 | + * @param namespace |
| 13 | + */ |
| 14 | +export async function istioResources(pkg: UDSPackage, namespace: string) { |
| 15 | + const pkgName = pkg.metadata!.name!; |
| 16 | + const generation = (pkg.metadata?.generation ?? 0).toString(); |
| 17 | + const ownerRefs = getOwnerRef(pkg); |
| 18 | + |
| 19 | + // Get the list of exposed services |
| 20 | + const exposeList = pkg.spec?.network?.expose ?? []; |
| 21 | + |
| 22 | + // Create a Set of processed hosts (to maintain uniqueness) |
| 23 | + const hosts = new Set<string>(); |
| 24 | + |
| 25 | + // Track which ServiceEntries we've created |
| 26 | + const serviceEntryNames: Map<string, boolean> = new Map(); |
| 27 | + |
| 28 | + // Iterate over each exposed service |
| 29 | + for (const expose of exposeList) { |
| 30 | + // Generate a VirtualService for this `expose` entry |
| 31 | + const vsPayload = generateVirtualService(expose, namespace, pkgName, generation, ownerRefs); |
| 32 | + |
| 33 | + Log.debug(vsPayload, `Applying VirtualService ${vsPayload.metadata?.name}`); |
| 34 | + |
| 35 | + // Apply the VirtualService and force overwrite any existing policy |
| 36 | + await K8s(IstioVirtualService).Apply(vsPayload, { force: true }); |
| 37 | + |
| 38 | + vsPayload.spec!.hosts!.forEach(h => hosts.add(h)); |
| 39 | + |
| 40 | + // Generate a ServiceEntry for this `expose` entry |
| 41 | + const sePayload = generateServiceEntry(expose, namespace, pkgName, generation, ownerRefs); |
| 42 | + |
| 43 | + // If we have already made a ServiceEntry with this name, skip (i.e. if advancedHTTP was used) |
| 44 | + if (serviceEntryNames.get(sePayload.metadata!.name!)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + Log.debug(sePayload, `Applying ServiceEntry ${sePayload.metadata?.name}`); |
| 49 | + |
| 50 | + // Apply the ServiceEntry and force overwrite any existing policy |
| 51 | + await K8s(IstioServiceEntry).Apply(sePayload, { force: true }); |
| 52 | + |
| 53 | + serviceEntryNames.set(sePayload.metadata!.name!, true); |
| 54 | + } |
| 55 | + |
| 56 | + // Get all related VirtualServices in the namespace |
| 57 | + const virtualServices = await K8s(IstioVirtualService) |
| 58 | + .InNamespace(namespace) |
| 59 | + .WithLabel("uds/package", pkgName) |
| 60 | + .Get(); |
| 61 | + |
| 62 | + // Find any orphaned VirtualServices (not matching the current generation) |
| 63 | + const orphanedVS = virtualServices.items.filter( |
| 64 | + vs => vs.metadata?.labels?.["uds/generation"] !== generation, |
| 65 | + ); |
| 66 | + |
| 67 | + // Delete any orphaned VirtualServices |
| 68 | + for (const vs of orphanedVS) { |
| 69 | + Log.debug(vs, `Deleting orphaned VirtualService ${vs.metadata!.name}`); |
| 70 | + await K8s(IstioVirtualService).Delete(vs); |
| 71 | + } |
| 72 | + |
| 73 | + // Get all related ServiceEntries in the namespace |
| 74 | + const serviceEntries = await K8s(IstioServiceEntry) |
| 75 | + .InNamespace(namespace) |
| 76 | + .WithLabel("uds/package", pkgName) |
| 77 | + .Get(); |
| 78 | + |
| 79 | + // Find any orphaned ServiceEntries (not matching the current generation) |
| 80 | + const orphanedSE = serviceEntries.items.filter( |
| 81 | + se => se.metadata?.labels?.["uds/generation"] !== generation, |
| 82 | + ); |
| 83 | + |
| 84 | + // Delete any orphaned ServiceEntries |
| 85 | + for (const se of orphanedSE) { |
| 86 | + Log.debug(se, `Deleting orphaned ServiceEntry ${se.metadata!.name}`); |
| 87 | + await K8s(IstioServiceEntry).Delete(se); |
| 88 | + } |
| 89 | + |
| 90 | + // Return the list of unique hostnames |
| 91 | + return [...hosts]; |
| 92 | +} |
0 commit comments