@@ -3,70 +3,22 @@ import { omit, kebabCase } from 'lodash'
33import {
44 deletePod ,
55 portForward ,
6- runCloudSqlProxyPod ,
7- runAlloyDbProxyPod ,
6+ runProxyPod ,
87 waitForPodReady ,
98} from '../kubectl/pods'
109import { Configuration , ConfigurationCreateAnswers } from '../types'
1110import { appendOrReplaceByKey , deleteByKey , findByKey } from '../util/array'
1211import { randomString } from '../util/string'
13- import { store , CURRENT_VERSION } from './store'
12+ import { store } from './store'
13+ import { configurationsKey } from './constants'
1414
15- const storeKey = 'configurations' as const
1615const searchKey = 'configurationName' as const
1716const excludeProperties = [ 'googleCloudProject' , 'confirmation' ] as const
1817
1918export const configurationPath = store . path
2019
21- type LegacyConfiguration = Omit < Configuration , 'databaseType' | 'databaseInstance' > & {
22- googleCloudSqlInstance : {
23- connectionName : string
24- port : number
25- }
26- }
27-
28- const isLegacyConfiguration = ( config : Configuration | LegacyConfiguration ) : config is LegacyConfiguration => {
29- return 'googleCloudSqlInstance' in config && ! ( 'databaseType' in config )
30- }
31-
32- const migrateLegacyConfiguration = ( legacy : LegacyConfiguration ) : Configuration => {
33- return {
34- configurationName : legacy . configurationName ,
35- databaseType : 'cloudsql' ,
36- databaseInstance : {
37- connectionName : legacy . googleCloudSqlInstance . connectionName ,
38- port : legacy . googleCloudSqlInstance . port ,
39- } ,
40- kubernetesContext : legacy . kubernetesContext ,
41- kubernetesNamespace : legacy . kubernetesNamespace ,
42- kubernetesServiceAccount : legacy . kubernetesServiceAccount ,
43- localPort : legacy . localPort ,
44- }
45- }
46-
47- const migrateConfigurationsIfNeeded = ( ) : void => {
48- const currentVersion = store . get ( 'version' )
49- const configurations = store . get ( storeKey ) as ( Configuration | LegacyConfiguration ) [ ]
50-
51- // Check if migration is needed (no version or configurations in old format)
52- const needsMigration = ! currentVersion || configurations . some ( isLegacyConfiguration )
53-
54- if ( needsMigration ) {
55- const migratedConfigurations = configurations . map ( ( config ) => {
56- if ( isLegacyConfiguration ( config ) ) {
57- return migrateLegacyConfiguration ( config )
58- }
59- return config
60- } )
61-
62- store . set ( storeKey , migratedConfigurations )
63- store . set ( 'version' , CURRENT_VERSION )
64- }
65- }
66-
6720export const getConfigurations = ( ) : Configuration [ ] => {
68- migrateConfigurationsIfNeeded ( )
69- return store . get ( storeKey )
21+ return store . get ( configurationsKey )
7022}
7123
7224export const getConfiguration = ( name : string ) : Configuration | undefined => {
@@ -77,24 +29,20 @@ export const getConfiguration = (name: string): Configuration | undefined => {
7729export const saveConfiguration = ( answers : ConfigurationCreateAnswers ) : void => {
7830 const configuration = omit ( answers , excludeProperties )
7931
80- const configurations = store . get ( storeKey )
32+ const configurations = store . get ( configurationsKey )
8133 appendOrReplaceByKey ( configurations , configuration , searchKey )
82- store . set ( storeKey , configurations )
83-
84- if ( ! store . get ( 'version' ) ) {
85- store . set ( 'version' , CURRENT_VERSION )
86- }
34+ store . set ( configurationsKey , configurations )
8735}
8836
8937export const deleteConfiguration = ( configuratioName : string ) : void => {
90- const configurations = store . get ( storeKey )
38+ const configurations = store . get ( configurationsKey )
9139 deleteByKey ( configurations , searchKey , configuratioName )
92- store . set ( storeKey , configurations )
40+ store . set ( configurationsKey , configurations )
9341}
9442
9543export const execConfiguration = ( configuration : Configuration ) => {
9644 const pod = {
97- name : `${ configuration . databaseType === 'alloydb' ? 'alloydb' : 'sql' } -proxy-${ kebabCase ( configuration . configurationName ) } -${ randomString ( ) } ` ,
45+ name : `${ configuration . databaseType } -proxy-${ kebabCase ( configuration . configurationName ) } -${ randomString ( ) } ` ,
9846 context : configuration . kubernetesContext ,
9947 namespace : configuration . kubernetesNamespace ,
10048 serviceAccount : configuration . kubernetesServiceAccount ,
@@ -108,12 +56,7 @@ export const execConfiguration = (configuration: Configuration) => {
10856 deletePod ( pod )
10957 } )
11058
111- if ( configuration . databaseType === 'alloydb' ) {
112- runAlloyDbProxyPod ( pod )
113- }
114- else {
115- runCloudSqlProxyPod ( pod )
116- }
59+ runProxyPod ( pod )
11760
11861 waitForPodReady ( pod )
11962 portForward ( pod )
0 commit comments