英文:
Generate YAML manifest from Kubernetes types
问题
我正在研究编写一个工具,用于为我们的项目以编程方式生成 Kubernetes 定义。
我发现 Kubernetes 中的 API 类型可以在 k8s.io/kubernetes/pkg/api
中找到。我想根据这些类型输出 YAML。
给定这样一个对象:
ns := &api.Namespace{
ObjectMeta: api.ObjectMeta{
Name: "test",
},
}
生成符合 kubectl create
预期的 YAML 输出的最佳方法是什么?
英文:
I'm looking into writing a tool that generates Kubernetes definitions programatically for our project.
I've found that the API types in Kubernetes can be found in k8s.io/kubernetes/pkg/api
. I would like to output YAML based on these types.
Given an object like this:
ns := &api.Namespace{
ObjectMeta: api.ObjectMeta{
Name: "test",
},
}
What's the best way to generate the YAML output expected by kubectl create
?
答案1
得分: 2
找到了,供参考,以下是你可能想要做的:
package main
import (
"os"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/runtime/serializer/json"
)
func main() {
ns := &v1.Namespace{
TypeMeta: unversioned.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
}
e := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil)
err := e.EncodeToStream(ns, os.Stdout)
if err != nil {
panic(err)
}
}
这可能不是最佳实践(欢迎提出更好的建议),但它能完成工作。
英文:
Found it, for future reference, here's what you might want to do:
package main
import (
"os"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/runtime/serializer/json"
)
func main() {
ns := &v1.Namespace{
TypeMeta: unversioned.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
}
e := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil)
err := e.EncodeToStream(ns, os.Stdout)
if err != nil {
panic(err)
}
}
This might not be the best practice (better suggestions welcome), but it does the job.
答案2
得分: 2
他们的API已经更新了,所以现在看起来是这样的:
import k8sJson "k8s.io/apimachinery/pkg/runtime/serializer/json"
...
serializer := k8sJson.NewSerializerWithOptions(
k8sJson.DefaultMetaFactory, nil, nil,
k8sJson.SerializerOptions{
Yaml: true,
Pretty: true,
Strict: true,
},
)
英文:
Their API has updated, so this is how it looks like:
import k8sJson "k8s.io/apimachinery/pkg/runtime/serializer/json"
...
serializer := k8sJson.NewSerializerWithOptions(
k8sJson.DefaultMetaFactory, nil, nil,
k8sJson.SerializerOptions{
Yaml: true,
Pretty: true,
Strict: true,
},
)
答案3
得分: 1
package main
import (
"os"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
)
func main() {
ns := &v1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
}
e := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil)
//err := e.EncodeToStream(ns, os.Stdout)
err := e.Encode(ns, os.Stdout)
if err != nil {
panic(err)
}
}
英文:
package main
import (
"os"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
)
func main() {
ns := &v1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
}
e := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil)
//err := e.EncodeToStream(ns, os.Stdout)
err := e.Encode(ns, os.Stdout)
if err != nil {
panic(err)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论