Skip to content

Commit

Permalink
error pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
odra committed Aug 2, 2019
1 parent cc31f2e commit cde74fb
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 7 deletions.
14 changes: 14 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package errors

import (
"fmt"
"strings"
)

func newInvalidCRD(apiGroup string) *KNError {
parts := strings.Split(apiGroup, ".")
name := parts[0]
msg := fmt.Sprintf("no Knative %s API found on the backend. Please verify the installation.", name)

return NewKNError(msg)
}
18 changes: 18 additions & 0 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errors

import (
"gotest.tools/assert"
"testing"
)

func TestNewInvalidCRD(t *testing.T) {
err := newInvalidCRD("serving.knative.dev")
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.")

err = newInvalidCRD("serving")
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.")

err = newInvalidCRD("")
assert.Error(t, err, "no Knative API found on the backend. Please verify the installation.")

}
34 changes: 34 additions & 0 deletions pkg/errors/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package errors

import (
api_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)

func isCRDError(status api_errors.APIStatus) bool {
for _, cause := range status.Status().Details.Causes {
if strings.HasPrefix(cause.Message, "404") && cause.Type == v1.CauseTypeUnexpectedServerResponse {
return true
}
}

return false
}

func Build(err error) error {
apiStatus, ok := err.(api_errors.APIStatus)
if !ok {
return err
}

var knerr *KNError

if isCRDError(apiStatus) {
knerr = newInvalidCRD(apiStatus.Status().Details.Group)
knerr.Status = apiStatus
return knerr
}

return err
}
43 changes: 43 additions & 0 deletions pkg/errors/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package errors

import (
"github.com/pkg/errors"
"gotest.tools/assert"
api_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"testing"
)

func TestBuild(t *testing.T) {
//default non api error
defaultError := errors.New("my-custom-error")
err := Build(defaultError)
assert.Error(t, err, "my-custom-error")

gv := schema.GroupResource{
Group: "serving.knative.dev",
Resource: "service",
}

//api error containing expected error when knative crd is not available
apiError := api_errors.NewNotFound(gv, "serv")
apiError.Status().Details.Causes = []v1.StatusCause{
{
Type: "UnexpectedServerResponse",
Message: "404 page not found",
},
}
err = Build(apiError)
assert.Error(t, err, "no Knative serving API found on the backend. Please verify the installation.")

//api error not registered in error factory
apiError = api_errors.NewAlreadyExists(gv, "serv")
err = Build(apiError)
assert.Error(t, err, "service.serving.knative.dev \"serv\" already exists")

//default not found api error
apiError = api_errors.NewNotFound(gv, "serv")
err = Build(apiError)
assert.Error(t, err, "service.serving.knative.dev \"serv\" not found")
}
11 changes: 11 additions & 0 deletions pkg/errors/knerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errors

func NewKNError(msg string) *KNError {
return &KNError{
msg: msg,
}
}

func (kne *KNError) Error() string {
return kne.msg
}
22 changes: 22 additions & 0 deletions pkg/errors/knerror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package errors

import (
"gotest.tools/assert"
"testing"
)

func TestNewKNError(t *testing.T) {
err := NewKNError("myerror")
assert.Error(t, err, "myerror")

err = NewKNError("")
assert.Error(t, err, "")
}

func TestKNError_Error(t *testing.T) {
err := NewKNError("myerror")
assert.Equal(t, err.Error(), "myerror")

err = NewKNError("")
assert.Equal(t, err.Error(), "")
}
8 changes: 8 additions & 0 deletions pkg/errors/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package errors

import api_errors "k8s.io/apimachinery/pkg/api/errors"

type KNError struct {
Status api_errors.APIStatus
msg string
}
3 changes: 0 additions & 3 deletions pkg/kn/commands/service/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ func flush(out io.Writer) {
func createService(client v1alpha1.KnClient, service *serving_v1alpha1_api.Service, namespace string, out io.Writer) error {
err := client.CreateService(service)
if err != nil {
if api_errors.IsNotFound(err) {
return errors.New("knative serving is not installed on the connected backend. Please verify your Knative installation")
}
return err
}
fmt.Fprintf(out, "Service '%s' successfully created in namespace '%s'.\n", service.Name, namespace)
Expand Down
15 changes: 11 additions & 4 deletions pkg/serving/v1alpha1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"github.com/knative/client/pkg/serving"
"github.com/knative/client/pkg/wait"

kn_errors "github.com/knative/client/pkg/errors"
api_serving "github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
client_v1alpha1 "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
Expand Down Expand Up @@ -130,7 +131,7 @@ func NewKnServingClient(client client_v1alpha1.ServingV1alpha1Interface, namespa
func (cl *knClient) GetService(name string) (*v1alpha1.Service, error) {
service, err := cl.client.Services(cl.namespace).Get(name, v1.GetOptions{})
if err != nil {
return nil, err
return nil, kn_errors.Build(err)
}
err = serving.UpdateGroupVersionKind(service, v1alpha1.SchemeGroupVersion)
if err != nil {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (cl *knClient) ListServices(config ...ListConfig) (*v1alpha1.ServiceList, e
func (cl *knClient) CreateService(service *v1alpha1.Service) error {
_, err := cl.client.Services(cl.namespace).Create(service)
if err != nil {
return err
return kn_errors.Build(err)
}
return updateServingGvk(service)
}
Expand All @@ -183,10 +184,15 @@ func (cl *knClient) UpdateService(service *v1alpha1.Service) error {

// Delete a service by name
func (cl *knClient) DeleteService(serviceName string) error {
return cl.client.Services(cl.namespace).Delete(
err := cl.client.Services(cl.namespace).Delete(
serviceName,
&v1.DeleteOptions{},
)
if err != nil {
return kn_errors.Build(err)
}

return nil
}

// Wait for a service to become ready, but not longer than provided timeout
Expand Down Expand Up @@ -308,3 +314,4 @@ func serviceConditionExtractor(obj runtime.Object) (apis.Conditions, error) {
}
return apis.Conditions(service.Status.Conditions), nil
}

0 comments on commit cde74fb

Please sign in to comment.