🌳
pt0/deployF/k8sF/generic1IngressTmplF.mts
1import * as _ from 'lodash-es'
3import { ingressTmpl } from './ingressF.mts'
9type GenericIngressTmplProps = {
10 name: string
11 namespace?: string
12 svcName?: string
13 portNo?: number
14 ingressClassName?: string
15 ingressAnnotationsH?: Record<string, string>
16 hostname?: string
17 hostnameA?: string[]
20export const genericIngressTmpl = ({
21 name, namespace='default', svcName, portNo=80, ingressClassName, ingressAnnotationsH,
22 hostname, hostnameA,
23}: GenericIngressTmplProps) => {
24 hostnameA ||= _.castArray(hostname)
26 throwIf(() => hostnameA.length == 0)
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 cert
33 const allWcHn = _.every(hostnameA, (hn) => isWcDnsSuffix(hn))
34 const baseAnnotations = allWcHn ? ingressBaseCommonAnnotH : ingressCommonAnnotationsH
36 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: portNo
60 }
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 }