Skip to content

Commit

Permalink
remove import assertion
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
Roy Razon committed May 28, 2024
1 parent d804e84 commit 5cc497d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/driver-kube-pod/src/driver/client/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export const bodyOrUndefined = async <T, Response extends { body: T } = { body:
export type HasMetadata = { metadata?: k8s.V1ObjectMeta }
export type HasKind = { kind?: string }
export type Package = { version: string; name: string }

export const defaultPackage: Package = { name: 'preevy', version: 'unknown' } as const
6 changes: 3 additions & 3 deletions packages/driver-kube-pod/src/driver/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
addAllTypesAnnotation,
readAllTypesAnnotation,
} from './metadata.js'
import { Package } from './common.js'
import { Package, defaultPackage } from './common.js'
import { logError } from './log-error.js'

const stringify = stringifyModule.default
Expand Down Expand Up @@ -186,7 +186,7 @@ export const kubeCreationClient = ({
namespace: string
profileId: string
template: Buffer | string | Promise<Buffer | string>
package: Package | Promise<Package>
package: Package | undefined | Promise<Package | undefined>
storageClass: string | undefined
storageSize: number
}) => {
Expand Down Expand Up @@ -278,7 +278,7 @@ export const kubeCreationClient = ({
})
),
strategy: serverSideApply
? applyStrategies.serverSideApply({ fieldManager: (await packageDetails).name })
? applyStrategies.serverSideApply({ fieldManager: (await packageDetails ?? defaultPackage).name })
: applyStrategies.clientSideApply,
})

Expand Down
6 changes: 3 additions & 3 deletions packages/driver-kube-pod/src/driver/client/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
},
) => (
Expand Down
13 changes: 11 additions & 2 deletions packages/driver-kube-pod/src/driver/creation-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -103,6 +104,14 @@ const machineCreationDriver = (

type FlagTypes = Omit<Interfaces.InferredFlags<typeof flags>, 'json'>

const tryReadPackage = async (): Promise<Package | undefined> => {
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
Expand All @@ -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'],
Expand Down
3 changes: 1 addition & 2 deletions packages/driver-kube-pod/src/static.ts
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 5cc497d

Please sign in to comment.