Create custom resource with go Kubernetes client

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

Create custom resource with go Kubernetes client

问题

我想使用Go Kubernetes客户端基于已部署的CRD部署自定义资源。根据客户端的文档,我将示例调整为以下形式:

u := &unstructured.Unstructured{}
u.Object = map[string]interface{}{
    "metadata": map[string]interface{}{
        "name": task.Name,
    },
    "spec": map[string]interface{}{
        "steps": []interface{}{
            map[string]interface{}{
                "image":   "ubuntu",
                "name":    "hello",
                "command": []interface{}{"echo"},
                "args":    []interface{}{"Hello World!"},
            },
        },
    },
}
u.SetGroupVersionKind(schema.GroupVersionKind{
    Group:   "tekton.dev",
    Version: "v1beta1",
    Kind:    "Task",
})

err := c.Create(context.Background(), u)
if err != nil {
    logger.Error("Error creating TektonTask!", "ERR", err)
} else {
    logger.Info("Created TektonTask.", "Task", u)
}

当我尝试执行该代码时,我没有从logger.Error得到反馈,而是出现了恐慌:

runtime error: invalid memory address or nil pointer dereference
goroutine 12

所有的操作都在处理HTTP请求的内部运行,但因为我之前已经使用过其他(非CRD基础)资源,所以我认为这不是问题所在。在扩展日志记录时,我发现在以下代码行创建资源之前一切正常:

err := c.Create(context.Background(), u)
英文:

I want to deploy a custom resource based on an already deployed CRD using the go Kubernetes client. Based on the documentation of the client I adapted the example to look like this:

u := &unstructured.Unstructured{}
u.Object = map[string]interface{}{
	"metadata": map[string]interface{}{
		"name": task.Name,
	},
	"spec": map[string]interface{}{
		"steps": []interface{}{
			map[string]interface{}{
				"image": "ubuntu",
				"name":  "hello",
				"command": []interface{}{
					"echo",
				},
				"args": []interface{}{
					"Hello World!",
				},
			},
		},
	},
}
u.SetGroupVersionKind(schema.GroupVersionKind{
	Group:   "tekton.dev",
	Version: "v1beta1",
	Kind:    "Task",
})

err := c.Create(context.Background(), u)
if err != nil {
	logger.Error("Error creating TektonTask!", "ERR", err)
} else {
	logger.Info("Created TektonTask.", "Task", u)
}

When I try to execute the code I don't get a feedback from the logger.Error but a panic with:

runtime error: invalid memory address or nil pointer dereference
goroutine 12

Everything is running inside a http request handling but because I used that with other (non crd based) resources already I would assume that this is not the issue.
When expanding logging I saw that everything works until the creation of the resource with the line

err := c.Create(context.Background(), u)

答案1

得分: 0

发现问题了。我忘记用以下代码初始化客户端:

c, err := client.New(config.GetConfigOrDie(), client.Options{})
英文:

Found the issue. I forgot to initialise the client with

c, err := client.New(config.GetConfigOrDie(), client.Options{})

huangapple
  • 本文由 发表于 2023年4月14日 17:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76013705.html
匿名

发表评论

匿名网友

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

确定