-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (98 loc) · 2.01 KB
/
index.js
File metadata and controls
107 lines (98 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import dayjs from 'dayjs'
/**
* @param {string} v
* @returns {string}
*/
export function cpu (v) {
if (v === '0') {
return 'Shared'
}
return `${v} vCPU`
}
/**
* @param {string} v
* @returns {string}
*/
export function cpuLimited (v) {
if (v === '0' || !v) {
return 'Cluster Default'
}
return `${v} vCPU`
}
/**
* @param {string} v
* @returns {string}
*/
export function memory (v) {
if (v === '0') {
return 'Shared'
}
const m = v.match(/^(\d+)(\w+)$/) ?? []
if (m.length !== 3) {
return v
}
return `${m[1]} ${m[2]}B`
}
/**
* @param {number} v
* @returns {string}
*/
export function storage (v) {
return (v / 1024 / 1024 / 1024).toLocaleString(undefined, { maximumFractionDigits: 2 }) + ' GiB'
}
/**
* @param {string} v
* @returns {string}
*/
export function datetime (v) {
if (!v) {
return ''
}
return dayjs(v).format('YYYY-MM-DD HH:mm:ss')
}
/**
* @param {string} v
* @returns {number}
*/
export function unixDatetime (v) {
if (!v) {
return 0
}
return dayjs(v).unix()
}
/**
* @param {string} project
* @param {string} name
* @param {string} gsa
* @param {string} locationProject
* @returns {string}
*/
export function gsaBinding (project, name, gsa, locationProject) {
const namespace = 'deploys'
const matched = gsa.match(/^.*@([^.]+)\.iam.gserviceaccount.com$/) ?? []
const googleProject = matched.length > 1 ? `\n --project ${matched[1]} \\` : ''
return `gcloud iam service-accounts add-iam-policy-binding \\
--role roles/iam.workloadIdentityUser \\
--member "serviceAccount:${locationProject}.svc.id.goog[${namespace}/${name}-${project}]" \\${googleProject}
${gsa}`
}
/**
* @param {Api.DeploymentType} t
* @returns {string}
*/
export function deploymentType (t) {
return {
WebService: 'Web Service',
TCPService: 'TCP Service',
InternalTCPService: 'Internal TCP Service',
Worker: 'Worker',
CronJob: 'Cron Job'
}[t] || t
}
/**
* @param {string} s
* @returns {string}
*/
export function shortDigest (s) {
return s.replace(/^sha256:/, '').slice(0, 12)
}