简体   繁体   English

使用 Pulumi 和 Typescript 创建 Secret (k8s)

[英]Create Secret (k8s) with Pulumi and Typescript

I want to create a secret with Pulumi in Typescript it should contain the following data:我想用 Pulumi 在 Typescript 中创建一个秘密,它应该包含以下数据:

    remote_write:
    - url: "example.com"
      basic_auth:
        username: "user"
        password: "XXX"  

the code looks like:代码如下:

    const databaseSecret = new k8s.core.v1.Secret(
  "secret-config", 
  {
    data: { 
      remote_write: [
        {
          url: "example.com",
          basic_auth:
          {
            username: "user",
            password: "XXX",
          }
        }
      ], 
    }
  }, 
k8sOpts
);

But this shows the follwing error message:但这显示了以下错误消息:

"Type '{ url: string; basic_auth: { username: string; password: string; }; }[]' is not assignable to type 'Input'" “类型 '{ url: string; basic_auth: { username: string; password: string; }; }[]' 不可分配给类型 'Input'”

I dont know how i can fix this?我不知道我该如何解决这个问题? How do I get such nested data into a secret?我如何将这样的嵌套数据变成秘密?

There are a couple of problems here.这里有几个问题。

Firstly: A Kubernetes secret takes an input with a key name, and then some string data.首先:一个 Kubernetes 密钥接受一个带有键名的输入,然后是一些字符串数据。 You're passing the key name as remote_write and then trying to pass a TypeScript object - you need to stringify it first.您将密钥名称作为remote_write传递,然后尝试传递 TypeScript object - 您需要先对其进行字符串化。 You can do take advantage of YAML being a superset of JSON to handle this:您可以利用 YAML 作为 JSON 的超集来处理此问题:

let secret = [
  {
    url: "example.com",
    basic_auth: {
      username: "user",
      password: "XXX",
    },
  },
];

const databaseSecret = new k8s.core.v1.Secret("secret-config", {
    data: {
      remote_write: JSON.stringify(secret),
    },
  });

However, there's an additional problem here: Kubernetes expects objects in secrets to be base64 encoded, so you'll need to encode it first:但是,这里还有一个问题: Kubernetes 期望机密中的对象是 base64 编码的,因此您需要先对其进行编码:

const databaseSecret = new k8s.core.v1.Secret("secret-config", {
  data: {
    remote_write: Buffer.from(JSON.stringify(secret), 'binary').toString('base64'),
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM