Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const project = new Cdk8sTeamJsiiProject({
// identical to npm defaults.
project.package.addField('publishConfig', { access: 'public' });

project.gitignore.exclude('.vscode/');
project.gitignore.exclude('.vscode/', '.idea/');

const importdir = path.join('src', 'imports');

Expand Down
46 changes: 46 additions & 0 deletions docs/plus/volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,49 @@ const redis = pod.addContainer({
// mount to the redis container.
redis.mount('/var/lib/redis', data);
```

## Using PersistentVolumeClaim Templates with StatefulSets

When working with StatefulSets, you can use PersistentVolumeClaim templates to create stable storage for each pod in the StatefulSet. This allows each pod to have its own storage that persists even if the pod is rescheduled to a different node.

```typescript
import * as kplus from 'cdk8s-plus-32';
import { Size } from 'cdk8s';

const dataVolume = Volume.fromName(chart, "pvc-template-data", "data-volume")

// Create a StatefulSet with a PVC template
const statefulSet = new kplus.StatefulSet(this, 'StatefulSet', {
containers: [{
image: 'nginx',
volumeMounts: [{
volume: dataVolume,
path: '/data',
}, {
volume: Volume.fromName(chart, "pvc-template-temp", "temp-volume"),
path: '/data',
}],
}],
// Define PVC templates during initialization
volumeClaimTemplates: [{
name: dataVolume.name,
storage: Size.gibibytes(10),
accessModes: [kplus.PersistentVolumeAccessMode.READ_WRITE_ONCE],
storageClassName: 'standard', // Optional: Specify the storage class
}, {
name: 'temp-volume', // Must match a volume mount name in a container
storage: Size.gibibytes(10),
accessModes: [kplus.PersistentVolumeAccessMode.READ_WRITE_ONCE],
storageClassName: 'standard', // Optional: Specify the storage class
}],
});

// Or add PVC templates after creation
statefulSet.addVolumeClaimTemplate({
name: 'logs-volume',
storage: Size.gibibytes(5),
accessModes: [kplus.PersistentVolumeAccessMode.READ_WRITE_ONCE],
});
```

Each pod in the StatefulSet will get its own PVC instance based on these templates, with names like `data-volume-my-statefulset-0`, `data-volume-my-statefulset-1`, etc.
41 changes: 27 additions & 14 deletions src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ export interface AbstractPodProps extends base.ResourceProps {
/**
* Properties for `Pod`.
*/
export interface PodProps extends AbstractPodProps {}
export interface PodProps extends AbstractPodProps {
}

/**
* Options for `LabelSelector.of`.
Expand Down Expand Up @@ -515,7 +516,8 @@ export class LabelSelector {

private constructor(
private readonly expressions: LabelExpression[],
private readonly labels: { [key: string]: string }) {}
private readonly labels: { [key: string]: string }) {
}

public isEmpty() {
return this.expressions.length === 0 && Object.keys(this.labels).length === 0;
Expand All @@ -529,7 +531,11 @@ export class LabelSelector {
return {};
}
return {
matchExpressions: undefinedIfEmpty(this.expressions.map(q => ({ key: q.key, operator: q.operator, values: q.values }))),
matchExpressions: undefinedIfEmpty(this.expressions.map(q => ({
key: q.key,
operator: q.operator,
values: q.values,
}))),
matchLabels: undefinedIfEmpty(this.labels),
};
}
Expand Down Expand Up @@ -1199,10 +1205,10 @@ export interface PodsSelectOptions {
readonly labels?: { [key: string]: string };

/**
* Expressions the pods must satisify.
*
* @default - no expressions requirements.
*/
* Expressions the pods must satisify.
*
* @default - no expressions requirements.
*/
readonly expressions?: LabelExpression[];

/**
Expand Down Expand Up @@ -1234,7 +1240,8 @@ export class Pods extends Construct implements IPodSelector {
return Pods.select(scope, id, { namespaces: options.namespaces });
}

constructor(scope: Construct, id: string,
constructor(
scope: Construct, id: string,
private readonly expressions?: LabelExpression[],
private readonly labels?: { [key: string]: string },
private readonly namespaces?: namespace.INamespaceSelector) {
Expand Down Expand Up @@ -1271,21 +1278,24 @@ export class Pods extends Construct implements IPodSelector {
* A node that is matched by label selectors.
*/
export class LabeledNode {
public constructor(public readonly labelSelector: NodeLabelQuery[]) {};
public constructor(public readonly labelSelector: NodeLabelQuery[]) {
};
}

/**
* A node that is matched by taint selectors.
*/
export class TaintedNode {
public constructor(public readonly taintSelector: NodeTaintQuery[]) {};
public constructor(public readonly taintSelector: NodeTaintQuery[]) {
};
}

/**
* A node that is matched by its name.
*/
export class NamedNode {
public constructor(public readonly name: string) {};
public constructor(public readonly name: string) {
};
}

/**
Expand Down Expand Up @@ -1361,7 +1371,8 @@ export class Topology {
return new Topology(key);
}

private constructor(public readonly key: string) {};
private constructor(public readonly key: string) {
};
}

/**
Expand Down Expand Up @@ -1428,7 +1439,8 @@ export class PodScheduling {
private _tolerations: k8s.Toleration[] = [];
private _nodeName?: string;

constructor(protected readonly instance: AbstractPod) {}
constructor(protected readonly instance: AbstractPod) {
}

/**
* Assign this pod a specific node by name.
Expand Down Expand Up @@ -1690,7 +1702,8 @@ export interface PodConnectionsAllowFromOptions {
*/
export class PodConnections {

constructor(protected readonly instance: AbstractPod) {}
constructor(protected readonly instance: AbstractPod) {
}

/**
* Allow network traffic from this pod to the peer.
Expand Down
Loading
Loading