Skip to content

Commit

Permalink
feat: add clusters fluent interface implementation (#1291)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Klick <[email protected]>
  • Loading branch information
nathanklick authored Feb 5, 2025
1 parent 33dc849 commit 95c71f0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/core/kube/clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export interface Clusters {
* Returns a list of clusters that are in the kubeconfig file
* @returns a list of cluster names
*/
list(): Promise<string[]>; // TODO was getClusters
list(): string[]; // TODO was getClusters

/**
* Returns the current cluster name as defined in the kubeconfig file
* @returns the current cluster name
*/
readCurrent(): Promise<string>; // TODO was getCurrentClusterName
readCurrent(): string; // TODO was getCurrentClusterName
}
11 changes: 11 additions & 0 deletions src/core/kube/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ import {type TDirectoryData} from './t_directory_data.js';
import {type V1Lease} from '@kubernetes/client-node';
import {type Namespaces} from './namespaces.js';
import {type NamespaceName} from './namespace_name.js';
import {type Clusters} from './clusters.js';

export interface K8 {
/**
* Fluent accessor for reading and manipulating namespaces.
* @returns an object instance providing namespace operations
*/
namespaces(): Namespaces;

/**
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
* returns an object instance providing cluster operations
*/
clusters(): Clusters;

/**
* Create a new namespace
* @param namespace - the namespace to create
Expand Down
28 changes: 18 additions & 10 deletions src/core/kube/k8_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {type K8} from './k8.js';
import {type TDirectoryData} from './t_directory_data.js';
import {type Namespaces} from './namespaces.js';
import {NamespaceName} from './namespace_name.js';
import K8ClientClusters from './k8_client/k8_client_clusters.js';
import {type Clusters} from './clusters.js';

/**
* A kubernetes API wrapper class providing custom functionalities required by solo
Expand All @@ -45,11 +47,14 @@ export class K8Client implements K8 {
constants.POD_CONDITION_READY,
constants.POD_CONDITION_STATUS_TRUE,
);

private kubeConfig!: k8s.KubeConfig;
kubeClient!: k8s.CoreV1Api;
private coordinationApiClient: k8s.CoordinationV1Api;
private networkingApi: k8s.NetworkingV1Api;

private k8Clusters: K8ClientClusters;

constructor(
@inject(ConfigManager) private readonly configManager?: ConfigManager,
@inject(SoloLogger) private readonly logger?: SoloLogger,
Expand Down Expand Up @@ -77,6 +82,8 @@ export class K8Client implements K8 {
this.networkingApi = this.kubeConfig.makeApiClient(k8s.NetworkingV1Api);
this.coordinationApiClient = this.kubeConfig.makeApiClient(k8s.CoordinationV1Api);

this.k8Clusters = new K8ClientClusters(this.kubeConfig);

return this; // to enable chaining
}

Expand All @@ -85,6 +92,14 @@ export class K8Client implements K8 {
return null;
}

/**
* Fluent accessor for reading and manipulating cluster information from the kubeconfig file.
* returns an object instance providing cluster operations
*/
public clusters(): Clusters {
return this.k8Clusters;
}

/**
* Apply filters to metadata
* @param items - list of items
Expand Down Expand Up @@ -246,13 +261,8 @@ export class K8Client implements K8 {
return this.filterItem(resp.body.items, {name});
}

public getClusters() {
const clusters: string[] = [];
for (const cluster of this.kubeConfig.getClusters()) {
clusters.push(cluster.name);
}

return clusters;
public getClusters(): string[] {
return this.clusters().list();
}

public getContextNames(): string[] {
Expand Down Expand Up @@ -1590,9 +1600,7 @@ export class K8Client implements K8 {
}

public getCurrentClusterName(): string {
const currentCluster = this.kubeConfig.getCurrentCluster();
if (!currentCluster) return '';
return currentCluster.name;
return this.clusters().readCurrent();
}

public async listSvcs(namespace: NamespaceName, labels: string[]): Promise<k8s.V1Service[]> {
Expand Down
28 changes: 28 additions & 0 deletions src/core/kube/k8_client/k8_client_clusters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/
import {type Clusters} from '../clusters.js';
import {type KubeConfig} from '@kubernetes/client-node';
import {IllegalArgumentError} from '../../errors.js';

export default class K8ClientClusters implements Clusters {
public constructor(private readonly kubeConfig: KubeConfig) {
if (!kubeConfig) {
throw new IllegalArgumentError('kubeConfig must not be null or undefined');
}
}

list(): string[] {
const clusters: string[] = [];
for (const cluster of this.kubeConfig.getClusters()) {
clusters.push(cluster.name);
}

return clusters;
}

readCurrent(): string {
const currentCluster = this.kubeConfig.getCurrentCluster();
return !currentCluster ? '' : currentCluster.name;
}
}

0 comments on commit 95c71f0

Please sign in to comment.