简体   繁体   English

Kubectl 补丁未正确更新 apiVersion

[英]Kubectl patch is not updating the apiVersion correctly

I run kubent and identify an outdated apiVersion我运行kubent并确定了一个过时的 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 its outdated.运行kubent仍然显示它已经过时。

Why doesn't patch work for updating apiVersion?为什么补丁不能用于更新 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).此外,当我运行kubectl edit PodDisruptionBudget mypdb --namespace mynamespace时,它​​显示 apiVersion 是更新的版本(“policy/v1”),但kubent仍然显示它已过时(policy/v1beta1)。

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)

在此处输入图像描述


How to update API?如何更新 API?

  • The secret is this, when you "get" resource K8S will "update" the apiVersion and will display the correct one.秘诀是,当你“获取”资源时,K8S 将“更新” apiVersion并显示正确的版本。
  • Then you can apply the "update" API back.然后您可以应用“更新”API。
  • 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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM