Skip to content

Commit f6a5e23

Browse files
committed
fixing gofmt, go_vet, gocyclo, golint errors
1 parent c3c5e56 commit f6a5e23

File tree

11 files changed

+53
-42
lines changed

11 files changed

+53
-42
lines changed

app/controllers/network_policy_controller.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +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
39+
// NetworkPolicyController strcut to hold information required by NetworkPolicyController
4040
type NetworkPolicyController struct {
4141
nodeIP net.IP
4242
nodeHostName string
@@ -81,7 +81,7 @@ type protocolAndPort struct {
8181
port string
8282
}
8383

84-
// Run: runs forver till we recive notification on stopCh
84+
// Run runs forver till we recive notification on stopCh
8585
func (npc *NetworkPolicyController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
8686
t := time.NewTicker(npc.syncPeriod)
8787
defer t.Stop()
@@ -117,7 +117,7 @@ func (npc *NetworkPolicyController) Run(stopCh <-chan struct{}, wg *sync.WaitGro
117117
}
118118
}
119119

120-
// OnPodUpdate: handles updates to pods from the Kubernetes api server
120+
// OnPodUpdate handles updates to pods from the Kubernetes api server
121121
func (npc *NetworkPolicyController) OnPodUpdate(podUpdate *watchers.PodUpdate) {
122122
glog.Infof("Received pod update namspace:%s pod name:%s", podUpdate.Pod.Namespace, podUpdate.Pod.Name)
123123
if watchers.PodWatcher.HasSynced() && watchers.NetworkPolicyWatcher.HasSynced() {
@@ -130,7 +130,7 @@ func (npc *NetworkPolicyController) OnPodUpdate(podUpdate *watchers.PodUpdate) {
130130
}
131131
}
132132

133-
// OnNetworkPolicyUpdate: handles updates to network policy from the kubernetes api server
133+
// OnNetworkPolicyUpdate handles updates to network policy from the kubernetes api server
134134
func (npc *NetworkPolicyController) OnNetworkPolicyUpdate(networkPolicyUpdate *watchers.NetworkPolicyUpdate) {
135135
if watchers.PodWatcher.HasSynced() && watchers.NetworkPolicyWatcher.HasSynced() {
136136
err := npc.Sync()
@@ -142,7 +142,7 @@ func (npc *NetworkPolicyController) OnNetworkPolicyUpdate(networkPolicyUpdate *w
142142
}
143143
}
144144

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

148148
// namespace (and annotations on it) has no significance in GA ver of network policy
@@ -598,7 +598,7 @@ func (npc *NetworkPolicyController) getFirewallEnabledPods(nodeIp string) (*map[
598598
}
599599
if npc.v1NetworkPolicy {
600600
podNeedsFirewall := false
601-
for _, policyObj:= range watchers.NetworkPolicyWatcher.List() {
601+
for _, policyObj := range watchers.NetworkPolicyWatcher.List() {
602602
policy, _ := policyObj.(*networking.NetworkPolicy)
603603

604604
// we are only interested in the network policies in same namespace that of pod
@@ -652,7 +652,7 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
652652

653653
NetworkPolicies := make([]networkPolicyInfo, 0)
654654

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

657657
policy, ok := policyObj.(*networking.NetworkPolicy)
658658
if !ok {
@@ -747,7 +747,7 @@ func buildBetaNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
747747

748748
NetworkPolicies := make([]networkPolicyInfo, 0)
749749

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

752752
policy, _ := policyObj.(*apiextensions.NetworkPolicy)
753753
newPolicy := networkPolicyInfo{
@@ -806,13 +806,11 @@ func getNameSpaceDefaultPolicy(namespace string) (string, error) {
806806
err := json.Unmarshal([]byte(networkPolicyAnnotation), &annot)
807807
if err == nil {
808808
return annot["ingress"]["isolation"], nil
809-
} else {
810-
glog.Errorf("Skipping invalid network-policy for namespace \"%s\": %s", namespace, err)
811-
return "DefaultAllow", errors.New("Invalid NetworkPolicy.")
812809
}
813-
} else {
814-
return "DefaultAllow", nil
810+
glog.Errorf("Skipping invalid network-policy for namespace \"%s\": %s", namespace, err)
811+
return "DefaultAllow", errors.New("Invalid NetworkPolicy.")
815812
}
813+
return "DefaultAllow", nil
816814
}
817815
}
818816
return "", errors.New("Failed to get the default ingress policy for the namespace: " + namespace)
@@ -857,7 +855,7 @@ func getNodeIP(node *apiv1.Node) (net.IP, error) {
857855
return nil, errors.New("host IP unknown")
858856
}
859857

860-
// Cleanup: cleanup configurations done
858+
// Cleanup cleanup configurations done
861859
func (npc *NetworkPolicyController) Cleanup() {
862860

863861
glog.Infof("Cleaning up iptables configuration permanently done by kube-router")
@@ -941,6 +939,7 @@ func (npc *NetworkPolicyController) Cleanup() {
941939
glog.Infof("Successfully cleaned the iptables configuration done by kube-router")
942940
}
943941

942+
// NewNetworkPolicyController returns new NetworkPolicyController object
944943
func NewNetworkPolicyController(clientset *kubernetes.Clientset, config *options.KubeRouterConfig) (*NetworkPolicyController, error) {
945944

946945
npc := NetworkPolicyController{}

app/controllers/network_routes_controller.go

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

36-
// struct to hold necessary information required by controller
36+
// NetworkRoutingController is struct to hold necessary information required by controller
3737
type NetworkRoutingController struct {
3838
nodeIP net.IP
3939
nodeHostName string
@@ -68,7 +68,7 @@ const (
6868
podSubnetIpSetName = "kube-router-pod-subnets"
6969
)
7070

71-
// Run: run forever till until we are notified on stop channel
71+
// Run runs forever till until we are notified on stop channel
7272
func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
7373
cidr, err := utils.GetPodCidrFromCniSpec("/etc/cni/net.d/10-kuberouter.conf")
7474
if err != nil {
@@ -310,7 +310,7 @@ func (nrc *NetworkRoutingController) getClusterIps() ([]string, error) {
310310
return clusterIpList, nil
311311
}
312312

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

316316
attrs := []bgp.PathAttributeInterface{
@@ -523,7 +523,7 @@ func (nrc *NetworkRoutingController) injectRoute(path *table.Path) error {
523523
return netlink.RouteReplace(route)
524524
}
525525

526-
// Cleanup: performs the cleanup of configurations done
526+
// Cleanup performs the cleanup of configurations done
527527
func (nrc *NetworkRoutingController) Cleanup() {
528528
err := deletePodEgressRule()
529529
if err != nil {
@@ -770,10 +770,9 @@ func (nrc *NetworkRoutingController) enablePolicyBasedRouting() error {
770770
f, err := os.OpenFile("/etc/iproute2/rt_tables", os.O_APPEND|os.O_WRONLY, 0600)
771771
if err != nil {
772772
return fmt.Errorf("Failed to enable required policy based routing. Failed to create custom route table to route tunneled traffic properly due to: %s", err.Error())
773-
} else {
774-
if _, err = f.WriteString("77 kube-router"); err != nil {
775-
return fmt.Errorf("Failed to enable required policy based routing.Failed to add custom router table entry in /etc/iproute2/rt_tables due to: %s", err.Error())
776-
}
773+
}
774+
if _, err = f.WriteString("77 kube-router"); err != nil {
775+
return fmt.Errorf("Failed to enable required policy based routing.Failed to add custom router table entry in /etc/iproute2/rt_tables due to: %s", err.Error())
777776
}
778777
}
779778

@@ -797,7 +796,7 @@ func (nrc *NetworkRoutingController) enablePolicyBasedRouting() error {
797796
return nil
798797
}
799798

800-
// OnNodeUpdate: Handle updates from Node watcher. Node watcher calls this method whenever there is
799+
// OnNodeUpdate Handle updates from Node watcher. Node watcher calls this method whenever there is
801800
// new node is added or old node is deleted. So peer up with new node and drop peering
802801
// from old node
803802
func (nrc *NetworkRoutingController) OnNodeUpdate(nodeUpdate *watchers.NodeUpdate) {
@@ -849,14 +848,13 @@ func (nrc *NetworkRoutingController) startBgpServer() error {
849848
if !ok {
850849
return errors.New("Could not find ASN number for the node. Node need to be annotated with ASN number " +
851850
"details to start BGP server.")
852-
} else {
853-
glog.Infof("Found ASN for the node to be %s from the node annotations", nodeasn)
854-
asnNo, err := strconv.ParseUint(nodeasn, 0, 32)
855-
if err != nil {
856-
return errors.New("Failed to parse ASN number specified for the the node")
857-
}
858-
nodeAsnNumber = uint32(asnNo)
859851
}
852+
glog.Infof("Found ASN for the node to be %s from the node annotations", nodeasn)
853+
asnNo, err := strconv.ParseUint(nodeasn, 0, 32)
854+
if err != nil {
855+
return errors.New("Failed to parse ASN number specified for the the node")
856+
}
857+
nodeAsnNumber = uint32(asnNo)
860858
nrc.nodeAsnNumber = nodeAsnNumber
861859
}
862860

@@ -964,6 +962,7 @@ func getNodeSubnet(nodeIp net.IP) (net.IPNet, string, error) {
964962
return net.IPNet{}, "", errors.New("Failed to find interface with specified node ip")
965963
}
966964

965+
// NewNetworkRoutingController returns new NetworkRoutingController object
967966
func NewNetworkRoutingController(clientset *kubernetes.Clientset,
968967
kubeRouterConfig *options.KubeRouterConfig) (*NetworkRoutingController, error) {
969968
// TODO: Remove lookup, ipset.New already does this.

app/controllers/network_services_controller.go

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

2929
const (
30-
// name of the dummy interface to which cluster ip are assigned
3130
KUBE_DUMMY_IF = "kube-dummy-if"
3231
IFACE_NOT_FOUND = "Link not found"
3332
IFACE_HAS_ADDR = "file exists"
@@ -66,7 +65,7 @@ var (
6665
// the kubernetes api server and syncs the ipvs configuration to reflect state of services
6766
// and endpoints
6867

69-
// struct for storing information needed by the controller
68+
// NetworkServicesController struct stores information needed by the controller
7069
type NetworkServicesController struct {
7170
nodeIP net.IP
7271
nodeHostName string
@@ -104,7 +103,7 @@ type endpointsInfo struct {
104103
// map of all endpoints, with unique service id(namespace name, service name, port) as key
105104
type endpointsInfoMap map[string][]endpointsInfo
106105

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

110109
t := time.NewTicker(nsc.syncPeriod)
@@ -172,7 +171,7 @@ func (nsc *NetworkServicesController) sync() {
172171
nsc.publishMetrics(nsc.serviceMap)
173172
}
174173

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

178177
nsc.mu.Lock()
@@ -194,7 +193,7 @@ func (nsc *NetworkServicesController) OnEndpointsUpdate(endpointsUpdate *watcher
194193
}
195194
}
196195

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

200199
nsc.mu.Lock()
@@ -260,7 +259,7 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
260259
activeServiceEndpointMap[clusterServiceId] = make([]string, 0)
261260

262261
// create IPVS service for the service to be exposed through the nodeport
263-
var ipvsNodeportSvc*ipvs.Service
262+
var ipvsNodeportSvc *ipvs.Service
264263
var nodeServiceId string
265264
if svc.nodePort != 0 {
266265
ipvsNodeportSvc, err = ipvsAddService(nsc.nodeIP, protocol, uint16(svc.nodePort), svc.sessionAffinity)
@@ -436,7 +435,7 @@ func shuffle(endPoints []endpointsInfo) []endpointsInfo {
436435
func buildEndpointsInfo() endpointsInfoMap {
437436
endpointsMap := make(endpointsInfoMap)
438437
for _, ep := range watchers.EndpointsWatcher.List() {
439-
for _, epSubset:= range ep.Subsets {
438+
for _, epSubset := range ep.Subsets {
440439
for _, port := range epSubset.Ports {
441440
svcId := generateServiceId(ep.Namespace, ep.Name, port.Name)
442441
endpoints := make([]endpointsInfo, 0)
@@ -781,7 +780,7 @@ func getKubeDummyInterface() (netlink.Link, error) {
781780
return dummyVipInterface, nil
782781
}
783782

784-
// Cleanup: clean all the configurations (IPVS, iptables, links) done
783+
// Cleanup cleans all the configurations (IPVS, iptables, links) done
785784
func (nsc *NetworkServicesController) Cleanup() {
786785
// cleanup ipvs rules by flush
787786
glog.Infof("Cleaning up IPVS configuration permanently")
@@ -824,6 +823,7 @@ func (nsc *NetworkServicesController) Cleanup() {
824823
glog.Infof("Successfully cleaned the ipvs configuration done by kube-router")
825824
}
826825

826+
// NewNetworkServicesController returns NetworkServicesController object
827827
func NewNetworkServicesController(clientset *kubernetes.Clientset, config *options.KubeRouterConfig) (*NetworkServicesController, error) {
828828

829829
var err error

app/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import (
1616
"k8s.io/client-go/tools/clientcmd"
1717
)
1818

19+
// KubeRouter holds the information needed to run server
1920
type KubeRouter struct {
2021
Client *kubernetes.Clientset
2122
Config *options.KubeRouterConfig
2223
}
2324

25+
// NewKubeRouterDefault returns a KubeRouter object
2426
func NewKubeRouterDefault(config *options.KubeRouterConfig) (*KubeRouter, error) {
2527

2628
var clientconfig *rest.Config
@@ -46,6 +48,7 @@ func NewKubeRouterDefault(config *options.KubeRouterConfig) (*KubeRouter, error)
4648
return &KubeRouter{Client: clientset, Config: config}, nil
4749
}
4850

51+
// CleanupConfigAndExit performs Cleanup on all three controllers
4952
func CleanupConfigAndExit() {
5053
npc := controllers.NetworkPolicyController{}
5154
npc.Cleanup()
@@ -104,6 +107,7 @@ func (kr *KubeRouter) stopApiWatchers() {
104107
watchers.StopNodeWatcher()
105108
}
106109

110+
// Run starts the controllers and waits forever till we get SIGINT or SIGTERM
107111
func (kr *KubeRouter) Run() error {
108112

109113
var err error

app/watchers/endpoints_watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (ew *endpointsWatcher) List() []*api.Endpoints {
8181
for i, ins := range objList {
8282
epInstances[i] = ins.(*api.Endpoints)
8383
}
84-
return epInstances
84+
return epInstances
8585
}
8686

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

app/watchers/node_watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (nw *nodeWatcher) List() []*api.Node {
6464
for i, ins := range objList {
6565
nodeInstances[i] = ins.(*api.Node)
6666
}
67-
return nodeInstances
67+
return nodeInstances
6868
}
6969

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

app/watchers/pods_watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (pw *podWatcher) List() []*api.Pod {
7272
for i, ins := range objList {
7373
podInstances[i] = ins.(*api.Pod)
7474
}
75-
return podInstances
75+
return podInstances
7676
}
7777

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

app/watchers/services_watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (svcw *serviceWatcher) RegisterHandler(handler ServiceUpdatesHandler) {
6767
func (svcw *serviceWatcher) List() []*api.Service {
6868
objList := svcw.serviceLister.List()
6969
svcInstances := make([]*api.Service, len(objList))
70-
for i, ins := range objList{
70+
for i, ins := range objList {
7171
svcInstances[i] = ins.(*api.Service)
7272
}
7373
return svcInstances

utils/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
apiv1 "k8s.io/client-go/pkg/api/v1"
1010
)
1111

12+
// GetNodeObject returns the node API object for the node
1213
func GetNodeObject(clientset *kubernetes.Clientset, hostnameOverride string) (*apiv1.Node, error) {
1314

1415
// assuming kube-router is running as pod, first check env NODE_NAME

utils/pod_cidr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"k8s.io/client-go/kubernetes"
1212
)
1313

14+
// GetPodCidrFromCniSpec gets pod CIDR allocated to the node from CNI spec file and returns it
1415
func GetPodCidrFromCniSpec(cniConfFilePath string) (net.IPNet, error) {
1516
netconfig, err := libcni.ConfFromFile(cniConfFilePath)
1617
if err != nil {
@@ -25,6 +26,8 @@ func GetPodCidrFromCniSpec(cniConfFilePath string) (net.IPNet, error) {
2526
return net.IPNet(ipamConfig.Subnet), nil
2627
}
2728

29+
// InsertPodCidrInCniSpec inserts the pod CIDR allocated to the node by kubernetes controlller manager
30+
// and stored it in the CNI specification
2831
func InsertPodCidrInCniSpec(cniConfFilePath string, cidr string) error {
2932
file, err := ioutil.ReadFile(cniConfFilePath)
3033
if err != nil {
@@ -45,6 +48,7 @@ func InsertPodCidrInCniSpec(cniConfFilePath string, cidr string) error {
4548
return nil
4649
}
4750

51+
// GetPodCidrFromNodeSpec reads the pod CIDR allocated to the node from API node object and returns it
4852
func GetPodCidrFromNodeSpec(clientset *kubernetes.Clientset, hostnameOverride string) (string, error) {
4953
node, err := GetNodeObject(clientset, hostnameOverride)
5054
if err != nil {

0 commit comments

Comments
 (0)