我有一个 YAML 文件,其中定义了各种类型的多个库伯内特资源(根据 YAML 规范分隔):---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# ...
spec:
# ...
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
# ...
rules:
# ...
---
# etc
现在,我想将此列表解析为实例切片,以便我可以应用一些筛选和转换,并最终使用client.Object
myClient.Patch( # myClient is a client.Client instance
ctx,
object, # object needs to be a client.Object
client.Apply,
client.ForceOwnership,
client.FieldOwner("my.operator.acme.inc"),
)
但是,我无法弄清楚如何从YAML文档转到.以下内容使我几乎到达那里:[]client.Object
results := make([]client.Object, 0)
scheme := runtime.NewScheme()
clientgoscheme.AddToScheme(scheme)
apiextensionsv1beta1.AddToScheme(scheme)
apiextensionsv1.AddToScheme(scheme)
decode := serializer.NewCodecFactory(scheme).UniversalDeserializer().Decode
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
for _, doc := range strings.Split(string(data), "---") {
object, gvk, err := decode([]byte(doc), nil, nil)
if err != nil {
return nil, err
}
// object is now a runtime.Object, and gvk is a schema.GroupVersionKind
// taken together, they have all the information I need to expose a
// client.Object (I think) but I have no idea how to actually construct a
// type that implements that interface
result = append(result, ?????)
}
return result, nil
当然,我对其他解析器实现完全开放,但我没有发现任何能让我走得更远的东西。但这似乎一定是库伯内特斯世界中一个已解决的问题......那么我该怎么做呢?
一只斗牛犬
相关分类