From 5cc497ddcc3cf69730685086b4b93d431ac74e8c Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Tue, 28 May 2024 18:36:09 +0300 Subject: [PATCH] remove import assertion - remove single usage of import assertion (import ... with/assert) which was breaking compat with older node 18 versions (see https://github.com/nodejs/node/pull/52104\#issuecomment-2000337367) --- .../driver-kube-pod/src/driver/client/common.ts | 2 ++ packages/driver-kube-pod/src/driver/client/index.ts | 6 +++--- .../driver-kube-pod/src/driver/client/metadata.ts | 6 +++--- .../driver-kube-pod/src/driver/creation-driver.ts | 13 +++++++++++-- packages/driver-kube-pod/src/static.ts | 3 +-- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/driver-kube-pod/src/driver/client/common.ts b/packages/driver-kube-pod/src/driver/client/common.ts index ab5602bd..ef0c28e5 100644 --- a/packages/driver-kube-pod/src/driver/client/common.ts +++ b/packages/driver-kube-pod/src/driver/client/common.ts @@ -15,3 +15,5 @@ export const bodyOrUndefined = async - package: Package | Promise + package: Package | undefined | Promise storageClass: string | undefined storageSize: number }) => { @@ -278,7 +278,7 @@ export const kubeCreationClient = ({ }) ), strategy: serverSideApply - ? applyStrategies.serverSideApply({ fieldManager: (await packageDetails).name }) + ? applyStrategies.serverSideApply({ fieldManager: (await packageDetails ?? defaultPackage).name }) : applyStrategies.clientSideApply, }) diff --git a/packages/driver-kube-pod/src/driver/client/metadata.ts b/packages/driver-kube-pod/src/driver/client/metadata.ts index b5ff3069..e453e124 100644 --- a/packages/driver-kube-pod/src/driver/client/metadata.ts +++ b/packages/driver-kube-pod/src/driver/client/metadata.ts @@ -3,7 +3,7 @@ import { ensureDefined, extractDefined, randomString, truncatePrefix } from '@pr import { pick } from 'lodash-es' import { tryParseJson } from '@preevy/common' import { sanitizeLabel, sanitizeLabels } from './labels.js' -import { HasMetadata, Package } from './common.js' +import { HasMetadata, Package, defaultPackage } from './common.js' import { KubernetesType } from './dynamic/index.js' export const MAX_LABEL_LENGTH = 63 @@ -70,12 +70,12 @@ export const addAllTypesAnnotation = ( } export const addEnvMetadata = ( - { profileId, envId, createdAt, instance, package: { name, version }, templateHash }: { + { profileId, envId, createdAt, instance, package: { name, version } = defaultPackage, templateHash }: { profileId: string envId: string createdAt: Date instance: string - package: Package + package?: Package templateHash: string }, ) => ( diff --git a/packages/driver-kube-pod/src/driver/creation-driver.ts b/packages/driver-kube-pod/src/driver/creation-driver.ts index 811d1e71..efabe705 100644 --- a/packages/driver-kube-pod/src/driver/creation-driver.ts +++ b/packages/driver-kube-pod/src/driver/creation-driver.ts @@ -8,7 +8,8 @@ import { inspect } from 'util' import { DeploymentMachine, ResourceType, StatefulSetMachine, k8sObjectToMachine } from './common.js' import { clientFromConfiguration, listMachines, machineConnection, flags as machineDriverFlags, parseRfc1123Flag } from './driver.js' import { Client, CreationClient, kubeCreationClient, loadKubeConfig } from './client/index.js' -import { DEFAULT_TEMPLATE, packageJson } from '../static.js' +import { DEFAULT_TEMPLATE, packageJsonPath } from '../static.js' +import { Package } from './client/common.js' export const flags = { ...machineDriverFlags, @@ -103,6 +104,14 @@ const machineCreationDriver = ( type FlagTypes = Omit, 'json'> +const tryReadPackage = async (): Promise => { + try { + return JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf-8')) + } catch (e) { + return undefined + } +} + const creationClientFromConfiguration = ({ flags: f, profileId, log, kc }: { flags: FlagTypes profileId: string @@ -113,7 +122,7 @@ const creationClientFromConfiguration = ({ flags: f, profileId, log, kc }: { namespace: f.namespace, kc, profileId, - package: packageJson, + package: tryReadPackage(), template: fs.readFileSync(f.template || DEFAULT_TEMPLATE, 'utf-8'), storageClass: f['storage-class'], storageSize: f['storage-size'], diff --git a/packages/driver-kube-pod/src/static.ts b/packages/driver-kube-pod/src/static.ts index bb4bb2f3..034ba4ea 100644 --- a/packages/driver-kube-pod/src/static.ts +++ b/packages/driver-kube-pod/src/static.ts @@ -1,10 +1,9 @@ import path from 'path' import url from 'url' -import packageJsonImport from '../package.json' with { type: 'json' } // eslint-disable-next-line no-underscore-dangle const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) export const DIR = path.join(__dirname, '../static') export const DEFAULT_TEMPLATE = path.join(DIR, './default-template.yaml.njk') -export const packageJson = packageJsonImport +export const packageJsonPath = path.join(__dirname, '../package.json')