我在我的开放轮班有豆荚,并希望工作在多个开放轮班应用程序。让我们这样说吧
sh-4.2$ oc获取吊舱
NAME READY STATUS RESTARTS AGE
jenkins-7fb689fc66-fs2xb 1/1 Running 0 4d
jenkins-disk-check-1587834000 0/1 Completed 0 21h
NAME READY STATUS RESTARTS AGE
jenkins-7fb689fc66-gsz9j 0/1 Running 735 9d
jenkins-disk-check-1587834000
NAME READY STATUS RESTARTS AGE
jenkins-9euygc66-gsz9j 0/1 Running 735 9d我试过以下命令
舱内舱
export POD=$(oc get pods | awk '{print $1}' | grep jenkins*)我想找到以“jenkins-7fb 689fc66-fs2xb”,jenkins-9 etc 66-gsz9j等.使用脚本并需要忽略磁盘检查荚。如果我抓住上面的豆荚,需要执行终端,并通过编程方式运行一些shell命令。有人能帮我吗?
发布于 2020-04-26 18:46:52
kubectl get (以及扩展到oc get)是一个非常通用的工具。不幸的是,在在线浏览了一段时间之后,如果不依赖awk或grep这样的外部工具,您肯定无法完成Regex操作。(我知道这不是你想要的,但我想我至少要试着看看这是否可能。
尽管如此,您还可以依靠几个技巧来筛选您的oc get输出,甚至在您不得不引入外部工具之前(加分是因为这种过滤发生在服务器上,甚至在它访问您的本地工具之前)。
我首先建议运行oc get pods --show-labels,因为如果您需要的吊舱被适当地标记,您可以使用标签选择器来获取您想要的吊舱,例如:
oc get pods --selector name=jenkins
oc get pods --selector <label_key>=<label_value>其次,如果您只关心Running荚(因为disk-check荚看起来已经是Completed),您可以使用字段选择器,例如:
oc get pods --field-selector status.phase=Running
oc get pods --field-selector <json_path>=<json_value>最后,如果需要某个特定值,则可以通过指定自定义列将该值提取到CLI中,然后对所关心的值进行grep处理,例如:
oc get pods -o custom-columns=NAME:.metadata.name,TYPES:.status.conditions[*].type | grep "Ready"最好的情况是,如果您依赖于标签选择器和/或字段选择器,则过滤将发生在服务器端,以减少最终使其成为自定义列的数据,从而使一切变得更加高效。
对于特定的用例,似乎只使用--field-selector就足够了,因为disk-check荚已经是Completed了。因此,不需要进一步了解Jenkins pod的JSON是如何构造的,这对您来说应该足够好:
oc get pods --field-selector status.phase=Running发布于 2020-04-26 14:31:48
假设您需要在第一个字段中打印jenkins id,请您尝试以下操作。
awk 'match($0,/jenkins[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file解释:添加对上述代码的解释。
awk ' ##Starting awk program from here.
match($0,/jenkins[^ ]*/){ ##Using match function in which mentioning regex jenkins till spacein current line.
print substr($0,RSTART,RLENGTH) ##Printing sub-string in current line where starting point is RSTART till RLENGTH value.
}
' Input_file ##Mentioning Input_file name here.发布于 2022-03-16 12:16:04
添加此答案以供其他人参考。你可以用这种方式。
export POD=$(oc get pods | awk '{print $1}' | grep jenkins* | grep -v jenkins-disk-check)https://stackoverflow.com/questions/61442341
复制相似问题