How to get pods/binding instance and modify pod data in kubernetes

Viewed 146

I have a project to modify the pod, and the modification depends on the pod nodeName field. I use the following webhook to intercept the pod, but it cannot intercept the update operation after the kube-scheduler allocates the node

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
...
...
  rules:
  - operations: ["UPDATE"]
    apiGroups: [""]
    apiVersions: ["v1"]
    resources: ["pods"]

Then I try to use interception pods/binding, but when I try to parse the pod binding, I find that the Binding type has been deprecated

  rules:
  - operations: ["UPDATE"]
    apiGroups: [""]
    apiVersions: ["v1"]
    resources: ["pods/binding"]
k8s.io/api/core/v1/types.go
// Binding ties one object to another; for example, a pod is bound to a node by a scheduler.
// Deprecated in 1.7, please use the bindings subresource of pods instead.
type Binding struct {

But I didn't find the bindings subresource of pods. Where is this resource and how to parse it to get the corresponding pod instance?

I really appreciate any help with this.

1 Answers

The API’s structure that we are talking about here is as the following example:

APIResponsiveness: Top latency metric: {Resource:pods Subresource:binding Verb:POST …

"bindings subresource of pods" references to the subresource like "podAffinity", "nodeAffinity", "nodeSelector" etc. Take a look into these URLs for more information about this part Binding [core/v1] and which api can implement“the bindings subresource of pods”.

Now, regarding the POD’s data modification, first you need to have in mind that we SHOULDN’T edit specifications of an existing POD other than the following ones.

*spec.containers[*].image
spec.initContainers[*].image
spec.activeDeadlineSeconds
spec.tolerations*

So, to modify the POD’s properties, there are 2 ways to do it:

1.Run the kubectl edit pod <pod name> command. You are gonna get the pod specification through an editor. Proceed to edit the properties that you need to. When saving it, you are going to be denied, as you are attempting to edit a field on the POD that is not editable, but a copy of the file with the changes you did is going to be created as a YAML file in a temporary location. Then delete the existing POD using command: kubectl delete pod <pod name>. Finally, create a new POD with changes using the temporary file kubectl create -f <newly created yaml file with changes>.

2.Extract the POD’s definition to a YAML file using kubectl get pod <pod name> -o yaml > my-new-pod.yaml Modify the exported file using an editor (vi editor), save it with vi my-new-pod.yaml Delete the current POD kubectl delete pod <pod-name> And finally create a new pod with the edited file kubectl create -f my-new-pod.yaml

Use the following URL as a guide.

Related