-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
59 lines (49 loc) · 1.93 KB
/
index.ts
File metadata and controls
59 lines (49 loc) · 1.93 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
import exitHook from 'exit-hook'
import _ from 'lodash'
import {
deletePod,
portForward,
runCloudSqlProxyPod,
waitForPodReady,
} from '../kubectl/pods.js'
import { Configuration, ConfigurationCreateAnswers } from '../types.js'
import { appendOrReplaceByKey, deleteByKey, findByKey } from '../util/array.js'
import { randomString } from '../util/string.js'
import { store } from './store.js'
const storeKey = 'configurations' as const
const searchKey = 'configurationName' as const
const excludeProperties = ['googleCloudProject', 'confirmation'] as const
export const configurationPath = store.path
export const getConfigurations = (): Configuration[] => store.get(storeKey)
export const getConfiguration = (name: string): Configuration | undefined => {
const configurations = getConfigurations()
return findByKey(configurations, searchKey, name)
}
export const saveConfiguration = (answers: ConfigurationCreateAnswers): void => {
const configuration = _.omit(answers, excludeProperties)
const configurations = store.get(storeKey)
appendOrReplaceByKey(configurations, configuration, searchKey)
store.set(storeKey, configurations)
}
export const deleteConfiguration = (configuratioName: string): void => {
const configurations = store.get(storeKey)
deleteByKey(configurations, searchKey, configuratioName)
store.set(storeKey, configurations)
}
export const execConfiguration = (configuration: Configuration) => {
const pod = {
name: `sql-proxy-${configuration.configurationName}-${randomString()}`,
context: configuration.kubernetesContext,
namespace: configuration.kubernetesNamespace,
serviceAccount: configuration.kubernetesServiceAccount,
instance: configuration.googleCloudSqlInstance.connectionName,
localPort: configuration.localPort,
remotePort: configuration.googleCloudSqlInstance.port,
}
exitHook(() => {
deletePod(pod)
})
runCloudSqlProxyPod(pod)
waitForPodReady(pod)
portForward(pod)
}