Skip to content

Commit fb92fd6

Browse files
authored
feat: add secrets fluent interface implementation (#1312)
Signed-off-by: Nathan Klick <[email protected]>
1 parent 136cd7b commit fb92fd6

27 files changed

+516
-335
lines changed

src/commands/node/tasks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,11 @@ export class NodeCommandTasks {
854854
for (const nodeAlias of ctx.config.nodeAliases) {
855855
const podRef = ctx.config.podRefs[nodeAlias];
856856
const containerRef = ContainerRef.of(podRef, constants.ROOT_CONTAINER);
857-
self.logger.debug(`Uploading state files to pod ${podRef.podName.name}`);
857+
self.logger.debug(`Uploading state files to pod ${podRef.name}`);
858858
await self.k8.copyTo(containerRef, zipFile, `${constants.HEDERA_HAPI_PATH}/data`);
859859

860860
self.logger.info(
861-
`Deleting the previous state files in pod ${podRef.podName.name} directory ${constants.HEDERA_HAPI_PATH}/data/saved`,
861+
`Deleting the previous state files in pod ${podRef.name} directory ${constants.HEDERA_HAPI_PATH}/data/saved`,
862862
);
863863
await self.k8.execContainer(containerRef, ['rm', '-rf', `${constants.HEDERA_HAPI_PATH}/data/saved/*`]);
864864
await self.k8.execContainer(containerRef, [
@@ -1023,7 +1023,7 @@ export class NodeCommandTasks {
10231023
'Enable port forwarding for JVM debugger',
10241024
async (ctx: any, task: ListrTaskWrapper<any, any, any>) => {
10251025
const podRef = PodRef.of(ctx.config.namespace, PodName.of(`network-${ctx.config.debugNodeAlias}-0`));
1026-
this.logger.debug(`Enable port forwarding for JVM debugger on pod ${podRef.podName.name}`);
1026+
this.logger.debug(`Enable port forwarding for JVM debugger on pod ${podRef.name}`);
10271027
await this.k8.portForward(podRef, constants.JVM_DEBUG_PORT, constants.JVM_DEBUG_PORT);
10281028
},
10291029
(ctx: any) => !ctx.config.debugNodeAlias,

src/core/kube/container_name.ts

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
/**
22
* SPDX-License-Identifier: Apache-2.0
33
*/
4-
import {isDns1123Label} from './kube_validation.js';
5-
import {ContainerNameInvalidError} from './errors/namespace_name_invalid_error.js';
4+
import {ResourceName} from './resource_name.js';
5+
import {ResourceType} from './resource_type.js';
66

77
/**
88
* Represents a Kubernetes container name. A Kubernetes container name must be a valid RFC-1123 DNS label.
99
*
1010
* @include DNS_1123_LABEL
1111
*/
12-
export class ContainerName {
13-
private constructor(public readonly name: string) {
14-
if (!this.isValid()) {
15-
throw new ContainerNameInvalidError(name);
16-
}
12+
export class ContainerName extends ResourceName {
13+
private constructor(name: string) {
14+
super(ResourceType.CONTAINER, name);
1715
}
1816

1917
/**
@@ -28,48 +26,4 @@ export class ContainerName {
2826
public static of(name: string): ContainerName {
2927
return new ContainerName(name);
3028
}
31-
32-
/**
33-
* Returns true if the container name is valid. A Kubernetes container name must be a valid RFC-1123 DNS label.
34-
*
35-
* @include DNS_1123_LABEL
36-
*
37-
* @returns true if the container name is valid.
38-
* @throws ContainerNameInvalidError if the container name is invalid.
39-
*/
40-
private isValid(): boolean {
41-
return isDns1123Label(this.name);
42-
}
43-
44-
/**
45-
* Compares this instance with another ContainerName.
46-
* @param other The other ContainerName instance.
47-
* @returns true if both instances have the same name.
48-
*/
49-
public equals(other: ContainerName): boolean {
50-
return other instanceof ContainerName && this.name === other.name;
51-
}
52-
53-
/**
54-
* Allows implicit conversion to a string.
55-
* @returns The container name as a string.
56-
*/
57-
public toString(): string {
58-
return this.name;
59-
}
60-
61-
/**
62-
* Allows `ContainerName` to be used as a primitive string in operations.
63-
*/
64-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
65-
public [Symbol.toPrimitive](hint: string): string {
66-
return this.name;
67-
}
68-
69-
/**
70-
* Returns the primitive value of the object.
71-
*/
72-
public valueOf(): string {
73-
return this.name;
74-
}
7529
}

src/core/kube/container_ref.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@
33
*/
44
import {type PodRef} from './pod_ref.js';
55
import {type ContainerName} from './container_name.js';
6-
import {MissingContainerNameError, MissingPodRefError} from './errors/namespace_name_invalid_error.js';
6+
import {NestedResourceRef} from './nested_resource_ref.js';
77

88
/**
99
* Represents a Kubernetes pod reference which includes the namespace name and pod name.
1010
*/
11-
export class ContainerRef {
12-
private constructor(
13-
public readonly podRef: PodRef,
14-
public readonly containerName: ContainerName,
15-
) {
16-
if (!podRef) {
17-
throw new MissingPodRefError();
18-
}
19-
if (!containerName) {
20-
throw new MissingContainerNameError();
21-
}
11+
export class ContainerRef extends NestedResourceRef<PodRef, ContainerName> {
12+
private constructor(parentRef: PodRef, name: ContainerName) {
13+
super(parentRef, name);
2214
}
2315

2416
/**
@@ -29,25 +21,4 @@ export class ContainerRef {
2921
public static of(podRef: PodRef, containerName: ContainerName): ContainerRef {
3022
return new ContainerRef(podRef, containerName);
3123
}
32-
33-
/**
34-
* Compares this instance with another ContainerRef.
35-
* @param other The other ContainerRef instance.
36-
* @returns true if both instances have the same pod ref and container name.
37-
*/
38-
public equals(other: ContainerRef): boolean {
39-
return (
40-
other instanceof ContainerRef &&
41-
this.podRef.equals(other.podRef) &&
42-
this.containerName.equals(other.containerName)
43-
);
44-
}
45-
46-
/**
47-
* Allows implicit conversion to a string.
48-
* @returns The container reference as a string.
49-
*/
50-
public toString(): string {
51-
return `{podRef: ${this.podRef.toString()}, containerName: ${this.containerName.toString()}}`;
52-
}
5324
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {SoloError} from '../../errors.js';
5+
import {type ResourceType} from '../resource_type.js';
6+
7+
const RFC_1123_POSTFIX = (prefix: string) => `${prefix} is invalid, must be a valid RFC-1123 DNS label. \` +
8+
"A DNS 1123 label must consist of lower case alphanumeric characters, '-' " +
9+
"or '.', and must start and end with an alphanumeric character.`;
10+
11+
export class InvalidResourceNameError extends SoloError {
12+
public static RESOURCE_NAME_INVALID = (type: ResourceType, name: string) =>
13+
RFC_1123_POSTFIX(`${type} name '${name}'`);
14+
15+
/**
16+
* Instantiates a new error with a message and an optional cause.
17+
*
18+
* @param name - the invalid pod name.
19+
* @param type - the type of the resource.
20+
* @param cause - optional underlying cause of the error.
21+
* @param meta - optional metadata to be reported.
22+
*/
23+
public constructor(name: string, type: ResourceType, cause: Error | any = {}, meta: any = {}) {
24+
super(InvalidResourceNameError.RESOURCE_NAME_INVALID(type, name), cause, meta);
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {SoloError} from '../../errors.js';
5+
6+
export class MissingNamespaceError extends SoloError {
7+
public static MISSING_NAMESPACE = 'Namespace is required.';
8+
9+
/**
10+
* Instantiates a new error with a message and an optional cause.
11+
*
12+
* @param cause - optional underlying cause of the error.
13+
* @param meta - optional metadata to be reported.
14+
*/
15+
public constructor(cause?: Error, meta?: object) {
16+
super(MissingNamespaceError.MISSING_NAMESPACE, cause, meta);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {SoloError} from '../../errors.js';
5+
6+
export class MissingParentResourceRefError extends SoloError {
7+
public static MISSING_PARENT_RESOURCE_REF = 'The parent ResourceRef is required.';
8+
9+
/**
10+
* Instantiates a new error with a message and an optional cause.
11+
*
12+
* @param cause - optional underlying cause of the error.
13+
* @param meta - optional metadata to be reported.
14+
*/
15+
public constructor(cause?: Error, meta?: object) {
16+
super(MissingParentResourceRefError.MISSING_PARENT_RESOURCE_REF, cause, meta);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
import {SoloError} from '../../errors.js';
5+
6+
export class MissingResourceNameError extends SoloError {
7+
public static MISSING_RESOURCE_NAME = 'Name is required.';
8+
9+
/**
10+
* Instantiates a new error with a message and an optional cause.
11+
*
12+
* @param cause - optional underlying cause of the error.
13+
* @param meta - optional metadata to be reported.
14+
*/
15+
public constructor(cause?: Error, meta?: object) {
16+
super(MissingResourceNameError.MISSING_RESOURCE_NAME, cause, meta);
17+
}
18+
}

src/core/kube/errors/namespace_name_invalid_error.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,6 @@ export class NamespaceNameInvalidError extends SoloError {
2222
}
2323
}
2424

25-
export class PodNameInvalidError extends SoloError {
26-
public static POD_NAME_INVALID = (name: string) => RFC_1123_POSTFIX(`Pod name '${name}'`);
27-
28-
/**
29-
* Instantiates a new error with a message and an optional cause.
30-
*
31-
* @param podName - the invalid pod name.
32-
* @param cause - optional underlying cause of the error.
33-
* @param meta - optional metadata to be reported.
34-
*/
35-
public constructor(podName: string, cause: Error | any = {}, meta: any = {}) {
36-
super(PodNameInvalidError.POD_NAME_INVALID(podName), cause, meta);
37-
}
38-
}
39-
40-
export class MissingNamespaceNameError extends SoloError {
41-
public static MISSING_NAMESPACE_NAME = 'Namespace name is required.';
42-
43-
/**
44-
* Instantiates a new error with a message and an optional cause.
45-
*
46-
* @param cause - optional underlying cause of the error.
47-
* @param meta - optional metadata to be reported.
48-
*/
49-
public constructor(cause: Error | any = {}, meta: any = {}) {
50-
super(MissingNamespaceNameError.MISSING_NAMESPACE_NAME, cause, meta);
51-
}
52-
}
53-
54-
export class MissingPodNameError extends SoloError {
55-
public static MISSING_POD_NAME = 'Pod name is required.';
56-
57-
/**
58-
* Instantiates a new error with a message and an optional cause.
59-
*
60-
* @param cause - optional underlying cause of the error.
61-
* @param meta - optional metadata to be reported.
62-
*/
63-
public constructor(cause: Error | any = {}, meta: any = {}) {
64-
super(MissingPodNameError.MISSING_POD_NAME, cause, meta);
65-
}
66-
}
67-
6825
export class ContainerNameInvalidError extends SoloError {
6926
public static CONTAINER_NAME_INVALID = (name: string) => RFC_1123_POSTFIX(`Container name '${name}'`);
7027

src/core/kube/k8.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {type Service} from './service.js';
2020
import {type Pods} from './pods.js';
2121
import {type Leases} from './leases.js';
2222
import {type IngressClasses} from './ingress_classes.js';
23+
import {type Secrets} from './secrets.js';
2324

2425
export interface K8 {
2526
/**
@@ -76,6 +77,12 @@ export interface K8 {
7677
*/
7778
leases(): Leases;
7879

80+
/**
81+
* Fluent accessor for reading and manipulating secrets in the kubernetes cluster.
82+
* @returns an object instance providing secret operations
83+
*/
84+
secrets(): Secrets;
85+
7986
/**
8087
* Fluent accessor for reading and manipulating ingress classes in the kubernetes cluster.
8188
* @returns an object instance providing ingress class operations

0 commit comments

Comments
 (0)