Skip to content

Commit

Permalink
fix ls showing other resources
Browse files Browse the repository at this point in the history
fixed #159
  • Loading branch information
Roy Razon committed Aug 6, 2023
1 parent abe1251 commit 4260796
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 107 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/commands/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const ls = async ({
}: {
machineDriver: MachineDriver
log: Logger
}) => machineDriver.listDeletableResources()
}) => machineDriver.listMachines()

export default ls
1 change: 1 addition & 0 deletions packages/core/src/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type MachineDriver<
stdio: PartialStdioOptions,
) => Promise<{ code: number } | { signal: string }>

listMachines: () => AsyncIterableIterator<Machine | PartialMachine>
listDeletableResources: () => AsyncIterableIterator<Resource<ResourceType>>
deleteResources: (wait: boolean, ...resource: Resource<string>[]) => Promise<void>
machineStatusCommand: (machine: MachineBase) => Promise<MachineStatusCommand | undefined>
Expand Down
43 changes: 24 additions & 19 deletions packages/driver-azure/src/driver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ const machineFromVm = (

const machineDriver = (
{ store, client: cl }: DriverContext,
): MachineDriver<SshMachine, ResourceType> => ({
customizationScripts: CUSTOMIZE_BARE_MACHINE,
friendlyName: 'Microsoft Azure',
getMachine: async ({ envId }) => await cl.getInstance(envId).then(vm => machineFromVm(vm)),

listDeletableResources: () => asyncMap(
): MachineDriver<SshMachine, ResourceType> => {
const listMachines = () => asyncMap(
rg => cl.getInstanceByRg(rg.name as string).then(vm => {
if (vm) {
return machineFromVm(vm)
Expand All @@ -110,23 +106,32 @@ const machineDriver = (
}
}),
cl.listResourceGroups()
),
)

deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === machineResourceType) {
return cl.deleteResourcesResourceGroup(providerId, wait)
}
throw new Error(`Unknown resource type "${type}"`)
}))
},
return ({
customizationScripts: CUSTOMIZE_BARE_MACHINE,
friendlyName: 'Microsoft Azure',
getMachine: async ({ envId }) => await cl.getInstance(envId).then(vm => machineFromVm(vm)),

listMachines,
listDeletableResources: listMachines,

deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === machineResourceType) {
return cl.deleteResourcesResourceGroup(providerId, wait)
}
throw new Error(`Unknown resource type "${type}"`)
}))
},

resourcePlurals: {},
resourcePlurals: {},

...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }),
...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }),

machineStatusCommand: async () => machineStatusNodeExporterCommand,
})
machineStatusCommand: async () => machineStatusNodeExporterCommand,
})
}

const flags = {
region: Flags.string({
Expand Down
46 changes: 24 additions & 22 deletions packages/driver-gce/src/driver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,34 @@ const machineFromInstance = (
}
}

const machineDriver = (
{ store, client }: DriverContext,
): MachineDriver<SshMachine, ResourceType> => ({
friendlyName: 'Google Cloud',

getMachine: async ({ envId }) => {
const instance = await client.findInstance(envId)
return instance && machineFromInstance(instance)
},
const machineDriver = ({ store, client }: DriverContext): MachineDriver<SshMachine, ResourceType> => {
const listMachines = () => asyncMap(machineFromInstance, client.listInstances())

listDeletableResources: () => asyncMap(machineFromInstance, client.listInstances()),
deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === 'machine') {
return client.deleteInstance(providerId, wait)
}
throw new Error(`Unknown resource type: "${type}"`)
}))
},
return ({
friendlyName: 'Google Cloud',

resourcePlurals: {},
getMachine: async ({ envId }) => {
const instance = await client.findInstance(envId)
return instance && machineFromInstance(instance)
},

...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }),
listMachines,
listDeletableResources: listMachines,

machineStatusCommand: async () => machineStatusNodeExporterCommand,
})
deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === 'machine') {
return client.deleteInstance(providerId, wait)
}
throw new Error(`Unknown resource type: "${type}"`)
}))
},

resourcePlurals: {},
...sshDriver({ getSshKey: () => getStoredSshKey(store, SSH_KEYPAIR_ALIAS) }),
machineStatusCommand: async () => machineStatusNodeExporterCommand,
})
}

const flags = {
'project-id': Flags.string({
Expand Down
127 changes: 66 additions & 61 deletions packages/driver-kube-pod/src/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,72 +66,77 @@ export const machineConnection = async (

const machineDriver = (
{ client, log }: DriverContext,
): MachineDriver<DeploymentMachine, ResourceType> => ({
friendlyName: 'Kubernetes single Pod',
): MachineDriver<DeploymentMachine, ResourceType> => {
const listMachines = () => asyncMap(machineFromDeployment, client.listProfileDeployments())

getMachine: async ({ envId }) => {
const deployment = await client.findMostRecentDeployment({ envId, deleted: false })
return deployment && machineFromDeployment(deployment)
},
return ({
friendlyName: 'Kubernetes single Pod',

getMachine: async ({ envId }) => {
const deployment = await client.findMostRecentDeployment({ envId, deleted: false })
return deployment && machineFromDeployment(deployment)
},

listDeletableResources: () => asyncMap(machineFromDeployment, client.listProfileDeployments()),
listMachines,
listDeletableResources: listMachines,

deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === 'machine') {
return client.deleteEnv(providerId, { wait })
}
throw new Error(`Unknown resource type: "${type}"`)
}))
},

deleteResources: async (wait, ...resources) => {
await Promise.all(resources.map(({ type, providerId }) => {
if (type === 'machine') {
return client.deleteEnv(providerId, { wait })
resourcePlurals: {},

spawnRemoteCommand: async (machine, command, stdio) => {
const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment)
const { stdin, stdout, stderr } = expandStdioOptions(stdio)
const opts = {
pod: extractName(pod),
container: pod.spec?.containers[0]?.name as string,
command: command.length > 0 ? command : ['sh'],
tty: [stdin, stdout, stderr].every(isTTY),
stdin,
stdout,
stderr,
}
throw new Error(`Unknown resource type: "${type}"`)
}))
},

resourcePlurals: {},

spawnRemoteCommand: async (machine, command, stdio) => {
const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment)
const { stdin, stdout, stderr } = expandStdioOptions(stdio)
const opts = {
pod: extractName(pod),
container: pod.spec?.containers[0]?.name as string,
command: command.length > 0 ? command : ['sh'],
tty: [stdin, stdout, stderr].every(isTTY),
stdin,
stdout,
stderr,
}
return await client.exec(opts)
},

connect: machine => machineConnection(client, machine as DeploymentMachine, log),

machineStatusCommand: async machine => {
const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment)
const apiServiceAddress = await client.apiServiceClusterAddress()
if (!apiServiceAddress) {
log.warn('API service not found for cluster')
return undefined
}
const [apiServiceHost, apiServicePort] = apiServiceAddress

return ({
contentType: 'application/vnd.kubectl-top-pod-containers',
recipe: {
type: 'docker',
command: ['top', 'pod', '--containers', '--no-headers', extractName(pod)],
image: 'rancher/kubectl:v1.26.7',
network: 'host',
tty: false,
env: {
KUBERNETES_SERVICE_HOST: apiServiceHost,
KUBERNETES_SERVICE_PORT: apiServicePort.toString(),
return await client.exec(opts)
},

connect: machine => machineConnection(client, machine as DeploymentMachine, log),

machineStatusCommand: async machine => {
const pod = await client.findReadyPodForDeployment((machine as DeploymentMachine).deployment)
const apiServiceAddress = await client.apiServiceClusterAddress()
if (!apiServiceAddress) {
log.warn('API service not found for cluster')
return undefined
}
const [apiServiceHost, apiServicePort] = apiServiceAddress

return ({
contentType: 'application/vnd.kubectl-top-pod-containers',
recipe: {
type: 'docker',
command: ['top', 'pod', '--containers', '--no-headers', extractName(pod)],
image: 'rancher/kubectl:v1.26.7',
network: 'host',
tty: false,
env: {
KUBERNETES_SERVICE_HOST: apiServiceHost,
KUBERNETES_SERVICE_PORT: apiServicePort.toString(),
},
bindMounts: [
'/var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount',
],
},
bindMounts: [
'/var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount',
],
},
})
},
})
})
},
})
}

export const flags = {
namespace: Flags.string({
Expand Down
8 changes: 4 additions & 4 deletions packages/driver-lightsail/src/driver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const machineDriver = ({
}: DriverContext): MachineDriver<SshMachine, ResourceType> => {
const keyAlias = region

const listMachines = () => asyncMap(machineFromInstance, client.listInstances())

return {
friendlyName: 'AWS Lightsail',
customizationScripts: CUSTOMIZE_BARE_MACHINE,
Expand All @@ -57,11 +59,9 @@ const machineDriver = ({
return instance && machineFromInstance(instance)
},

listMachines,
listDeletableResources: () => {
const machines = asyncMap(
machineFromInstance,
client.listInstances(),
)
const machines = listMachines()

const snapshots = asyncMap(
({ name }) => ({ type: 'snapshot' as ResourceType, providerId: name as string }),
Expand Down

0 comments on commit 4260796

Please sign in to comment.