Create custom resources using the go Kubernetes client

PHPz
Release: 2024-02-08 21:24:34
forward
599 people have browsed it

使用 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)
}
Copy after login

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
Copy after login

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)
Copy after login

Solution

Found the problem. I forgot to initialize the client

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

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!