🌳
pt0/deployF/dockerF/dockRegF.mts
1import * as _ from 'lodash-es'
7import { type secretNameType } from '../../serverF/secretsF/getPlainSecAI.mts'
9import fs from 'fs'
11type RegCredSecNameProps = { name?: string, dockreg_host: string, cluster_name?: string }
13export const regCredSecName = ({name, dockreg_host, cluster_name}: RegCredSecNameProps) => {
14 cluster_name ||= getKlusterCtx().cluster_name
15 name ||= dockreg_host.split('.')[0]
16 return [`regcred`, name, cluster_name].join('-')
19export const genRegCredSecName = (dockreg_host: string, cluster_name?: string) => {
20 dockreg_host = getDockregCanonicalHost(dockreg_host)
21 const baseName = regCredSecName({dockreg_host, cluster_name})
22 return baseName + '.json'
25type GetCredsProps = { dockreg_host: string }
27export const getCreds = ({dockreg_host}: GetCredsProps) => {
28 dockreg_host = getDockregCanonicalHost(dockreg_host)
29 assertDefined(dockreg_host)
31 const secretName = genRegCredSecName(dockreg_host) as secretNameType
32 const secretPath = `${secretsDir}/${secretName}`
34 let sec = getOptJsonSecret(secretName)
35 if (!sec) {
36 const username = 'reguser'
37 const password = randomUrlSafeBytes(24)
38 sec = {username, password}
39 fs.writeFileSync(secretPath, JSON.stringify(sec, null, 2))
40 console.log(`Generated registry credentials: ${secretPath}`)
41 }
42 if (_.isString(sec)) {
43 sec = JSON.parse(sec)
44 }
45 const {username, password} = sec
47 const auth = Buffer.from(username + ':' + password).toString('base64')
49 return {username, password, auth}
52type RegcredSecTemplProps = {
53 name?: string
54 dockreg_host: string
55 cluster_name?: string
56 dockLanHost?: string
59export const regcredSecTempl = ({name, dockreg_host, dockLanHost}: RegcredSecTemplProps) => {
60 assertDefined(dockreg_host)
62 name ||= regCredSecName({name, dockreg_host})
64 const {auth} = getCreds({dockreg_host})
66 const auths: Record<string, {auth: string}> = {[`https://${dockreg_host}`]: { auth }}
67 if (dockLanHost) {
68 auths[`https://${dockLanHost}`] = { auth }
69 }
70 const jsonS = JSON.stringify({auths})
71 const secretsH = {
72 '.dockerconfigjson': jsonS
73 }
75 return {
76 ...secretTemplate({name, secretsH}),
77 type: 'kubernetes.io/dockerconfigjson'
78 }