


How to get 'friendly' responses from Kubernetes APIServer using ReST interface
php editor Apple will introduce to you how to use the ReST interface to obtain a "friendly" response from the Kubernetes APIServer. Kubernetes is a popular container orchestration platform that provides APIs to manage and monitor various resources in the cluster. By using the ReST interface, we can easily interact with the Kubernetes APIServer and obtain the information we need. In this article, we will explore in detail how to authenticate, send requests and handle responses using the ReST interface, as well as how to handle possible error conditions. Whether you are new to Kubernetes or an experienced Kubernetes user, this article will provide you with useful guidance and practical advice.
Question content
I am using golang client-go library to communicate with kubernetes api server at rest get, post level. The response received is not a well-formed json structure, nor is it a "kind" of api object.
The program fragment is:
kubeconfig := filepath.join( os.getenv("home"), ".kube", "config", ) config, err := clientcmd.buildconfigfromflags("", kubeconfig) if err != nil { log.fatal(err) } config.negotiatedserializer = scheme.codecs.withoutconversion() groupversion, _ := schema.parsegroupversion("api/v1") config.groupversion = &groupversion config.contenttype = "application/json" config.acceptcontenttypes = "application/json" examplerestclient, err := rest.restclientfor(config) if err != nil { panic(err) } var statuscode int var contenttype string response, err := examplerestclient. get(). resource("nodes"). do(context.background()). statuscode(&statuscode). contenttype(&contenttype). get() if err != nil { panic(err) } fmt.printf("content-type is %s\n", contenttype) fmt.printf("status code is %d\n", statuscode) fmt.printf("received response %v\n", response)
The response starts with:
status code is 200 received response &nodelist{listmeta:{ 17299 <nil>},items:[]node{node{objectmeta:{dev-cluster-control-plane 7fe038c9-8be6-41a9-9f3f-5900abb0e34b 16922 0 2023-02-19 16:32:44 +0530 ist <nil> <nil> map[beta.kubernetes.io/arch:amd64 beta.kubernetes.io/os:linux kubernetes.io/arch:amd64 kubernetes.io/hostname:dev-cluster-control-plane kubernetes.io/os:linux node-role.kubernetes.io/control-plane: node.kubernetes.io/exclude-from-external-load-balancers:] map[kubeadm.alpha.kubernetes.io/cri-socket:unix:///run/containerd/containerd.sock node.alpha.kubernetes.io/ttl:0 volumes.kubernetes.io/controller-managed-attach-detach:true] [] [] ...
I expect output similar to what the following command returns:
$ kubectl get --raw /api/v1/nodes {"kind":"NodeList","apiVersion":"v1","metadata":{"resourceVersion":"17481"},"items":[{"metadata":{"name":"dev-cluster-control-plane","uid":"7fe038c9-8be6-41a9-9f3f-5900abb0e34b","resourceVersion":"17351","creationTimestamp":"2023-02-19T11:02:44Z","labels":{"beta.kubernetes.io/arch":"amd64","beta.kubernetes.io/os":"linux","kubernetes.io/arch":"amd64","kubernetes.io/hostname":"dev-cluster-control-plane","kubernetes.io/os":"linux","node-role.kubernetes.io/control-plane":"","node.kubernetes.io/exclude-from-external-load-balancers":""},"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"unix:///run/containerd/containerd.sock","node.alpha.kubernetes.io/ttl":"0" ...
Solution
The response received is not a well-formed json structure
I think you are confused about how the client-go
module works.
The response from the rest api is definitely a well-formed json response, but this will be unmarshaled into a go data structure (such as this).
If you want to access the returned nodes, you can interact with the results using standard go syntax:
response, err := examplerestclient. get(). resource("nodes"). do(context.background()). statuscode(&statuscode). contenttype(&contenttype). get() if err != nil { panic(err) } nodes := response.(*v1.nodelist) for _, node := range nodes.items { fmt.printf("name: %s\n", node.objectmeta.getname()) fmt.printf("addresses:\n") for _, addr := range node.status.addresses { fmt.printf(" %s: %s\n", addr.type, addr.address) } }
I expect output similar to what the following command returns:
Why?client-go
Bindings return data useful to your go code. If you want to generate json output, you need to explicitly marshal the resource to json format:
response, err := exampleRestClient. Get(). Resource("nodes"). Do(context.Background()). StatusCode(&statusCode). ContentType(&contentType). Get() if err != nil { panic(err) } out, err := json.Marshal(response) fmt.Print(string(out))
The above is the detailed content of How to get 'friendly' responses from Kubernetes APIServer using ReST interface. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys
