Home >Backend Development >Golang >Create custom resources using the go Kubernetes client
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.
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)
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!