diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index b106a729a8a..3adbb99bf65 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -134,11 +134,8 @@ func (sc ServiceCheck) Check() (warnings, errorList []error) { return []error{err}, nil } - warnings = []error{} - if !initSystem.ServiceExists(sc.Service) { - warnings = append(warnings, errors.Errorf("%s service does not exist", sc.Service)) - return warnings, nil + return []error{errors.Errorf("%s service does not exist", sc.Service)}, nil } if !initSystem.ServiceIsEnabled(sc.Service) { @@ -175,19 +172,17 @@ func (fc FirewalldCheck) Check() (warnings, errorList []error) { return []error{err}, nil } - warnings = []error{} - if !initSystem.ServiceExists("firewalld") { return nil, nil } if initSystem.ServiceIsActive("firewalld") { - warnings = append(warnings, - errors.Errorf("firewalld is active, please ensure ports %v are open or your cluster may not function correctly", - fc.ports)) + err := errors.Errorf("firewalld is active, please ensure ports %v are open or your cluster may not function correctly", + fc.ports) + return []error{err}, nil } - return warnings, errorList + return nil, nil } // PortOpenCheck ensures the given port is available for use. @@ -207,10 +202,10 @@ func (poc PortOpenCheck) Name() string { // Check validates if the particular port is available. func (poc PortOpenCheck) Check() (warnings, errorList []error) { klog.V(1).Infof("validating availability of port %d", poc.port) - errorList = []error{} + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", poc.port)) if err != nil { - errorList = append(errorList, errors.Errorf("Port %d is in use", poc.port)) + errorList = []error{errors.Errorf("Port %d is in use", poc.port)} } if ln != nil { ln.Close() @@ -252,7 +247,7 @@ func (dac DirAvailableCheck) Name() string { // Check validates if a directory does not exist or empty. func (dac DirAvailableCheck) Check() (warnings, errorList []error) { klog.V(1).Infof("validating the existence and emptiness of directory %s", dac.Path) - errorList = []error{} + // If it doesn't exist we are good: if _, err := os.Stat(dac.Path); os.IsNotExist(err) { return nil, nil @@ -260,17 +255,16 @@ func (dac DirAvailableCheck) Check() (warnings, errorList []error) { f, err := os.Open(dac.Path) if err != nil { - errorList = append(errorList, errors.Wrapf(err, "unable to check if %s is empty", dac.Path)) - return nil, errorList + return nil, []error{errors.Wrapf(err, "unable to check if %s is empty", dac.Path)} } defer f.Close() _, err = f.Readdirnames(1) if err != io.EOF { - errorList = append(errorList, errors.Errorf("%s is not empty", dac.Path)) + return nil, []error{errors.Errorf("%s is not empty", dac.Path)} } - return nil, errorList + return nil, nil } // FileAvailableCheck checks that the given file does not already exist. @@ -290,11 +284,11 @@ func (fac FileAvailableCheck) Name() string { // Check validates if the given file does not already exist. func (fac FileAvailableCheck) Check() (warnings, errorList []error) { klog.V(1).Infof("validating the existence of file %s", fac.Path) - errorList = []error{} + if _, err := os.Stat(fac.Path); err == nil { - errorList = append(errorList, errors.Errorf("%s already exists", fac.Path)) + return nil, []error{errors.Errorf("%s already exists", fac.Path)} } - return nil, errorList + return nil, nil } // FileExistingCheck checks that the given file does not already exist. @@ -314,11 +308,11 @@ func (fac FileExistingCheck) Name() string { // Check validates if the given file already exists. func (fac FileExistingCheck) Check() (warnings, errorList []error) { klog.V(1).Infof("validating the existence of file %s", fac.Path) - errorList = []error{} + if _, err := os.Stat(fac.Path); err != nil { - errorList = append(errorList, errors.Errorf("%s doesn't exist", fac.Path)) + return nil, []error{errors.Errorf("%s doesn't exist", fac.Path)} } - return nil, errorList + return nil, nil } // FileContentCheck checks that the given file contains the string Content. @@ -410,8 +404,7 @@ func (HostnameCheck) Name() string { // Check validates if hostname match dns sub domain regex. func (hc HostnameCheck) Check() (warnings, errorList []error) { klog.V(1).Infoln("checking whether the given node name is reachable using net.LookupHost") - errorList = []error{} - warnings = []error{} + addr, err := net.LookupHost(hc.nodeName) if addr == nil { warnings = append(warnings, errors.Errorf("hostname \"%s\" could not be reached", hc.nodeName)) diff --git a/cmd/kubeadm/app/preflight/checks_linux.go b/cmd/kubeadm/app/preflight/checks_linux.go index 9ae7147e9c5..d2e83f25604 100644 --- a/cmd/kubeadm/app/preflight/checks_linux.go +++ b/cmd/kubeadm/app/preflight/checks_linux.go @@ -29,11 +29,9 @@ import ( // Check validates if Docker is setup to use systemd as the cgroup driver. func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) { - warnings = []error{} driver, err := util.GetCgroupDriverDocker(exec.New()) if err != nil { - errorList = append(errorList, err) - return nil, errorList + return nil, []error{err} } if driver != util.CgroupDriverSystemd { err = errors.Errorf("detected %q as the Docker cgroup driver. "+ @@ -41,9 +39,9 @@ func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) { "Please follow the guide at https://kubernetes.io/docs/setup/cri/", driver, util.CgroupDriverSystemd) - warnings = append(warnings, err) + return []error{err}, nil } - return warnings, nil + return nil, nil } // Check determines if IPVS proxier can be used or not @@ -51,7 +49,7 @@ func (ipvspc IPVSProxierCheck) Check() (warnings, errors []error) { ipsetInterface := utilipset.New(ipvspc.exec) kernelHandler := ipvs.NewLinuxKernelHandler() if _, err := ipvs.CanUseIPVSProxier(kernelHandler, ipsetInterface); err != nil { - return nil, append(errors, err) + return nil, []error{err} } return nil, nil } diff --git a/cmd/kubeadm/app/preflight/checks_unix.go b/cmd/kubeadm/app/preflight/checks_unix.go index b04edaf12ce..2bbbd9ca2d3 100644 --- a/cmd/kubeadm/app/preflight/checks_unix.go +++ b/cmd/kubeadm/app/preflight/checks_unix.go @@ -26,10 +26,9 @@ import ( // Check validates if an user has elevated (root) privileges. func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) { - errorList = []error{} if os.Getuid() != 0 { - errorList = append(errorList, errors.New("user is not running as root")) + return nil, []error{errors.New("user is not running as root")} } - return nil, errorList + return nil, nil } diff --git a/cmd/kubeadm/app/preflight/checks_windows.go b/cmd/kubeadm/app/preflight/checks_windows.go index 58ebcaa2a47..05add8cfa41 100644 --- a/cmd/kubeadm/app/preflight/checks_windows.go +++ b/cmd/kubeadm/app/preflight/checks_windows.go @@ -30,28 +30,23 @@ const administratorSID = "S-1-5-32-544" // Check validates if a user has elevated (administrator) privileges. func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) { - errorList = []error{} - currUser, err := user.Current() if err != nil { - errorList = append(errorList, errors.New("cannot get current user")) - return nil, errorList + return nil, []error{errors.New("cannot get current user")} } groupIds, err := currUser.GroupIds() if err != nil { - errorList = append(errorList, errors.New("cannot get group IDs for current user")) - return nil, errorList + return nil, []error{errors.New("cannot get group IDs for current user")} } for _, sid := range groupIds { if sid == administratorSID { - return nil, errorList + return nil, nil } } - errorList = append(errorList, errors.New("user is not running as administrator")) - return nil, errorList + return nil, []error{errors.New("user is not running as administrator")} } // Check validates if Docker is setup to use systemd as the cgroup driver.