cleanup: resolve nlreturn linter issues

nlreturn linter requires a new line before return
and branch statements except when the return is alone
inside a statement group (such as an if statement) to
increase code clarity. This commit addresses such issues.

Updates: #1586

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R
2021-07-22 11:15:17 +05:30
committed by mergify[bot]
parent 5c016b4b94
commit 43f753760b
74 changed files with 716 additions and 0 deletions

View File

@ -52,6 +52,7 @@ func ExecCommand(ctx context.Context, program string, args ...string) (string, s
if ctx != context.TODO() {
UsefulLog(ctx, "%s", err)
}
return stdout, stderr, err
}
@ -98,6 +99,7 @@ func GetPoolName(monitors string, cr *Credentials, poolID int64) (string, error)
} else if err != nil {
return "", fmt.Errorf("failed to get pool ID %d: %w", poolID, err)
}
return name, nil
}
@ -136,6 +138,7 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam
if errors.Is(err, ErrPoolNotFound) {
err = JoinErrors(ErrObjectNotFound, err)
}
return err
}
defer ioctx.Destroy()
@ -149,6 +152,7 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam
return JoinErrors(ErrObjectExists, err)
} else if err != nil {
ErrorLog(ctx, "failed creating omap (%s) in pool (%s): (%v)", objectName, poolName, err)
return err
}
@ -170,6 +174,7 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam
if errors.Is(err, ErrPoolNotFound) {
err = JoinErrors(ErrObjectNotFound, err)
}
return err
}
defer ioctx.Destroy()
@ -183,6 +188,7 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam
return JoinErrors(ErrObjectNotFound, err)
} else if err != nil {
ErrorLog(ctx, "failed removing omap (%s) in pool (%s): (%v)", oMapName, poolName, err)
return err
}

View File

@ -72,5 +72,6 @@ if any ceph commands fails it will log below error message
// createKeyRingFile creates the keyring files to fix above error message logging.
func createKeyRingFile() error {
_, err := os.Create(keyRing)
return err
}

View File

@ -111,8 +111,10 @@ func (cp *ConnPool) getConn(unique string) *rados.Conn {
ce, exists := cp.conns[unique]
if exists {
ce.get()
return ce.conn
}
return nil
}
@ -159,6 +161,7 @@ func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
if oldConn := cp.getConn(unique); oldConn != nil {
// there was a race, oldConn already exists
ce.destroy()
return oldConn, nil
}
// this really is a new connection, add it to the map
@ -176,6 +179,7 @@ func (cp *ConnPool) Copy(conn *rados.Conn) *rados.Conn {
for _, ce := range cp.conns {
if ce.conn == conn {
ce.get()
return ce.conn
}
}
@ -192,6 +196,7 @@ func (cp *ConnPool) Put(conn *rados.Conn) {
for _, ce := range cp.conns {
if ce.conn == conn {
ce.put()
return
}
}

View File

@ -65,6 +65,7 @@ func (cp *ConnPool) fakeGet(monitors, user, keyfile string) (*rados.Conn, string
if oldConn := cp.getConn(unique); oldConn != nil {
// there was a race, oldConn already exists
ce.destroy()
return oldConn, unique, nil
}
// this really is a new connection, add it to the map
@ -83,6 +84,7 @@ func TestConnPool(t *testing.T) {
err := ioutil.WriteFile(keyfile, []byte("the-key"), 0o600)
if err != nil {
t.Errorf("failed to create keyfile: %v", err)
return
}
defer os.Remove(keyfile)

View File

@ -98,6 +98,7 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) {
} else {
err = fmt.Errorf("failed to open IOContext for pool %s: %w", pool, err)
}
return nil, err
}
@ -137,5 +138,6 @@ func (cc *ClusterConnection) DisableDiscardOnZeroedWriteSame() error {
}
cc.discardOnZeroedWriteSameDisabled = true
return nil
}

View File

@ -58,6 +58,7 @@ func storeKey(key string) (string, error) {
keyFile := tmpfile.Name()
if keyFile == "" {
err = fmt.Errorf("error reading temporary filename for key: %w", err)
return "", err
}
@ -115,5 +116,6 @@ func GetMonValFromSecret(secrets map[string]string) (string, error) {
if mons, ok := secrets[credMonitors]; ok {
return mons, nil
}
return "", fmt.Errorf("missing %q", credMonitors)
}

View File

@ -87,6 +87,7 @@ func NewVolumeEncryption(id string, kms EncryptionKMS) (*VolumeEncryption, error
}
ve.dekStore = dekStore
return ve, nil
}
@ -196,6 +197,7 @@ func (ve *VolumeEncryption) StoreCryptoPassphrase(volumeID, passphrase string) e
if err != nil {
return fmt.Errorf("failed to save the passphrase for %s: %w", volumeID, err)
}
return nil
}
@ -226,6 +228,7 @@ func generateNewEncryptionPassphrase() (string, error) {
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(bytesPassphrase), nil
}
@ -233,6 +236,7 @@ func generateNewEncryptionPassphrase() (string, error) {
func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
mapperFile = mapperFilePrefix + volumeID
mapperFilePath = path.Join(mapperFilePathPrefix, mapperFile)
return mapperFile, mapperFilePath
}
@ -242,6 +246,7 @@ func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
if _, _, err := LuksFormat(devicePath, passphrase); err != nil {
return fmt.Errorf("failed to encrypt device %s with LUKS: %w", devicePath, err)
}
return nil
}
@ -252,6 +257,7 @@ func OpenEncryptedVolume(ctx context.Context, devicePath, mapperFile, passphrase
if err != nil {
WarningLog(ctx, "failed to open LUKS device %q: %s", devicePath, stderr)
}
return err
}
@ -259,12 +265,14 @@ func OpenEncryptedVolume(ctx context.Context, devicePath, mapperFile, passphrase
func CloseEncryptedVolume(ctx context.Context, mapperFile string) error {
DebugLog(ctx, "Closing LUKS device %s", mapperFile)
_, _, err := LuksClose(mapperFile)
return err
}
// IsDeviceOpen determines if encrypted device is already open.
func IsDeviceOpen(ctx context.Context, device string) (bool, error) {
_, mappedFile, err := DeviceEncryptionStatus(ctx, device)
return (mappedFile != ""), err
}
@ -279,6 +287,7 @@ func DeviceEncryptionStatus(ctx context.Context, devicePath string) (mappedDevic
stdout, _, err := LuksStatus(mapPath)
if err != nil {
DebugLog(ctx, "device %s is not an active LUKS device: %v", devicePath, err)
return devicePath, "", nil
}
lines := strings.Split(string(stdout), "\n")

View File

@ -72,6 +72,7 @@ func readClusterInfo(pathToConfig, clusterID string) (*ClusterInfo, error) {
content, err := ioutil.ReadFile(pathToConfig)
if err != nil {
err = fmt.Errorf("error fetching configuration for cluster ID %q: %w", clusterID, err)
return nil, err
}
@ -100,6 +101,7 @@ func Mons(pathToConfig, clusterID string) (string, error) {
if len(cluster.Monitors) == 0 {
return "", fmt.Errorf("empty monitor list for cluster ID (%s) in config", clusterID)
}
return strings.Join(cluster.Monitors, ","), nil
}
@ -109,6 +111,7 @@ func RadosNamespace(pathToConfig, clusterID string) (string, error) {
if err != nil {
return "", err
}
return cluster.RadosNamespace, nil
}
@ -122,6 +125,7 @@ func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) {
if cluster.CephFS.SubvolumeGroup == "" {
return defaultCsiSubvolumeGroup, nil
}
return cluster.CephFS.SubvolumeGroup, nil
}

View File

@ -14,6 +14,7 @@ import (
// ValidateURL validates the url.
func ValidateURL(c *Config) error {
_, err := url.Parse(c.MetricsPath)
return err
}

View File

@ -51,6 +51,7 @@ func (vl *VolumeLocks) TryAcquire(volumeID string) bool {
return false
}
vl.locks.Insert(volumeID)
return true
}
@ -96,6 +97,7 @@ func NewOperationLock() *OperationLock {
lock[cloneOpt] = make(map[string]int)
lock[restoreOp] = make(map[string]int)
lock[expandOp] = make(map[string]int)
return &OperationLock{
locks: lock,
}
@ -176,6 +178,7 @@ func (ol *OperationLock) tryAcquire(op operation, volumeID string) error {
default:
return fmt.Errorf("%v operation not supported", op)
}
return nil
}

View File

@ -44,5 +44,6 @@ func NewK8sClient() *k8s.Clientset {
if err != nil {
FatalLogMsg("Failed to create client with error: %v\n", err)
}
return client
}

View File

@ -181,6 +181,7 @@ func getKMSProvider(config map[string]interface{}) (string, error) {
return "", fmt.Errorf("could not convert KMS provider"+
"type (%v) to string", providerName)
}
return name, nil
}
@ -191,6 +192,7 @@ func getKMSProvider(config map[string]interface{}) (string, error) {
return "", fmt.Errorf("could not convert KMS provider"+
"type (%v) to string", providerName)
}
return name, nil
}

View File

@ -49,6 +49,7 @@ func Log(ctx context.Context, format string) string {
return a + format
}
a += fmt.Sprintf("Req-ID: %v ", reqID)
return a + format
}

View File

@ -53,6 +53,7 @@ func getCgroupPidsFile() (string, error) {
}
if parts[1] == "pids" {
slice = parts[2]
break
}
}
@ -61,6 +62,7 @@ func getCgroupPidsFile() (string, error) {
}
pidsMax := fmt.Sprintf(sysPidsMaxFmt, slice)
return pidsMax, nil
}
@ -91,6 +93,7 @@ func GetPIDLimit() (int, error) {
return 0, err
}
}
return maxPids, nil
}
@ -115,6 +118,7 @@ func SetPIDLimit(limit int) error {
_, err = f.WriteString(limitStr)
if err != nil {
f.Close() // #nosec: a write error will be more useful to return
return err
}

View File

@ -64,6 +64,7 @@ func initSecretsKMS(secrets map[string]string) (EncryptionKMS, error) {
if !ok {
return nil, errors.New("missing encryption passphrase in secrets")
}
return SecretsKMS{passphrase: passphraseValue}, nil
}
@ -267,6 +268,7 @@ func generateCipher(passphrase, salt string) (cipher.AEAD, error) {
if err != nil {
return nil, err
}
return aead, nil
}

View File

@ -48,11 +48,13 @@ func stripKey(out []string) bool {
for i := range out {
if strings.HasPrefix(out[i], keyArg) {
out[i] = strippedKey
return true
}
if strings.HasPrefix(out[i], keyFileArg) {
out[i] = strippedKeyFile
return true
}
}

View File

@ -102,6 +102,7 @@ func GetTopologyFromDomainLabels(domainLabels, nodeName, driverName string) (map
missingLabels = append(missingLabels, key)
}
}
return nil, fmt.Errorf("missing domain labels %v on node %q", missingLabels, nodeName)
}
@ -173,6 +174,7 @@ func MatchTopologyForPool(topologyPools *[]TopologyConstrainedPool,
for _, value := range *topologyPools {
if value.PoolName == poolName {
topologyPool = append(topologyPool, value)
break
}
}
@ -230,6 +232,7 @@ func matchPoolToTopology(topologyPools *[]TopologyConstrainedPool, topology *csi
for _, segment := range topologyPool.DomainSegments {
if domainValue, ok := domainMap[segment.DomainLabel]; !ok || domainValue != segment.DomainValue {
mismatch = true
break
}
}

View File

@ -235,6 +235,7 @@ func TestFindPoolAndTopology(t *testing.T) {
topologyPrefix+"/"+label1+l1Value1, topologyPrefix+"/"+label2+l2Value1,
poolName, topoSegment)
}
return nil
}
// Test nil values

View File

@ -52,6 +52,7 @@ func RoundOffBytes(bytes int64) int64 {
num = int64(math.Ceil(floatBytes / helpers.GiB))
num *= helpers.GiB
}
return num
}
@ -131,10 +132,12 @@ func ValidateDriverName(driverName string) error {
for _, msg := range validation.IsDNS1123Subdomain(strings.ToLower(driverName)) {
if err == nil {
err = errors.New(msg)
continue
}
err = fmt.Errorf("%s: %w", msg, err)
}
return err
}
@ -145,6 +148,7 @@ func GetKernelVersion() (string, error) {
if err := unix.Uname(&utsname); err != nil {
return "", err
}
return strings.TrimRight(string(utsname.Release[:]), "\x00"), nil
}
@ -213,6 +217,7 @@ func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool
version, patchlevel, sublevel, extraversion, err := parseKernelRelease(release)
if err != nil {
ErrorLogMsg("%v", err)
return false
}
@ -238,6 +243,7 @@ func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool
}
}
ErrorLogMsg("kernel %s does not support required features", release)
return false
}
@ -282,6 +288,7 @@ func checkDirExists(p string) bool {
if _, err := os.Stat(p); os.IsNotExist(err) {
return false
}
return true
}
@ -299,6 +306,7 @@ func IsMountPoint(p string) (bool, error) {
// Mount mounts the source to target path.
func Mount(source, target, fstype string, options []string) error {
dummyMount := mount.New("")
return dummyMount.Mount(source, target, fstype, options)
}
@ -354,5 +362,6 @@ func getKeys(m map[string]interface{}) []string {
func CallStack() string {
stack := make([]byte, 2048)
_ = runtime.Stack(stack, false)
return string(stack)
}

View File

@ -32,6 +32,7 @@ func ValidateNodeStageVolumeRequest(req *csi.NodeStageVolumeRequest) error {
"staging path %s does not exist on node",
req.GetStagingTargetPath())
}
return nil
}
@ -93,5 +94,6 @@ func CheckReadOnlyManyIsSupported(req *csi.CreateVolumeRequest) error {
}
}
}
return nil
}

View File

@ -105,6 +105,7 @@ func setConfigString(option *string, config map[string]interface{}, key string)
}
*option = s
return nil
}
@ -389,6 +390,7 @@ func detectAuthMountPath(path string) (string, error) {
for _, part := range parts {
if part == "auth" {
match = true
continue
}
if part == "login" {

View File

@ -134,6 +134,7 @@ func transformConfig(svMap map[string]interface{}) (map[string]interface{}, erro
if err != nil {
return nil, fmt.Errorf("failed to Unmarshal the CSI vault configuration: %w", err)
}
return jsonMap, nil
}
@ -577,5 +578,6 @@ func fetchTenantConfig(config map[string]interface{}, tenant string) (map[string
if !ok {
return nil, false
}
return tenantConfig, true
}

View File

@ -138,6 +138,7 @@ func TestStdVaultToCSIConfig(t *testing.T) {
err := json.Unmarshal([]byte(vaultConfigMap), sv)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}