英文:
Access a property in the body of a kubernetes resource using a field path
问题
我可以帮你翻译这段代码。以下是翻译的结果:
我想获取在Pod
的downwardAPI
部分声明的字段的值。
apiVersion: v1
kind: Pod
metadata:
name: sample
namespace: default
spec:
containers:
- image: rpa
imagePullPolicy: Always
name: testbot
volumeMounts:
- mountPath: /etc/pod-info
name: pod-info
volumes:
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.labels
path: labels
- fieldRef:
apiVersion: v1
fieldPath: metadata.name
path: pod-name
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: pod-namespace
name: pod-info
使用client-go
,我可以使用pod.Spec.Volumes[0].DownwardAPI.Items
来获取包含fieldPath
的项切片。但是,我现在需要动态地获取在fieldPath
中声明的任何值。所以,从第一个项开始,我想要访问metadata.labels
的值。我可以使用pod.ObjectMeta.Labels
,但我想要动态地访问该字段。在JavaScript中,可以这样做:
var foo = "metadata.labels";
var fooarr = foo.split(".");
var bar = {
metadata: {
labels: "foobar"
}
};
var temp = bar;
for (lm of fooarr) {
temp = temp[lm];
}
console.log(temp);
我该如何在client-go
中实现类似的功能呢?
英文:
I want to get the values of the fields declared in the downwardAPI
section of a Pod
.
apiVersion: v1
kind: Pod
metadata:
name: sample
namespace: default
spec:
containers:
- image: rpa
imagePullPolicy: Always
name: testbot
volumeMounts:
- mountPath: /etc/pod-info
name: pod-info
volumes:
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.labels
path: labels
- fieldRef:
apiVersion: v1
fieldPath: metadata.name
path: pod-name
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: pod-namespace
name: pod-info
Using client-go
, I can use pod.Spec.Volumes[0].DownwardAPI.Items
to get the item slice including the fieldPath
. But I would now need to dynamically able to fetch whatever values has been declared in the fieldPath
. So, from the first item, I would like to access the value of metadata.labels
. I could do pod.ObjectMeta.Labels
but I would like to access the field dynamically. In terms of Javascript it would have been something like
var foo="metadata.labels"
var fooarr = foo.split(".")
var bar={
metadata:{
labels: "foobar"
}
}
var temp = oof
for(lm of lmaoarr){
temp = temp[lm]
}
console.log(temp)
How do I do something similar using client-go
?
答案1
得分: 1
标准的kubelet代码具有将下行API字段转换为环境变量的逻辑。然而,这并不简单也不通用:在堆栈的底部,只支持Kubernetes文档中列出的特定字段。只匹配这些特定字段可能是不完整的,但与标准的Kubernetes不一致也不是错误的。
实际的代码如下:
- 调用
pods.ConvertDownwardAPIFieldLabel
函数,它对字段进行了一些轻量级的规范化和验证:只允许在metadata.annotations
和metadata.labels
上使用下标,只允许使用十几个特定的字段名,并且将spec.host
重写为spec.nodeName
。 - 处理依赖于Pod规范或运行时数据中的非元数据字段的
spec.*
和status.*
变量。 - 委托给
fieldpath.ExtractFieldPathAsString
函数,它知道如何处理metadata.*
变量。
k8s.io/kubernetes/pkg/fieldpath
包中包含了一些在处理下行API时使用的辅助函数。对于你的具体问题,一个非常简短的答案可能就是调用fieldpath.ExtractFieldPathAsString
函数,并将Pod对象作为参数传递给它,它将处理元数据字段但不处理其他字段。
英文:
The standard kubelet code has logic to translate the downward API fields into environment variables. It is neither simple nor generic, though: at the bottom of the stack, only the specific fields listed in the Kubernetes documentation are supported. It would be incomplete, but not wrong or inconsistent with standard Kubernetes, to just match on these specific fields:
for _, item := range downwardApiObject.Items {
switch item.FieldPath.FieldRef {
case "metadata.name":
return pod.ObjectMeta.Name
}
}
The actual code:
- Calls
pods.ConvertDownwardAPIFieldLabel
which does some very lightweight normalization and validation: subscripts are only allowed onmetadata.annotations
andmetadata.labels
, only the dozen or so specific field names are allowed, andspec.host
is rewritten tospec.nodeName
. - Handles the
spec.*
andstatus.*
variables that depend on non-metadata fields in the pod spec or runtime data. - Delegates to
fieldpath.ExtractFieldPathAsString
which knows how to handle themetadata.*
variables.
The k8s.io/kubernetes/pkg/fieldpath
package contains a couple of helpers that are used in processing the downward API, and a really short answer to your specific question could be just to call fieldpath.ExtractFieldPathAsString
passing it the Pod object, which will handle the metadata fields but nothing else.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论