【问题标题】:Pulumi modify kubernetes resource based on selected stackPulumi 根据选择的栈修改 Kubernetes 资源
【发布时间】:2020-02-03 21:13:26
【问题描述】:

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

  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 和Type 'Output<string>' is not assignable to type 'string' 上给出错误。

interface 似乎是在 github 上为标签 1.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 {

我已验证我在本地使用 v1.4.5:

12:37PM /Users/paymahn/qwil/ledger/pulumi/infra  ✘ 1 pulumi ⬆ ⬇ ✱ ➜
 ❯❯❯ npm list "@pulumi/kubernetes"
kubernetes-typescript@ /Users/paymahn/qwil/ledger/pulumi/infra
├── @pulumi/kubernetes@1.4.5
└─┬ @pulumi/kubernetesx@0.1.1
  └── @pulumi/kubernetes@1.3.3

【问题讨论】:

  • 为什么这个标签是python?
  • 哎呀,这是我的错误。它应该用打字稿标记。我的错!

标签: typescript kubernetes pulumi


【解决方案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 属性将不是数组而是数组的输入,因此您必须将其转换为输出、应用、创建新数组并分配回.

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2022-11-11
    • 2019-03-08
    相关资源
    最近更新 更多