我试图为使用Tanka/Jsonnet的部署编写一些包装逻辑,但似乎遇到了一些问题,这可能很容易解决。
我试图为一个statefulSet创建一些包装器函数,它可以在多个位置添加一些stdLabels,但是仍然会在stateful.new上得到一个关于Expected token OPERATOR but got "."的错误。它的工作没有局部变量,但我不知道为什么或如何解决这个问题?
{
local k = import 'github.com/grafana/jsonnet-libs/ksonnet-util/kausal.libsonnet',
local container = k.core.v1.container,
local stateful = k.apps.v1.statefulSet,
fns: {
customStateful(name, config, c):: {
local stdLabels = {
realm: $._config.ingress.realm,
app: name,
'app.kubernetes.io/part-of': config.name,
},
stateful.new(
name=name,
replicas=null,
containers=[]
)
+ stateful.metadata.withLabels(stdLabels)
+ stateful.spec.template.metadata.withLabels(stdLabels)
}
},
$.aed.customStateful('test', cfg, container.new('test', alpine:latest)),
}发布于 2022-10-05 13:17:54
主要问题是customStateful():: {...}正在返回一个对象,因此那里的每个条目都应该是{ key1: value1, key2: value, ...},您需要将它改为customStateful():: (...)。
粘贴在固定示例下面:
main.jsonnet
对于其他读者,请注意您需要运行tk init才能尝试这段代码
// main.jsonnet
//
{
local k = import 'github.com/grafana/jsonnet-libs/ksonnet-util/kausal.libsonnet',
local container = k.core.v1.container,
local stateful = k.apps.v1.statefulSet,
// plug just to make below code work when referring to $._config, as per provided example
_config:: { ingress:: { realm:: 'bar' } },
// cfg used by customStateful 2nd argument in the provided example, create some foo setting
local cfg = { name: 'foo' },
fns:: {
// NB: returning () instead of {}, to be able to return stateful.new() + overloads,
// else it would need to be a named field, e.g. { sts: stateful.new(...) }
customStateful(name, config, c):: (
local stdLabels = {
realm: $._config.ingress.realm,
app: name,
'app.kubernetes.io/part-of': config.name,
};
stateful.new(
name=name,
replicas=null,
containers=[]
)
+ stateful.metadata.withLabels(stdLabels)
+ stateful.spec.template.metadata.withLabels(stdLabels)
),
},
foo: $.fns.customStateful('test', cfg, container.new('test', 'alpine:latest')),
}输出
$ tk init
$ tk eval main.jsonnet
{
"foo": {
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": {
"labels": {
"app": "test",
"app.kubernetes.io/part-of": "foo",
"realm": "bar"
},
"name": "test"
},
"spec": {
"replicas": null,
"selector": {
"matchLabels": {
"name": "test"
}
},
"template": {
"metadata": {
"labels": {
"app": "test",
"app.kubernetes.io/part-of": "foo",
"realm": "bar"
}
},
"spec": {
"containers": []
}
},
"updateStrategy": {
"type": "RollingUpdate"
}
}
}
}https://stackoverflow.com/questions/73960741
复制相似问题