1import * as _ from 'lodash-es'2import { ingressBaseCommonAnnotH, ingressCommonAnnotationsH } from '../ingressBaseCommonAnnotHF.mts'3import { ingressTmpl } from './ingressF.mts'4import { getNonWcDns, getCfProxied } from './get1NonWcDnsF.mts'5import { arrAllSame } from '../../sharedF/assertsF/arrAllSameF.mts'6import { isWcDnsSuffix } from '../harvF/getWcSuffixF.mts'7import { throwIf } from '../../sharedF/throwErrorF/throwIfF.mts'9type GenericIngressTmplProps = {10 name: string11 namespace?: string12 svcName?: string13 portNo?: number14 ingressClassName?: string15 ingressAnnotationsH?: Record<string, string>16 hostname?: string17 hostnameA?: string[]18}20export const genericIngressTmpl = ({21 name, namespace='default', svcName, portNo=80, ingressClassName, ingressAnnotationsH,22 hostname, hostnameA,23}: GenericIngressTmplProps) => {24 hostnameA ||= _.castArray(hostname)28 const nonWcDns = arrAllSame(_.map(hostnameA, getNonWcDns))29 const cfProxied = arrAllSame(_.map(hostnameA, getCfProxied))31 // Don't include cert-manager issuer annotation when using wildcard certs -32 // it would overwrite the shared wildcard secret with a hostname-specific cert33 const allWcHn = _.every(hostnameA, (hn) => isWcDnsSuffix(hn))34 const baseAnnotations = allWcHn ? ingressBaseCommonAnnotH : ingressCommonAnnotationsH36 const annotations = {37 ...baseAnnotations,38 ...ingressAnnotationsH,39 }41 if (nonWcDns) {42 const nonWildcardHostnames = hostnameA.filter(hn => !hn.startsWith('*.'))43 Object.assign(annotations, {44 'external-dns.alpha.kubernetes.io/cloudflare-proxied': cfProxied ? 'true' : 'false',45 'external-dns.alpha.kubernetes.io/hostname': nonWildcardHostnames.join(', ')46 })47 }49 const rules = _.map(hostnameA, (hostname) => {50 return {51 host: hostname,52 http: {53 paths: [54 {55 backend: {56 service: {57 name: svcName || name,58 port: {59 number: portNo60 }61 }62 },63 pathType: 'ImplementationSpecific',64 }65 ]66 }67 }68 })70 return {71 apiVersion: 'networking.k8s.io/v1',72 kind: 'Ingress',73 metadata: {74 annotations,75 name, namespace,76 },77 spec: {78 ...ingressTmpl({ingressClassName, hostnameA, name}),79 rules,80 },81 }82}