Skip to content

Commit 95c71f0

Browse files
authored
feat: add clusters fluent interface implementation (#1291)
Signed-off-by: Nathan Klick <[email protected]>
1 parent 33dc849 commit 95c71f0

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

src/core/kube/clusters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export interface Clusters {
66
* Returns a list of clusters that are in the kubeconfig file
77
* @returns a list of cluster names
88
*/
9-
list(): Promise<string[]>; // TODO was getClusters
9+
list(): string[]; // TODO was getClusters
1010

1111
/**
1212
* Returns the current cluster name as defined in the kubeconfig file
1313
* @returns the current cluster name
1414
*/
15-
readCurrent(): Promise<string>; // TODO was getCurrentClusterName
15+
readCurrent(): string; // TODO was getCurrentClusterName
1616
}

src/core/kube/k8.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ import {type TDirectoryData} from './t_directory_data.js';
88
import {type V1Lease} from '@kubernetes/client-node';
99
import {type Namespaces} from './namespaces.js';
1010
import {type NamespaceName} from './namespace_name.js';
11+
import {type Clusters} from './clusters.js';
1112

1213
export interface K8 {
14+
/**
15+
* Fluent accessor for reading and manipulating namespaces.
16+
* @returns an object instance providing namespace operations
17+
*/
1318
namespaces(): Namespaces;
1419

20+
/**
21+
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
22+
* returns an object instance providing cluster operations
23+
*/
24+
clusters(): Clusters;
25+
1526
/**
1627
* Create a new namespace
1728
* @param namespace - the namespace to create

src/core/kube/k8_client.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {type K8} from './k8.js';
2929
import {type TDirectoryData} from './t_directory_data.js';
3030
import {type Namespaces} from './namespaces.js';
3131
import {NamespaceName} from './namespace_name.js';
32+
import K8ClientClusters from './k8_client/k8_client_clusters.js';
33+
import {type Clusters} from './clusters.js';
3234

3335
/**
3436
* A kubernetes API wrapper class providing custom functionalities required by solo
@@ -45,11 +47,14 @@ export class K8Client implements K8 {
4547
constants.POD_CONDITION_READY,
4648
constants.POD_CONDITION_STATUS_TRUE,
4749
);
50+
4851
private kubeConfig!: k8s.KubeConfig;
4952
kubeClient!: k8s.CoreV1Api;
5053
private coordinationApiClient: k8s.CoordinationV1Api;
5154
private networkingApi: k8s.NetworkingV1Api;
5255

56+
private k8Clusters: K8ClientClusters;
57+
5358
constructor(
5459
@inject(ConfigManager) private readonly configManager?: ConfigManager,
5560
@inject(SoloLogger) private readonly logger?: SoloLogger,
@@ -77,6 +82,8 @@ export class K8Client implements K8 {
7782
this.networkingApi = this.kubeConfig.makeApiClient(k8s.NetworkingV1Api);
7883
this.coordinationApiClient = this.kubeConfig.makeApiClient(k8s.CoordinationV1Api);
7984

85+
this.k8Clusters = new K8ClientClusters(this.kubeConfig);
86+
8087
return this; // to enable chaining
8188
}
8289

@@ -85,6 +92,14 @@ export class K8Client implements K8 {
8592
return null;
8693
}
8794

95+
/**
96+
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
97+
* returns an object instance providing cluster operations
98+
*/
99+
public clusters(): Clusters {
100+
return this.k8Clusters;
101+
}
102+
88103
/**
89104
* Apply filters to metadata
90105
* @param items - list of items
@@ -246,13 +261,8 @@ export class K8Client implements K8 {
246261
return this.filterItem(resp.body.items, {name});
247262
}
248263

249-
public getClusters() {
250-
const clusters: string[] = [];
251-
for (const cluster of this.kubeConfig.getClusters()) {
252-
clusters.push(cluster.name);
253-
}
254-
255-
return clusters;
264+
public getClusters(): string[] {
265+
return this.clusters().list();
256266
}
257267

258268
public getContextNames(): string[] {
@@ -1590,9 +1600,7 @@ export class K8Client implements K8 {
15901600
}
15911601

15921602
public getCurrentClusterName(): string {
1593-
const currentCluster = this.kubeConfig.getCurrentCluster();
1594-
if (!currentCluster) return '';
1595-
return currentCluster.name;
1603+
return this.clusters().readCurrent();
15961604
}
15971605

15981606
public async listSvcs(namespace: NamespaceName, labels: string[]): Promise<k8s.V1Service[]> {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {type Clusters} from '../clusters.js';
5+
import {type KubeConfig} from '@kubernetes/client-node';
6+
import {IllegalArgumentError} from '../../errors.js';
7+
8+
export default class K8ClientClusters implements Clusters {
9+
public constructor(private readonly kubeConfig: KubeConfig) {
10+
if (!kubeConfig) {
11+
throw new IllegalArgumentError('kubeConfig must not be null or undefined');
12+
}
13+
}
14+
15+
list(): string[] {
16+
const clusters: string[] = [];
17+
for (const cluster of this.kubeConfig.getClusters()) {
18+
clusters.push(cluster.name);
19+
}
20+
21+
return clusters;
22+
}
23+
24+
readCurrent(): string {
25+
const currentCluster = this.kubeConfig.getCurrentCluster();
26+
return !currentCluster ? '' : currentCluster.name;
27+
}
28+
}

0 commit comments

Comments
 (0)