gpt4 book ai didi

javascript - 如何从 yaml 文件为 Kubernetes nodejs API 客户端生成 `V1Job` 对象?

转载 作者:行者123 更新时间:2023-11-30 19:25:08 29 4
gpt4 key购买 nike

我之前在 python 中使用过:

    with open(path.join(path.dirname(__file__), "job.yaml")) as f:
body= yaml.safe_load(f)

try:
api_response = api_instance.create_namespaced_job(namespace, body)

查看 nodejs api 客户端的源代码:

    public createNamespacedJob (namespace: string, body: V1Job, includeUninitialized?: boolean, pretty?: string, dryRun?: string, options: any = {}) : Promise<{ response: http.IncomingMessage; body: V1Job;  }> {

如何生成 V1Job


我已经尝试了下面的方法,但得到了一条非常详细的错误消息/响应:

const k8s = require('@kubernetes/client-node');
const yaml = require('js-yaml');
const fs = require('fs');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.BatchV1Api);

var namespace = {
metadata: {
name: 'test123',
},
};

try {
var job = yaml.safeLoad(fs.readFileSync('job.yaml', 'utf8'));

k8sApi.createNamespacedJob(namespace, job).then(
(response) => {
console.log('Created namespace');
console.log("Success!")
},
(err) => {
console.log(err);
console.log(job);
console.log("Err")
},
);

} catch (e) {
console.log(e);
}

最佳答案

  1. V1Job 似乎是一个普通的对象,所以下面的工作。
  2. 命名空间必须是一个字符串而不是一个对象...
const k8s = require('@kubernetes/client-node');
const yaml = require('js-yaml');
const fs = require('fs');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.BatchV1Api);

try {
var job = yaml.safeLoad(fs.readFileSync('job.yaml', 'utf8'));

k8sApi.createNamespacedJob("default", job).then(
(response) => {
console.log("Success")
},
(err) => {
console.log(e);
process.exit(1);
},
);

} catch (e) {
console.log(e);
process.exit(1);
}

关于javascript - 如何从 yaml 文件为 Kubernetes nodejs API 客户端生成 `V1Job` 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990265/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com