cleanup: address gomnd warnings

Direct usage of numbers should be avoided.

Issue reported:
mnd: Magic number: X, in <argument> detected (gomnd)

Signed-off-by: Yug <yuggupta27@gmail.com>
This commit is contained in:
Yug
2020-07-21 10:40:13 +05:30
committed by mergify[bot]
parent e73fe64a0d
commit 71ddf51544
11 changed files with 38 additions and 22 deletions

View File

@ -52,7 +52,8 @@ func GetTopologyFromDomainLabels(domainLabels, nodeName, driverName string) (map
// size checks on domain label prefix
topologyPrefix := strings.ToLower("topology." + driverName)
if len(topologyPrefix) > 63 {
const lenLimit = 63
if len(topologyPrefix) > lenLimit {
return nil, fmt.Errorf("computed topology label prefix (%s) for node exceeds length limits", topologyPrefix)
}
// driverName is validated, and we are adding a lowercase "topology." to it, so no validation for conformance

View File

@ -125,7 +125,8 @@ func ValidateDriverName(driverName string) error {
return errors.New("driver name is empty")
}
if len(driverName) > 63 {
const reqDriverNameLen = 63
if len(driverName) > reqDriverNameLen {
return errors.New("driver name length should be less than 63 chars")
}
var err error
@ -191,7 +192,8 @@ func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool
return false
}
sublevel := 0
if len(vers) >= 3 {
const minLenForSublvl = 3
if len(vers) >= minLenForSublvl {
sublevel, err = strconv.Atoi(vers[2])
if err != nil {
klog.Errorf("failed to parse sublevel from %s: %v", release, err)
@ -200,7 +202,8 @@ func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool
}
extra := strings.SplitN(release, "-", 2)
extraversion := 0
if len(extra) == 2 {
const expectedExtraLen = 2
if len(extra) == expectedExtraLen {
// ignore errors, 1st component of extraversion does not need to be an int
extraversion, err = strconv.Atoi(strings.Split(extra[1], ".")[0])
if err != nil {

View File

@ -151,7 +151,7 @@ func (kms *VaultKMS) GetPassphrase(key string) (string, error) {
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
if resp.StatusCode == http.StatusNotFound {
return "", MissingPassphrase{fmt.Errorf("passphrase for %s not found", key)}
}
err = kms.processError(resp, fmt.Sprintf("get passphrase for %s", key))
@ -219,7 +219,7 @@ func (kms *VaultKMS) DeletePassphrase(key string) error {
return fmt.Errorf("delete passphrase at %s request to vault failed: %s", key, err)
}
defer resp.Body.Close()
if resp.StatusCode != 404 {
if resp.StatusCode != http.StatusNotFound {
err = kms.processError(resp, "delete passphrase")
if err != nil {
return err

View File

@ -127,9 +127,12 @@ func (ci *CSIIdentifier) DecomposeCSIID(composedCSIID string) (err error) {
ci.ClusterID = composedCSIID[10 : 10+clusterIDLength]
// additional 1 for '-' separator
bytesToProcess -= (clusterIDLength + 1)
nextFieldStartIdx := 10 + clusterIDLength + 1
nextFieldStartIdx := (10 + clusterIDLength + 1)
if bytesToProcess < 17 {
// minLenToDecode is now 17 as composedCSIID should include
// atleast 16 for poolID encoding and 1 for '-' separator.
const minLenToDecode = 17
if bytesToProcess < minLenToDecode {
return errors.New("failed to decode CSI identifier, string underflow")
}
buf64, err := hex.DecodeString(composedCSIID[nextFieldStartIdx : nextFieldStartIdx+16])