如何单独过滤PersistentVolumeClaim yaml文件的事件?

时间:2020-09-29 13:41:50

标签: shell kubernetes kubectl

我们已经创建了pvc,并且它们处于待处理状态。因此,为了检查状态,我们执行

Kubectl describe -f <pvc.yml>

它显示结果如下

Name:          myproj-pvc-2020-09-29-04-02-1601377369-49419-1
Namespace:     default
StorageClass:  myproj-storageclass-2020-09-29-04-02-1601377366
Status:        Pending
Volume:
Labels:        ansible=csitest-2020-09-29-04-02-1601377369-49419-1
               pvcRef=csi-pvc-ansibles-1
Annotations:   volume.beta.kubernetes.io/storage-provisioner: csi.myorg.com
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:
Access Modes:
VolumeMode:    Filesystem
Mounted By:    <none>
Events:
  Type     Reason                Age                  From                                                                                   Message
  ----     ------                ----                 ----                                                                                   -------
  Normal   Provisioning          85s (x8 over 4m43s)  csi.myorg.com_csicentos76w3.mylab.myprojstorage.com_2e1a7c1d-7542-42a5-a2e1-491e1d04b4ee  External provisioner is provisioning volume for claim "default/myproj-pvc-2020-09-29-04-02-1601377369-49419-1"
  Warning  ProvisioningFailed    74s (x8 over 4m33s)  csi.myorg.com_csicentos76w3.mylab.myprojstorage.com_2e1a7c1d-7542-42a5-a2e1-491e1d04b4ee  failed to provision volume with StorageClass "myproj-storageclass-2020-09-29-04-02-1601377366": rpc error: code = Unavailable desc = Failed to get storage provider from secrets, Request failed with status code 401 and errors Error code (Unauthorized) and message (HTTP 401 Unauthorized.)
  Normal   ExternalProvisioning  6s (x20 over 4m43s)  persistentvolume-controller                                                            waiting for a volume to be created, either by external provisioner "csi.myorg.com" or manually created by system administrat

我需要过滤的只是该pvc.yaml的事件?如果我执行kubectl get -f pvc.yaml -o json,它不会在json中显示错误事件

我可以做kubectl describe -f <pvc.yml> | grep -A10 Events:,但不能保证总是只有10行会出错。

我也以这种方式找到了kubectl get events --field-selector involvedObject.kind="PersistentVolumeClaim",但这将显示与所有pvc相关的所有事件。我需要获取pvc.yml文件中提到的Pvc列表的事件。

如何在pvc.yaml中过滤所有pvc的事件?

1 个答案:

答案 0 :(得分:1)

kubectl get -f pvc.yaml -o json确实有效,但是我认为您是说kubectl get events -f pvc.yaml -o json

kubectl似乎不允许这种过滤。您可能要在kubectl github issues上打开功能请求。

但是与此同时,这是我想出的一种选择:

kubectl get -f <pvc.yml> -ojson \
 | jq ".items[] | .metadata.name" \
 | xargs -I{} kubectl get events --field-selector involvedObject.kind="PersistentVolumeClaim",involvedObject.name={} --no-headers --ignore-not-found

请注意,您需要安装jq。您还可以使用yq,并且在转换为json时不需要第一个技巧,但是需要稍微调整yq过滤器。我还假定您已经安装了kubectl,当然xargs当然应该在所有linux机器上都可用。