简体   繁体   English

kubectl 如何等待 crd 创建?

[英]How to kubectl wait for crd creation?

What is the best method for checking to see if a custom resource definition exists before running a script, using only kubectl command line?在运行脚本之前仅使用kubectl命令行检查自定义资源定义是否存在的最佳方法是什么?

We have a yaml file that contains definitions for a NATS cluster ServiceAccount , Role , ClusterRoleBinding and Deployment .我们有一个 yaml 文件,其中包含 NATS 集群ServiceAccountRoleClusterRoleBindingDeployment The image used in the Deployment creates the crd , and the second script uses that crd to deploy a set of pods . Deployment使用的映像创建crd ,第二个脚本使用该crd部署一组pods At the moment our CI pipeline needs to run the second script a few times, only completing successfully once the crd has been fully created.目前我们的 CI 管道需要多次运行第二个脚本,只有在crd完全创建后才能成功完成。 I've tried to use kubectl wait but cannot figure out what condition to use that applies to the completion of a crd .我尝试使用kubectl wait但无法弄清楚适用于完成crd

Below is my most recent, albeit completely wrong, attempt, however this illustrates the general sequence we'd like.下面是我最近的尝试,尽管完全错误,但这说明了我们想要的一般顺序。

kubectl wait --for=condition=complete kubectl apply -f 1.nats-cluster-operator.yaml kubectl apply -f 2.nats-cluster.yaml

The condition for a CRD would be established : CRD 的条件将established

kubectl -n <namespace-here> wait --for condition=established --timeout=60s crd/<crd-name-here>

You may want to adjust --timeout appropriately.您可能需要适当调整--timeout

In case you are wanting to wait for a resource that may not exist yet, you can try something like this:如果你想等待一个可能还不存在的资源,你可以尝试这样的事情:

{ grep -q -m 1 "crontabs.stable.example.com"; kill $!; } < <(kubectl get crd -w)

or或者

{ sed -n /crontabs.stable.example.com/q; kill $!; } < <(kubectl get crd -w)

I understand the question would prefer to only use kubectl , however this answer helped in my case.我知道这个问题更愿意只使用kubectl ,但是这个答案对我有帮助。 The downside to this method is that the timeout will have to be set in a different way and that the condition itself is not actually checked.这种方法的缺点是必须以不同的方式设置超时,并且实际上不会检查条件本身。

In order to check the condition more thoroughly, I made the following:为了更彻底地检查情况,我做了以下事情:

#!/bin/bash

condition-established() {
    local name="crontabs.stable.example.com"
    local condition="Established"

    jq --arg NAME $name --arg CONDITION $condition -n \
        'first(inputs | if (.metadata.name==$NAME) and (.status.conditions[]?.type==$CONDITION) then
            null | halt_error else empty end)' 

    # This is similar to the first, but the full condition is sent to stdout
    #jq --arg NAME $name --arg CONDITION $condition -n \
    #    'first(inputs | if (.metadata.name==$NAME) and (.status.conditions[]?.type==$CONDITION) then
    #        .status.conditions[] | select(.type==$CONDITION) else empty end)' 
}

{ condition-established; kill $!; } < <(kubectl get crd -w -o json)

echo Complete

To explain what is happening, $!为了解释发生了什么, $! refers to the command run by bash's process substitution.指的是 bash 的进程替换运行的命令。 I'm not sure how well this might work in other shells.我不确定这在其他 shell 中的效果如何。

I tested with the CRD from the official kubernetes documentation.我使用官方 kubernetes 文档中的CRD进行了测试。

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

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