Is there any API or go programming logic to get capacity of node in a kubernetes cluster? - kubernetes

Using kubectl describe nodes, i am able to get the capacity of resources(memory,cpu) of a node. I want to get the same via go client or kube API (if available). Can anyone help me out?
i am using minikube version: v1.7.2
kubectl version :
Client : GitVersion:"v1.16.3"
Server : GitVersion:"v1.16.2"
I am using metric-server to access the kubernetes resource.
Expected result:
Capacity of resources should be accessible through go program or kube API

There isn't any API call you could use to get kubectl describe nodes this is because this command is generating all the output.
Kubectl retrieves all relevant pods (every pod that isn't failed or succeeded) on a node and sums up all their resource definitions.
You can look into the code and find the function responsible for generating information about node here.
Same for collecting all requests and limits for pods, function is available here and it's called getPodsTotalRequestsAndLimits
Lastly the function what puts all that together can be seen here.
There is a really nice article about Kubernetes API: Allocatable Node Resources? The author is doing exactly what you are asking for but using Python.

Kubernetes Client libraries are the ones you need to look at https://kubernetes.io/docs/reference/using-api/client-libraries/#officially-supported-kubernetes-client-libraries
The following client libraries are officially maintained by Kubernetes SIG API Machinery.
Language Client Library
Go github.com/kubernetes/client-go/
Python github.com/kubernetes-client/python/
Java github.com/kubernetes-client/java
dotnet github.com/kubernetes-client/csharp
JavaScript github.com/kubernetes-client/javascript
Haskell github.com/kubernetes-client/haskell
Community-maintained client libraries
The following Kubernetes API client libraries are provided and maintained by their authors, not the Kubernetes team.
Language Client Library
Clojure github.com/yanatan16/clj-kubernetes-api
Go github.com/ericchiang/k8s
Java (OSGi) bitbucket.org/amdatulabs/amdatu-kubernetes
Java (Fabric8, OSGi) github.com/fabric8io/kubernetes-client
Lisp github.com/brendandburns/cl-k8s
Lisp github.com/xh4/cube
Node.js (TypeScript) github.com/Goyoo/node-k8s-client
Node.js github.com/tenxcloud/node-kubernetes-client
Node.js github.com/godaddy/kubernetes-client
Node.js github.com/ajpauwels/easy-k8s
Perl metacpan.org/pod/Net::Kubernetes
PHP github.com/maclof/kubernetes-client
PHP github.com/allansun/kubernetes-php-client
PHP github.com/travisghansen/kubernetes-client-php
Python github.com/eldarion-gondor/pykube
Python github.com/mnubo/kubernetes-py
Python github.com/tomplus/kubernetes_asyncio
Ruby github.com/Ch00k/kuber
Ruby github.com/abonas/kubeclient
Ruby github.com/kontena/k8s-client
Rust github.com/clux/kube-rs
Rust github.com/ynqa/kubernetes-rust
Scala github.com/doriordan/skuber
dotNet github.com/tonnyeremin/kubernetes_gen
DotNet (RestSharp) github.com/masroorhasan/Kubernetes.DotNet
Elixir github.com/obmarg/kazan
Elixir github.com/coryodaniel/k8s
Haskell github.com/kubernetes-client/haskell

Related

How can I remotely call the kube-apiserver command via REST operations?

There is a command kube-apiserver --feature-gates=APIPriorityAndFairness=true --runtime-config=flowcontrol.apiserver.k8s.io/v1beta1=true,flowcontrol.apiserver.k8s.io/v1beta2=true. But it seems like run on the master machine.
Now I want to remotely call the command in my laptop. I have connected the master machine via kubeconfig already.
Which statement or format of statement should I use to accomplish this function?
Please refer Kubernetes API access documentation how you can programmatically send API requests. Kubernetes supports various mechanisms and various clients in GO, Python, Java etc to send api requests. Below is the sample python code which will list all the pods from all namespaces.
from kubernetes import client, config
config.load_kube_config()
v1=client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Also when follow command is executed "kubectl get pods -v 9" it will give verbose information of rest request which kubectl tool will send to api server, this will help you to understand how to construct the api requests.
I don't know if it is a real question.
This command kube-apiserver --feature-gates=APIPriorityAndFairness=true --runtime-config=flowcontrol.apiserver.k8s.io/v1beta1=true,flowcontrol.apiserver.k8s.io/v1beta2=true seems not like kubectl xxx. It's not a service that cluster offers to user to use.
I ssh the master machine to modify some flag or properties of the api-server.

Get all resources created by a helm release using k8s rest API

Is there a way I can get a list of all helm releases, and then all resources that have been created by this release, using the Kubernetes REST API?
I mean something similar to helm and kubectl commands
helm list
kubectl get all --all-namespaces -l=release-name
but using REST - https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/
Helm is built on top of the Kubernetes API layer, in that it creates objects like Deployments and Services and stores its state in Secrets; but a Helm release is not itself a Kubernetes object and you can't directly use the Kubernetes API to access it.
If your application is in Go, then Helm 3 includes a Go SDK, which essentially exposes most of the helm binary as Go-native library calls. This isn't a network-visible API, though, and if your application is in any other language you won't be able to integrate with it.
If instead it's more important to be able to manipulate your application using Kubernetes's REST API than to be using Helm proper, one alternative is to write a program (a Kubernetes controller) that interacts with the Kubernetes API, and have it be driven by a custom resource, a Kubernetes object that would include your application-specific configuration. This pair is commonly called an operator. Much more so than a Helm chart, though, this involves actually writing code and not just dropping in bits of Kubernetes YAML. (And, much more so than a Helm chart, you can unit-test more complex logic using your host language's native test tools.)
But in short, no, unless you can use the Helm Go SDK then there's not a good way to programmatically interact with a Helm chart beyond shelling out to the helm command.

kubectl vs json api for customdefinition or operator

is it possible to use json api instead of all kubectl commands.
Also how would be update a resource or custom definition in that case.
You could take a look at API client libraries.
To call the Kubernetes API from a programming language, you can use client libraries. Officially supported client libraries:
Kubernetes Go client library
Kubernetes Python client library
Kubernetes Java client library
Kubernetes JavaScript client library
To write applications using the Kubernetes REST API, you do not need to implement the API calls and request/response types yourself. You can use a client library for the programming language you are using.
Client libraries often handle common tasks such as authentication for you. Most client libraries can discover and use the Kubernetes Service Account to authenticate if the API client is running inside the Kubernetes cluster, or can understand the kubeconfig file format to read the credentials and the API Server address.
The two paths that support extending the API with custom resources are:
CustomResourceDefinition for basic CRUD needs.
aggregator for a full set of Kubernetes API semantics to
implement their own apiserver.
For example there is a way of creating crd resource with python and some code examples.
list all pods
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
watch on namespace object:
from kubernetes import client, config, watch
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
count = 10
w = watch.Watch()
for event in w.stream(v1.list_namespace, _request_timeout=60):
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
count -= 1
if not count:
w.stop()
print("Ended.")
More examples can be found in examples folder.

How to access the Kubernetes API in Go and run kubectl commands

I want to access my Kubernetes cluster API in Go to run kubectl command to get available namespaces in my k8s cluster which is running on google cloud.
My sole purpose is to get namespaces available in my cluster by running kubectl command: kindly let me know if there is any alternative.
You can start with kubernetes/client-go, the Go client for Kubernetes, made for talking to a kubernetes cluster. (not through kubectl though: directly through the Kubernetes API)
It includes a NamespaceLister, which helps list Namespaces.
See "Building stuff with the Kubernetes API — Using Go" from Vladimir Vivien
Michael Hausenblas (Developer Advocate at Red Hat) proposes in the comments documentations with using-client-go.cloudnative.sh
A versioned collection of snippets showing how to use client-go.

pod creation through kubernete api

I want to create pod using kubernete API from a code written in java. I went through the documentation available https://kubernetes.io/docs/api-reference/v1.6/#pod-v1-core but did not find it any helpful. I need to understand how the values are received to the respective API's and which fields are mandatory and optional while creation of pods.
From https://github.com/kubernetes/community/blob/master/contributors/devel/client-libraries.md
Java (OSGi)
Java (Fabric8, OSGi)