Golang中的if语句可以使用”或”条件。

huangapple go评论74阅读模式
英文:

golang if with or condidition

问题

你可以将第316到318行修改为以下代码,以便检查是否找到CiliumK8sNodeSubnetAnnotation12CiliumK8sNodeSubnetAnnotation13,如果两者都未找到,则继续执行:

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 Golang中的if语句可以使用”或”条件。

答案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)
+                                       }
+                               }
+
+                       }

huangapple
  • 本文由 发表于 2023年4月26日 07:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76106173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定