kubeadm: prefer to use pkg/errors package and cleanup fmt.Errorf

This commit is contained in:
SataQiu 2022-07-26 16:44:59 +08:00
parent 5f6b60bb13
commit 6cab9800a7
9 changed files with 37 additions and 29 deletions

View File

@ -17,11 +17,12 @@ limitations under the License.
package phases
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
"k8s.io/klog/v2"
utilsexec "k8s.io/utils/exec"

View File

@ -17,9 +17,10 @@ limitations under the License.
package phases
import (
"errors"
"fmt"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"

View File

@ -17,10 +17,11 @@ limitations under the License.
package phases
import (
"errors"
"fmt"
"path/filepath"
"github.com/pkg/errors"
"k8s.io/klog/v2"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"

View File

@ -316,7 +316,7 @@ func phaseBuilder6(name string, args cobra.PositionalArgs, phases ...Phase) Phas
func customArgs(cmd *cobra.Command, args []string) error {
for _, a := range args {
if a != "qux" {
return fmt.Errorf("arg %s does not equal qux", a)
return errors.Errorf("arg %s does not equal qux", a)
}
}
return nil

View File

@ -573,7 +573,7 @@ func EtcdSupportedVersion(supportedEtcdVersion map[uint8]string, versionString s
if desiredVersion > max {
etcdStringVersion = supportedEtcdVersion[max]
}
warning = fmt.Errorf("could not find officially supported version of etcd for Kubernetes %s, falling back to the nearest etcd version (%s)",
warning = errors.Errorf("could not find officially supported version of etcd for Kubernetes %s, falling back to the nearest etcd version (%s)",
versionString, etcdStringVersion)
}

View File

@ -23,6 +23,8 @@ import (
"fmt"
"os/exec"
"strings"
"github.com/pkg/errors"
)
// OpenRCInitSystem defines openrc
@ -87,7 +89,7 @@ func (sysd SystemdInitSystem) EnableCommand(service string) string {
// reloadSystemd reloads the systemd daemon
func (sysd SystemdInitSystem) reloadSystemd() error {
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
return fmt.Errorf("failed to reload systemd: %v", err)
return errors.Wrap(err, "failed to reload systemd")
}
return nil
}
@ -161,5 +163,5 @@ func GetInitSystem() (InitSystem, error) {
return &OpenRCInitSystem{}, nil
}
return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
return nil, errors.New("no supported init system detected, skipping checking for services")
}

View File

@ -23,6 +23,7 @@ import (
"fmt"
"time"
"github.com/pkg/errors"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
@ -46,14 +47,14 @@ func (sysd WindowsInitSystem) ServiceStart(service string) error {
s, err := m.OpenService(service)
if err != nil {
return fmt.Errorf("could not access service %s: %v", service, err)
return errors.Wrapf(err, "could not access service %s", service)
}
defer s.Close()
// Check if service is already started
status, err := s.Query()
if err != nil {
return fmt.Errorf("could not query service %s: %v", service, err)
return errors.Wrapf(err, "could not query service %s", service)
}
if status.State != svc.Stopped && status.State != svc.StopPending {
@ -63,35 +64,35 @@ func (sysd WindowsInitSystem) ServiceStart(service string) error {
timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if timeout.Before(time.Now()) {
return fmt.Errorf("timeout waiting for %s service to stop", service)
return errors.Errorf("timeout waiting for %s service to stop", service)
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not retrieve %s service status: %v", service, err)
return errors.Wrapf(err, "could not retrieve %s service status", service)
}
}
// Start the service
err = s.Start("is", "manual-started")
if err != nil {
return fmt.Errorf("could not start service %s: %v", service, err)
return errors.Wrapf(err, "could not start service %s", service)
}
// Check that the start was successful
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not query service %s: %v", service, err)
return errors.Wrapf(err, "could not query service %s", service)
}
timeout = time.Now().Add(10 * time.Second)
for status.State != svc.Running {
if timeout.Before(time.Now()) {
return fmt.Errorf("timeout waiting for %s service to start", service)
return errors.Errorf("timeout waiting for %s service to start", service)
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not retrieve %s service status: %v", service, err)
return errors.Wrapf(err, "could not retrieve %s service status", service)
}
}
return nil
@ -100,10 +101,10 @@ func (sysd WindowsInitSystem) ServiceStart(service string) error {
// ServiceRestart tries to reload the environment and restart the specific service
func (sysd WindowsInitSystem) ServiceRestart(service string) error {
if err := sysd.ServiceStop(service); err != nil {
return fmt.Errorf("couldn't stop service %s: %v", service, err)
return errors.Wrapf(err, "couldn't stop service %s", service)
}
if err := sysd.ServiceStart(service); err != nil {
return fmt.Errorf("couldn't start service %s: %v", service, err)
return errors.Wrapf(err, "couldn't start service %s", service)
}
return nil
@ -120,14 +121,14 @@ func (sysd WindowsInitSystem) ServiceStop(service string) error {
s, err := m.OpenService(service)
if err != nil {
return fmt.Errorf("could not access service %s: %v", service, err)
return errors.Wrapf(err, "could not access service %s", service)
}
defer s.Close()
// Check if service is already stopped
status, err := s.Query()
if err != nil {
return fmt.Errorf("could not query service %s: %v", service, err)
return errors.Wrapf(err, "could not query service %s", service)
}
if status.State == svc.Stopped {
@ -139,12 +140,12 @@ func (sysd WindowsInitSystem) ServiceStop(service string) error {
timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if timeout.Before(time.Now()) {
return fmt.Errorf("timeout waiting for %s service to stop", service)
return errors.Errorf("timeout waiting for %s service to stop", service)
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not retrieve %s service status: %v", service, err)
return errors.Wrapf(err, "could not retrieve %s service status", service)
}
}
return nil
@ -153,23 +154,23 @@ func (sysd WindowsInitSystem) ServiceStop(service string) error {
// Stop the service
status, err = s.Control(svc.Stop)
if err != nil {
return fmt.Errorf("could not stop service %s: %v", service, err)
return errors.Wrapf(err, "could not stop service %s", service)
}
// Check that the stop was successful
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not query service %s: %v", service, err)
return errors.Wrapf(err, "could not query service %s", service)
}
timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if timeout.Before(time.Now()) {
return fmt.Errorf("timeout waiting for %s service to stop", service)
return errors.Errorf("timeout waiting for %s service to stop", service)
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not retrieve %s service status: %v", service, err)
return errors.Wrapf(err, "could not retrieve %s service status", service)
}
}
return nil
@ -239,7 +240,7 @@ func (sysd WindowsInitSystem) ServiceIsActive(service string) bool {
func GetInitSystem() (InitSystem, error) {
m, err := mgr.Connect()
if err != nil {
return nil, fmt.Errorf("no supported init system detected: %v", err)
return nil, errors.Wrap(err, "no supported init system detected")
}
defer m.Disconnect()
return &WindowsInitSystem{}, nil

View File

@ -17,10 +17,11 @@ limitations under the License.
package util
import (
"errors"
"os"
"strings"
"testing"
"github.com/pkg/errors"
)
func TestGetHostname(t *testing.T) {

View File

@ -17,13 +17,14 @@ limitations under the License.
package util
import (
"errors"
"fmt"
"path"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
)
@ -480,5 +481,5 @@ func TestValidateStableVersion(t *testing.T) {
}
func errorFetcher(url string, timeout time.Duration) (string, error) {
return "should not make internet calls", fmt.Errorf("should not make internet calls, tried to request url: %s", url)
return "should not make internet calls", errors.Errorf("should not make internet calls, tried to request url: %s", url)
}