Go语言 如果有或有条件

amrnrhlw  于 2023-05-04  发布在  Go
关注(0)|答案(1)|浏览(139)

我有代码像

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                         }

如何修改316 to 318行,以便检查是否找到CiliumK8sNodeSubnetAnnotation12CiliumK8sNodeSubnetAnnotation13,如果都没有找到,则继续?我不太熟悉if nodesubnet, ok :=...的语法。对不起这个蹩脚的问题:)

bq9c1y66

bq9c1y661#

我采用了@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)
+                                       }
+                               }
+
+                       }

相关问题