diff --git a/pkg/hns/endpoint_windows.go b/pkg/hns/endpoint_windows.go index b0c462854..35fece7ab 100644 --- a/pkg/hns/endpoint_windows.go +++ b/pkg/hns/endpoint_windows.go @@ -39,7 +39,7 @@ type EndpointInfo struct { NetworkId string Gateway net.IP IpAddress net.IP - MacAddress string + MacAddress string } // GetSandboxContainerID returns the sandbox ID of this pod. @@ -205,7 +205,8 @@ func ConstructHnsResult(hnsNetwork *hcsshim.HNSNetwork, hnsEndpoint *hcsshim.HNS resultIPConfig := ¤t.IPConfig{ Address: net.IPNet{ IP: hnsEndpoint.IPAddress, - Mask: ipSubnet.Mask}, + Mask: ipSubnet.Mask, + }, Gateway: net.ParseIP(hnsEndpoint.GatewayAddress), } result := ¤t.Result{ @@ -249,7 +250,7 @@ func GenerateHcnEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcn.HostComputeEndp Minor: 0, }, Name: epInfo.EndpointName, - MacAddress: epInfo.MacAddress, + MacAddress: epInfo.MacAddress, HostComputeNetwork: epInfo.NetworkId, Dns: hcn.Dns{ Domain: epInfo.DNS.Domain, @@ -289,7 +290,7 @@ func RemoveHcnEndpoint(epName string) error { if epNamespace != nil { err = hcn.RemoveNamespaceEndpoint(hcnEndpoint.HostComputeNamespace, hcnEndpoint.Id) if err != nil && !hcn.IsNotFoundError(err) { - return errors.Annotatef(err,"error removing endpoint: %s from namespace", epName) + return errors.Annotatef(err, "error removing endpoint: %s from namespace", epName) } } @@ -361,7 +362,8 @@ func ConstructHcnResult(hcnNetwork *hcn.HostComputeNetwork, hcnEndpoint *hcn.Hos resultIPConfig := ¤t.IPConfig{ Address: net.IPNet{ IP: ipAddress, - Mask: ipSubnet.Mask}, + Mask: ipSubnet.Mask, + }, Gateway: net.ParseIP(hcnEndpoint.Routes[0].NextHop), } result := ¤t.Result{ diff --git a/pkg/hns/netconf_suite_windows_test.go b/pkg/hns/netconf_suite_windows_test.go index 0877bed10..f08660d57 100644 --- a/pkg/hns/netconf_suite_windows_test.go +++ b/pkg/hns/netconf_suite_windows_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -14,10 +14,10 @@ package hns import ( + "testing" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - - "testing" ) func TestNetConf(t *testing.T) { diff --git a/pkg/hns/netconf_windows.go b/pkg/hns/netconf_windows.go index cae0589f7..837cc1d6d 100644 --- a/pkg/hns/netconf_windows.go +++ b/pkg/hns/netconf_windows.go @@ -66,9 +66,9 @@ var protocolEnums = map[string]uint32{ } func (p *PortMapEntry) GetProtocolEnum() (uint32, error) { - var u, err = strconv.ParseUint(p.Protocol, 0, 10) + u, err := strconv.ParseUint(p.Protocol, 0, 10) if err != nil { - var pe, exist = protocolEnums[strings.ToLower(p.Protocol)] + pe, exist := protocolEnums[strings.ToLower(p.Protocol)] if !exist { return 0, errors.New("invalid protocol supplied to port mapping policy") } @@ -323,7 +323,7 @@ func (n *NetConf) ApplyPortMappingPolicy(portMappings []PortMapEntry) { toPolicyValue := func(p *PortMapEntry) json.RawMessage { if n.ApiVersion == 2 { - var protocolEnum, _ = p.GetProtocolEnum() + protocolEnum, _ := p.GetProtocolEnum() return bprintf(`{"Type": "PortMapping", "Settings": {"InternalPort": %d, "ExternalPort": %d, "Protocol": %d, "VIP": "%s"}}`, p.ContainerPort, p.HostPort, protocolEnum, p.HostIP) } return bprintf(`{"Type": "NAT", "InternalPort": %d, "ExternalPort": %d, "Protocol": "%s"}`, p.ContainerPort, p.HostPort, p.Protocol) diff --git a/pkg/hns/netconf_windows_test.go b/pkg/hns/netconf_windows_test.go index 9cd7c4346..211387051 100644 --- a/pkg/hns/netconf_windows_test.go +++ b/pkg/hns/netconf_windows_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, diff --git a/plugins/ipam/host-local/backend/allocator/allocator.go b/plugins/ipam/host-local/backend/allocator/allocator.go index ec5963223..aad6d5d6f 100644 --- a/plugins/ipam/host-local/backend/allocator/allocator.go +++ b/plugins/ipam/host-local/backend/allocator/allocator.go @@ -63,21 +63,20 @@ func (a *IPAllocator) GetByPodNsAndName(id string, ifname string, requestedIP ne Address: *reservedIP, Gateway: gw, }, nil - } else { - // reserve ip for new pod - ipCfg, err := a.Get(id, ifname, requestedIP) + } + // reserve ip for new pod + ipCfg, err := a.Get(id, ifname, requestedIP) + if err != nil { + return ipCfg, err + } + + if ipCfg != nil { + _, err := a.store.ReservePodInfo(id, ipCfg.Address.IP, podNs, podName, podIPIsExist) if err != nil { return ipCfg, err } - - if ipCfg != nil { - _, err := a.store.ReservePodInfo(id, ipCfg.Address.IP, podNs, podName, podIPIsExist) - if err != nil { - return ipCfg, err - } - } - return ipCfg, nil } + return ipCfg, nil } return a.Get(id, ifname, requestedIP) diff --git a/plugins/ipam/host-local/backend/disk/backend.go b/plugins/ipam/host-local/backend/disk/backend.go index bdd38e7b9..1e0ea1c7f 100644 --- a/plugins/ipam/host-local/backend/disk/backend.go +++ b/plugins/ipam/host-local/backend/disk/backend.go @@ -235,34 +235,31 @@ func (s *Store) ReservePodInfo(id string, ip net.IP, podNs, podName string, podI if podIPIsExist { // pod Ns/Name file is exist, update ip file with new container id. fname := GetEscapedPath(s.dataDir, ip.String()) - err := os.WriteFile(fname, []byte(strings.TrimSpace(id)), 0644) + err := os.WriteFile(fname, []byte(strings.TrimSpace(id)), 0o644) if err != nil { return false, err } - } else { + } else if len(podName) != 0 { // for new pod, create a new file named "PodIP_PodNs_PodName", // if there is already file named with prefix "ip_", rename the old file with new PodNs and PodName. - if len(podName) != 0 { - podIPNsNameFile := GetEscapedPath(s.dataDir, podFileName(ip.String(), podNs, podName)) - podIPNsNameFileName, err := s.findPodFileName(ip.String(), "", "") - if err != nil { - return false, err - } - - if len(podIPNsNameFileName) != 0 { - oldPodIPNsNameFile := GetEscapedPath(s.dataDir, podIPNsNameFileName) - err = os.Rename(oldPodIPNsNameFile, podIPNsNameFile) - if err != nil { - return false, err - } else { - return true, nil - } - } + podIPNsNameFile := GetEscapedPath(s.dataDir, podFileName(ip.String(), podNs, podName)) + podIPNsNameFileName, err := s.findPodFileName(ip.String(), "", "") + if err != nil { + return false, err + } - err = os.WriteFile(podIPNsNameFile, []byte{}, 0644) + if len(podIPNsNameFileName) != 0 { + oldPodIPNsNameFile := GetEscapedPath(s.dataDir, podIPNsNameFileName) + err = os.Rename(oldPodIPNsNameFile, podIPNsNameFile) if err != nil { return false, err } + return true, nil + } + + err = os.WriteFile(podIPNsNameFile, []byte{}, 0o644) + if err != nil { + return false, err } } @@ -277,24 +274,29 @@ func podFileName(ip, ns, name string) string { return name } -func resolvePodFileName(fName string) (ip, ns, name string) { +func resolvePodFileName(fName string) (string, string, string) { parts := strings.Split(fName, "_") + ip := "" + ns := "" + name := "" if len(parts) == 3 { ip = parts[0] ns = parts[1] name = parts[2] } - return + return ip, ns, name } func (s *Store) findPodFileName(ip, ns, name string) (string, error) { var pattern string - if len(ip) != 0 { + + switch { + case len(ip) != 0: pattern = fmt.Sprintf("%s_*", ip) - } else if len(ns) != 0 && len(name) != 0 { + case len(ns) != 0 && len(name) != 0: pattern = fmt.Sprintf("*_%s_%s", ns, name) - } else { + default: return "", nil } pattern = GetEscapedPath(s.dataDir, pattern) diff --git a/plugins/ipam/host-local/backend/testing/fake_store.go b/plugins/ipam/host-local/backend/testing/fake_store.go index 86e99c907..834939642 100644 --- a/plugins/ipam/host-local/backend/testing/fake_store.go +++ b/plugins/ipam/host-local/backend/testing/fake_store.go @@ -90,11 +90,11 @@ func (s *FakeStore) SetIPMap(m map[string]string) { s.ipMap = m } -func (s *FakeStore) ReleaseByPodName(podName string) error { +func (s *FakeStore) ReleaseByPodName(_ string) error { return nil } -func (s *FakeStore) HasReservedIP(podNs, podName string) (bool, net.IP) { +func (s *FakeStore) HasReservedIP(_, podName string) (bool, net.IP) { ip := net.IP{} if podName == "" { return false, ip @@ -102,6 +102,6 @@ func (s *FakeStore) HasReservedIP(podNs, podName string) (bool, net.IP) { return false, ip } -func (s *FakeStore) ReservePodInfo(id string, ip net.IP, podNs, podName string, podIsExist bool) (bool, error) { +func (s *FakeStore) ReservePodInfo(_ string, _ net.IP, _, _ string, _ bool) (bool, error) { return true, nil } diff --git a/plugins/ipam/host-local/main.go b/plugins/ipam/host-local/main.go index dfee84294..a6875544c 100644 --- a/plugins/ipam/host-local/main.go +++ b/plugins/ipam/host-local/main.go @@ -140,7 +140,6 @@ func cmdAdd(args *skel.CmdArgs) error { } ipConf, err := allocator.GetByPodNsAndName(args.ContainerID, args.IfName, requestedIP, podNs, podName) - if err != nil { // Deallocate all already allocated IPs for _, alloc := range allocs { diff --git a/plugins/main/windows/win-overlay/win-overlay_windows.go b/plugins/main/windows/win-overlay/win-overlay_windows.go index 2df1b74b8..74f58802a 100644 --- a/plugins/main/windows/win-overlay/win-overlay_windows.go +++ b/plugins/main/windows/win-overlay/win-overlay_windows.go @@ -106,13 +106,13 @@ func cmdHcnAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) { return nil, errors.Annotatef(err, "error while hcn.GetNetworkByName(%s)", networkName) } if hcnNetwork == nil { - return nil, fmt.Errorf("network %v is not found", networkName) + return nil, fmt.Errorf("network %v is not found", networkName) } if hnsNetwork == nil { return nil, fmt.Errorf("network %v not found", networkName) } - if !strings.EqualFold(string (hcnNetwork.Type), "Overlay") { + if !strings.EqualFold(string(hcnNetwork.Type), "Overlay") { return nil, fmt.Errorf("network %v is of an unexpected type: %v", networkName, hcnNetwork.Type) } @@ -131,7 +131,6 @@ func cmdHcnAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) { n.ApplyOutboundNatPolicy(hnsNetwork.Subnets[0].AddressPrefix) } hcnEndpoint, err := hns.GenerateHcnEndpoint(epInfo, &n.NetConf) - if err != nil { return nil, errors.Annotate(err, "error while generating HostComputeEndpoint") } @@ -142,15 +141,14 @@ func cmdHcnAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) { } result, err := hns.ConstructHcnResult(hcnNetwork, hcnEndpoint) - if err != nil { ipam.ExecDel(n.IPAM.Type, args.StdinData) return nil, errors.Annotate(err, "error while constructing HostComputeEndpoint addition result") } return result, nil - } + func cmdHnsAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) { success := false @@ -243,6 +241,7 @@ func cmdHnsAdd(args *skel.CmdArgs, n *NetConf) (*current.Result, error) { success = true return result, nil } + func cmdAdd(args *skel.CmdArgs) error { n, cniVersion, err := loadNetConf(args.StdinData) if err != nil {