Home >Backend Development >Golang >How to Create and Retrieve Custom Kubernetes Resources using the Go Client Library?
In Kubernetes, you can define and manage custom resources, which extend the capabilities of the platform. Creating and getting custom resources can be done programmatically using the Go client library.
To create a custom resource such as the KongPlugin, you'll need to use the RESTClient of the Kubernetes clientset. Here's how:
<code class="go">// Create a KongPlugin custom resource. kongPlugin := &KongPlugin{ TypeMeta: metav1.TypeMeta{ APIVersion: "configuration.konghq.com/v1", Kind: "KongPlugin", }, ObjectMeta: metav1.ObjectMeta{ Name: "add-response-header", }, Config: KongPluginConfig{ Add: KongPluginConfigAdd{ Headers: []string{"demo: injected-by-kong"}, }, }, Plugin: "response-transformer", } body, err := json.Marshal(kongPlugin) if err != nil { panic(err) } data, err := clientset.RESTClient(). Post(). AbsPath("/apis/configuration.konghq.com/v1/namespaces/" + namespace + "/kongplugins"). Body(body). DoRaw(context.TODO())</code>
To retrieve a custom resource, you can use the Get() method of the RESTClient:
<code class="go">// Get the KongPlugin custom resource. data, err := clientset.RESTClient(). Get(). AbsPath("/apis/configuration.konghq.com/v1/namespaces/" + namespace + "/kongplugins/add-response-header"). DoRaw(context.TODO())</code>
AbsPath() Notes:
The above is the detailed content of How to Create and Retrieve Custom Kubernetes Resources using the Go Client Library?. For more information, please follow other related articles on the PHP Chinese website!