@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
117 lines • 4.58 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { V1ObjectMeta, V1Secret } from '@kubernetes/client-node';
import { ResourceNotFoundError } from '../../../errors/resource-operation-errors.js';
import { ResourceType } from '../../../resources/resource-type.js';
import { Duration } from '../../../../../core/time/duration.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
export class K8ClientSecrets {
kubeClient;
constructor(kubeClient) {
this.kubeClient = kubeClient;
}
async create(namespace, name, secretType, data, labels) {
return await this.createOrReplaceWithForce(namespace, name, secretType, data, labels, false, true);
}
async createOrReplace(namespace, name, secretType, data, labels) {
return await this.createOrReplaceWithForce(namespace, name, secretType, data, labels, false, false);
}
async delete(namespace, name) {
try {
await this.kubeClient.deleteNamespacedSecret({ name, namespace: namespace.name });
}
catch (error) {
if (KubeApiResponse.isNotFound(error)) {
return true;
}
KubeApiResponse.throwError(error, ResourceOperation.DELETE, ResourceType.SECRET, namespace, name);
}
return true;
}
async replace(namespace, name, secretType, data, labels) {
return this.createOrReplaceWithForce(namespace, name, secretType, data, labels, true);
}
async read(namespace, name) {
let result;
try {
result = await this.kubeClient.readNamespacedSecret({ name, namespace: namespace.name });
}
catch (error) {
KubeApiResponse.throwError(error, ResourceOperation.READ, ResourceType.SECRET, namespace, name);
}
return {
name: result.metadata.name,
labels: result.metadata.labels,
namespace: result.metadata.namespace,
type: result.type,
data: result.data,
};
}
async list(namespace, labels) {
const labelSelector = labels ? labels.join(',') : undefined;
let secretList;
try {
secretList = await this.kubeClient.listNamespacedSecret({
namespace: namespace.toString(),
labelSelector,
timeoutSeconds: Duration.ofMinutes(5).toMillis(),
});
}
catch (error) {
KubeApiResponse.throwError(error, ResourceOperation.LIST, ResourceType.SECRET, namespace, '');
}
return secretList.items.map((secret) => {
return {
name: secret.metadata.name,
labels: secret.metadata.labels,
namespace: secret.metadata.namespace,
type: secret.type,
data: secret.data,
};
});
}
async exists(namespace, name) {
try {
const cm = await this.read(namespace, name);
return !!cm;
}
catch (error) {
if (error instanceof ResourceNotFoundError) {
return false;
}
else {
throw error;
}
}
}
async createOrReplaceWithForce(namespace, name, secretType, data, labels, forceReplace, forceCreate) {
const replace = await this.shouldReplace(namespace, name, forceReplace, forceCreate);
const v1Secret = new V1Secret();
v1Secret.apiVersion = 'v1';
v1Secret.kind = 'Secret';
v1Secret.type = secretType;
v1Secret.data = data;
v1Secret.metadata = new V1ObjectMeta();
v1Secret.metadata.name = name;
v1Secret.metadata.labels = labels;
try {
await (replace
? this.kubeClient.replaceNamespacedSecret({ name, namespace: namespace.name, body: v1Secret })
: this.kubeClient.createNamespacedSecret({ namespace: namespace.name, body: v1Secret }));
}
catch (error) {
KubeApiResponse.throwError(error, replace ? ResourceOperation.REPLACE : ResourceOperation.CREATE, ResourceType.SECRET, namespace, name);
}
return true;
}
async shouldReplace(namespace, name, forceReplace, forceCreate) {
if (forceReplace && !forceCreate) {
return true;
}
if (forceCreate) {
return false;
}
return await this.exists(namespace, name);
}
}
//# sourceMappingURL=k8-client-secrets.js.map