Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
Signed-off-by: Armin Schlegel <[email protected]>
  • Loading branch information
siredmar committed Dec 6, 2023
1 parent 80482a9 commit 99a3ad5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 56 deletions.
12 changes: 7 additions & 5 deletions pkg/hns/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -205,7 +205,8 @@ func ConstructHnsResult(hnsNetwork *hcsshim.HNSNetwork, hnsEndpoint *hcsshim.HNS
resultIPConfig := &current.IPConfig{
Address: net.IPNet{
IP: hnsEndpoint.IPAddress,
Mask: ipSubnet.Mask},
Mask: ipSubnet.Mask,
},
Gateway: net.ParseIP(hnsEndpoint.GatewayAddress),
}
result := &current.Result{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -361,7 +362,8 @@ func ConstructHcnResult(hcnNetwork *hcn.HostComputeNetwork, hcnEndpoint *hcn.Hos
resultIPConfig := &current.IPConfig{
Address: net.IPNet{
IP: ipAddress,
Mask: ipSubnet.Mask},
Mask: ipSubnet.Mask,
},
Gateway: net.ParseIP(hcnEndpoint.Routes[0].NextHop),
}
result := &current.Result{
Expand Down
6 changes: 3 additions & 3 deletions pkg/hns/netconf_suite_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,10 +14,10 @@
package hns

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"testing"
)

func TestNetConf(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/hns/netconf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/hns/netconf_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 10 additions & 11 deletions plugins/ipam/host-local/backend/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 26 additions & 24 deletions plugins/ipam/host-local/backend/disk/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions plugins/ipam/host-local/backend/testing/fake_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ 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
}
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
}
1 change: 0 additions & 1 deletion plugins/ipam/host-local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions plugins/main/windows/win-overlay/win-overlay_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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")
}
Expand All @@ -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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 99a3ad5

Please sign in to comment.