生成Kubernetes类型的YAML清单文件。

huangapple go评论82阅读模式
英文:

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)
	}
}

huangapple
  • 本文由 发表于 2016年1月27日 18:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/35035464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定