k8s 的版本迭代很快,虽然主要版本一直没有变化,但是次要版本一直在迭代,2022年一年就发布了三个次要版本,同时不同的次要版本之间 API 资源一直在变化,有新加入的,也有废弃删除的。不同版本的 api 资源版本也有不同,往往不是向下兼容的,比如在低版本中 API 资源版本为 v1beta1,而高版本可能升级为 v1。但是在高版本中不能运行低版本的API资源。
所以在 k8s 版本升级的时候,需要对之前废弃的和删除的 API 资源 做出清理,需要升级的做升级,或者替换为其他的 API 资源。 在这之前,需要一个工具来检查 API 资源对象,那些事已经废弃的,那些将要废弃。
Kubepug 即是这样一个工具,一个升级前检查器,可帮助在迁移到新的主要版本之前在 Kubernetes 资源中找到已弃用和已删除的 API
KubePug/Deprecations 作为一个 kubectl 插件,他可以实现下面的功能:
从特定的 Kubernetes 版本下载 swagger.json
解析此 Json 发现弃用通知
验证当前的 kubernetes 集群或输入文件,检查此已弃用的 API 版本中是否存在对象,允许用户在迁移前进行检查
┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew] └─$kubectl kubepug --k8s-version=v1.22.2 --swagger-dir=./swagger/folder W0108 23:32:02.991528 30270 warnings.go:70] v1 ComponentStatus is deprecated in v1.19+ W0108 23:32:02.995308 30270 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+ RESULTS: Deprecated APIs:
ComponentStatus found in /v1 ├─ ComponentStatus (and ComponentStatusList) holds the cluster validation info. Deprecated: This API is deprecated in v1.19+ -> GLOBAL: scheduler -> GLOBAL: controller-manager -> GLOBAL: etcd-0
PodSecurityPolicy found in policy/v1beta1 ├─ PodSecurityPolicy governs the ability to make requests that affect the Security Context that will be applied to a pod and container. Deprecated in 1.21. -> GLOBAL: controller -> GLOBAL: speaker
┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew] └─$kubectl kubepug --k8s-version=v1.25.2 --swagger-dir=./swagger/folder W0108 23:43:37.187999 41352 warnings.go:70] v1 ComponentStatus is deprecated in v1.19+ W0108 23:43:37.339503 41352 warnings.go:70] policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+ RESULTS: Deprecated APIs:
ComponentStatus found in /v1 ├─ ComponentStatus (and ComponentStatusList) holds the cluster validation info. Deprecated: This API is deprecated in v1.19+ -> GLOBAL: scheduler -> GLOBAL: controller-manager -> GLOBAL: etcd-0
Deleted APIs:
PodSecurityPolicy found in policy/v1beta1 ├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!! -> GLOBAL: controller -> GLOBAL: speaker
通过检查可以看到 ,1.25 之后的废弃和已经删除的 API 资源。如果要升级到 1.25 需要把删除的 API 处理掉。
calicoApiConfig found in /v1 ├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!! -> OBJECT: namespace: default location: /root/ansible//calicoctl.j2
Error: found 1 Deleted APIs and 0 Deprecated APIs time="2023-01-08T23:50:39+08:00" level=error msg="An error has occurred: found 1 Deleted APIs and 0 Deprecated APIs"
PodDisruptionBudget found in policy/v1beta1 ├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!! -> OBJECT: calico-kube-controllers namespace: kube-system location: /root/ansible//calico.yaml
calicoApiConfig found in /v1 ├─ API REMOVED FROM THE CURRENT VERSION AND SHOULD BE MIGRATED IMMEDIATELY!! -> OBJECT: namespace: default location: /root/ansible//calicoctl.j2
Error: found 2 Deleted APIs and 0 Deprecated APIs time="2023-01-08T23:52:37+08:00" level=error msg="An error has occurred: found 2 Deleted APIs and 0 Deprecated APIs"