英文:
golang if with or condidition
问题
你可以将第316到318行修改为以下代码,以便检查是否找到CiliumK8sNodeSubnetAnnotation12
或CiliumK8sNodeSubnetAnnotation13
,如果两者都未找到,则继续执行:
if nodesubnet, ok := node.ObjectMeta.Annotations[CiliumK8sNodeSubnetAnnotation12]; !ok {
if nodesubnet, ok := node.ObjectMeta.Annotations[CiliumK8sNodeSubnetAnnotation13]; !ok {
log.Warningf("Node subnet annotation not found on node %v, static route not added", node.Name)
continue
}
}
这段代码使用了Go语言的if语句的特殊写法。if nodesubnet, ok := node.ObjectMeta.Annotations[CiliumK8sNodeSubnetAnnotation12]; !ok
表示尝试从node.ObjectMeta.Annotations
中获取CiliumK8sNodeSubnetAnnotation12
对应的值,并将其赋给nodesubnet
变量。如果获取成功,ok
的值为true
,否则为false
。通过!ok
可以判断是否未找到注解。如果未找到注解,则继续执行内部的if语句块,尝试获取CiliumK8sNodeSubnetAnnotation13
对应的值。如果两者都未找到,则输出警告信息并执行continue
跳过当前循环。
英文:
I have code like
CILIUM_K8S = "cilium-k8s"
CiliumK8sNodeSubnetAnnotation12 = "io.cilium.network.ipv4-pod-cidr"
CiliumK8sNodeSubnetAnnotation13 = "network.cilium.io/ipv4-pod-cidr
315 } else if ctlr.OrchestrationCNI == CILIUM_K8S {
316 if nodesubnet, ok := node.ObjectMeta.Annotations[CiliumK8sNodeSubnetAnnotation12]; !ok {
317 log.Warningf("Node subnet annotation %v not found on node %v static route not added", CiliumK8s NodeSubnetAnnotation, node.Name)
318 continue
319 } else {
320 route.Network = nodesubnet
321 nodeAddrs := node.Status.Addresses
322 for _, addr := range nodeAddrs {
323 if addr.Type == addrType {
324 route.Gateway = addr.Address
325 route.Name = fmt.Sprintf("k8s-%v-%v", node.Name, addr.Address)
326 }
327 }
328
329 }
how do I modify line 316 to 318
so that I can check if either CiliumK8sNodeSubnetAnnotation12
or CiliumK8sNodeSubnetAnnotation13
is found, if neither found, continue ? I am not familiar with the if nodesubnet, ok :=...
syntax. sorry for this lame question
答案1
得分: 1
我采纳了@zerkms的建议,并使用以下代码:
+ CILIUM_K8S = "cilium-k8s"
+ CiliumK8sNodeSubnetAnnotation12 = "io.cilium.network.ipv4-pod-cidr"
+ CiliumK8sNodeSubnetAnnotation13 = "network.cilium.io/ipv4-pod-cidr"
+func ciliumPodCidr(annotation map[string]string) string {
+ if subnet, ok := annotation[CiliumK8sNodeSubnetAnnotation13]; ok {
+ return subnet
+ } else if subnet, ok := annotation[CiliumK8sNodeSubnetAnnotation12]; ok {
+ return subnet
+ }
+ return ""
+}
+ } else if ctlr.OrchestrationCNI == CILIUM_K8S {
+ nodesubnet := ciliumPodCidr(node.ObjectMeta.Annotations)
+ if nodesubnet == "" {
+ log.Warningf("Cilium node podCIDR annotation not found on node %v, node has spec.podCIDR ?", node.Name)
+ continue
+ } else {
+ route.Network = nodesubnet
+ nodeAddrs := node.Status.Addresses
+ for _, addr := range nodeAddrs {
+ if addr.Type == addrType {
+ route.Gateway = addr.Address
+ route.Name = fmt.Sprintf("k8s-%v-%v", node.Name, addr.Address)
+ }
+ }
+
+ }
英文:
I adopted @zerkms suggestion with code like below:
+ CILIUM_K8S = "cilium-k8s"
+ CiliumK8sNodeSubnetAnnotation12 = "io.cilium.network.ipv4-pod-cidr"
+ CiliumK8sNodeSubnetAnnotation13 = "network.cilium.io/ipv4-pod-cidr"
+func ciliumPodCidr(annotation map[string]string) string {
+ if subnet, ok := annotation[CiliumK8sNodeSubnetAnnotation13]; ok {
+ return subnet
+ } else if subnet, ok := annotation[CiliumK8sNodeSubnetAnnotation12]; ok {
+ return subnet
+ }
+ return ""
+}
+ } else if ctlr.OrchestrationCNI == CILIUM_K8S {
+ nodesubnet := ciliumPodCidr(node.ObjectMeta.Annotations)
+ if nodesubnet == "" {
+ log.Warningf("Cilium node podCIDR annotation not found on node %v, node has spec.podCIDR ?", node.Name)
+ continue
+ } else {
+ route.Network = nodesubnet
+ nodeAddrs := node.Status.Addresses
+ for _, addr := range nodeAddrs {
+ if addr.Type == addrType {
+ route.Gateway = addr.Address
+ route.Name = fmt.Sprintf("k8s-%v-%v", node.Name, addr.Address)
+ }
+ }
+
+ }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论