Create custom resource with go Kubernetes client

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

Create custom resource with go Kubernetes client

问题

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

  1. u := &unstructured.Unstructured{}
  2. u.Object = map[string]interface{}{
  3. "metadata": map[string]interface{}{
  4. "name": task.Name,
  5. },
  6. "spec": map[string]interface{}{
  7. "steps": []interface{}{
  8. map[string]interface{}{
  9. "image": "ubuntu",
  10. "name": "hello",
  11. "command": []interface{}{"echo"},
  12. "args": []interface{}{"Hello World!"},
  13. },
  14. },
  15. },
  16. }
  17. u.SetGroupVersionKind(schema.GroupVersionKind{
  18. Group: "tekton.dev",
  19. Version: "v1beta1",
  20. Kind: "Task",
  21. })
  22. err := c.Create(context.Background(), u)
  23. if err != nil {
  24. logger.Error("Error creating TektonTask!", "ERR", err)
  25. } else {
  26. logger.Info("Created TektonTask.", "Task", u)
  27. }

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

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

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

  1. 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:

  1. u := &unstructured.Unstructured{}
  2. u.Object = map[string]interface{}{
  3. "metadata": map[string]interface{}{
  4. "name": task.Name,
  5. },
  6. "spec": map[string]interface{}{
  7. "steps": []interface{}{
  8. map[string]interface{}{
  9. "image": "ubuntu",
  10. "name": "hello",
  11. "command": []interface{}{
  12. "echo",
  13. },
  14. "args": []interface{}{
  15. "Hello World!",
  16. },
  17. },
  18. },
  19. },
  20. }
  21. u.SetGroupVersionKind(schema.GroupVersionKind{
  22. Group: "tekton.dev",
  23. Version: "v1beta1",
  24. Kind: "Task",
  25. })
  26. err := c.Create(context.Background(), u)
  27. if err != nil {
  28. logger.Error("Error creating TektonTask!", "ERR", err)
  29. } else {
  30. logger.Info("Created TektonTask.", "Task", u)
  31. }

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

  1. runtime error: invalid memory address or nil pointer dereference
  2. 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

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

答案1

得分: 0

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

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

Found the issue. I forgot to initialise the client with

  1. 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:

确定