Pulumi根据所选堆栈修改kubernetes资源

时间:2020-01-30 11:42:29

标签: typescript kubernetes pulumi

根据选择的堆栈来修改kubernetes容器的最佳方法是什么?我看到输入类型无法修改。我找到了传播算子,例如:

this.http.get(...).pipe(map((response: Response) => response.json();));

但是感觉有点不好,在我看来,这增加了大型资源声明的复杂性(尤其是当条件变得更加复杂时)。我尝试将容器定义提取到变量中,但是由于无法声明变量的类型,因此在为变量中的键之一分配 const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", { metadata: { namespace: ledgerNamespace.metadata.name, }, spec: { template: { metadata: { labels: {name: "ledger"}, }, spec: { containers: [ // containers ], volumes: [ { //volume 1 }, { // volume 2 }, // conditional volume based on the stack ...(stack == 'dev' ? [{ name: dbTLS.metadata.name, secret: {secretName: dbTLS.metadata.name, defaultMode: 256} }] : []) ] } } } }); 时遇到了一些问题:

Output

使用 let apiContainer = { name: "api", image: appImage.imageName, // more stuff ... volumeMounts: [ { mountPath: "/app/gunicorn-socket", name: "gunicorn-socket-dir", readOnly: false }, ], } if (stack != 'local') { apiContainer.volumeMounts.push({ mountPath: "/etc/secret/db", name: dbTLS.metadata.name, readOnly: true }) } name上给出错误。

似乎interface是在github上为标签Type 'Output<string>' is not assignable to type 'string'导出的,但是当我在本地查看接口时,它没有导出:

1.4.5

我已验证自己在本地v1.4.5上:

12:41PM /Users/paymahn/qwil/ledger/pulumi/infra pulumi ⬆ ⬇ ✱ ➜
 ❯❯❯ rg "interface Container" --no-ignore -g "**/*kube*/**"
node_modules/@pulumi/kubernetes/types/input.d.ts
8424:        interface Container {
8575:        interface ContainerImage {
8589:        interface ContainerPort {
8619:        interface ContainerState {
8636:        interface ContainerStateRunning {
8645:        interface ContainerStateTerminated {
8678:        interface ContainerStateWaiting {
8691:        interface ContainerStatus {

node_modules/@pulumi/kubernetes/types/output.d.ts
8508:        interface Container {
8659:        interface ContainerImage {
8673:        interface ContainerPort {
8703:        interface ContainerState {
8720:        interface ContainerStateRunning {
8729:        interface ContainerStateTerminated {
8762:        interface ContainerStateWaiting {
8775:        interface ContainerStatus {

1 个答案:

答案 0 :(得分:1)

可用的类型,也许您正在寻找错误的包装。例如。 k8s.types.input.core.v1.Container是正确的类型名称。

要动态构建卷安装的集合,最简单的方法是直接对安装阵列进行操作:

const volumeMounts: k8s.types.input.core.v1.VolumeMount[] = [{
    mountPath: "/app/gunicorn-socket",
    name: "gunicorn-socket-dir",
    readOnly: false
}];

if (stack != 'local') {
    volumeMounts.push({
        mountPath: "/etc/secret/db",
        name: dbTLS.metadata.name,
        readOnly: true
    });
}

const apiContainer: k8s.types.input.core.v1.Container = {
    name: "api",
    image: appImage.imageName, 
    volumeMounts,
};

如果您声明了k8s.types.input.core.v1.Container的实例,那么现在volumeMounts属性将不是数组而是数组的输入,因此您必须将其转换为输出,应用并创建一个新数组,然后分配回来。