red888
red888

Reputation: 31520

Kubectl patch is not updating the apiVersion correctly

I run kubent and identify an outdated apiVersion

> kubent
...
KIND                  NAMESPACE                                                  NAME                                               API_VERSION      REPLACE_WITH (SINCE)
PodDisruptionBudget   mynamespace                                                mypdb                                              policy/v1beta1   policy/v1 (1.21.0)

I try to patch the resource in place but that doesn't seem to work:

kubectl patch PodDisruptionBudget mypdb --namespace mynamespace -p'{"apiVersion":"policy/v1"}'
poddisruptionbudget.policy/mypdb patched (no change)

Running kubent still shows it's outdated.

Why doesn't patch work for updating apiVersion? I need to do this for many resources in many namespaces I want to script it out.

Also, when I run kubectl edit PodDisruptionBudget mypdb --namespace mynamespace it shows the apiVersion is the updated one ("policy/v1"), but kubent still shows it as outdated (policy/v1beta1).

Edit

Per the suggested answer I did this, but it did not work. It applied without error, but running kubent again still shows the resources outdated:

kubectl get PodDisruptionBudget \
        -A                      \
        -o yaml > updated.yaml  \
        && kubectl apply -f updated.yaml

Edit 2

Maybe kubent is not reporting the apiVersion correctly because after running apply if I run kubectl get poddisruptionbudget.v1.policy -A it returns the same resources kubent says are using outdated versions.

Upvotes: 1

Views: 1580

Answers (1)

CodeWizard
CodeWizard

Reputation: 141946

Here is a solution for you.

kubent

### output:
(⎈ |minikube:default)[23:23:23] [~/repositories/KubernetesLabs] git(master) 🔥 ❱❱❱ kubent
11:23PM INF >>> Kube No Trouble `kubent` <<<
11:23PM INF version 0.5.1 (git sha a762ff3c6b5622650b86dc982652843cc2bd123c)
11:23PM INF Initializing collectors and retrieving data
11:23PM INF Target K8s version is 1.23.3
11:23PM INF Retrieved 52 resources from collector name=Cluster
11:23PM INF Retrieved 0 resources from collector name="Helm v2"
11:23PM INF Retrieved 9 resources from collector name="Helm v3"
11:23PM INF Loaded ruleset name=custom.rego.tmpl
11:23PM INF Loaded ruleset name=deprecated-1-16.rego
11:23PM INF Loaded ruleset name=deprecated-1-22.rego
11:23PM INF Loaded ruleset name=deprecated-1-25.rego
__________________________________________________________________________________________
>>> Deprecated APIs removed in 1.25 <<<
------------------------------------------------------------------------------------------
KIND                  NAMESPACE      NAME                   API_VERSION      REPLACE_WITH (SINCE)
PodDisruptionBudget   istio-system   istio-ingressgateway   policy/v1beta1   policy/v1 (1.21.0)
PodDisruptionBudget   istio-system   istiod                 policy/v1beta1   policy/v1 (1.21.0)

enter image description here


How to update API?

  • The secret is this, when you "get" resource K8S will "update" the apiVersion and will display the correct one.
  • Then you can apply the "update" API back.
  • It will allow you to write script which does it for you as asked
# Get the list of outdated resources in a json format
kubent -o json > outdated.json 

# Check the output
cat outdated.json 

# Grab the desired resource name(s) from the json and loop over them ==> script

Update script:

  • The content of the updated script will be:
# The content of your script will be 
# (loop over the list and run the following command):

 - Get the updated API using kubectl get
 - Save the updated content to file
 - apply the changes

kubectl get <resourceType> \
        <resourceName> \
        -n <namespace> \
        -o yaml > newUpdatedApi.yaml \
        && kubectl apply -f newUpdatedApi.yaml

Demo:


# Print out the outdated resources
kubent 

# Get the updated apiVersion and save to file
# also apply the changes
kubectl get PodDisruptionBudget \
        istiod                  \
        -n istio-system         \
        -o yaml > updated.yaml  \
        && kubectl apply -f updated.yaml

# Check to verify that the updated.yaml indeed have the desired apiVersion
head -1 updated.yaml

# Verify that the "patch" is made
kubent

enter image description here

Upvotes: 3

Related Questions