Home  >  Article  >  Backend Development  >  Create custom resources using the go Kubernetes client

Create custom resources using the go Kubernetes client

PHPz
PHPzforward
2024-02-08 21:24:34622browse

使用 go Kubernetes 客户端创建自定义资源

php editor Xiaoxin brings you an introduction to using the go Kubernetes client to create custom resources. With the development of cloud native technology, more and more enterprises are beginning to adopt Kubernetes as a container orchestration platform. Creating custom resources (CRD) is an important feature of Kubernetes, which can help developers seamlessly integrate their applications and services into Kubernetes. This article will introduce in detail how to create custom resources through the use of go Kubernetes client, allowing you to more flexibly manage and deploy your own applications in Kubernetes.

Question content

I want to use go kubernetes client to deploy custom resources based on deployed crd. Based on the customer's documentation, I modified 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 get no feedback from the logger. Error, but panic:

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

Everything is running within http request handling, but since I've used it with other (non-crd based) resources I don't think that's an issue. When extending logging I found that everything works fine until the resource is created using the line

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

Solution

Found the problem. I forgot to initialize the client

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

The above is the detailed content of Create custom resources using the go Kubernetes client. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete