go - 使用 client-go 模拟 'kubectl wait' 等待 Pod 准备就绪
问题描述
在 bash 脚本中,我通常使用 'kubectl wait' 来阻止,直到某个 pod 资源准备好,例如类似于以下内容:
kubectl wait --for=condition=Ready --timeout=2m -n mynamespace pod -l myselector
这很好用,因为很多时候我不知道我需要等待的 pod 的确切名称,而“kubectl wait”允许我根据选择器定位 pod,然后阻塞直到它准备好。
现在我需要在 golang 代码中做类似的事情。我已经看到了使用client-go库来通过名称进行身份验证和“获取”特定 pod 的示例。但是我有几个关于如何最好地使这个例子适应我的需要的问题......
我不知道能够“获取()”它的 pod 的确切/全名,这就是为什么“kubectl wait”是完美的,因为它允许我使用选择器找到 pod。我假设我应该使用 client-go 库来代替执行 CoreV1().Pods().List() 调用而不是 Get() 以允许我使用选择器找到我想要的 pod?
此外,pod 可能不会立即存在,并且可能仅在 1 分钟左右后才创建,“kubectl wait”为我处理。在代码中,我是否需要循环/睡眠并继续执行 List() 直到 pod 存在?
与#2 类似的问题...一旦 List() 确实返回一个 pod 名称,golang 中“等待”该 pod 处于“就绪”状态的最佳方式是什么?如果可以避免的话,我不想对睡眠进行任何丑陋的民意调查......那么使用 golang 'wait' 包或类似包有更好的选择吗?你有什么建议吗?
解决方案
如何完全按照它的方式kubectl
做呢?基本上,使用List(...)根据字段选择器列出,然后Watch(...)
片段:
...
// List with a name field selector to get the current resourceVersion to watch from (not the object's resourceVersion)
gottenObjList, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).List(context.TODO(), metav1.ListOptions{FieldSelector: nameSelector})
if apierrors.IsNotFound(err) {
return info.Object, true, nil
}
if err != nil {
// TODO this could do something slightly fancier if we wish
return info.Object, false, err
}
if len(gottenObjList.Items) != 1 {
return info.Object, true, nil
}
gottenObj := &gottenObjList.Items[0]
resourceLocation := ResourceLocation{
GroupResource: info.Mapping.Resource.GroupResource(),
Namespace: gottenObj.GetNamespace(),
Name: gottenObj.GetName(),
}
if uid, ok := o.UIDMap[resourceLocation]; ok {
if gottenObj.GetUID() != uid {
return gottenObj, true, nil
}
}
watchOptions := metav1.ListOptions{}
watchOptions.FieldSelector = nameSelector
watchOptions.ResourceVersion = gottenObjList.GetResourceVersion()
objWatch, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(context.TODO(), watchOptions)
if err != nil {
return gottenObj, false, err
}
...
✌️
推荐阅读
- c++ - 如何使用旧语法连接 const 限定的 Qt 信号?
- html - HTML/CSS - 文本未对齐/居中对齐
- docker - 公开在 docker 容器内运行的 SSH 隧道的端口
- bash - 为什么在“echo”和变量中使用 awk 命令时无法获得类似的结果
- spring-boot - Spring引导数据redis不从存储库返回值
- signalr - 如何将 SignalR 与多页 Web 应用程序一起使用?
- onedrive - OneDrive Graph API 在移动到其他文件夹之前检测文件是否被任何用户打开
- flutter - 在 SharedPreferences 上颤振保存对象
- shiny - 控制搜索框位置 RenderDataTable R
- xamarin.forms - Azure DevOps Xamarin.iOS 模拟器生成输出