Skip to content

Commit

Permalink
fix golangci issues
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasferrandiz authored and aauren committed Oct 7, 2023
1 parent 12561f9 commit d7e2a14
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/kube-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (kr *KubeRouter) Run() error {
}

if kr.Config.RunFirewall {
iptablesCmdHandlers, ipSetHandlers, err := netpol.NewIpTablesHandler(kr.Config)
iptablesCmdHandlers, ipSetHandlers, err := netpol.NewIPTablesHandler(kr.Config)
if err != nil {
return errors.New("Failed to create iptables handlers: " + err.Error())
}
Expand Down
29 changes: 18 additions & 11 deletions pkg/controllers/netpol/network_policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,12 @@ func (npc *NetworkPolicyController) iptablesCmdHandlerForCIDR(cidr *net.IPNet) (
return nil, fmt.Errorf("invalid CIDR")
}

func (npc *NetworkPolicyController) allowTrafficToClusterIpRange(
func (npc *NetworkPolicyController) allowTrafficToClusterIPRange(
serviceVIPPosition int,
serviceClusterIPRange *net.IPNet,
addUUIDForRuleSpec func(chain string, ruleSpec *[]string) (string, error),
ensureRuleAtPosition func(iptablesCmdHandler utils.IPTablesHandler, chain string, ruleSpec []string, uuid string, position int),
ensureRuleAtPosition func(iptablesCmdHandler utils.IPTablesHandler,
chain string, ruleSpec []string, uuid string, position int),
comment string) {
whitelistServiceVips := []string{"-m", "comment", "--comment", comment,
"-d", serviceClusterIPRange.String(), "-j", "RETURN"}
Expand Down Expand Up @@ -417,8 +418,8 @@ func (npc *NetworkPolicyController) ensureTopLevelChains() {
}

if len(npc.serviceClusterIPRanges) > 0 {
for _, serviceClusterIPRange := range npc.serviceClusterIPRanges {
npc.allowTrafficToClusterIpRange(rulePosition, &serviceClusterIPRange,
for i := range npc.serviceClusterIPRanges {
npc.allowTrafficToClusterIPRange(rulePosition, &npc.serviceClusterIPRanges[i],
addUUIDForRuleSpec, ensureRuleAtPosition, "allow traffic to primary/secondary cluster IP range")
rulePosition++
}
Expand Down Expand Up @@ -665,7 +666,8 @@ func (npc *NetworkPolicyController) Cleanup() {
klog.Infof("Successfully cleaned the NetworkPolicyController configurations done by kube-router")
}

func NewIpTablesHandler(config *options.KubeRouterConfig) (map[v1core.IPFamily]utils.IPTablesHandler, map[v1core.IPFamily]utils.IPSetHandler, error) {
func NewIPTablesHandler(config *options.KubeRouterConfig) (
map[v1core.IPFamily]utils.IPTablesHandler, map[v1core.IPFamily]utils.IPSetHandler, error) {
iptablesCmdHandlers := make(map[v1core.IPFamily]utils.IPTablesHandler, 2)
ipSetHandlers := make(map[v1core.IPFamily]utils.IPSetHandler, 2)

Expand Down Expand Up @@ -728,15 +730,17 @@ func NewNetworkPolicyController(clientset kubernetes.Interface,
}
npc.serviceClusterIPRanges = append(npc.serviceClusterIPRanges, *primaryIpnet)

//Validate that ClusterIP service range type matches the configuration
// Validate that ClusterIP service range type matches the configuration
if config.EnableIPv4 && !config.EnableIPv6 {
if !netutils.IsIPv4CIDR(&npc.serviceClusterIPRanges[0]) {
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: IPv4 is enabled but only IPv6 address is provided")
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: " +
"IPv4 is enabled but only IPv6 address is provided")
}
}
if !config.EnableIPv4 && config.EnableIPv6 {
if !netutils.IsIPv6CIDR(&npc.serviceClusterIPRanges[0]) {
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: IPv6 is enabled but only IPv4 address is provided")
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: " +
"IPv6 is enabled but only IPv4 address is provided")
}
}

Expand All @@ -748,10 +752,13 @@ func NewNetworkPolicyController(clientset kubernetes.Interface,
}
npc.serviceClusterIPRanges = append(npc.serviceClusterIPRanges, *secondaryIpnet)

ipv4Provided := netutils.IsIPv4CIDR(&npc.serviceClusterIPRanges[0]) || netutils.IsIPv4CIDR(&npc.serviceClusterIPRanges[1])
ipv6Provided := netutils.IsIPv6CIDR(&npc.serviceClusterIPRanges[0]) || netutils.IsIPv6CIDR(&npc.serviceClusterIPRanges[1])
ipv4Provided := netutils.IsIPv4CIDR(&npc.serviceClusterIPRanges[0]) ||
netutils.IsIPv4CIDR(&npc.serviceClusterIPRanges[1])
ipv6Provided := netutils.IsIPv6CIDR(&npc.serviceClusterIPRanges[0]) ||
netutils.IsIPv6CIDR(&npc.serviceClusterIPRanges[1])
if !(ipv4Provided && ipv6Provided) {
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: dual-stack is enabled, both IPv4 and IPv6 addresses should be provided")
return nil, fmt.Errorf("failed to get parse --service-cluster-ip-range parameter: " +
"dual-stack is enabled, both IPv4 and IPv6 addresses should be provided")
}
} else {
return nil, fmt.Errorf("too many CIDRs provided in --service-cluster-ip-range parameter: " +
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (ipset *IPSet) Get(setName string) *Set {
return set
}

//Sets returns all sets from ipset
// Sets returns all sets from ipset
func (ipset *IPSet) Sets() map[string]*Set {
return ipset.sets
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@ func Append(buffer *bytes.Buffer, chain string, rule []string) {
buffer.WriteString(ruleStr)
}

//IPTablesSaveRestorer interface that defines functions to save and restore tables
// IPTablesSaveRestorer interface that defines functions to save and restore tables
type IPTablesSaveRestorer interface {
SaveInto(table string, buffer *bytes.Buffer) error
Restore(table string, data []byte) error
}

//IPTablesSaveRestore struct stores shell commands to save and restore iptables state
// IPTablesSaveRestore struct stores shell commands to save and restore iptables state
type IPTablesSaveRestore struct {
saveCmd string
restoreCmd string
}

//NewIPTablesSaveRestore returns an IPTablesSaveRestore
//with apparopriate commands based on ipFamily (IPv4 or IPv6)
// NewIPTablesSaveRestore returns an IPTablesSaveRestore
// with apparopriate commands based on ipFamily (IPv4 or IPv6)
func NewIPTablesSaveRestore(ipFamily v1core.IPFamily) *IPTablesSaveRestore {
switch ipFamily {
case v1core.IPv6Protocol:
Expand Down Expand Up @@ -191,12 +191,12 @@ func (i *IPTablesSaveRestore) exec(cmdName string, args []string, data []byte, s
return nil
}

//SaveInto saves the content of iptables table into buffer
// SaveInto saves the content of iptables table into buffer
func (i *IPTablesSaveRestore) SaveInto(table string, buffer *bytes.Buffer) error {
return i.exec(i.saveCmd, []string{"-t", table}, nil, buffer)
}

//Restore updates table with the content of data
// Restore updates table with the content of data
func (i *IPTablesSaveRestore) Restore(table string, data []byte) error {
var args []string
if hasWait {
Expand Down
3 changes: 2 additions & 1 deletion pkg/utils/pod_cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ func GetPodCidrFromNodeSpec(clientset kubernetes.Interface, hostnameOverride str
return node.Spec.PodCIDR, nil
}

//GetPodCidrsFromNodeSpecDualStack reads the IPv4 and IPv6 pod CIDR allocated to the node from API node object and returns them
// GetPodCidrsFromNodeSpecDualStack reads the IPv4 and IPv6 pod CIDR allocated
// to the node from API node object and returns them
func GetPodCidrsFromNodeSpecDualStack(node *v1core.Node) (string, string, error) {
var podCidrv4, podCidrv6 string

Expand Down

0 comments on commit d7e2a14

Please sign in to comment.