Use errdefs package

This commit is contained in:
Brian Goff
2019-05-20 14:11:13 -07:00
parent b9711abff3
commit 02623170cc
24 changed files with 93 additions and 72 deletions

View File

@@ -18,8 +18,7 @@ import (
"context" "context"
"strings" "strings"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/providers" "github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/version" "github.com/virtual-kubelet/virtual-kubelet/version"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@@ -100,7 +99,7 @@ func getTaint(c Opts) (*corev1.Taint, error) {
case "PreferNoSchedule": case "PreferNoSchedule":
effect = corev1.TaintEffectPreferNoSchedule effect = corev1.TaintEffectPreferNoSchedule
default: default:
return nil, strongerrors.InvalidArgument(errors.Errorf("taint effect %q is not supported", effectEnv)) return nil, errdefs.InvalidInputf("taint effect %q is not supported", effectEnv)
} }
return &corev1.Taint{ return &corev1.Taint{

View File

@@ -20,9 +20,9 @@ import (
"path" "path"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers" "github.com/virtual-kubelet/virtual-kubelet/providers"
@@ -64,11 +64,11 @@ func runRootCommand(ctx context.Context, c Opts) error {
defer cancel() defer cancel()
if ok := providers.ValidOperatingSystems[c.OperatingSystem]; !ok { if ok := providers.ValidOperatingSystems[c.OperatingSystem]; !ok {
return strongerrors.InvalidArgument(errors.Errorf("operating system %q is not supported", c.OperatingSystem)) return errdefs.InvalidInputf("operating system %q is not supported", c.OperatingSystem)
} }
if c.PodSyncWorkers == 0 { if c.PodSyncWorkers == 0 {
return strongerrors.InvalidArgument(errors.New("pod sync workers must be greater than 0")) return errdefs.InvalidInput("pod sync workers must be greater than 0")
} }
var taint *corev1.Taint var taint *corev1.Taint

View File

@@ -22,8 +22,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/trace/opencensus" "github.com/virtual-kubelet/virtual-kubelet/trace/opencensus"
octrace "go.opencensus.io/trace" octrace "go.opencensus.io/trace"
@@ -41,7 +41,7 @@ var (
func setupTracing(ctx context.Context, c Opts) error { func setupTracing(ctx context.Context, c Opts) error {
for k := range c.TraceConfig.Tags { for k := range c.TraceConfig.Tags {
if reservedTagNames[k] { if reservedTagNames[k] {
return strongerrors.InvalidArgument(errors.Errorf("invalid trace tag %q, must not use a reserved tag key", k)) return errdefs.InvalidInputf("invalid trace tag %q, must not use a reserved tag key", k)
} }
} }
if c.TraceConfig.Tags == nil { if c.TraceConfig.Tags == nil {
@@ -72,10 +72,10 @@ func setupTracing(ctx context.Context, c Opts) error {
default: default:
rate, err := strconv.Atoi(c.TraceSampleRate) rate, err := strconv.Atoi(c.TraceSampleRate)
if err != nil { if err != nil {
return strongerrors.InvalidArgument(errors.Wrap(err, "unsupported trace sample rate")) return errdefs.AsInvalidInput(errors.Wrap(err, "unsupported trace sample rate"))
} }
if rate < 0 || rate > 100 { if rate < 0 || rate > 100 {
return strongerrors.InvalidArgument(errors.Wrap(err, "trace sample rate must be between 0 and 100")) return errdefs.AsInvalidInput(errors.Wrap(err, "trace sample rate must be between 0 and 100"))
} }
s = octrace.ProbabilitySampler(float64(rate) / 100) s = octrace.ProbabilitySampler(float64(rate) / 100)
} }

View File

@@ -17,7 +17,7 @@ import (
"time" "time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers/alibabacloud/eci" "github.com/virtual-kubelet/virtual-kubelet/providers/alibabacloud/eci"
@@ -254,7 +254,7 @@ func (p *ECIProvider) DeletePod(ctx context.Context, pod *v1.Pod) error {
} }
} }
if eciId == "" { if eciId == "" {
return strongerrors.NotFound(fmt.Errorf("DeletePod can't find Pod %s-%s", pod.Namespace, pod.Name)) return errdefs.NotFoundf("DeletePod can't find Pod %s-%s", pod.Namespace, pod.Name)
} }
request := eci.CreateDeleteContainerGroupRequest() request := eci.CreateDeleteContainerGroupRequest()

View File

@@ -4,7 +4,7 @@ import (
"net/http" "net/http"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
) )
func wrapError(err error) error { func wrapError(err error) error {
@@ -19,7 +19,7 @@ func wrapError(err error) error {
switch se.HttpStatus() { switch se.HttpStatus() {
case http.StatusNotFound: case http.StatusNotFound:
return strongerrors.NotFound(err) return errdefs.AsNotFound(err)
default: default:
return err return err
} }

View File

@@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/ecs" "github.com/aws/aws-sdk-go/service/ecs"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/vkubelet/api" "github.com/virtual-kubelet/virtual-kubelet/vkubelet/api"
k8sTypes "k8s.io/apimachinery/pkg/types" k8sTypes "k8s.io/apimachinery/pkg/types"
) )
@@ -276,7 +276,7 @@ func (c *Cluster) GetPod(namespace string, name string) (*Pod, error) {
tag := buildTaskDefinitionTag(c.name, namespace, name) tag := buildTaskDefinitionTag(c.name, namespace, name)
pod, ok := c.pods[tag] pod, ok := c.pods[tag]
if !ok { if !ok {
return nil, strongerrors.NotFound(fmt.Errorf("pod %s/%s is not found", namespace, name)) return nil, errdefs.NotFoundf("pod %s/%s is not found", namespace, name)
} }
return pod, nil return pod, nil

View File

@@ -19,11 +19,11 @@ import (
"sync" "sync"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
client "github.com/virtual-kubelet/azure-aci/client" client "github.com/virtual-kubelet/azure-aci/client"
"github.com/virtual-kubelet/azure-aci/client/aci" "github.com/virtual-kubelet/azure-aci/client/aci"
"github.com/virtual-kubelet/azure-aci/client/network" "github.com/virtual-kubelet/azure-aci/client/network"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/trace" "github.com/virtual-kubelet/virtual-kubelet/trace"
@@ -777,7 +777,7 @@ func (p *ACIProvider) GetContainerLogs(ctx context.Context, namespace, podName,
} }
if cg.Tags["NodeName"] != p.nodeName { if cg.Tags["NodeName"] != p.nodeName {
return nil, strongerrors.NotFound(errors.New("got unexpected pod node name")) return nil, errdefs.NotFound("got unexpected pod node name")
} }
// get logs from cg // get logs from cg

View File

@@ -3,8 +3,8 @@ package azure
import ( import (
"net/http" "net/http"
"github.com/cpuguy83/strongerrors"
"github.com/virtual-kubelet/azure-aci/client/api" "github.com/virtual-kubelet/azure-aci/client/api"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
) )
func wrapError(err error) error { func wrapError(err error) error {
@@ -19,7 +19,7 @@ func wrapError(err error) error {
switch e.StatusCode { switch e.StatusCode {
case http.StatusNotFound: case http.StatusNotFound:
return strongerrors.NotFound(err) return errdefs.AsNotFound(err)
default: default:
return err return err
} }

View File

@@ -5,7 +5,7 @@ import (
"github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
) )
func wrapError(err error) error { func wrapError(err error) error {
@@ -14,7 +14,7 @@ func wrapError(err error) error {
} }
switch { switch {
case isStatus(err, http.StatusNotFound): case isStatus(err, http.StatusNotFound):
return strongerrors.NotFound(err) return errdefs.AsNotFound(err)
default: default:
return err return err
} }

View File

@@ -16,8 +16,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/cpuguy83/strongerrors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers" "github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/vkubelet/api" "github.com/virtual-kubelet/virtual-kubelet/vkubelet/api"
@@ -569,7 +569,7 @@ func (p *CRIProvider) DeletePod(ctx context.Context, pod *v1.Pod) error {
ps, ok := p.podStatus[pod.UID] ps, ok := p.podStatus[pod.UID]
if !ok { if !ok {
return strongerrors.NotFound(fmt.Errorf("Pod %s not found", pod.UID)) return errdefs.NotFoundf("Pod %s not found", pod.UID)
} }
// TODO: Check pod status for running state // TODO: Check pod status for running state
@@ -601,7 +601,7 @@ func (p *CRIProvider) GetPod(ctx context.Context, namespace, name string) (*v1.P
pod := p.findPodByName(namespace, name) pod := p.findPodByName(namespace, name)
if pod == nil { if pod == nil {
return nil, strongerrors.NotFound(fmt.Errorf("Pod %s in namespace %s could not be found on the node", name, namespace)) return nil, errdefs.NotFoundf("Pod %s in namespace %s could not be found on the node", name, namespace)
} }
return createPodSpecFromCRI(pod, p.nodeName), nil return createPodSpecFromCRI(pod, p.nodeName), nil
@@ -638,11 +638,11 @@ func (p *CRIProvider) GetContainerLogs(ctx context.Context, namespace, podName,
pod := p.findPodByName(namespace, podName) pod := p.findPodByName(namespace, podName)
if pod == nil { if pod == nil {
return nil, strongerrors.NotFound(fmt.Errorf("Pod %s in namespace %s not found", podName, namespace)) return nil, errdefs.NotFoundf("Pod %s in namespace %s not found", podName, namespace)
} }
container := pod.containers[containerName] container := pod.containers[containerName]
if container == nil { if container == nil {
return nil, strongerrors.NotFound(fmt.Errorf("Cannot find container %s in pod %s namespace %s", containerName, podName, namespace)) return nil, errdefs.NotFoundf("Cannot find container %s in pod %s namespace %s", containerName, podName, namespace)
} }
return readLogFile(container.LogPath, opts) return readLogFile(container.LogPath, opts)
@@ -686,7 +686,7 @@ func (p *CRIProvider) GetPodStatus(ctx context.Context, namespace, name string)
pod := p.findPodByName(namespace, name) pod := p.findPodByName(namespace, name)
if pod == nil { if pod == nil {
return nil, strongerrors.NotFound(fmt.Errorf("Pod %s in namespace %s could not be found on the node", name, namespace)) return nil, errdefs.NotFoundf("Pod %s in namespace %s could not be found on the node", name, namespace)
} }
return createPodStatusFromCRI(pod), nil return createPodStatusFromCRI(pod), nil

View File

@@ -15,7 +15,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers/huawei/auth" "github.com/virtual-kubelet/virtual-kubelet/providers/huawei/auth"
"github.com/virtual-kubelet/virtual-kubelet/vkubelet/api" "github.com/virtual-kubelet/virtual-kubelet/vkubelet/api"
@@ -257,7 +257,7 @@ func errorFromResponse(resp *http.Response) error {
switch resp.StatusCode { switch resp.StatusCode {
case http.StatusNotFound: case http.StatusNotFound:
return strongerrors.NotFound(err) return errdefs.AsNotFound(err)
default: default:
return err return err
} }

View File

@@ -10,8 +10,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/cpuguy83/strongerrors/status/ocstatus" "github.com/cpuguy83/strongerrors/status/ocstatus"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/trace" "github.com/virtual-kubelet/virtual-kubelet/trace"
"github.com/virtual-kubelet/virtual-kubelet/vkubelet/api" "github.com/virtual-kubelet/virtual-kubelet/vkubelet/api"
@@ -189,7 +189,7 @@ func (p *MockProvider) DeletePod(ctx context.Context, pod *v1.Pod) (err error) {
} }
if _, exists := p.pods[key]; !exists { if _, exists := p.pods[key]; !exists {
return strongerrors.NotFound(fmt.Errorf("pod not found")) return errdefs.NotFound("pod not found")
} }
delete(p.pods, key) delete(p.pods, key)
@@ -218,7 +218,7 @@ func (p *MockProvider) GetPod(ctx context.Context, namespace, name string) (pod
if pod, ok := p.pods[key]; ok { if pod, ok := p.pods[key]; ok {
return pod, nil return pod, nil
} }
return nil, strongerrors.NotFound(fmt.Errorf("pod \"%s/%s\" is not known to the provider", namespace, name)) return nil, errdefs.NotFoundf("pod \"%s/%s\" is not known to the provider", namespace, name)
} }
// GetContainerLogs retrieves the logs of a container by name from the provider. // GetContainerLogs retrieves the logs of a container by name from the provider.

View File

@@ -3,8 +3,7 @@ package register
import ( import (
"sort" "sort"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers" "github.com/virtual-kubelet/virtual-kubelet/providers"
) )
@@ -27,7 +26,7 @@ type initFunc func(InitConfig) (providers.Provider, error)
func GetProvider(name string, cfg InitConfig) (providers.Provider, error) { func GetProvider(name string, cfg InitConfig) (providers.Provider, error) {
f, ok := providerInits[name] f, ok := providerInits[name]
if !ok { if !ok {
return nil, strongerrors.NotFound(errors.Errorf("provider not found: %s", name)) return nil, errdefs.NotFoundf("provider not found: %s", name)
} }
return f(cfg) return f(cfg)
} }

View File

@@ -29,7 +29,7 @@ import (
"time" "time"
"github.com/cenkalti/backoff" "github.com/cenkalti/backoff"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/vkubelet/api" "github.com/virtual-kubelet/virtual-kubelet/vkubelet/api"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
) )
@@ -292,7 +292,7 @@ func checkResponseStatus(r *http.Response, err error) error {
if r.StatusCode < 200 || r.StatusCode > 299 { if r.StatusCode < 200 || r.StatusCode > 299 {
switch r.StatusCode { switch r.StatusCode {
case http.StatusNotFound: case http.StatusNotFound:
return strongerrors.NotFound(errors.New(r.Status)) return errdefs.NotFound(r.Status)
default: default:
return errors.New(r.Status) return errors.New(r.Status)
} }

View File

@@ -6,8 +6,7 @@ import (
"os" "os"
"contrib.go.opencensus.io/exporter/ocagent" "contrib.go.opencensus.io/exporter/ocagent"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/pkg/errors"
"go.opencensus.io/trace" "go.opencensus.io/trace"
) )
@@ -22,7 +21,7 @@ func NewOCAgentExporter(opts TracingExporterOptions) (trace.Exporter, error) {
if endpoint := os.Getenv("OCAGENT_ENDPOINT"); endpoint != "" { if endpoint := os.Getenv("OCAGENT_ENDPOINT"); endpoint != "" {
agentOpts = append(agentOpts, ocagent.WithAddress(endpoint)) agentOpts = append(agentOpts, ocagent.WithAddress(endpoint))
} else { } else {
return nil, strongerrors.InvalidArgument(errors.New("must set endpoint address in OCAGENT_ENDPOINT")) return nil, errdefs.InvalidInput("must set endpoint address in OCAGENT_ENDPOINT")
} }
switch os.Getenv("OCAGENT_INSECURE") { switch os.Getenv("OCAGENT_INSECURE") {
@@ -30,7 +29,7 @@ func NewOCAgentExporter(opts TracingExporterOptions) (trace.Exporter, error) {
case "1", "yes", "y", "on": case "1", "yes", "y", "on":
agentOpts = append(agentOpts, ocagent.WithInsecure()) agentOpts = append(agentOpts, ocagent.WithInsecure())
default: default:
return nil, strongerrors.InvalidArgument(errors.New("invalid value for OCAGENT_INSECURE")) return nil, errdefs.InvalidInput("invalid value for OCAGENT_INSECURE")
} }
return ocagent.NewExporter(agentOpts...) return ocagent.NewExporter(agentOpts...)

View File

@@ -1,8 +1,7 @@
package opencensus package opencensus
import ( import (
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/pkg/errors"
"go.opencensus.io/trace" "go.opencensus.io/trace"
) )
@@ -30,7 +29,7 @@ func RegisterTracingExporter(name string, f TracingExporterInitFunc) {
func GetTracingExporter(name string, opts TracingExporterOptions) (trace.Exporter, error) { func GetTracingExporter(name string, opts TracingExporterOptions) (trace.Exporter, error) {
f, ok := tracingExporters[name] f, ok := tracingExporters[name]
if !ok { if !ok {
return nil, strongerrors.NotFound(errors.Errorf("tracing exporter %q not found", name)) return nil, errdefs.NotFoundf("tracing exporter %q not found", name)
} }
return f(opts) return f(opts)
} }

View File

@@ -3,8 +3,7 @@ package opencensus
import ( import (
"testing" "testing"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"go.opencensus.io/trace" "go.opencensus.io/trace"
) )
@@ -16,7 +15,7 @@ func TestGetTracingExporter(t *testing.T) {
} }
_, err := GetTracingExporter("notexist", TracingExporterOptions{}) _, err := GetTracingExporter("notexist", TracingExporterOptions{})
if !strongerrors.IsNotFound(err) { if !errdefs.IsNotFound(err) {
t.Fatalf("expected not found error, got: %v", err) t.Fatalf("expected not found error, got: %v", err)
} }

View File

@@ -7,9 +7,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
remoteutils "k8s.io/client-go/tools/remotecommand" remoteutils "k8s.io/client-go/tools/remotecommand"
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
@@ -56,7 +56,7 @@ func HandleContainerExec(h ContainerExecHandlerFunc) http.HandlerFunc {
streamOpts, err := getExecOptions(req) streamOpts, err := getExecOptions(req)
if err != nil { if err != nil {
return strongerrors.InvalidArgument(err) return errdefs.AsInvalidInput(err)
} }
idleTimeout := time.Second * 30 idleTimeout := time.Second * 30

View File

@@ -4,7 +4,7 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/cpuguy83/strongerrors/status" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
) )
@@ -17,7 +17,7 @@ func handleError(f handlerFunc) http.HandlerFunc {
return return
} }
code, _ := status.HTTPCode(err) code := httpStatusCode(err)
w.WriteHeader(code) w.WriteHeader(code)
io.WriteString(w, err.Error()) io.WriteString(w, err.Error())
logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code) logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code)
@@ -55,3 +55,16 @@ func (fw *flushWriter) Write(p []byte) (int, error) {
} }
return n, err return n, err
} }
func httpStatusCode(err error) int {
switch {
case err == nil:
return http.StatusOK
case errdefs.IsNotFound(err):
return http.StatusNotFound
case errdefs.IsInvalidInput(err):
return http.StatusBadRequest
default:
return http.StatusInternalServerError
}
}

View File

@@ -7,9 +7,9 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
) )
@@ -33,7 +33,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
return handleError(func(w http.ResponseWriter, req *http.Request) error { return handleError(func(w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(req) vars := mux.Vars(req)
if len(vars) != 3 { if len(vars) != 3 {
return strongerrors.NotFound(errors.New("not found")) return errdefs.NotFound("not found")
} }
ctx := req.Context() ctx := req.Context()
@@ -47,7 +47,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
if queryTail := q.Get("tailLines"); queryTail != "" { if queryTail := q.Get("tailLines"); queryTail != "" {
t, err := strconv.Atoi(queryTail) t, err := strconv.Atoi(queryTail)
if err != nil { if err != nil {
return strongerrors.InvalidArgument(errors.Wrap(err, "could not parse \"tailLines\"")) return errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"tailLines\""))
} }
tail = t tail = t
} }
@@ -73,7 +73,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
} }
if _, err := io.Copy(flushOnWrite(w), logs); err != nil { if _, err := io.Copy(flushOnWrite(w), logs); err != nil {
return strongerrors.Unknown(errors.Wrap(err, "error writing response to client")) return errors.Wrap(err, "error writing response to client")
} }
return nil return nil
}) })

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"net/http" "net/http"
"github.com/cpuguy83/strongerrors"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@@ -36,13 +35,13 @@ func HandleRunningPods(getPods PodListerFunc) http.HandlerFunc {
codec := codecs.LegacyCodec(v1.SchemeGroupVersion) codec := codecs.LegacyCodec(v1.SchemeGroupVersion)
data, err := runtime.Encode(codec, podList) data, err := runtime.Encode(codec, podList)
if err != nil { if err != nil {
return strongerrors.System(err) return err
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data) _, err = w.Write(data)
if err != nil { if err != nil {
return strongerrors.System(err) return err
} }
return nil return nil
}) })

View File

@@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors" "github.com/pkg/errors"
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
) )
@@ -21,20 +20,36 @@ func HandlePodStatsSummary(h PodStatsSummaryHandlerFunc) http.HandlerFunc {
return handleError(func(w http.ResponseWriter, req *http.Request) error { return handleError(func(w http.ResponseWriter, req *http.Request) error {
stats, err := h(req.Context()) stats, err := h(req.Context())
if err != nil { if err != nil {
if errors.Cause(err) == context.Canceled { if isCancelled(err) {
return strongerrors.Cancelled(err) return err
} }
return errors.Wrap(err, "error getting status from provider") return errors.Wrap(err, "error getting status from provider")
} }
b, err := json.Marshal(stats) b, err := json.Marshal(stats)
if err != nil { if err != nil {
return strongerrors.Unknown(errors.Wrap(err, "error marshalling stats")) return errors.Wrap(err, "error marshalling stats")
} }
if _, err := w.Write(b); err != nil { if _, err := w.Write(b); err != nil {
return strongerrors.Unknown(errors.Wrap(err, "could not write to client")) return errors.Wrap(err, "could not write to client")
} }
return nil return nil
}) })
} }
func isCancelled(err error) bool {
if err == context.Canceled {
return true
}
if e, ok := err.(causal); ok {
return isCancelled(e.Cause())
}
return false
}
type causal interface {
Cause() error
error
}

View File

@@ -5,8 +5,7 @@ import (
"path" "path"
"testing" "testing"
"github.com/cpuguy83/strongerrors" "github.com/virtual-kubelet/virtual-kubelet/errdefs"
pkgerrors "github.com/pkg/errors"
testutil "github.com/virtual-kubelet/virtual-kubelet/test/util" testutil "github.com/virtual-kubelet/virtual-kubelet/test/util"
"gotest.tools/assert" "gotest.tools/assert"
is "gotest.tools/assert/cmp" is "gotest.tools/assert/cmp"
@@ -37,7 +36,7 @@ func (m *mockProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) error {
func (m *mockProvider) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error) { func (m *mockProvider) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error) {
p := m.pods[path.Join(namespace, name)] p := m.pods[path.Join(namespace, name)]
if p == nil { if p == nil {
return nil, strongerrors.NotFound(pkgerrors.New("not found")) return nil, errdefs.NotFound("not found")
} }
return p, nil return p, nil
} }
@@ -45,7 +44,7 @@ func (m *mockProvider) GetPod(ctx context.Context, namespace, name string) (*cor
func (m *mockProvider) GetPodStatus(ctx context.Context, namespace, name string) (*corev1.PodStatus, error) { func (m *mockProvider) GetPodStatus(ctx context.Context, namespace, name string) (*corev1.PodStatus, error) {
p := m.pods[path.Join(namespace, name)] p := m.pods[path.Join(namespace, name)]
if p == nil { if p == nil {
return nil, strongerrors.NotFound(pkgerrors.New("not found")) return nil, errdefs.NotFound("not found")
} }
return &p.Status, nil return &p.Status, nil
} }

View File

@@ -22,9 +22,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/cpuguy83/strongerrors"
"github.com/cpuguy83/strongerrors/status/ocstatus" "github.com/cpuguy83/strongerrors/status/ocstatus"
pkgerrors "github.com/pkg/errors" pkgerrors "github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/manager" "github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/trace" "github.com/virtual-kubelet/virtual-kubelet/trace"
@@ -117,13 +117,13 @@ type PodControllerConfig struct {
func NewPodController(cfg PodControllerConfig) (*PodController, error) { func NewPodController(cfg PodControllerConfig) (*PodController, error) {
if cfg.PodClient == nil { if cfg.PodClient == nil {
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set core client")) return nil, errdefs.InvalidInput("must set core client")
} }
if cfg.EventRecorder == nil { if cfg.EventRecorder == nil {
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set event recorder")) return nil, errdefs.InvalidInput("must set event recorder")
} }
if cfg.PodInformer == nil { if cfg.PodInformer == nil {
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set informer")) return nil, errdefs.InvalidInput("must set informer")
} }
return &PodController{ return &PodController{