Home > Backend Development > Golang > How do I Create and Get Custom Kubernetes Resources using RESTClient?

How do I Create and Get Custom Kubernetes Resources using RESTClient?

DDD
Release: 2024-10-29 01:26:30
Original
929 people have browsed it

How do I Create and Get Custom Kubernetes Resources using RESTClient?

Create/Get a Custom Kubernetes Resource

Creating a Custom Kubernetes Resource

To create a custom Kubernetes resource, such as the KongPlugin example provided, use the RESTClient() method to specify the path to the resource. You can use the fluent interface:

<code class="go">data, err := clientset.RESTClient().
        Get().
        AbsPath("/apis/<api>/<version>").
        Namespace("<namespace>").
        Resource("kongplugins").
        Name("kongplugin-sample").
        DoRaw(context.TODO())</code>
Copy after login

Or specify the path manually:

<code class="go">data, err := clientset.RESTClient().
        Get().
        AbsPath("/apis/<api>/<version>/namespaces/<namespace>/kongplugins/kongplugin-sample").
        DoRaw(context.TODO())</code>
Copy after login

You can find the AbsPath in the self-link of the custom resource.

Getting a Custom Kubernetes Resource

Similarly, to get a custom Kubernetes resource, use the RESTClient() method to specify the path to the resource. Marshall the data to be posted using the json package and use the Body() method to send the data:

<code class="go">kongPlugin := &KongPlugin{
        TypeMeta: metav1.TypeMeta{
            APIVersion: "<api>/<version>",
            Kind:       "KongPlugin",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name:      "kongplugin-sample",
            Namespace: "<namespace>",
        },
        ...}}

body, err := json.Marshal(kongPlugin)

data, err := clientset.RESTClient().
        Post().
        AbsPath("/apis/<api>/<version>/namespaces/<namespace>/kongplugins").
        Body(body).
        DoRaw(context.TODO())</code>
Copy after login

Note that the argument of the Body() method is an empty interface, allowing you to use different types of arguments according to the Kubernetes documentation.

The above is the detailed content of How do I Create and Get Custom Kubernetes Resources using RESTClient?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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