英文:
Kubernetes operator create Deployment using yaml template
问题
我正在尝试根据这个链接创建自己的Kubernetes Operator:link。在Reconcile函数中,我需要创建多个Deployments,每个Deployment的某些属性(例如名称)都会有所不同,而且配置非常庞大。我想知道是否有一种方法可以提供一个YAML模板文件,并读取该文件以获取appsv1.Deployment对象,而不是像上面的代码一样使用appsv1.Deployment并在其中创建每个属性。
可以使用一些便捷的工具函数来实现类似下面的方式吗?
dep := utils.parseYaml(deploymentYamlFile)
英文:
I am trying my hands on creating my own kubernetes operator by following this link. In the Reconcile function, I need to create multiple Deployments and each will vary in some attributes (like name for e.g.) and the configuration is huge. Instead of creating the deployment by using appsv1.Deployment and creating each attributes within it (like below code), is there a way wherein I can provide a yaml template file and read this file to obtain the appsv1.Deployment object?
dep := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: customName,
Namespace: m.Namespace,
},
Spec: appsv1.DeploymentSpec{
Strategy: appsv1.DeploymentStrategy{
Type: "RollingUpdate",
},
... and so on
Instead of above, can something like below possible with some handy util functions?
dep := utils.parseYaml(deploymentYamlFile)
答案1
得分: 2
是的,你可以将你的Deployment
配置文件保存为一个yaml文件,并在代码中读取它。
假设你的文件结构如下:
example.go
manifests/deployment.yaml
在example.go
文件中,你可以这样写:
import (
"io/ioutil"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/yaml"
)
func example() {
var bs []byte
{
bs, err = ioutil.ReadFile("manifests/deployment.yaml")
if err != nil {
// 处理错误
}
}
var deployment appsv1.Deployment
err = yaml.Unmarshal(bs, &deployment)
if err != nil {
// 处理错误
}
// 现在你可以在`deployment`变量中获取到你的Deployment配置
}
英文:
Yes, you can have your Deployment
in a yaml file and read it in code.
Given this file structure:
example.go
manifests/deployment.yaml
You would have something like this in example.go
:
import (
"io/ioutil"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/yaml"
)
func example() {
var bs []byte
{
bs, err = ioutil.ReadFile("manifests/deployment.yaml")
if err != nil {
// handle err
}
}
var deployment appsv1.Deployment
err = yaml.Unmarshal(bs, &deployment)
if err != nil {
// handle err
}
// now you have your deployment load into `deployment` var
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论