Skip to content

Commit 2874f3f

Browse files
refactor: added namespaced fluent interface implementation (#1299)
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
1 parent cc8eba4 commit 2874f3f

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

src/core/kube/k8_client.ts

+9-45
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {inject, injectable} from 'tsyringe-neo';
1717
import {patchInject} from './../container_helper.js';
1818
import {type K8} from './k8.js';
1919
import {type Namespaces} from './namespaces.js';
20-
import {NamespaceName} from './namespace_name.js';
20+
import {type NamespaceName} from './namespace_name.js';
2121
import {K8ClientClusters} from './k8_client/k8_client_clusters.js';
2222
import {type Clusters} from './clusters.js';
2323
import {type ConfigMaps} from './config_maps.js';
@@ -38,6 +38,7 @@ import {type Pvcs} from './pvcs.js';
3838
import {K8ClientPvcs} from './k8_client/k8_client_pvcs.js';
3939
import {type Leases} from './leases.js';
4040
import {K8ClientLeases} from './k8_client/k8_client_leases.js';
41+
import {K8ClientNamespaces} from './k8_client/k8_client_namespaces.js';
4142

4243
/**
4344
* A kubernetes API wrapper class providing custom functionalities required by solo
@@ -63,6 +64,7 @@ export class K8Client extends K8ClientBase implements K8 {
6364
private k8Contexts: Contexts;
6465
private k8Services: Services;
6566
private k8Pvcs: Pvcs;
67+
private k8Namespaces: Namespaces;
6668

6769
constructor(
6870
@inject(ConfigManager) private readonly configManager?: ConfigManager,
@@ -100,46 +102,27 @@ export class K8Client extends K8ClientBase implements K8 {
100102
this.k8Pods = new K8ClientPods(this.kubeClient, this.kubeConfig);
101103
this.k8Pvcs = new K8ClientPvcs(this.kubeClient);
102104
this.k8Leases = new K8ClientLeases(this.coordinationApiClient);
105+
this.k8Namespaces = new K8ClientNamespaces(this.kubeClient);
103106

104107
return this; // to enable chaining
105108
}
106109

107-
/**
108-
* Fluent accessor for reading and manipulating namespaces in the kubernetes cluster.
109-
* @returns an object instance providing namespace operations
110-
*/
111110
public namespaces(): Namespaces {
112-
return null;
111+
return this.k8Namespaces;
113112
}
114113

115-
/**
116-
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
117-
* @returns an object instance providing cluster operations
118-
*/
119114
public clusters(): Clusters {
120115
return this.k8Clusters;
121116
}
122117

123-
/**
124-
* Fluent accessor for reading and manipulating config maps in the kubernetes cluster.
125-
* @returns an object instance providing config map operations
126-
*/
127118
public configMaps(): ConfigMaps {
128119
return this.k8ConfigMaps;
129120
}
130121

131-
/**
132-
* Fluent accessor for reading and manipulating containers.
133-
* returns an object instance providing container operations
134-
*/
135122
public containers(): Containers {
136123
return this.k8Containers;
137124
}
138125

139-
/**
140-
* Fluent accessor for reading and manipulating contexts in the kubeconfig file.
141-
* @returns an object instance providing context operations
142-
*/
143126
public contexts(): Contexts {
144127
return this.k8Contexts;
145128
}
@@ -173,38 +156,19 @@ export class K8Client extends K8ClientBase implements K8 {
173156
}
174157

175158
public async createNamespace(namespace: NamespaceName) {
176-
const payload = {
177-
metadata: {
178-
name: namespace.name,
179-
},
180-
};
181-
182-
const resp = await this.kubeClient.createNamespace(payload);
183-
return resp.response.statusCode === StatusCodes.CREATED;
159+
return this.namespaces().create(namespace);
184160
}
185161

186162
public async deleteNamespace(namespace: NamespaceName) {
187-
const resp = await this.kubeClient.deleteNamespace(namespace.name);
188-
return resp.response.statusCode === StatusCodes.OK;
163+
return this.namespaces().delete(namespace);
189164
}
190165

191166
public async getNamespaces() {
192-
const resp = await this.kubeClient.listNamespace();
193-
if (resp.body && resp.body.items) {
194-
const namespaces: NamespaceName[] = [];
195-
resp.body.items.forEach(item => {
196-
namespaces.push(NamespaceName.of(item.metadata!.name));
197-
});
198-
199-
return namespaces;
200-
}
201-
202-
throw new SoloError('incorrect response received from kubernetes API. Unable to list namespaces');
167+
return this.namespaces().list();
203168
}
204169

205170
public async hasNamespace(namespace: NamespaceName) {
206-
const namespaces = await this.getNamespaces();
207-
return namespaces.some(namespaces => namespaces.equals(namespace));
171+
return this.namespaces().has(namespace);
208172
}
209173

210174
public async getPodByName(podRef: PodRef): Promise<k8s.V1Pod> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {type Namespaces} from '../namespaces.js';
5+
import {NamespaceName} from '../namespace_name.js';
6+
import {type CoreV1Api} from '@kubernetes/client-node';
7+
import {StatusCodes} from 'http-status-codes';
8+
import {SoloError} from '../../errors.js';
9+
10+
export class K8ClientNamespaces implements Namespaces {
11+
constructor(private readonly kubeClient: CoreV1Api) {}
12+
13+
public async create(namespace: NamespaceName): Promise<boolean> {
14+
const payload = {
15+
metadata: {
16+
name: namespace.name,
17+
},
18+
};
19+
20+
const resp = await this.kubeClient.createNamespace(payload);
21+
return resp.response.statusCode === StatusCodes.CREATED;
22+
}
23+
24+
public async delete(namespace: NamespaceName): Promise<boolean> {
25+
const resp = await this.kubeClient.deleteNamespace(namespace.name);
26+
return resp.response.statusCode === StatusCodes.OK;
27+
}
28+
29+
public async has(namespace: NamespaceName): Promise<boolean> {
30+
const namespaces = await this.list();
31+
return namespaces.some(namespaces => namespaces.equals(namespace));
32+
}
33+
34+
public async list(): Promise<NamespaceName[]> {
35+
const resp = await this.kubeClient.listNamespace();
36+
if (resp.body && resp.body.items) {
37+
const namespaces: NamespaceName[] = [];
38+
resp.body.items.forEach(item => {
39+
namespaces.push(NamespaceName.of(item.metadata!.name));
40+
});
41+
42+
return namespaces;
43+
}
44+
45+
throw new SoloError('incorrect response received from kubernetes API. Unable to list namespaces');
46+
}
47+
}

src/core/kube/namespaces.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
*/
44
import {type NamespaceName} from './namespace_name.js';
55

6-
/**
7-
* SPDX-License-Identifier: Apache-2.0
8-
*/
96
export interface Namespaces {
107
/**
118
* Create a new namespace
@@ -22,6 +19,7 @@ export interface Namespaces {
2219
/**
2320
* List all namespaces
2421
* @returns a list of namespace names
22+
* @throws SoloError if the response from the kubernetes API is incorrect
2523
*/
2624
list(): Promise<NamespaceName[]>; // TODO was getNamespaces
2725

0 commit comments

Comments
 (0)