From f03f917c332122adaefbc4d332982820fe9307f7 Mon Sep 17 00:00:00 2001 From: unclejack Date: Tue, 6 Dec 2016 23:55:41 +0200 Subject: [PATCH] return directly instead of using if where possible Signed-off-by: Cristian Staretu --- drivers/ovsdriver.go | 15 ++------------ netmaster/docknet/docknet.go | 14 ++----------- netmaster/main.go | 6 +----- netmaster/master/bgp.go | 7 +------ netmaster/master/endpointGroup.go | 13 ++---------- netmaster/master/netmaster.go | 14 ++----------- netmaster/master/network.go | 7 +------ netmaster/master/provider.go | 13 ++---------- netmaster/objApi/apiController.go | 12 ++--------- netmaster/objApi/extContracts.go | 6 +----- netmaster/resources/stateresourcemanager.go | 7 +------ netmaster/resources/vlanresource.go | 20 +++---------------- netmaster/resources/vxlanresource.go | 20 +++---------------- netplugin/cluster/cluster.go | 5 +---- .../svcplugin/skydns2extension/skydns2.go | 6 ++---- state/consulstatedriver.go | 19 +++--------------- state/etcdstatedriver.go | 14 ++----------- state/fakestatedriver.go | 14 ++----------- 18 files changed, 33 insertions(+), 179 deletions(-) diff --git a/drivers/ovsdriver.go b/drivers/ovsdriver.go index 90c1fe651..021d7c3b9 100755 --- a/drivers/ovsdriver.go +++ b/drivers/ovsdriver.go @@ -603,12 +603,7 @@ func (d *OvsDriver) AddMaster(node core.ServiceInfo) error { if err != nil { return err } - err = d.switchDb["vxlan"].AddMaster(node) - - if err != nil { - return err - } - return nil + return d.switchDb["vxlan"].AddMaster(node) } // DeleteMaster deletes master node @@ -620,13 +615,7 @@ func (d *OvsDriver) DeleteMaster(node core.ServiceInfo) error { if err != nil { return err } - err = d.switchDb["vxlan"].DeleteMaster(node) - - if err != nil { - return err - } - - return nil + return d.switchDb["vxlan"].DeleteMaster(node) } // AddBgp adds bgp config by named identifier diff --git a/netmaster/docknet/docknet.go b/netmaster/docknet/docknet.go index b73ccb547..93c2561c0 100644 --- a/netmaster/docknet/docknet.go +++ b/netmaster/docknet/docknet.go @@ -196,12 +196,7 @@ func CreateDockNet(tenantName, networkName, serviceName string, nwCfg *mastercfg dnetOper.StateDriver = stateDriver // write the dnet oper state - err = dnetOper.Write() - if err != nil { - return err - } - - return nil + return dnetOper.Write() } // DeleteDockNet deletes a network in docker daemon @@ -238,12 +233,7 @@ func DeleteDockNet(tenantName, networkName, serviceName string) error { dnetOper.StateDriver = stateDriver // write the dnet oper state - err = dnetOper.Clear() - if err != nil { - return err - } - - return nil + return dnetOper.Clear() } // FindDocknetByUUID find the docknet by UUID diff --git a/netmaster/main.go b/netmaster/main.go index c7380cca6..ed253d356 100755 --- a/netmaster/main.go +++ b/netmaster/main.go @@ -74,11 +74,7 @@ func parseOpts(opts *cliOpts) error { false, "prints current version") - if err := flagSet.Parse(os.Args[1:]); err != nil { - return err - } - - return nil + return flagSet.Parse(os.Args[1:]) } func execOpts(opts *cliOpts) { diff --git a/netmaster/master/bgp.go b/netmaster/master/bgp.go index f0cc95ac2..b57542ebf 100755 --- a/netmaster/master/bgp.go +++ b/netmaster/master/bgp.go @@ -41,12 +41,7 @@ func AddBgp(stateDriver core.StateDriver, bgpCfg *intent.ConfigBgp) error { bgpState.Neighbor = bgpCfg.Neighbor bgpState.StateDriver = stateDriver bgpState.ID = bgpCfg.Hostname - err := bgpState.Write() - - if err != nil { - return err - } - return nil + return bgpState.Write() } //DeleteBgp deletes from etcd state diff --git a/netmaster/master/endpointGroup.go b/netmaster/master/endpointGroup.go index 5fefe8ad5..7fcef10e0 100644 --- a/netmaster/master/endpointGroup.go +++ b/netmaster/master/endpointGroup.go @@ -125,12 +125,7 @@ func CreateEndpointGroup(tenantName, networkName, groupName string) error { log.Debugf("ACI -- Allocated vlan %v for epg %v", pktTag, groupName) } - err = epgCfg.Write() - if err != nil { - return err - } - - return nil + return epgCfg.Write() } // DeleteEndpointGroup handles endpoint group deletes @@ -224,9 +219,5 @@ func UpdateEndpointGroup(bandwidth, groupName, tenantName string, Dscp, burst in epCfg.Burst = burst //Write to etcd - err = epCfg.Write() - if err != nil { - return err - } - return nil + return epCfg.Write() } diff --git a/netmaster/master/netmaster.go b/netmaster/master/netmaster.go index 3e1dd1ff6..63f1070ee 100755 --- a/netmaster/master/netmaster.go +++ b/netmaster/master/netmaster.go @@ -193,12 +193,7 @@ func CreateGlobal(stateDriver core.StateDriver, gc *intent.ConfigGlobal) error { return err } } - err = masterGc.Write() - if err != nil { - return err - } - - return nil + return masterGc.Write() } // UpdateGlobal updates the global state @@ -287,12 +282,7 @@ func UpdateGlobal(stateDriver core.StateDriver, gc *intent.ConfigGlobal) error { } } - err = masterGc.Write() - if err != nil { - return err - } - - return nil + return masterGc.Write() } // DeleteGlobal delete global state diff --git a/netmaster/master/network.go b/netmaster/master/network.go index 7344f0c6b..f507847be 100755 --- a/netmaster/master/network.go +++ b/netmaster/master/network.go @@ -297,12 +297,7 @@ func attachServiceContainer(tenantName, networkName string, stateDriver core.Sta log.Infof("Dns server for network %s: %s", networkName, nwCfg.DNSServer) // write the network config - err = nwCfg.Write() - if err != nil { - return err - } - - return nil + return nwCfg.Write() } // detachServiceContainer detaches the service container's endpoint during network delete diff --git a/netmaster/master/provider.go b/netmaster/master/provider.go index c0f0b5be5..197d91f5f 100755 --- a/netmaster/master/provider.go +++ b/netmaster/master/provider.go @@ -35,11 +35,7 @@ func SvcProviderUpdate(serviceID string, isDelete bool) error { if _, present := mastercfg.ServiceLBDb[serviceID]; !present { svcProvider.ID = serviceID - err = svcProvider.Clear() - if err != nil { - return err - } - return nil + return svcProvider.Clear() } for _, provider := range mastercfg.ServiceLBDb[serviceID].Providers { @@ -55,12 +51,7 @@ func SvcProviderUpdate(serviceID string, isDelete bool) error { log.Infof("Updating service providers with {%v} on service %s", svcProvider.Providers, serviceID) - err = svcProvider.Write() - - if err != nil { - return err - } - return nil + return svcProvider.Write() } func getProviderID(provider *mastercfg.Provider) string { diff --git a/netmaster/objApi/apiController.go b/netmaster/objApi/apiController.go index e433593da..aff0b24d6 100755 --- a/netmaster/objApi/apiController.go +++ b/netmaster/objApi/apiController.go @@ -1133,12 +1133,7 @@ func (ac *APIController) NetworkDelete(network *contivModel.Network) error { } // Save the tenant too since we removed the links - err = tenant.Write() - if err != nil { - return err - } - - return nil + return tenant.Write() } // NetprofileCreate creates the network rule @@ -2149,10 +2144,7 @@ func (ac *APIController) ServiceLBGetOper(serviceLB *contivModel.ServiceLBInspec } func validateSelectors(selector string) bool { - if strings.Count(selector, "=") == 1 { - return true - } - return false + return strings.Count(selector, "=") == 1 } func validatePorts(ports []string) bool { diff --git a/netmaster/objApi/extContracts.go b/netmaster/objApi/extContracts.go index d81e97a46..a5114df43 100644 --- a/netmaster/objApi/extContracts.go +++ b/netmaster/objApi/extContracts.go @@ -81,11 +81,7 @@ func setupExternalContracts(endpointGroup *contivModel.EndpointGroup, extContrac // Check if the external contracts are being used by any of the EPGs. func isExtContractsGroupUsed(contractsGroup *contivModel.ExtContractsGroup) bool { - if len(contractsGroup.LinkSets.EndpointGroups) > 0 { - return true - } - - return false + return len(contractsGroup.LinkSets.EndpointGroups) > 0 } // ExtContractsGroupCreate creates a new group of external contracts diff --git a/netmaster/resources/stateresourcemanager.go b/netmaster/resources/stateresourcemanager.go index 7b2ed65a6..950ea6e51 100644 --- a/netmaster/resources/stateresourcemanager.go +++ b/netmaster/resources/stateresourcemanager.go @@ -127,12 +127,7 @@ func (rm *StateResourceManager) DefineResource(id, desc string, return core.Errorf("Resource with id: %q already exists", id) } - err = rsrc.Init(rsrcCfg) - if err != nil { - return err - } - - return nil + return rsrc.Init(rsrcCfg) } // UndefineResource deinitializes a resource. diff --git a/netmaster/resources/vlanresource.go b/netmaster/resources/vlanresource.go index b3a75f192..e55d4d2e6 100644 --- a/netmaster/resources/vlanresource.go +++ b/netmaster/resources/vlanresource.go @@ -91,12 +91,7 @@ func (r *AutoVLANCfgResource) Init(rsrcCfg interface{}) error { oper := &AutoVLANOperResource{FreeVLANs: r.VLANs} oper.StateDriver = r.StateDriver oper.ID = r.ID - err = oper.Write() - if err != nil { - return err - } - - return nil + return oper.Write() } // Deinit the resource. @@ -148,12 +143,7 @@ func (r *AutoVLANCfgResource) Reinit(rsrcCfg interface{}) error { oper.FreeVLANs.Clear(i) } - err = oper.Write() - if err != nil { - return err - } - - return nil + return oper.Write() } // Description is a description of this resource. returns AutoVLANResource. @@ -268,11 +258,7 @@ func (r *AutoVLANCfgResource) Deallocate(value interface{}) error { } oper.FreeVLANs.Set(vlan) - err = oper.Write() - if err != nil { - return err - } - return nil + return oper.Write() } // AutoVLANOperResource is an implementation of core.State. diff --git a/netmaster/resources/vxlanresource.go b/netmaster/resources/vxlanresource.go index 337735cd5..cc8bb2857 100644 --- a/netmaster/resources/vxlanresource.go +++ b/netmaster/resources/vxlanresource.go @@ -100,12 +100,7 @@ func (r *AutoVXLANCfgResource) Init(rsrcCfg interface{}) error { oper := &AutoVXLANOperResource{FreeVXLANs: r.VXLANs, FreeLocalVLANs: r.LocalVLANs} oper.StateDriver = r.StateDriver oper.ID = r.ID - err = oper.Write() - if err != nil { - return err - } - - return nil + return oper.Write() } // Deinit the resource. @@ -169,12 +164,7 @@ func (r *AutoVXLANCfgResource) Reinit(rsrcCfg interface{}) error { oper.FreeLocalVLANs = oper.FreeLocalVLANs.Intersection(r.LocalVLANs) - err = oper.Write() - if err != nil { - return err - } - - return nil + return oper.Write() } // Description is a string description of the resource. Returns AutoVXLANResource. @@ -288,11 +278,7 @@ func (r *AutoVXLANCfgResource) Deallocate(value interface{}) error { vlan := pair.VLAN oper.FreeLocalVLANs.Set(vlan) - err = oper.Write() - if err != nil { - return err - } - return nil + return oper.Write() } // AutoVXLANOperResource is an implementation of core.State diff --git a/netplugin/cluster/cluster.go b/netplugin/cluster/cluster.go index d44647c0e..4f230bfc0 100755 --- a/netplugin/cluster/cluster.go +++ b/netplugin/cluster/cluster.go @@ -348,11 +348,8 @@ func Init(storeURL string) error { // Create an objdb client ObjdbClient, err = objdb.NewClient(storeURL) - if err != nil { - return err - } - return nil + return err } // RunLoop registers netplugin service with cluster store and runs peer discovery diff --git a/netplugin/svcplugin/skydns2extension/skydns2.go b/netplugin/svcplugin/skydns2extension/skydns2.go index 5776a67c9..0f9b045f9 100644 --- a/netplugin/svcplugin/skydns2extension/skydns2.go +++ b/netplugin/svcplugin/skydns2extension/skydns2.go @@ -47,10 +47,8 @@ type Skydns2Adapter struct { // Ping will try to connect to skydns2 by attempting to retrieve a key func (r *Skydns2Adapter) Ping() error { _, err := r.client.Get(context.Background(), "/", &client.GetOptions{Recursive: true, Sort: true}) - if err != nil { - return err - } - return nil + + return err } // Register will register Skydns2Adapter's interface with RegistryAdapter diff --git a/state/consulstatedriver.go b/state/consulstatedriver.go index 1c2add5e0..f8f448bbe 100644 --- a/state/consulstatedriver.go +++ b/state/consulstatedriver.go @@ -54,11 +54,8 @@ func (d *ConsulStateDriver) Init(instInfo *core.InstanceInfo) error { } d.Client, err = api.NewClient(&cfg) - if err != nil { - return err - } - return nil + return err } // Deinit is currently a no-op. @@ -270,12 +267,7 @@ func (d *ConsulStateDriver) ReadState(key string, value core.State, return err } - err = unmarshal(encodedState, value) - if err != nil { - return err - } - - return nil + return unmarshal(encodedState, value) } // ReadAllState Reads all the state from baseKey and returns a list of core.State. @@ -313,10 +305,5 @@ func (d *ConsulStateDriver) WriteState(key string, value core.State, return err } - err = d.Write(key, encodedState) - if err != nil { - return err - } - - return nil + return d.Write(key, encodedState) } diff --git a/state/etcdstatedriver.go b/state/etcdstatedriver.go index e54ad8b26..0475e4772 100644 --- a/state/etcdstatedriver.go +++ b/state/etcdstatedriver.go @@ -210,12 +210,7 @@ func (d *EtcdStateDriver) ReadState(key string, value core.State, return err } - err = unmarshal(encodedState, value) - if err != nil { - return err - } - - return nil + return unmarshal(encodedState, value) } // readAllStateCommon reads and unmarshals (given a function) all state into a @@ -334,10 +329,5 @@ func (d *EtcdStateDriver) WriteState(key string, value core.State, return err } - err = d.Write(key, encodedState) - if err != nil { - return err - } - - return nil + return d.Write(key, encodedState) } diff --git a/state/fakestatedriver.go b/state/fakestatedriver.go index f4e3fe5bb..b43be23c2 100644 --- a/state/fakestatedriver.go +++ b/state/fakestatedriver.go @@ -84,12 +84,7 @@ func (d *FakeStateDriver) ReadState(key string, value core.State, return err } - err = unmarshal(encodedState, value) - if err != nil { - return err - } - - return nil + return unmarshal(encodedState, value) } // ReadAllState reads all state from baseKey of a given type @@ -112,12 +107,7 @@ func (d *FakeStateDriver) WriteState(key string, value core.State, return err } - err = d.Write(key, encodedState) - if err != nil { - return err - } - - return nil + return d.Write(key, encodedState) } // DumpState is a debugging tool.