From b53382de1ace42392868a838f5054f5c1bc015cc Mon Sep 17 00:00:00 2001 From: edmondshtogu Date: Wed, 2 Aug 2023 20:42:58 +0200 Subject: [PATCH] Fixing lint issues.. --- provider/pkg/esxi/autoNamingService.go | 4 -- provider/pkg/esxi/connectionInfo.go | 6 +- provider/pkg/esxi/host.go | 21 ++++--- provider/pkg/esxi/portGroup.go | 28 +++++----- provider/pkg/esxi/types.go | 2 + provider/pkg/esxi/utils.go | 3 +- provider/pkg/esxi/virtualDisk.go | 46 +++++++--------- provider/pkg/esxi/virtualMacineUtils.go | 6 +- provider/pkg/esxi/virtualSwitch.go | 73 ++++++++++++------------- 9 files changed, 93 insertions(+), 96 deletions(-) diff --git a/provider/pkg/esxi/autoNamingService.go b/provider/pkg/esxi/autoNamingService.go index 46226d3..6ca91ab 100644 --- a/provider/pkg/esxi/autoNamingService.go +++ b/provider/pkg/esxi/autoNamingService.go @@ -6,10 +6,6 @@ type AutoNamingSpec struct { MaxLength int } -type nameSpec struct { - minLength int - maxLength int -} type AutoNamingService struct { rules map[string]AutoNamingSpec } diff --git a/provider/pkg/esxi/connectionInfo.go b/provider/pkg/esxi/connectionInfo.go index 0e76a25..ffadb6f 100644 --- a/provider/pkg/esxi/connectionInfo.go +++ b/provider/pkg/esxi/connectionInfo.go @@ -6,13 +6,13 @@ import ( type ConnectionInfo struct { Host string - SshPort string + SSHPort string SslPort string UserName string Password string OvfLocation string } -func (c *ConnectionInfo) getSshConnection() string { - return fmt.Sprintf("%s:%s", c.Host, c.SshPort) +func (c *ConnectionInfo) getSSHConnection() string { + return fmt.Sprintf("%s:%s", c.Host, c.SSHPort) } diff --git a/provider/pkg/esxi/host.go b/provider/pkg/esxi/host.go index 9ff31a5..e72ac0d 100644 --- a/provider/pkg/esxi/host.go +++ b/provider/pkg/esxi/host.go @@ -2,6 +2,7 @@ package esxi import ( "fmt" + "net" "os" "strings" "time" @@ -13,6 +14,8 @@ import ( const ( failedToConnect = "failed to connect to esxi host" + + attempts = 10 ) type Host struct { @@ -23,7 +26,7 @@ type Host struct { func NewHost(host, sshPort, sslPort, user, pass string) (*Host, error) { connection := ConnectionInfo{ Host: host, - SshPort: sshPort, + SSHPort: sshPort, SslPort: sslPort, UserName: user, Password: pass, @@ -42,7 +45,9 @@ func NewHost(host, sshPort, sslPort, user, pass string) (*Host, error) { }), }, } - clientConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey() + clientConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error { + return nil + } instance := &Host{ Connection: &connection, @@ -64,11 +69,11 @@ func (esxi *Host) validateCreds() error { remoteCmd = "vmware --version" _, err = esxi.Execute(remoteCmd, "Connectivity test, get vmware version") if err != nil { - return fmt.Errorf("failed to connect to esxi host: %s", err) + return fmt.Errorf("failed to connect to esxi host: %w", err) } mkdir, err := esxi.Execute("mkdir -p ~", "Create home directory if missing") - logging.V(logLevel).Infof("ValidateCreds: Create home! %s %s", mkdir, err) + logging.V(logLevel).Infof("ValidateCreds: Create home! %s %s", mkdir, err.Error()) if err != nil { return err @@ -80,7 +85,7 @@ func (esxi *Host) validateCreds() error { // Connect to esxi host using ssh func (esxi *Host) connect(attempt int) (*ssh.Client, *ssh.Session, error) { for attempt > 0 { - client, err := ssh.Dial("tcp", esxi.Connection.getSshConnection(), esxi.ClientConfig) + client, err := ssh.Dial("tcp", esxi.Connection.getSSHConnection(), esxi.ClientConfig) if err != nil { logging.V(logLevel).Infof("Connect: Retry attempt %d", attempt) attempt -= 1 @@ -90,7 +95,7 @@ func (esxi *Host) connect(attempt int) (*ssh.Client, *ssh.Session, error) { if err != nil { closeErr := client.Close() if closeErr != nil { - return nil, nil, fmt.Errorf("session connection error. (closing client error: %s)", closeErr) + return nil, nil, fmt.Errorf("session connection error. (closing client error: %w)", closeErr) } return nil, nil, fmt.Errorf("session connection error") } @@ -153,7 +158,7 @@ func (esxi *Host) WriteFile(content string, path string, shortCmdDesc string) (s } defer os.Remove(f.Name()) - client, session, err := esxi.connect(10) + client, session, err := esxi.connect(attempts) if err != nil { logging.V(logLevel).Infof("Execute: Failed connecting to host! %s", err) return failedToConnect, err @@ -173,7 +178,7 @@ func (esxi *Host) WriteFile(content string, path string, shortCmdDesc string) (s func (esxi *Host) CopyFile(localPath string, hostPath string, shortCmdDesc string) (string, error) { logging.V(logLevel).Infof("CopyFile: %s", shortCmdDesc) - client, session, err := esxi.connect(10) + client, session, err := esxi.connect(attempts) if err != nil { logging.V(logLevel).Infof("Execute: Failed connecting to host! %s", err) return failedToConnect, err diff --git a/provider/pkg/esxi/portGroup.go b/provider/pkg/esxi/portGroup.go index a04a0f0..2836959 100644 --- a/provider/pkg/esxi/portGroup.go +++ b/provider/pkg/esxi/portGroup.go @@ -23,7 +23,7 @@ func PortGroupCreate(inputs resource.PropertyMap, esxi *Host) (string, resource. stdout, err := esxi.Execute(command, "create port group") if err != nil { - return "", nil, fmt.Errorf("failed to create port group: %s err:%s", stdout, err) + return "", nil, fmt.Errorf("failed to create port group: %s err:%w", stdout, err) } err = esxi.updatePortGroup(pg) @@ -62,7 +62,7 @@ func PortGroupDelete(id string, esxi *Host) error { stdout, err := esxi.Execute(command, "delete port group") if err != nil { - return fmt.Errorf("failed to delete port group: %s err:%s", stdout, err) + return fmt.Errorf("failed to delete port group: %s err:%w", stdout, err) } return nil @@ -141,7 +141,7 @@ func (esxi *Host) updatePortGroup(pg PortGroup) error { stdout, err := esxi.Execute(command, "port group set vlan") if err != nil { - return fmt.Errorf("failed to set port group vlan: %s err:%s", stdout, err) + return fmt.Errorf("failed to set port group vlan: %s err:%w", stdout, err) } command = fmt.Sprintf("esxcli network vswitch standard portgroup policy security set --use-vswitch --portgroup-name=\"%s\"", pg.Name) @@ -158,7 +158,7 @@ func (esxi *Host) updatePortGroup(pg PortGroup) error { stdout, err = esxi.Execute(command, "port group set security policy") if err != nil { - return fmt.Errorf("failed to set port group security policy: %s err:%s", stdout, err) + return fmt.Errorf("failed to set port group security policy: %s err:%w", stdout, err) } return nil @@ -170,19 +170,21 @@ func (esxi *Host) readPortGroup(pg PortGroup) (string, resource.PropertyMap, err stdout, err := esxi.Execute(command, "port group list") if stdout == "" { - return "", nil, fmt.Errorf("failed to list port group: %s err: %s", stdout, err) + return "", nil, fmt.Errorf("failed to list port group: %s err: %w", stdout, err) } - re, _ := regexp.Compile("( {2}.* {2}) +[0-9]+ +[0-9]+$") - if len(re.FindStringSubmatch(stdout)) > 0 { - pg.VSwitch = strings.Trim(re.FindStringSubmatch(stdout)[1], " ") + re := regexp.MustCompile("( {2}.* {2}) +[0-9]+ +[0-9]+$") + matches := re.FindStringSubmatch(stdout) + if len(matches) > 0 { + pg.VSwitch = strings.Trim(matches[1], " ") } else { pg.VSwitch = "" } - re, _ = regexp.Compile(" +([0-9]+)$") - if len(re.FindStringSubmatch(stdout)) > 0 { - pg.Vlan, _ = strconv.Atoi(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(" +([0-9]+)$") + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + pg.Vlan, _ = strconv.Atoi(matches[1]) } else { pg.Vlan = 0 } @@ -204,12 +206,12 @@ func (esxi *Host) readPortGroupSecurityPolicy(name string) (*PortGroupSecurityPo command := fmt.Sprintf("esxcli --formatter=csv network vswitch standard portgroup policy security get -p \"%s\"", name) stdout, err := esxi.Execute(command, "port group security policy") if stdout == "" { - return nil, fmt.Errorf("failed to get the port group security policy: %s\n%s\n", stdout, err) + return nil, fmt.Errorf("failed to get the port group security policy: %s err: %w", stdout, err) } var policies []PortGroupSecurityPolicy if err = csvutil.Unmarshal([]byte(stdout), &policies); err != nil || len(policies) != 1 { - return nil, fmt.Errorf("failed to parse the port group security policy: %s\n%s\n", stdout, err) + return nil, fmt.Errorf("failed to parse the port group security policy: %s err: %w", stdout, err) } return &policies[0], nil diff --git a/provider/pkg/esxi/types.go b/provider/pkg/esxi/types.go index 7192b8f..af96936 100644 --- a/provider/pkg/esxi/types.go +++ b/provider/pkg/esxi/types.go @@ -8,6 +8,8 @@ const ( vmTurnedOff = "off" vmTurnedSuspended = "suspended" vmSleepBetweenPowerStateChecks = 3 + + esxiUnknown = "Unknown" ) type KeyValuePair struct { diff --git a/provider/pkg/esxi/utils.go b/provider/pkg/esxi/utils.go index dc7a107..042e8eb 100644 --- a/provider/pkg/esxi/utils.go +++ b/provider/pkg/esxi/utils.go @@ -38,7 +38,8 @@ func structToMap(dataStruct interface{}) map[string]interface{} { fieldType := typeOfStruct.Field(i) // Convert the first letter of the field name to lowercase - key := string(fieldType.Name[0]+32) + fieldType.Name[1:] + const lowercaseBits = 32 + key := string(fieldType.Name[0]+lowercaseBits) + fieldType.Name[1:] switch field.Kind() { case reflect.Struct: diff --git a/provider/pkg/esxi/virtualDisk.go b/provider/pkg/esxi/virtualDisk.go index 0361e4b..ade1783 100644 --- a/provider/pkg/esxi/virtualDisk.go +++ b/provider/pkg/esxi/virtualDisk.go @@ -10,12 +10,7 @@ import ( ) func VirtualDiskCreate(inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) { - var vd VirtualDisk - if parsed, err := parseVirtualDisk("", inputs); err == nil { - vd = parsed - } else { - return "", nil, err - } + vd := parseVirtualDisk("", inputs) // create vd var id, command string var err error @@ -60,12 +55,7 @@ func VirtualDiskCreate(inputs resource.PropertyMap, esxi *Host) (string, resourc } func VirtualDiskUpdate(id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) { - var vd VirtualDisk - if parsed, err := parseVirtualDisk(id, inputs); err == nil { - vd = parsed - } else { - return "", nil, err - } + vd := parseVirtualDisk(id, inputs) changed, err := esxi.growVirtualDisk(vd.Id, vd.Size) if err != nil && !changed { @@ -95,7 +85,7 @@ func VirtualDiskDelete(id string, esxi *Host) error { command = fmt.Sprintf("ls -al \"/vmfs/volumes/%s/%s/\" |wc -l", vd.DiskStore, vd.Directory) - stdout, err = esxi.Execute(command, "check if storage dir is empty") + stdout, _ = esxi.Execute(command, "check if storage dir is empty") if stdout == "3" { { // Delete empty dir. Ignore stdout and errors. @@ -111,7 +101,7 @@ func VirtualDiskRead(id string, _ resource.PropertyMap, esxi *Host) (string, res return esxi.readVirtualDisk(id) } -func parseVirtualDisk(id string, inputs resource.PropertyMap) (VirtualDisk, error) { +func parseVirtualDisk(id string, inputs resource.PropertyMap) VirtualDisk { vd := VirtualDisk{} if len(id) > 0 { vd.Id = id @@ -132,7 +122,7 @@ func parseVirtualDisk(id string, inputs resource.PropertyMap) (VirtualDisk, erro vd.Size = 1 } - return vd, nil + return vd } func (esxi *Host) readVirtualDisk(id string) (string, resource.PropertyMap, error) { @@ -202,17 +192,18 @@ func (esxi *Host) getVirtualDisk(id string) (VirtualDisk, error) { var flatSizeI64 int64 var s []string - path := strings.TrimLeft(id, "/vmfs/volumes/") + const pathParts = 3 + path := strings.TrimPrefix(id, "/vmfs/volumes/") // Extract the values from the id string parts := strings.Split(path, "/") - if len(parts) < 3 { + if len(parts) < pathParts { return VirtualDisk{}, fmt.Errorf("invalid virtual disk id: '%s'", id) } // Access the individual parts diskStore = parts[0] diskName = parts[len(parts)-1] - if len(parts) == 3 { + if len(parts) == pathParts { diskDir = parts[1] } else { diskDir = strings.TrimLeft(path, fmt.Sprintf("%s/", diskStore)) @@ -227,8 +218,9 @@ func (esxi *Host) getVirtualDisk(id string) (VirtualDisk, error) { } // Get virtual disk flat size + const diskNameParts = 2 s = strings.Split(diskName, ".") - if len(s) < 2 { + if len(s) < diskNameParts { return VirtualDisk{}, fmt.Errorf("virtual disk name %s is not valid", diskName) } diskNameFlat := fmt.Sprintf("%s-flat.%s", s[0], s[1]) @@ -240,7 +232,8 @@ func (esxi *Host) getVirtualDisk(id string) (VirtualDisk, error) { return VirtualDisk{}, fmt.Errorf("failed to read virtual disk %s size, err: %s %s", id, flatSize, err) } flatSizeI64, _ = strconv.ParseInt(flatSize, 10, 64) - diskSize = int(flatSizeI64 / 1024 / 1024 / 1024) + const bytesSize = 1024 + diskSize = int(flatSizeI64 / bytesSize / bytesSize / bytesSize) // Determine virtual disk type (only works if Guest is powered off) command = fmt.Sprintf("vmkfstools -t0 \"%s\" |grep -q 'VMFS Z- LVID:' && echo true", id) @@ -252,14 +245,15 @@ func (esxi *Host) getVirtualDisk(id string) (VirtualDisk, error) { command = fmt.Sprintf("vmkfstools -t0 \"%s\" |grep -q 'NOMP -- :' && echo true", id) isThin, _ := esxi.Execute(command, "Get disk type. Is thin.") - if isThin == "true" { + switch { + case isThin == "true": diskType = "thin" - } else if isZeroedThick == "true" { + case isZeroedThick == "true": diskType = "zeroedthick" - } else if isEagerZeroedThick == "true" { + case isEagerZeroedThick == "true": diskType = "eagerzeroedthick" - } else { - diskType = "Unknown" + default: + diskType = esxiUnknown } return VirtualDisk{ @@ -272,7 +266,7 @@ func (vd *VirtualDisk) toMap(keepId ...bool) map[string]interface{} { if len(keepId) != 0 && !keepId[0] { delete(outputs, "id") } - if vd.DiskType == "Unknown" { + if vd.DiskType == esxiUnknown { delete(outputs, "diskType") } return outputs diff --git a/provider/pkg/esxi/virtualMacineUtils.go b/provider/pkg/esxi/virtualMacineUtils.go index 1f4ae11..adb087f 100644 --- a/provider/pkg/esxi/virtualMacineUtils.go +++ b/provider/pkg/esxi/virtualMacineUtils.go @@ -661,7 +661,7 @@ func (esxi *Host) getVirtualMachinePowerState(id string) string { command := fmt.Sprintf("vim-cmd vmsvc/power.getstate %s", id) stdout, _ := esxi.Execute(command, "vmsvc/power.getstate") if strings.Contains(stdout, "Unable to find a VM corresponding") { - return "Unknown" + return esxiUnknown } switch { @@ -672,7 +672,7 @@ func (esxi *Host) getVirtualMachinePowerState(id string) string { case strings.Contains(stdout, "Suspended"): return vmTurnedSuspended default: - return "Unknown" + return esxiUnknown } } @@ -727,7 +727,7 @@ func (vm *VirtualMachine) toMap(keepId ...bool) map[string]interface{} { delete(outputs, "ovfProperties") delete(outputs, "ovfPropertiesTimer") - if vm.BootDiskType == "Unknown" || len(vm.BootDiskType) == 0 { + if vm.BootDiskType == esxiUnknown || len(vm.BootDiskType) == 0 { delete(outputs, "bootDiskType") } diff --git a/provider/pkg/esxi/virtualSwitch.go b/provider/pkg/esxi/virtualSwitch.go index cb77516..62add65 100644 --- a/provider/pkg/esxi/virtualSwitch.go +++ b/provider/pkg/esxi/virtualSwitch.go @@ -11,12 +11,7 @@ import ( ) func VirtualSwitchCreate(inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) { - var vs VirtualSwitch - if parsed, err := parseVirtualSwitch("", inputs); err == nil { - vs = parsed - } else { - return "", nil, err - } + vs := parseVirtualSwitch("", inputs) // Create vswitch command := fmt.Sprintf("esxcli network vswitch standard add -P %d -v \"%s\"", vs.Ports, vs.Name) @@ -48,12 +43,7 @@ func VirtualSwitchCreate(inputs resource.PropertyMap, esxi *Host) (string, resou } func VirtualSwitchUpdate(id string, inputs resource.PropertyMap, esxi *Host) (string, resource.PropertyMap, error) { - var vs VirtualSwitch - if parsed, err := parseVirtualSwitch(id, inputs); err == nil { - vs = parsed - } else { - return "", nil, err - } + vs := parseVirtualSwitch(id, inputs) err := esxi.updateVirtualSwitch(vs) if err != nil { @@ -78,7 +68,7 @@ func VirtualSwitchRead(id string, _ resource.PropertyMap, esxi *Host) (string, r return esxi.readVirtualSwitch(id) } -func parseVirtualSwitch(id string, inputs resource.PropertyMap) (VirtualSwitch, error) { +func parseVirtualSwitch(id string, inputs resource.PropertyMap) VirtualSwitch { vs := VirtualSwitch{} if len(id) > 0 { @@ -132,7 +122,7 @@ func parseVirtualSwitch(id string, inputs resource.PropertyMap) (VirtualSwitch, vs.Uplinks = make([]Uplink, 0) } - return vs, nil + return vs } func (esxi *Host) readVirtualSwitch(name string) (string, resource.PropertyMap, error) { @@ -146,7 +136,6 @@ func (esxi *Host) readVirtualSwitch(name string) (string, resource.PropertyMap, } func (esxi *Host) updateVirtualSwitch(vs VirtualSwitch) error { - var foundUplinks []string var command, stdout string var err error @@ -178,8 +167,9 @@ func (esxi *Host) updateVirtualSwitch(vs VirtualSwitch) error { re := regexp.MustCompile(`Uplinks: (.*)`) foundUplinksRaw := strings.Fields(re.FindStringSubmatch(stdout)[1]) + foundUplinks := make([]string, 0, len(foundUplinksRaw)) for _, s := range foundUplinksRaw { - foundUplinks = append(foundUplinks, strings.Replace(s, ",", "", -1)) + foundUplinks = append(foundUplinks, strings.ReplaceAll(s, ",", "")) } // Add uplink if needed @@ -231,32 +221,36 @@ func (esxi *Host) getVirtualSwitch(name string) (VirtualSwitch, error) { return VirtualSwitch{}, fmt.Errorf(stdout) } - re, _ := regexp.Compile(`Configured Ports: ([0-9]*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.Ports, _ = strconv.Atoi(re.FindStringSubmatch(stdout)[1]) + re := regexp.MustCompile(`Configured Ports: ([0-9]*)`) + matches := re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.Ports, _ = strconv.Atoi(matches[1]) } else { vs.Ports = 128 } - re, _ = regexp.Compile(`MTU: ([0-9]*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.Mtu, _ = strconv.Atoi(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(`MTU: ([0-9]*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.Mtu, _ = strconv.Atoi(matches[1]) } else { vs.Mtu = 1500 } - re, _ = regexp.Compile(`CDP Status: ([a-z]*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.LinkDiscoveryMode = re.FindStringSubmatch(stdout)[1] + re = regexp.MustCompile(`CDP Status: ([a-z]*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.LinkDiscoveryMode = matches[1] } else { vs.LinkDiscoveryMode = "listen" } - re, _ = regexp.Compile(`Uplinks: (.*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - foundUplinks := strings.Fields(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(`Uplinks: (.*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + foundUplinks := strings.Fields(matches[1]) for _, s := range foundUplinks { - vs.Uplinks = append(vs.Uplinks, Uplink{Name: strings.Replace(s, ",", "", -1)}) + vs.Uplinks = append(vs.Uplinks, Uplink{Name: strings.ReplaceAll(s, ",", "")}) } } else { vs.Uplinks = vs.Uplinks[:0] @@ -270,23 +264,26 @@ func (esxi *Host) getVirtualSwitch(name string) (VirtualSwitch, error) { return VirtualSwitch{}, fmt.Errorf(stdout) } - re, _ = regexp.Compile(`Allow Promiscuous: (.*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.PromiscuousMode, _ = strconv.ParseBool(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(`Allow Promiscuous: (.*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.PromiscuousMode, _ = strconv.ParseBool(matches[1]) } else { vs.PromiscuousMode = false } - re, _ = regexp.Compile(`Allow MAC Address Change: (.*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.MacChanges, _ = strconv.ParseBool(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(`Allow MAC Address Change: (.*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.MacChanges, _ = strconv.ParseBool(matches[1]) } else { vs.MacChanges = false } - re, _ = regexp.Compile(`Allow Forged Transmits: (.*)`) - if len(re.FindStringSubmatch(stdout)) > 0 { - vs.ForgedTransmits, _ = strconv.ParseBool(re.FindStringSubmatch(stdout)[1]) + re = regexp.MustCompile(`Allow Forged Transmits: (.*)`) + matches = re.FindStringSubmatch(stdout) + if len(matches) > 0 { + vs.ForgedTransmits, _ = strconv.ParseBool(matches[1]) } else { vs.ForgedTransmits = false }