Unverified Commit 88258431 authored by k8s-ci-robot's avatar k8s-ci-robot Committed by GitHub

Merge pull request #70323 from qingsenLi/git181010

[kubeadm/app/..add other packages]Switch to github.com/pkg/errors
parents e0ce8478 e94dd19e
...@@ -15,6 +15,7 @@ go_library( ...@@ -15,6 +15,7 @@ go_library(
"//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
"//staging/src/k8s.io/cluster-bootstrap/token/api:go_default_library", "//staging/src/k8s.io/cluster-bootstrap/token/api:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
], ],
) )
...@@ -35,5 +36,8 @@ go_test( ...@@ -35,5 +36,8 @@ go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["constants_test.go"], srcs = ["constants_test.go"],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = ["//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library"], deps = [
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
) )
...@@ -25,6 +25,8 @@ import ( ...@@ -25,6 +25,8 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/pkg/errors"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/version"
bootstrapapi "k8s.io/cluster-bootstrap/token/api" bootstrapapi "k8s.io/cluster-bootstrap/token/api"
...@@ -382,7 +384,7 @@ func EtcdSupportedVersion(versionString string) (*version.Version, error) { ...@@ -382,7 +384,7 @@ func EtcdSupportedVersion(versionString string) (*version.Version, error) {
} }
return etcdVersion, nil return etcdVersion, nil
} }
return nil, fmt.Errorf("Unsupported or unknown Kubernetes version(%v)", kubernetesVersion) return nil, errors.Errorf("Unsupported or unknown Kubernetes version(%v)", kubernetesVersion)
} }
// GetStaticPodDirectory returns the location on the disk where the Static Pod should be present // GetStaticPodDirectory returns the location on the disk where the Static Pod should be present
...@@ -420,12 +422,12 @@ func CreateTempDirForKubeadm(dirName string) (string, error) { ...@@ -420,12 +422,12 @@ func CreateTempDirForKubeadm(dirName string) (string, error) {
tempDir := path.Join(KubernetesDir, TempDirForKubeadm) tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
// creates target folder if not already exists // creates target folder if not already exists
if err := os.MkdirAll(tempDir, 0700); err != nil { if err := os.MkdirAll(tempDir, 0700); err != nil {
return "", fmt.Errorf("failed to create directory %q: %v", tempDir, err) return "", errors.Wrapf(err, "failed to create directory %q", tempDir)
} }
tempDir, err := ioutil.TempDir(tempDir, dirName) tempDir, err := ioutil.TempDir(tempDir, dirName)
if err != nil { if err != nil {
return "", fmt.Errorf("couldn't create a temporary directory: %v", err) return "", errors.Wrap(err, "couldn't create a temporary directory")
} }
return tempDir, nil return tempDir, nil
} }
...@@ -435,13 +437,13 @@ func CreateTimestampDirForKubeadm(dirName string) (string, error) { ...@@ -435,13 +437,13 @@ func CreateTimestampDirForKubeadm(dirName string) (string, error) {
tempDir := path.Join(KubernetesDir, TempDirForKubeadm) tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
// creates target folder if not already exists // creates target folder if not already exists
if err := os.MkdirAll(tempDir, 0700); err != nil { if err := os.MkdirAll(tempDir, 0700); err != nil {
return "", fmt.Errorf("failed to create directory %q: %v", tempDir, err) return "", errors.Wrapf(err, "failed to create directory %q", tempDir)
} }
timestampDirName := fmt.Sprintf("%s-%s", dirName, time.Now().Format("2006-01-02-15-04-05")) timestampDirName := fmt.Sprintf("%s-%s", dirName, time.Now().Format("2006-01-02-15-04-05"))
timestampDir := path.Join(tempDir, timestampDirName) timestampDir := path.Join(tempDir, timestampDirName)
if err := os.Mkdir(timestampDir, 0700); err != nil { if err := os.Mkdir(timestampDir, 0700); err != nil {
return "", fmt.Errorf("could not create timestamp directory: %v", err) return "", errors.Wrap(err, "could not create timestamp directory")
} }
return timestampDir, nil return timestampDir, nil
...@@ -452,13 +454,13 @@ func GetDNSIP(svcSubnet string) (net.IP, error) { ...@@ -452,13 +454,13 @@ func GetDNSIP(svcSubnet string) (net.IP, error) {
// Get the service subnet CIDR // Get the service subnet CIDR
_, svcSubnetCIDR, err := net.ParseCIDR(svcSubnet) _, svcSubnetCIDR, err := net.ParseCIDR(svcSubnet)
if err != nil { if err != nil {
return nil, fmt.Errorf("couldn't parse service subnet CIDR %q: %v", svcSubnet, err) return nil, errors.Wrapf(err, "couldn't parse service subnet CIDR %q", svcSubnet)
} }
// Selects the 10th IP in service subnet CIDR range as dnsIP // Selects the 10th IP in service subnet CIDR range as dnsIP
dnsIP, err := ipallocator.GetIndexedIP(svcSubnetCIDR, 10) dnsIP, err := ipallocator.GetIndexedIP(svcSubnetCIDR, 10)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to get tenth IP address from service subnet CIDR %s: %v", svcSubnetCIDR.String(), err) return nil, errors.Wrapf(err, "unable to get tenth IP address from service subnet CIDR %s", svcSubnetCIDR.String())
} }
return dnsIP, nil return dnsIP, nil
......
...@@ -17,11 +17,12 @@ limitations under the License. ...@@ -17,11 +17,12 @@ limitations under the License.
package constants package constants
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/version"
) )
...@@ -151,7 +152,7 @@ func TestEtcdSupportedVersion(t *testing.T) { ...@@ -151,7 +152,7 @@ func TestEtcdSupportedVersion(t *testing.T) {
{ {
kubernetesVersion: "1.99.0", kubernetesVersion: "1.99.0",
expectedVersion: nil, expectedVersion: nil,
expectedError: fmt.Errorf("Unsupported or unknown Kubernetes version(1.99.0)"), expectedError: errors.New("Unsupported or unknown Kubernetes version(1.99.0)"),
}, },
{ {
kubernetesVersion: "1.10.0", kubernetesVersion: "1.10.0",
......
...@@ -13,6 +13,7 @@ go_library( ...@@ -13,6 +13,7 @@ go_library(
deps = [ deps = [
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library", "//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
], ],
) )
......
...@@ -22,6 +22,8 @@ import ( ...@@ -22,6 +22,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/version"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
) )
...@@ -63,12 +65,12 @@ func ValidateVersion(allFeatures FeatureList, requestedFeatures map[string]bool, ...@@ -63,12 +65,12 @@ func ValidateVersion(allFeatures FeatureList, requestedFeatures map[string]bool,
} }
parsedExpVersion, err := version.ParseSemantic(requestedVersion) parsedExpVersion, err := version.ParseSemantic(requestedVersion)
if err != nil { if err != nil {
return fmt.Errorf("Error parsing version %s: %v", requestedVersion, err) return errors.Wrapf(err, "error parsing version %s", requestedVersion)
} }
for k := range requestedFeatures { for k := range requestedFeatures {
if minVersion := allFeatures[k].MinimumVersion; minVersion != nil { if minVersion := allFeatures[k].MinimumVersion; minVersion != nil {
if !parsedExpVersion.AtLeast(minVersion) { if !parsedExpVersion.AtLeast(minVersion) {
return fmt.Errorf( return errors.Errorf(
"the requested Kubernetes version (%s) is incompatible with the %s feature gate, which needs %s as a minimum", "the requested Kubernetes version (%s) is incompatible with the %s feature gate, which needs %s as a minimum",
requestedVersion, k, minVersion) requestedVersion, k, minVersion)
} }
...@@ -134,7 +136,7 @@ func NewFeatureGate(f *FeatureList, value string) (map[string]bool, error) { ...@@ -134,7 +136,7 @@ func NewFeatureGate(f *FeatureList, value string) (map[string]bool, error) {
arr := strings.SplitN(s, "=", 2) arr := strings.SplitN(s, "=", 2)
if len(arr) != 2 { if len(arr) != 2 {
return nil, fmt.Errorf("missing bool value for feature-gate key:%s", s) return nil, errors.Errorf("missing bool value for feature-gate key:%s", s)
} }
k := strings.TrimSpace(arr[0]) k := strings.TrimSpace(arr[0])
...@@ -142,16 +144,16 @@ func NewFeatureGate(f *FeatureList, value string) (map[string]bool, error) { ...@@ -142,16 +144,16 @@ func NewFeatureGate(f *FeatureList, value string) (map[string]bool, error) {
featureSpec, ok := (*f)[k] featureSpec, ok := (*f)[k]
if !ok { if !ok {
return nil, fmt.Errorf("unrecognized feature-gate key: %s", k) return nil, errors.Errorf("unrecognized feature-gate key: %s", k)
} }
if featureSpec.PreRelease == utilfeature.Deprecated { if featureSpec.PreRelease == utilfeature.Deprecated {
return nil, fmt.Errorf("feature-gate key is deprecated: %s", k) return nil, errors.Errorf("feature-gate key is deprecated: %s", k)
} }
boolValue, err := strconv.ParseBool(v) boolValue, err := strconv.ParseBool(v)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid value %v for feature-gate key: %s, use true|false instead", v, k) return nil, errors.Errorf("invalid value %v for feature-gate key: %s, use true|false instead", v, k)
} }
featureGate[k] = boolValue featureGate[k] = boolValue
} }
......
...@@ -31,6 +31,7 @@ go_library( ...@@ -31,6 +31,7 @@ go_library(
"//vendor/github.com/PuerkitoBio/purell:go_default_library", "//vendor/github.com/PuerkitoBio/purell:go_default_library",
"//vendor/github.com/blang/semver:go_default_library", "//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/k8s.io/utils/exec:go_default_library", "//vendor/k8s.io/utils/exec:go_default_library",
], ],
) )
...@@ -47,6 +48,7 @@ go_test( ...@@ -47,6 +48,7 @@ go_test(
"//cmd/kubeadm/app/apis/kubeadm/v1beta1:go_default_library", "//cmd/kubeadm/app/apis/kubeadm/v1beta1:go_default_library",
"//cmd/kubeadm/app/util/runtime:go_default_library", "//cmd/kubeadm/app/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/github.com/renstrom/dedent:go_default_library", "//vendor/github.com/renstrom/dedent:go_default_library",
"//vendor/k8s.io/utils/exec:go_default_library", "//vendor/k8s.io/utils/exec:go_default_library",
"//vendor/k8s.io/utils/exec/testing:go_default_library", "//vendor/k8s.io/utils/exec/testing:go_default_library",
......
...@@ -18,11 +18,11 @@ package preflight ...@@ -18,11 +18,11 @@ package preflight
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
"github.com/pkg/errors"
"github.com/renstrom/dedent" "github.com/renstrom/dedent"
"net/http" "net/http"
...@@ -173,12 +173,12 @@ func (pfct preflightCheckTest) Name() string { ...@@ -173,12 +173,12 @@ func (pfct preflightCheckTest) Name() string {
return "preflightCheckTest" return "preflightCheckTest"
} }
func (pfct preflightCheckTest) Check() (warning, errors []error) { func (pfct preflightCheckTest) Check() (warning, errorList []error) {
if pfct.msg == "warning" { if pfct.msg == "warning" {
return []error{fmt.Errorf("warning")}, nil return []error{errors.New("warning")}, nil
} }
if pfct.msg != "" { if pfct.msg != "" {
return nil, []error{fmt.Errorf("fake error")} return nil, []error{errors.New("fake error")}
} }
return return
} }
......
...@@ -19,16 +19,17 @@ limitations under the License. ...@@ -19,16 +19,17 @@ limitations under the License.
package preflight package preflight
import ( import (
"fmt"
"os" "os"
"github.com/pkg/errors"
) )
// Check validates if an user has elevated (root) privileges. // Check validates if an user has elevated (root) privileges.
func (ipuc IsPrivilegedUserCheck) Check() (warnings, errors []error) { func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) {
errors = []error{} errorList = []error{}
if os.Getuid() != 0 { if os.Getuid() != 0 {
errors = append(errors, fmt.Errorf("user is not running as root")) errorList = append(errorList, errors.New("user is not running as root"))
} }
return nil, errors return nil, errorList
} }
...@@ -19,14 +19,15 @@ limitations under the License. ...@@ -19,14 +19,15 @@ limitations under the License.
package preflight package preflight
import ( import (
"fmt"
"os/exec" "os/exec"
"strings" "strings"
"github.com/pkg/errors"
) )
// Check validates if an user has elevated (administrator) privileges. // Check validates if an user has elevated (administrator) privileges.
func (ipuc IsPrivilegedUserCheck) Check() (warnings, errors []error) { func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) {
errors = []error{} errorList = []error{}
// The "Well-known SID" of Administrator group is S-1-5-32-544 // The "Well-known SID" of Administrator group is S-1-5-32-544
// The following powershell will return "True" if run as an administrator, "False" otherwise // The following powershell will return "True" if run as an administrator, "False" otherwise
...@@ -35,10 +36,10 @@ func (ipuc IsPrivilegedUserCheck) Check() (warnings, errors []error) { ...@@ -35,10 +36,10 @@ func (ipuc IsPrivilegedUserCheck) Check() (warnings, errors []error) {
isAdmin, err := exec.Command("powershell", args...).Output() isAdmin, err := exec.Command("powershell", args...).Output()
if err != nil { if err != nil {
errors = append(errors, fmt.Errorf("unable to determine if user is running as administrator: %s", err)) errorList = append(errorList, errors.Wrap(err, "unable to determine if user is running as administrator"))
} else if strings.EqualFold(strings.TrimSpace(string(isAdmin)), "false") { } else if strings.EqualFold(strings.TrimSpace(string(isAdmin)), "false") {
errors = append(errors, fmt.Errorf("user is not running as administrator")) errorList = append(errorList, errors.New("user is not running as administrator"))
} }
return nil, errors return nil, errorList
} }
...@@ -17,10 +17,11 @@ limitations under the License. ...@@ -17,10 +17,11 @@ limitations under the License.
package preflight package preflight
import ( import (
"fmt"
"regexp" "regexp"
"strings" "strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/version"
utilsexec "k8s.io/utils/exec" utilsexec "k8s.io/utils/exec"
) )
...@@ -38,7 +39,7 @@ func GetKubeletVersion(execer utilsexec.Interface) (*version.Version, error) { ...@@ -38,7 +39,7 @@ func GetKubeletVersion(execer utilsexec.Interface) (*version.Version, error) {
cleanOutput := strings.TrimSpace(string(out)) cleanOutput := strings.TrimSpace(string(out))
subs := kubeletVersionRegex.FindAllStringSubmatch(cleanOutput, -1) subs := kubeletVersionRegex.FindAllStringSubmatch(cleanOutput, -1)
if len(subs) != 1 || len(subs[0]) < 2 { if len(subs) != 1 || len(subs[0]) < 2 {
return nil, fmt.Errorf("Unable to parse output from Kubelet: %q", cleanOutput) return nil, errors.Errorf("Unable to parse output from Kubelet: %q", cleanOutput)
} }
return version.ParseSemantic(subs[0][1]) return version.ParseSemantic(subs[0][1])
} }
...@@ -17,9 +17,10 @@ limitations under the License. ...@@ -17,9 +17,10 @@ limitations under the License.
package preflight package preflight
import ( import (
"fmt"
"testing" "testing"
"github.com/pkg/errors"
utilsexec "k8s.io/utils/exec" utilsexec "k8s.io/utils/exec"
fakeexec "k8s.io/utils/exec/testing" fakeexec "k8s.io/utils/exec/testing"
) )
...@@ -34,7 +35,7 @@ func TestGetKubeletVersion(t *testing.T) { ...@@ -34,7 +35,7 @@ func TestGetKubeletVersion(t *testing.T) {
{"Kubernetes v1.7.0", "1.7.0", nil, true}, {"Kubernetes v1.7.0", "1.7.0", nil, true},
{"Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true}, {"Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true},
{"something-invalid", "", nil, false}, {"something-invalid", "", nil, false},
{"command not found", "", fmt.Errorf("kubelet not found"), false}, {"command not found", "", errors.New("kubelet not found"), false},
{"", "", nil, false}, {"", "", nil, false},
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment