-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrun.ts
195 lines (173 loc) · 5.54 KB
/
run.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as core from '@actions/core'
import {
CoreV1ApiCreateNamespacedSecretRequest,
CoreV1ApiDeleteNamespacedSecretRequest,
CoreV1Api,
KubeConfig,
V1ObjectMeta,
V1Secret
} from '@kubernetes/client-node'
export function checkClusterContext() {
if (!process.env['KUBECONFIG']) {
throw new Error(
'Cluster context not set. Use k8s-set-context/aks-set-context action to set cluster context'
)
}
}
export type DockerConfigJSON = {
auths: {
[key: string]: {
username: string
password: string
email?: string
auth: string
}
}
}
export function buildContainerRegistryDockerConfigJSON(
registryUrl: string,
registryUserName: string,
registryPassword: string,
registryEmail
): DockerConfigJSON {
const authString = Buffer.from(
`${registryUserName}:${registryPassword}`
).toString('base64')
const dockerConfigJson: DockerConfigJSON = {
auths: {
[registryUrl]: {
username: registryUserName,
password: registryPassword,
auth: authString
}
}
}
if (registryEmail) {
dockerConfigJson.auths[registryUrl].email = registryEmail
}
return dockerConfigJson //Buffer.from(JSON.stringify(dockerConfigJson)).toString('base64');
}
export async function buildSecret(
secretName: string,
namespace: string
): Promise<V1Secret> {
// The secret type for the new secret
const secretType: string = core.getInput('secret-type')
const metaData: V1ObjectMeta = {
name: secretName,
namespace: namespace
}
const containerRegistryURL = core.getInput('container-registry-url')
const containerRegistryUserName = core.getInput(
'container-registry-username'
)
const containerRegistryPassword = core.getInput(
'container-registry-password'
)
const containerRegistryEmail = core.getInput('container-registry-email')
// Check if any container registry credentials are provided
if (
containerRegistryURL ||
containerRegistryUserName ||
containerRegistryPassword ||
containerRegistryEmail
) {
if (!containerRegistryURL) {
core.setFailed(
'container-registry-url is required when container-registry-username or container-registry-password is provided'
)
}
if (!containerRegistryUserName) {
core.setFailed(
'container-registry-username is required when container-registry-url or container-registry-password is provided'
)
}
if (!containerRegistryPassword) {
core.setFailed(
'container-registry-password is required when container-registry-url or container-registry-username or container-registry-email is provided'
)
}
const dockerConfigJSON = buildContainerRegistryDockerConfigJSON(
containerRegistryURL,
containerRegistryUserName,
containerRegistryPassword,
containerRegistryEmail
)
const dockerConfigJSONString = JSON.stringify(dockerConfigJSON)
const dockerConfigBase64 = Buffer.from(dockerConfigJSONString).toString(
'base64'
)
const data = {
'.dockerconfigjson': dockerConfigBase64
}
return {
apiVersion: 'v1',
kind: 'Secret',
metadata: metaData,
type: secretType,
data: data
}
}
// The serialized form of the secret data is a base64 encoded string
let data: {[key: string]: string} = {}
if (core.getInput('data')) {
core.debug(`loading 'data' field`)
data = JSON.parse(core.getInput('data'))
}
// The plaintext form of the secret data
let stringData: {[key: string]: string} = {}
if (core.getInput('string-data')) {
core.debug(`loading 'string-data' field`)
stringData = JSON.parse(core.getInput('string-data'))
}
// Create secret object for passing to the api
core.debug(`creating V1Secret`)
const secret: V1Secret = {
apiVersion: 'v1',
type: secretType,
data: data,
stringData: stringData,
metadata: metaData
}
return secret
}
export async function run() {
checkClusterContext()
// Create kubeconfig and load values from 'KUBECONFIG' environment variable
const kc: KubeConfig = new KubeConfig()
core.debug(`loading kubeconfig from defaults...`)
kc.loadFromDefault()
const api: CoreV1Api = kc.makeApiClient(CoreV1Api)
// The name of the new secret
const secretName: string = core.getInput('secret-name', {required: true})
// The namespace in which to place the secret
const namespace: string = core.getInput('namespace') || 'default'
// Delete if exists
let deleteSecretResponse
try {
let deleteRequest: CoreV1ApiDeleteNamespacedSecretRequest = {
name: secretName,
namespace: namespace
}
deleteSecretResponse = await api.deleteNamespacedSecret(deleteRequest)
} catch ({response}) {
core.warning(
`Failed to delete secret with statusCode: ${response?.statusCode}`
)
core.warning(response?.body?.metadata)
}
core.info('Deleting secret:')
core.info(JSON.stringify(deleteSecretResponse?.response?.body, undefined, 2))
const secret = await buildSecret(secretName, namespace)
core.info('Creating secret')
try {
let secretRequest: CoreV1ApiCreateNamespacedSecretRequest = {
namespace: namespace,
body: secret
}
await api.createNamespacedSecret(secretRequest)
} catch (err) {
core.info(JSON.stringify(err))
core.setFailed(err.message)
}
}