1import * as _ from 'lodash-es'2import { genericIngressTmpl } from '../../generic1IngressTmplF.mts'3import { kubeActionPvc } from '../../pvcF/kubeActionPvcF.mts'4import { resourcesAction } from '../../resourcesActionF.mts'5import { criticalPriorityClassTmpl } from '../../priorityClassF.mts'6import { kubeSvcTmpl } from '../../kubeSvcTmplF.mts'7import { buildHelpStr } from '../../../cliF/cliBaseF.mts'8import { k8sBuiltinActions } from '../../cliF/k8sCliBaseF.mts'9import { importMetaUrlCtx } from '../../../../serverF/importMetaUrlCtxF.mts'10import { getImportMetaUrlPath } from '../../../../serverF/isDirectlyRunF.mts'11import { absPathToPtPath } from '../../../../serverF/pathF/absPathToPtPathF.mts'12import { ptnodeBin } from '../../../constantsF.mts'13import { assertDefined } from '../../../../sharedF/assertsF/assertDefinedF.mts'14import { betLog } from '../../../../sharedF/betterConsLogF.mts'15import { getKlusterCtx } from '../../ctxF/klusterCtxF.mts'16import { getAction } from '../../../ctxF/actionCtxF.mts'17import { resActionsH } from '../../resActionsF/res1ActionF.mts'18import { secretTemplate } from '../../k8sSecF.mts'19import { getCreds, regCredSecName, regcredSecTempl } from '../../../dockerF/dockRegF.mts'20import { ensureSaPullSecret } from '../../../dockerF/dockRegF/getImagePullSecretsF.mts'21import { doExec } from '../../../../serverF/execF.mts'22import { getDockRegKubeResA } from '../../../dockerF/dockRegF/getDockRegKubeResAF.mts'23import { inspect3KubeRes } from '../../inspectKubeResF.mts'24import { getZotCfgMap, getZotDeploymentTmpl, zotPortNo } from './zotResTmplF.mts'25import { regcacheGc } from './regcacheGcAI.mts'26import { regcacheName } from './regcacheConstsF.mts'28const regcacheIngressAnnotationsH = {29 'nginx.ingress.kubernetes.io/proxy-body-size': '2g',30 'nginx.ingress.kubernetes.io/client-body-buffer-size': '100m',31 'nginx.ingress.kubernetes.io/proxy-read-timeout': '600',32 'nginx.ingress.kubernetes.io/proxy-send-timeout': '600',33}35export const k8sRegCache = async ({36 pvcReplicaCnt, sizeGb=50,37 regcacheHost, regcacheLanHost,38}: {39 pvcReplicaCnt?: number, sizeGb?: number,40 regcacheHost?: string, regcacheLanHost?: string,41}) => {42 const {cluster_name} = getKlusterCtx()44 assertDefined(regcacheHost || regcacheLanHost)45 const name = regcacheName47 if (action === 'help') {48 const actionsH = {49 ..._.pick(k8sBuiltinActions, ['apply', 'delete', 'info', 'fexec']),50 ..._.pick(resActionsH(), ['logs', 'pods', 'restart']),51 uses: {cliDescript: 'show k8s resources using this cache'},52 gc: {cliDescript: regcacheGc.cliDescript},53 }54 const {importMetaUrl} = importMetaUrlCtx.getStore() || {}55 const rawPath = importMetaUrl ? getImportMetaUrlPath(importMetaUrl as any) : null56 const epPath = rawPath ? absPathToPtPath(rawPath as Parameters<typeof absPathToPtPath>[0]) : '<sync.mjs>'57 console.log(buildHelpStr({scriptName: `${ptnodeBin} ${epPath} --steps=${name}`, actionsH: actionsH as any}))59 return60 }62 if (action == 'uses') {63 const resA = await getDockRegKubeResA({dockReg: name})65 return66 }68 if (action == 'gc') {69 await regcacheGc({regcacheHost: regcacheHost!, regcacheLanHost})70 return71 }73 const zotCfgName = `${name}-zotcfg`74 const htpasswdSecretName = `${name}-htpasswd`76 let secretsH = {}77 if (action == 'apply') {79 // hardcode 'docker' — local `docker run` for htpasswd generation, same as dockreg80 const {stdout: htpasswd} = await doExec(`docker run --entrypoint htpasswd httpd:2 -Bbn ${username} ${password}`)81 secretsH = { htpasswd }82 }84 const resources = [86 getZotCfgMap({zotCfgName}),87 getZotDeploymentTmpl({zotCfgName, htpasswdSecretName}),88 secretTemplate({name: htpasswdSecretName, secretsH}),89 regcredSecTempl({dockreg_host: regcacheHost!, dockLanHost: regcacheLanHost}),90 kubeSvcTmpl({name, portNo: zotPortNo}),91 genericIngressTmpl({name, hostname: regcacheHost!, portNo: zotPortNo, ingressAnnotationsH: regcacheIngressAnnotationsH}),92 ]93 if (regcacheLanHost) {94 resources.push(genericIngressTmpl({name: name + '-lan', svcName: name, hostname: regcacheLanHost, portNo: zotPortNo, ingressAnnotationsH: regcacheIngressAnnotationsH}))95 }97 await kubeActionPvc({action, name, sizeGb, pvcReplicaCnt})98 await resourcesAction({resources, action, cluster_name})100 if (action == 'apply') {101 await ensureSaPullSecret({secretName: regCredSecName({dockreg_host: regcacheHost!}), action, cluster_name})102 }103}