Skip to content

Instantly share code, notes, and snippets.

@spion
Last active September 9, 2022 16:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spion/632e707bccbce602da0deab0cad47c71 to your computer and use it in GitHub Desktop.
import { ApiObject, GroupVersionKind, Include } from "cdk8s";
import { Construct } from "constructs";
import * as immer from "immer";
import * as fs from "fs";
type UnReadonly<T> = T extends string | number | boolean | null
? T
: T extends ReadonlyArray<infer U>
? Array<UnReadonly<U>>
: { -readonly [K in keyof T]: UnReadonly<T[K]> };
export function modify<T extends ApiObject, Props>(
_constructor: { new (construct: any, name: string, props: Props): T },
obj: T,
f: (original: UnReadonly<Props>) => any
) {
(obj as any).props = immer.produce((obj as any).props, f);
// Object.assign((obj as any).props, nextState);
}
export type IncludeDirOptions = {
path: string;
};
type ApiObjectConstructor<Options> = {
new (scope: Construct, name: string, opts: Options): ApiObject;
readonly GVK: GroupVersionKind;
};
export class IncludeDir extends Construct {
private includes: Include[];
constructor(scope: Construct, name: string, opts: IncludeDirOptions) {
super(scope, name);
const baseDir = opts.path;
this.includes = fs.readdirSync(baseDir).map(
item =>
new Include(this, name + "-" + item, {
url: baseDir + "/" + item,
})
);
}
patch<T>(constructor: ApiObjectConstructor<T>, patchFn: (original: UnReadonly<T>) => any) {
for (let include of this.includes)
for (let apiObj of include.apiObjects)
if (
apiObj.apiVersion === constructor.GVK.apiVersion &&
apiObj.kind === constructor.GVK.kind
) {
(apiObj as any).props = immer.produce((apiObj as any).props, patchFn);
}
}
}
import * as mon from "./imports/monitoring.coreos.com";
export class MonitoringSetup extends Chart {
constructor(scope: Construct, name: string) {
super(scope, name, { namespace: "monitoring" });
const resources = new IncludeDir(this, "monitoring", {
path: "./configs/kube-prometheus-0.10.0/install",
});
resources.patch(mon.Prometheus, res => {
res.spec.replicas = 1;
res.spec.scrapeInterval = "1m";
res.spec.evaluationInterval = "1m";
res.spec.storage = {
volumeClaimTemplate: {
spec: {
storageClassName: "hcloud-volumes",
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "10Gi",
} as any,
},
},
},
};
});
resources.patch(mon.Alertmanager, res => {
res.spec.replicas = 1;
});
}
}
cdk8s import github:CrunchyData/postgres-operator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment