Skip to content

Commit 87e660d

Browse files
refactor: create k8.Container interface, move container methods from Pod to Container, implement K8ClientContainer and implement across codebase (#1294)
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
1 parent 88d5b1e commit 87e660d

File tree

7 files changed

+673
-578
lines changed

7 files changed

+673
-578
lines changed

src/core/kube/container.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
import {type TarCreateFilter} from '../../types/aliases.js';
6+
import {type TDirectoryData} from './t_directory_data.js';
7+
8+
export interface Container {
9+
/**
10+
* Copy a file from a container
11+
*
12+
* It overwrites any existing file at the destination directory
13+
* @param srcPath - the path to the file to copy
14+
* @param destDir - the destination directory
15+
*/
16+
copyFrom(srcPath: string, destDir: string): Promise<unknown>;
17+
18+
/**
19+
* Copy a file into a container
20+
*
21+
* It overwrites any existing file inside the container at the destination directory
22+
* @param srcPath - the path of the local file to copy
23+
* @param destDir - the remote destination directory
24+
* @param [filter] - the filter to pass to tar to keep or skip files or directories
25+
* @returns a Promise that performs the copy operation
26+
*/
27+
copyTo(srcPath: string, destDir: string, filter: TarCreateFilter | undefined): Promise<boolean>;
28+
29+
/**
30+
* Invoke sh command within a container and return the console output as string
31+
* @param command - sh commands as an array to be run within the containerName (e.g 'ls -la /opt/hgcapp')
32+
* @returns console output as string
33+
*/
34+
execContainer(command: string | string[]): Promise<string>;
35+
36+
/**
37+
* Check if a directory exists in the specified container
38+
* @param destPath - the path to the directory inside the container
39+
*/
40+
hasDir(destPath: string): Promise<boolean>;
41+
42+
/**
43+
* Check if a file exists in the specified container
44+
* @param destPath - the remote path to the file
45+
* @param [filters] - optional filters to apply to the tar stream
46+
*/
47+
hasFile(destPath: string, filters: object): Promise<boolean>;
48+
49+
/**
50+
* List files and directories in a container
51+
*
52+
* It runs ls -la on the specified path and returns a list of object containing the entries.
53+
* For example:
54+
* [{
55+
* directory: false,
56+
* owner: hedera,
57+
* group: hedera,
58+
* size: 121,
59+
* modifiedAt: Jan 15 13:50
60+
* name: config.txt
61+
* }]
62+
* @param destPath - the remote path to the directory
63+
* @returns a promise that returns array of directory entries, custom object
64+
*/
65+
listDir(destPath: string): Promise<any[] | TDirectoryData[]>;
66+
67+
/**
68+
* Make a directory in the specified container
69+
* @param destPath - the remote path to the directory
70+
*/
71+
mkdir(destPath: string): Promise<string>;
72+
}

src/core/kube/containers.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {type ContainerRef} from './container_ref.js';
5+
import {type Container} from './container.js';
6+
7+
export interface Containers {
8+
/**
9+
* Get a container by reference for running operations against
10+
* @param containerRef - the reference to the container
11+
* @returns a container object
12+
*/
13+
readByRef(containerRef: ContainerRef): Container;
14+
}

src/core/kube/k8.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {type TDirectoryData} from './t_directory_data.js';
99
import {type V1Lease} from '@kubernetes/client-node';
1010
import {type Namespaces} from './namespaces.js';
1111
import {type NamespaceName} from './namespace_name.js';
12+
import {type Containers} from './containers.js';
1213
import {type Clusters} from './clusters.js';
1314
import {type ConfigMaps} from './config_maps.js';
14-
import {type ContainerName} from './container_name.js';
1515
import {type ContainerRef} from './container_ref.js';
1616

1717
export interface K8 {
@@ -21,6 +21,12 @@ export interface K8 {
2121
*/
2222
namespaces(): Namespaces;
2323

24+
/**
25+
* Fluent accessor for reading and manipulating containers.
26+
* returns an object instance providing container operations
27+
*/
28+
containers(): Containers;
29+
2430
/**
2531
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
2632
* @returns an object instance providing cluster operations

0 commit comments

Comments
 (0)