Kubernetes python API get instances of CRD - kubernetes

I'm currently using the Python APIs for Kubernetes and I have to:
Retrieve the instance of a custom resource name FADepl.
Edit the value of that instance.
In the terminal, I would simply list all FADepls with kubectl get fadepl and then edit the right one using kubectl edit fadepl <fadepl_name>. I checked the K8s APIs for Python but I can't find what I need. Is it something I can do with the APIs?
Thank you in advance!

You're right. Using get_namespaced_custom_object you can retrieve the instance. This method returns a namespace scoped custom object. By default it uses a synchronous HTTP request.
Since the output of that method returns an object, you can simply replace it using replace_cluster_custom_object.
Here you can find implementation examples.
See also whole list of API Reference for Python.

You can use list_cluster_custom_object method from kubernetes.client.CustomObjectsApi.
Let's say that I need to get all calico globalnetworksets instances which are a CRD from calico project.
So first, we need to retrieve some crd data using kubectl as follows:
# get api group, version and plural
> kubectl api-resources -o wide | grep globalnetworkset
globalnetworksets crd.projectcalico.org/v1 false GlobalNetworkSet
--------------- ------------------- --
`plural` api group v
With that in mind, we implement list_cluster_custom_object
from kubernetes.client import CustomObjectsApi
group = "crd.projectcalico.org"
v = "v1"
plural = "globalnetworksets"
global_network_sets = CustomObjectsApi.list_cluster_custom_object(group, v, plural)

I found the get_namespaced_custom_object call that should do the trick

Related

GKE pod replica count in cluster

How can we obtain the gke pod counts running in the cluster? I found there are ways to get node count but we needed pod count as well. it will be better if we can use something with no logging needed in gcp operations.
You can do it with Kubernetes Python Client library as shown in this question, posted by Pradeep Padmanaban C, where he was looking for more effective way of doing it, but his example is actually the best what you can do to perform such operation as there is no specific method which would allow you just to count pods without retrieving their entire json manifests:
from kubernetes import client , config
config.load_kube_config()
v1= client.CoreV1Api()
ret_pod = v1.list_pod_for_all_namespaces(watch=False)
print(len(ret_pod.items))
You can also use a different method, which allows to retrieve pods only from specific namespace e.g.:
list_namespaced_pod("default")
In kubectl way you can do it as follows (as proposed here by RammusXu):
kubectl get pods --all-namespaces --no-headers | wc -l
You can directly access the kubernetes API using a restful API call. You will need to make sure you provide the authentication token in your call by including a bearer token.
Once you are able to query the api server directly, you can use GET <master_endpoint>/api/v1/pods to list all the pods in the cluster. You can also search for specific namespaces by specifying the namespace /api/v1/namespaces/<namespace>/pods.
Keep in mind that the kubectl cli tool is just a wrapper for API calls, each kubectl command will form a RESTful API call in a similar format to the one listed above, so any interaction you have with the cluster using kubectl can also be achieved through RESTful API calls

Kube API for Listing the CustomResource instances in a namespace (or on a cluster) in Kubernetes

Using command-line utility kubectl we can list a custom resource instances as follows
kubectl get <customresource_kind>
In a similar fashion, do we have a REST API to achieve the same? i.e. the API takes the Kind of the CustomResource and lists all the instances created?
I am referring to this API Reference :
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/
You can list crds like every other API resources using Kubernetes REST API.
The final URL path will differ, depending on object's scope: Cluster or Namespaced.
The general rule for constructing the URL path is described here in official documentation.
Just to give you an example based on calico's clusterinformations.crd.projectcalico.org (v1):
kubectl proxy --port=8080 &
curl http://localhost:8080/apis/crd.projectcalico.org/v1/clusterinformations | jq '.items[].metadata.name
"default" <- I have only one instance of this type of custom resource

Get names of all deployment configs with no running pods

Is there a simple method (that won't require googling at every use) to get names of all deployment configs with no running pods (scaled to 0) in Kubernetes / Openshift? Methods without JSON tokens and awk please.
The docs of oc get dc --help are way too long to decipher for the occasional need.
The only CLI arg for advanced filtering without working with JSON is a --field-selector, but it has a limited scope which not include spec.replicas field.
So, there will be some magic around JSON with other flag - jsonpath.
Here is a command to filter and print names of all deployments which are scaled to 0:
kubectl get deployments --all-namespaces -o=jsonpath='{range .items[?(#.spec.replicas==0)]}{.metadata.name}{"\n"}{end}'
Jsonpath reference is here.

List Kubernetes resources by apiVersion

Is there a easy way to list all kubernetes objects related to an API version?
Lets say, API version apps/v1beta1 is getting deprecated and I want to know if I have any objects in my cluster using this version, how can I find such objects?
you can do something similar like this
kubectl get pod -o=custom-columns=NAME:.metadata.name,API-version:.metadata.owner_references[].api_version
by using kubectl just print respective data and api version
The reason I asked this question was that I was upgrading my kubernetes cluster from v1.15 to v1.16 and this brings a lot of breaking changes
The kubepug tool allowed me to easily find a list of resources that I need to change to be able to upgrade seamlessly from 1.15 to 1.16
Edit:
Another alternative is pluto command line tool to get information about resources which might have deprecated or removed API versions. This is helpful specially if you use helm extensively since pluto can look at helm state.
To list Kubernetes resource versions use this command:
kubectl api-resources
And if the you know what resource you are searching for then just filter it from the list using grep:
kubectl api-resources | grep the_resource_name_you_want
In the below example we search for the api version of 'persistentvolumes' resource:
kubectl api-resources | grep persistentvolumes
Result:
deployments deploy apps/v1 true Deployment
There are additional options to get the resource version:
Use the official documentation:
The official and updated with the newest versions is in the Kubernetes API documentation
The left navigation bar lists the resources, click on one & the version is the first row of the page.

Can somene explain the different Kubernetes yaml files and types?

First off, I'm completely new with Kubernetes so I may have missed something completely obvious but the documentation is exactly helping, so I'm turning to you guys for help.
I'm trying to figure out just how many types of "deployment files" there are for Kubernetes. I call them "deployment files" because I really don't know what else to call them and they're usually associated with a deployment.
So far, every yml/yaml file I've seen start like this:
apiVersion:
kind: << this is what I'm asking about >>
metadata:
And so far I have seen this many "kind"(s)
ClusterConfig
ClusterRole
ClusterRoleBinding
CronJob
Deployment
Job
PersistentVolumeClaim
Pod
ReplicationController
Role
RoleBinding
Secret
Service
ServiceAccount
I'm sure there are many more. But I can't seem to find a location where they are listed and the contexts broken down.
So what I want to know is this,
Where can I find an explanation for these yaml files?
Where can I learn about the different kinds?
Where can I get a broken down explanation of the minimum required fields/values are for any of these?
Are there templates for these files?
Thanks
This question will need a blog to answer but still in short you can try these options and command to learn from your kubectl CLI.
Learn to use kubectl explain command which shows you a list of Kubernetes objects:
$ kubectl explain
You can get detailed information about any of listed resources using this syntax
`$ kubectl explain pod
$ kubectl explain pod.spec
$ kubectl explain pod.spec.containers`
Or you can get yam template of the object by adding --recursive flag to explain command.
$ kubectl explain pod --recursive
This will also give you official document link.
So in short running kubectl explain with recursive option will list every thing.
When you are talking about specific yaml file containing the definition of specific kubernetes object, you can call them yaml manifests or simply yaml definition files. Using word Deployment for all of them isn't a good idea as there is already specific resource type defined and called by this name in kubernetes. So it's better you don't call them all deployments for consistency.
I'm sure there are many more. But I can't seem to find a location
where they are listed and the contexts broken down.
Yes, there are a lot more of them and you can list those which are available by running:
kubectl api-resources
These different objects are actually called api-resources. As you can see they are listed in three columns: NAME, SHORTNAMES, APIGROUP, NAMESPACED and KIND
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
Note that the name of resource corresponds to its KIND but it is slightly different. NAME simply describes resource types as we are referring to them e.g. using kubectl command line utility. Just to give one example, when you want to list pods available in your cluster you simply type kubectl get pods. You don't have to use resource kind i.e. Pod in this context. You can but you don't have to. So kubectl get Pod or kubectl get ConfigMap will also return desired result. You can also refer to them by their shournames so kubectl get daemonsets and kubectl get ds are equivalent.
It's totally different when it comes to specific resource/object definition. In context of yaml definition file we must to use proper KIND of the resource. They are mostly start with capital letter and are written by co called CamelCase but there are exceptions from this rule.
I really recommend you to familiarize with kubernetes documentation. It is very user-friendly and nicely explains both key kubernetes concepts as well as all very tiny details.
Here you have even more useful commands for exploring API resources:
kubectl api-resources --namespaced=true # All namespaced resources
kubectl api-resources --namespaced=false # All non-namespaced resources
kubectl api-resources -o name # All resources with simple output (just the resource name)
kubectl api-resources -o wide # All resources with expanded (aka "wide") output
kubectl api-resources --verbs=list,get # All resources that support the "list" and "get" request verbs
kubectl api-resources --api-group=extensions # All resources in the "extensions" API group
As #wargre already suggested in his comment, kubernetes official documentetion is definitely the best place to start as you will find there very detailed description of every resource.
Understanding Kubernetes Objects
You may start from reading this article: Understanding Kubernetes Objects
Kubernetes Objects are persistent entities in the Kubernetes system. Kubernetes uses these entities to represent the state of your cluster. Specifically, they can describe:
What containerized applications are running (and on which nodes)
The resources available to those applications
The policies around how those applications behave, such as restart policies, upgrades, and fault-tolerance
A Kubernetes object is a “record of intent”–once you create the object, the Kubernetes system will constantly work to ensure that object exists. By creating an object, you’re effectively telling the Kubernetes system what you want your cluster’s workload to look like; this is your cluster’s desired state.
K8s API reference
A detailed description of all objects can be found in the Kubernetes API reference guide.