diff --git a/httperror/error.go b/httperror/error.go index 91f6c738..aeed7652 100644 --- a/httperror/error.go +++ b/httperror/error.go @@ -46,6 +46,13 @@ type APIError struct { fieldName string } +func NewAPIErrorLong(status int, code, message string) error { + return NewAPIError(ErrorCode{ + code: code, + status: status, + }, message) +} + func NewAPIError(code ErrorCode, message string) error { return &APIError{ code: code, diff --git a/store/crd/crd_store.go b/store/crd/crd_store.go index 6521c9ab..efe451de 100644 --- a/store/crd/crd_store.go +++ b/store/crd/crd_store.go @@ -15,7 +15,7 @@ import ( type Store struct { Factory *Factory k8sClient rest.Interface - schemaStores map[string]*proxy.Store + schemaStores map[string]types.Store } func NewCRDStoreFromConfig(config rest.Config) (*Store, error) { @@ -44,7 +44,7 @@ func NewCRDStoreFromClients(apiExtClientSet apiextclientset.Interface, k8sClient APIExtClientSet: apiExtClientSet, }, k8sClient: k8sClient, - schemaStores: map[string]*proxy.Store{}, + schemaStores: map[string]types.Store{}, } } diff --git a/store/proxy/error_wrapper.go b/store/proxy/error_wrapper.go new file mode 100644 index 00000000..2d5d6d23 --- /dev/null +++ b/store/proxy/error_wrapper.go @@ -0,0 +1,52 @@ +package proxy + +import ( + "github.com/rancher/norman/httperror" + "github.com/rancher/norman/types" + "k8s.io/apimachinery/pkg/api/errors" +) + +type errorStore struct { + types.Store +} + +func (e *errorStore) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) { + data, err := e.Store.ByID(apiContext, schema, id) + return data, translateError(err) +} + +func (e *errorStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) { + data, err := e.Store.List(apiContext, schema, opt) + return data, translateError(err) +} + +func (e *errorStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) { + data, err := e.Store.Create(apiContext, schema, data) + return data, translateError(err) + +} + +func (e *errorStore) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) { + data, err := e.Store.Update(apiContext, schema, data, id) + return data, translateError(err) + +} + +func (e *errorStore) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) { + data, err := e.Store.Delete(apiContext, schema, id) + return data, translateError(err) + +} + +func (e *errorStore) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) { + data, err := e.Store.Watch(apiContext, schema, opt) + return data, translateError(err) +} + +func translateError(err error) error { + if apiError, ok := err.(errors.APIStatus); ok { + status := apiError.Status() + return httperror.NewAPIErrorLong(int(status.Code), string(status.Reason), status.Message) + } + return err +} diff --git a/store/proxy/proxy_store.go b/store/proxy/proxy_store.go index 5e3a8e56..48601b8b 100644 --- a/store/proxy/proxy_store.go +++ b/store/proxy/proxy_store.go @@ -40,6 +40,24 @@ type Store struct { } func NewProxyStore(k8sClient rest.Interface, + prefix []string, group, version, kind, resourcePlural string) types.Store { + return &errorStore{ + Store: &Store{ + k8sClient: k8sClient, + prefix: prefix, + group: group, + version: version, + kind: kind, + resourcePlural: resourcePlural, + authContext: map[string]string{ + "apiGroup": group, + "resource": resourcePlural, + }, + }, + } +} + +func NewRawProxyStore(k8sClient rest.Interface, prefix []string, group, version, kind, resourcePlural string) *Store { return &Store{ k8sClient: k8sClient,