Skip to content

Commit c3c5e56

Browse files
authored
Merge pull request #148 from cloudnativelabs/goreportcard
fixing gofmt, go_vet, gocyclo, golint errors
2 parents c9c6107 + 020d932 commit c3c5e56

File tree

8 files changed

+69
-57
lines changed

8 files changed

+69
-57
lines changed

app/controllers/network_policy_controller.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
// by one or more network policy chains, till there is a match which will accept the packet, or gets
3737
// dropped by the rule in the pod chain, if there is no match.
3838

39+
// strcut to hold information required by NetworkPolicyController
3940
type NetworkPolicyController struct {
4041
nodeIP net.IP
4142
nodeHostName string
@@ -80,6 +81,7 @@ type protocolAndPort struct {
8081
port string
8182
}
8283

84+
// Run: runs forver till we recive notification on stopCh
8385
func (npc *NetworkPolicyController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
8486
t := time.NewTicker(npc.syncPeriod)
8587
defer t.Stop()
@@ -115,6 +117,7 @@ func (npc *NetworkPolicyController) Run(stopCh <-chan struct{}, wg *sync.WaitGro
115117
}
116118
}
117119

120+
// OnPodUpdate: handles updates to pods from the Kubernetes api server
118121
func (npc *NetworkPolicyController) OnPodUpdate(podUpdate *watchers.PodUpdate) {
119122
glog.Infof("Received pod update namspace:%s pod name:%s", podUpdate.Pod.Namespace, podUpdate.Pod.Name)
120123
if watchers.PodWatcher.HasSynced() && watchers.NetworkPolicyWatcher.HasSynced() {
@@ -127,6 +130,7 @@ func (npc *NetworkPolicyController) OnPodUpdate(podUpdate *watchers.PodUpdate) {
127130
}
128131
}
129132

133+
// OnNetworkPolicyUpdate: handles updates to network policy from the kubernetes api server
130134
func (npc *NetworkPolicyController) OnNetworkPolicyUpdate(networkPolicyUpdate *watchers.NetworkPolicyUpdate) {
131135
if watchers.PodWatcher.HasSynced() && watchers.NetworkPolicyWatcher.HasSynced() {
132136
err := npc.Sync()
@@ -138,6 +142,7 @@ func (npc *NetworkPolicyController) OnNetworkPolicyUpdate(networkPolicyUpdate *w
138142
}
139143
}
140144

145+
// OnNamespaceUpdate: handles updates to namespace from kubernetes api server
141146
func (npc *NetworkPolicyController) OnNamespaceUpdate(namespaceUpdate *watchers.NamespaceUpdate) {
142147

143148
// namespace (and annotations on it) has no significance in GA ver of network policy
@@ -511,7 +516,7 @@ func cleanupStaleRules(activePolicyChains, activePodFwChains map[string]bool) er
511516
}
512517

513518
// TODO delete rule by spec, than rule number to avoid extra loop
514-
var realRuleNo int = 0
519+
var realRuleNo int
515520
for i, rule := range forwardChainRules {
516521
if strings.Contains(rule, chain) {
517522
err = iptablesCmdHandler.Delete("filter", "FORWARD", strconv.Itoa(i-realRuleNo))
@@ -593,8 +598,8 @@ func (npc *NetworkPolicyController) getFirewallEnabledPods(nodeIp string) (*map[
593598
}
594599
if npc.v1NetworkPolicy {
595600
podNeedsFirewall := false
596-
for _, policy_obj := range watchers.NetworkPolicyWatcher.List() {
597-
policy, _ := policy_obj.(*networking.NetworkPolicy)
601+
for _, policyObj:= range watchers.NetworkPolicyWatcher.List() {
602+
policy, _ := policyObj.(*networking.NetworkPolicy)
598603

599604
// we are only interested in the network policies in same namespace that of pod
600605
if policy.Namespace != pod.ObjectMeta.Namespace {
@@ -627,11 +632,11 @@ func (npc *NetworkPolicyController) getFirewallEnabledPods(nodeIp string) (*map[
627632
continue
628633
}
629634
} else {
630-
default_policy, err := getNameSpaceDefaultPolicy(pod.ObjectMeta.Namespace)
635+
defaultPolicy, err := getNameSpaceDefaultPolicy(pod.ObjectMeta.Namespace)
631636
if err != nil {
632637
return nil, fmt.Errorf("Failed to get the namespace default ingress policy %s", err.Error())
633638
}
634-
if strings.Compare(default_policy, "DefaultDeny") != 0 {
639+
if strings.Compare(defaultPolicy, "DefaultDeny") != 0 {
635640
continue
636641
}
637642
}
@@ -647,9 +652,9 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
647652

648653
NetworkPolicies := make([]networkPolicyInfo, 0)
649654

650-
for _, policy_obj := range watchers.NetworkPolicyWatcher.List() {
655+
for _, policyObj:= range watchers.NetworkPolicyWatcher.List() {
651656

652-
policy, ok := policy_obj.(*networking.NetworkPolicy)
657+
policy, ok := policyObj.(*networking.NetworkPolicy)
653658
if !ok {
654659
return nil, fmt.Errorf("Failed to convert")
655660
}
@@ -742,9 +747,9 @@ func buildBetaNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
742747

743748
NetworkPolicies := make([]networkPolicyInfo, 0)
744749

745-
for _, policy_obj := range watchers.NetworkPolicyWatcher.List() {
750+
for _, policyObj:= range watchers.NetworkPolicyWatcher.List() {
746751

747-
policy, _ := policy_obj.(*apiextensions.NetworkPolicy)
752+
policy, _ := policyObj.(*apiextensions.NetworkPolicy)
748753
newPolicy := networkPolicyInfo{
749754
name: policy.Name,
750755
namespace: policy.Namespace,
@@ -852,6 +857,7 @@ func getNodeIP(node *apiv1.Node) (net.IP, error) {
852857
return nil, errors.New("host IP unknown")
853858
}
854859

860+
// Cleanup: cleanup configurations done
855861
func (npc *NetworkPolicyController) Cleanup() {
856862

857863
glog.Infof("Cleaning up iptables configuration permanently done by kube-router")
@@ -869,7 +875,7 @@ func (npc *NetworkPolicyController) Cleanup() {
869875
}
870876

871877
// TODO: need a better way to delte rule with out using number
872-
var realRuleNo int = 0
878+
var realRuleNo int
873879
for i, rule := range forwardChainRules {
874880
if strings.Contains(rule, "KUBE-POD-FW-") {
875881
err = iptablesCmdHandler.Delete("filter", "FORWARD", strconv.Itoa(i-realRuleNo))

app/controllers/network_routes_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/client-go/kubernetes"
3434
)
3535

36+
// struct to hold necessary information required by controller
3637
type NetworkRoutingController struct {
3738
nodeIP net.IP
3839
nodeHostName string
@@ -67,6 +68,7 @@ const (
6768
podSubnetIpSetName = "kube-router-pod-subnets"
6869
)
6970

71+
// Run: run forever till until we are notified on stop channel
7072
func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
7173
cidr, err := utils.GetPodCidrFromCniSpec("/etc/cni/net.d/10-kuberouter.conf")
7274
if err != nil {
@@ -308,6 +310,7 @@ func (nrc *NetworkRoutingController) getClusterIps() ([]string, error) {
308310
return clusterIpList, nil
309311
}
310312

313+
// AdvertiseClusterIp: advertises the service cluster ip the configured peers
311314
func (nrc *NetworkRoutingController) AdvertiseClusterIp(clusterIp string) error {
312315

313316
attrs := []bgp.PathAttributeInterface{
@@ -520,6 +523,7 @@ func (nrc *NetworkRoutingController) injectRoute(path *table.Path) error {
520523
return netlink.RouteReplace(route)
521524
}
522525

526+
// Cleanup: performs the cleanup of configurations done
523527
func (nrc *NetworkRoutingController) Cleanup() {
524528
err := deletePodEgressRule()
525529
if err != nil {
@@ -793,7 +797,7 @@ func (nrc *NetworkRoutingController) enablePolicyBasedRouting() error {
793797
return nil
794798
}
795799

796-
// Handle updates from Node watcher. Node watcher calls this method whenever there is
800+
// OnNodeUpdate: Handle updates from Node watcher. Node watcher calls this method whenever there is
797801
// new node is added or old node is deleted. So peer up with new node and drop peering
798802
// from old node
799803
func (nrc *NetworkRoutingController) OnNodeUpdate(nodeUpdate *watchers.NodeUpdate) {

app/controllers/network_services_controller.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
const (
30+
// name of the dummy interface to which cluster ip are assigned
3031
KUBE_DUMMY_IF = "kube-dummy-if"
3132
IFACE_NOT_FOUND = "Link not found"
3233
IFACE_HAS_ADDR = "file exists"
@@ -58,13 +59,14 @@ var (
5859
}, []string{"namespace", "service_name", "backend"})
5960
)
6061

61-
// Network services controller enables local node as network service proxy through IPVS/LVS.
62+
// NetworkServicesController enables local node as network service proxy through IPVS/LVS.
6263
// Support only Kubernetes network services of type NodePort, ClusterIP, and LoadBalancer. For each service a
6364
// IPVS service is created and for each service endpoint a server is added to the IPVS service.
6465
// As services and endpoints are updated, network service controller gets the updates from
6566
// the kubernetes api server and syncs the ipvs configuration to reflect state of services
6667
// and endpoints
6768

69+
// struct for storing information needed by the controller
6870
type NetworkServicesController struct {
6971
nodeIP net.IP
7072
nodeHostName string
@@ -102,7 +104,7 @@ type endpointsInfo struct {
102104
// map of all endpoints, with unique service id(namespace name, service name, port) as key
103105
type endpointsInfoMap map[string][]endpointsInfo
104106

105-
// periodically sync ipvs configuration to reflect desired state of services and endpoints
107+
// Run: periodically sync ipvs configuration to reflect desired state of services and endpoints
106108
func (nsc *NetworkServicesController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) error {
107109

108110
t := time.NewTicker(nsc.syncPeriod)
@@ -170,7 +172,7 @@ func (nsc *NetworkServicesController) sync() {
170172
nsc.publishMetrics(nsc.serviceMap)
171173
}
172174

173-
// handle change in endpoints update from the API server
175+
// OnEndpointsUpdate: handle change in endpoints update from the API server
174176
func (nsc *NetworkServicesController) OnEndpointsUpdate(endpointsUpdate *watchers.EndpointsUpdate) {
175177

176178
nsc.mu.Lock()
@@ -192,7 +194,7 @@ func (nsc *NetworkServicesController) OnEndpointsUpdate(endpointsUpdate *watcher
192194
}
193195
}
194196

195-
// handle change in service update from the API server
197+
// OnServiceUpdate: handle change in service update from the API server
196198
func (nsc *NetworkServicesController) OnServiceUpdate(serviceUpdate *watchers.ServiceUpdate) {
197199

198200
nsc.mu.Lock()
@@ -241,15 +243,15 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
241243
}
242244

243245
// assign cluster IP of the service to the dummy interface so that its routable from the pod's on the node
244-
vip := &netlink.Addr{IPNet: &net.IPNet{svc.clusterIP, net.IPv4Mask(255, 255, 255, 255)}, Scope: syscall.RT_SCOPE_LINK}
246+
vip := &netlink.Addr{IPNet: &net.IPNet{IP: svc.clusterIP, Mask: net.IPv4Mask(255, 255, 255, 255)}, Scope: syscall.RT_SCOPE_LINK}
245247
err := netlink.AddrAdd(dummyVipInterface, vip)
246248
if err != nil && err.Error() != IFACE_HAS_ADDR {
247249
glog.Errorf("Failed to assign cluster ip to dummy interface %s", err)
248250
continue
249251
}
250252

251253
// create IPVS service for the service to be exposed through the cluster ip
252-
ipvs_cluster_vip_svc, err := ipvsAddService(svc.clusterIP, protocol, uint16(svc.port), svc.sessionAffinity)
254+
ipvsClusterVipSvc, err := ipvsAddService(svc.clusterIP, protocol, uint16(svc.port), svc.sessionAffinity)
253255
if err != nil {
254256
glog.Errorf("Failed to create ipvs service for cluster ip: %s", err.Error())
255257
continue
@@ -258,10 +260,10 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
258260
activeServiceEndpointMap[clusterServiceId] = make([]string, 0)
259261

260262
// create IPVS service for the service to be exposed through the nodeport
261-
var ipvs_nodeport_svc *ipvs.Service
263+
var ipvsNodeportSvc*ipvs.Service
262264
var nodeServiceId string
263265
if svc.nodePort != 0 {
264-
ipvs_nodeport_svc, err = ipvsAddService(nsc.nodeIP, protocol, uint16(svc.nodePort), svc.sessionAffinity)
266+
ipvsNodeportSvc, err = ipvsAddService(nsc.nodeIP, protocol, uint16(svc.nodePort), svc.sessionAffinity)
265267
if err != nil {
266268
glog.Errorf("Failed to create ipvs service for node port")
267269
continue
@@ -280,7 +282,7 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
280282
Weight: 1,
281283
}
282284

283-
err := ipvsAddServer(ipvs_cluster_vip_svc, &dst)
285+
err := ipvsAddServer(ipvsClusterVipSvc, &dst)
284286
if err != nil {
285287
glog.Errorf(err.Error())
286288
}
@@ -289,7 +291,7 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
289291
append(activeServiceEndpointMap[clusterServiceId], endpoint.ip)
290292

291293
if svc.nodePort != 0 {
292-
err := ipvsAddServer(ipvs_nodeport_svc, &dst)
294+
err := ipvsAddServer(ipvsNodeportSvc, &dst)
293295
if err != nil {
294296
glog.Errorf(err.Error())
295297
}
@@ -434,11 +436,11 @@ func shuffle(endPoints []endpointsInfo) []endpointsInfo {
434436
func buildEndpointsInfo() endpointsInfoMap {
435437
endpointsMap := make(endpointsInfoMap)
436438
for _, ep := range watchers.EndpointsWatcher.List() {
437-
for _, ep_subset := range ep.Subsets {
438-
for _, port := range ep_subset.Ports {
439+
for _, epSubset:= range ep.Subsets {
440+
for _, port := range epSubset.Ports {
439441
svcId := generateServiceId(ep.Namespace, ep.Name, port.Name)
440442
endpoints := make([]endpointsInfo, 0)
441-
for _, addr := range ep_subset.Addresses {
443+
for _, addr := range epSubset.Addresses {
442444
endpoints = append(endpoints, endpointsInfo{ip: addr.IP, port: int(port.Port)})
443445
}
444446
endpointsMap[svcId] = shuffle(endpoints)
@@ -726,7 +728,7 @@ func ipvsAddService(vip net.IP, protocol, port uint16, persistent bool) (*ipvs.S
726728
svc.Timeout = 180 * 60
727729
}
728730
if err := h.NewService(&svc); err != nil {
729-
return nil, fmt.Errorf("Failed to create service: %s:%s:%s", vip.String(), protocol, strconv.Itoa(int(port)))
731+
return nil, fmt.Errorf("Failed to create service: %s:%s:%s", vip.String(), strconv.Itoa(int(protocol)), strconv.Itoa(int(port)))
730732
}
731733
glog.Infof("Successfully added service: %s:%s:%s", vip.String(), protocol, strconv.Itoa(int(port)))
732734
return &svc, nil
@@ -743,10 +745,10 @@ func ipvsAddServer(service *ipvs.Service, dest *ipvs.Destination) error {
743745

744746
if strings.Contains(err.Error(), IPVS_SERVER_EXISTS) {
745747
glog.Infof("ipvs destination %s:%s already exists in the ipvs service %s:%s:%s so not adding destination", dest.Address,
746-
strconv.Itoa(int(dest.Port)), service.Address, service.Protocol, strconv.Itoa(int(service.Port)))
748+
strconv.Itoa(int(dest.Port)), service.Address, strconv.Itoa(int(service.Protocol)), strconv.Itoa(int(service.Port)))
747749
} else {
748750
return fmt.Errorf("Failed to add ipvs destination %s:%s to the ipvs service %s:%s:%s due to : %s", dest.Address,
749-
strconv.Itoa(int(dest.Port)), service.Address, service.Protocol, strconv.Itoa(int(service.Port)), err.Error())
751+
strconv.Itoa(int(dest.Port)), service.Address, strconv.Itoa(int(service.Protocol)), strconv.Itoa(int(service.Port)), err.Error())
750752
}
751753
return nil
752754
}
@@ -779,7 +781,7 @@ func getKubeDummyInterface() (netlink.Link, error) {
779781
return dummyVipInterface, nil
780782
}
781783

782-
// clean up all the configurations (IPVS, iptables, links)
784+
// Cleanup: clean all the configurations (IPVS, iptables, links) done
783785
func (nsc *NetworkServicesController) Cleanup() {
784786
// cleanup ipvs rules by flush
785787
glog.Infof("Cleaning up IPVS configuration permanently")

app/watchers/endpoints_watcher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func (ew *endpointsWatcher) RegisterHandler(handler EndpointsUpdatesHandler) {
7676
}
7777

7878
func (ew *endpointsWatcher) List() []*api.Endpoints {
79-
obj_list := ew.endpointsLister.List()
80-
ep_instances := make([]*api.Endpoints, len(obj_list))
81-
for i, ins := range obj_list {
82-
ep_instances[i] = ins.(*api.Endpoints)
79+
objList := ew.endpointsLister.List()
80+
epInstances := make([]*api.Endpoints, len(objList))
81+
for i, ins := range objList {
82+
epInstances[i] = ins.(*api.Endpoints)
8383
}
84-
return ep_instances
84+
return epInstances
8585
}
8686

8787
func (ew *endpointsWatcher) HasSynced() bool {

app/watchers/namespace_watcher.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/cloudnativelabs/kube-router/utils"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/fields"
10+
"k8s.io/apimachinery/pkg/labels"
1011
"k8s.io/client-go/kubernetes"
12+
listers "k8s.io/client-go/listers/core/v1"
1113
api "k8s.io/client-go/pkg/api/v1"
1214
cache "k8s.io/client-go/tools/cache"
13-
listers "k8s.io/client-go/listers/core/v1"
14-
"k8s.io/apimachinery/pkg/labels"
1515
)
1616

1717
type NamespaceUpdate struct {
@@ -62,22 +62,21 @@ func (nsw *namespaceWatcher) namespaceUpdateEventHandler(oldObj, newObj interfac
6262
}
6363

6464
func (nsw *namespaceWatcher) List() []*api.Namespace {
65-
obj_list := nsw.namespaceLister.List()
66-
namespace_instances := make([]*api.Namespace, len(obj_list))
67-
for i, ins := range obj_list {
68-
namespace_instances[i] = ins.(*api.Namespace)
65+
objList := nsw.namespaceLister.List()
66+
namespaceInstances := make([]*api.Namespace, len(objList))
67+
for i, ins := range objList {
68+
namespaceInstances[i] = ins.(*api.Namespace)
6969
}
70-
return namespace_instances
70+
return namespaceInstances
7171
}
7272

7373
func (nsw *namespaceWatcher) ListByLabels(set labels.Set) ([]*api.Namespace, error) {
7474
namespaceLister := listers.NewNamespaceLister(nsw.namespaceLister)
7575
matchedNamespaces, err := namespaceLister.List(set.AsSelector())
7676
if err != nil {
7777
return nil, err
78-
} else {
79-
return matchedNamespaces, nil
8078
}
79+
return matchedNamespaces, nil
8180
}
8281

8382
func (nsw *namespaceWatcher) RegisterHandler(handler NamespaceUpdatesHandler) {

app/watchers/node_watcher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func (nw *nodeWatcher) RegisterHandler(handler NodeUpdatesHandler) {
5959
}
6060

6161
func (nw *nodeWatcher) List() []*api.Node {
62-
obj_list := nw.nodeLister.List()
63-
node_instances := make([]*api.Node, len(obj_list))
64-
for i, ins := range obj_list {
65-
node_instances[i] = ins.(*api.Node)
62+
objList := nw.nodeLister.List()
63+
nodeInstances := make([]*api.Node, len(objList))
64+
for i, ins := range objList {
65+
nodeInstances[i] = ins.(*api.Node)
6666
}
67-
return node_instances
67+
return nodeInstances
6868
}
6969

7070
func (nw *nodeWatcher) HasSynced() bool {

app/watchers/pods_watcher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func (pw *podWatcher) RegisterHandler(handler PodUpdatesHandler) {
6767
}
6868

6969
func (pw *podWatcher) List() []*api.Pod {
70-
obj_list := pw.podLister.List()
71-
pod_instances := make([]*api.Pod, len(obj_list))
72-
for i, ins := range obj_list {
73-
pod_instances[i] = ins.(*api.Pod)
70+
objList := pw.podLister.List()
71+
podInstances := make([]*api.Pod, len(objList))
72+
for i, ins := range objList {
73+
podInstances[i] = ins.(*api.Pod)
7474
}
75-
return pod_instances
75+
return podInstances
7676
}
7777

7878
func (pw *podWatcher) ListByNamespaceAndLabels(namespace string, labelsToMatch labels.Set) (ret []*api.Pod, err error) {

0 commit comments

Comments
 (0)