Merge pull request #70 from crobby/exposewarning

Expose WarningBuffer type for external use
This commit is contained in:
Chad Roberts 2023-01-03 13:00:00 -05:00 committed by GitHub
commit f4338dd839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,15 +61,12 @@ type ClientGetter interface {
TableAdminClientForWatch(ctx *types.APIRequest, schema *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error) TableAdminClientForWatch(ctx *types.APIRequest, schema *types.APISchema, namespace string, warningHandler rest.WarningHandler) (dynamic.ResourceInterface, error)
} }
type warningBuffer struct { // WarningBuffer holds warnings that may be returned from the kubernetes api
Warnings []types.Warning type WarningBuffer []types.Warning
}
func (w *warningBuffer) HandleWarningHeader(code int, agent string, text string) { // HandleWarningHeader takes the components of a kubernetes warning header and stores them
if w.Warnings == nil { func (w *WarningBuffer) HandleWarningHeader(code int, agent string, text string) {
w.Warnings = []types.Warning{} *w = append(*w, types.Warning{
}
w.Warnings = append(w.Warnings, types.Warning{
Code: code, Code: code,
Agent: agent, Agent: agent,
Text: text, Text: text,
@ -115,8 +112,8 @@ func decodeParams(apiOp *types.APIRequest, target runtime.Object) error {
} }
func (s *Store) byID(apiOp *types.APIRequest, schema *types.APISchema, namespace, id string) (*unstructured.Unstructured, []types.Warning, error) { func (s *Store) byID(apiOp *types.APIRequest, schema *types.APISchema, namespace, id string) (*unstructured.Unstructured, []types.Warning, error) {
buffer := &warningBuffer{} buffer := WarningBuffer{}
k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, namespace, buffer)) k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, namespace, &buffer))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -128,7 +125,7 @@ func (s *Store) byID(apiOp *types.APIRequest, schema *types.APISchema, namespace
obj, err := k8sClient.Get(apiOp, id, opts) obj, err := k8sClient.Get(apiOp, id, opts)
rowToObject(obj) rowToObject(obj)
return obj, buffer.Warnings, err return obj, buffer, err
} }
func moveFromUnderscore(obj map[string]interface{}) map[string]interface{} { func moveFromUnderscore(obj map[string]interface{}) map[string]interface{} {
@ -208,8 +205,8 @@ func (s *Store) ByNames(apiOp *types.APIRequest, schema *types.APISchema, names
// this as an invalid situation instead of listing all objects in the cluster and filtering by name. // this as an invalid situation instead of listing all objects in the cluster and filtering by name.
return nil, nil, nil return nil, nil, nil
} }
buffer := &warningBuffer{} buffer := WarningBuffer{}
adminClient, err := s.clientGetter.TableAdminClient(apiOp, schema, apiOp.Namespace, buffer) adminClient, err := s.clientGetter.TableAdminClient(apiOp, schema, apiOp.Namespace, &buffer)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -227,18 +224,18 @@ func (s *Store) ByNames(apiOp *types.APIRequest, schema *types.APISchema, names
} }
objs.Items = filtered objs.Items = filtered
return objs, buffer.Warnings, nil return objs, buffer, nil
} }
// List returns an unstructured list of resources. // List returns an unstructured list of resources.
func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (*unstructured.UnstructuredList, []types.Warning, error) { func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (*unstructured.UnstructuredList, []types.Warning, error) {
buffer := &warningBuffer{} buffer := WarningBuffer{}
client, err := s.clientGetter.TableClient(apiOp, schema, apiOp.Namespace, buffer) client, err := s.clientGetter.TableClient(apiOp, schema, apiOp.Namespace, &buffer)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
result, err := s.list(apiOp, schema, client) result, err := s.list(apiOp, schema, client)
return result, buffer.Warnings, err return result, buffer, err
} }
func (s *Store) list(apiOp *types.APIRequest, schema *types.APISchema, client dynamic.ResourceInterface) (*unstructured.UnstructuredList, error) { func (s *Store) list(apiOp *types.APIRequest, schema *types.APISchema, client dynamic.ResourceInterface) (*unstructured.UnstructuredList, error) {
@ -350,7 +347,7 @@ func (s *Store) listAndWatch(apiOp *types.APIRequest, client dynamic.ResourceInt
// With this filter, the request can be performed successfully, and only the allowed resources will // With this filter, the request can be performed successfully, and only the allowed resources will
// be returned in watch. // be returned in watch.
func (s *Store) WatchNames(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest, names sets.String) (chan watch.Event, error) { func (s *Store) WatchNames(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest, names sets.String) (chan watch.Event, error) {
buffer := &warningBuffer{} buffer := &WarningBuffer{}
adminClient, err := s.clientGetter.TableAdminClientForWatch(apiOp, schema, apiOp.Namespace, buffer) adminClient, err := s.clientGetter.TableAdminClientForWatch(apiOp, schema, apiOp.Namespace, buffer)
if err != nil { if err != nil {
return nil, err return nil, err
@ -380,7 +377,7 @@ func (s *Store) WatchNames(apiOp *types.APIRequest, schema *types.APISchema, w t
// Watch returns a channel of events for a list or resource. // Watch returns a channel of events for a list or resource.
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest) (chan watch.Event, error) { func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest) (chan watch.Event, error) {
buffer := &warningBuffer{} buffer := &WarningBuffer{}
client, err := s.clientGetter.TableClientForWatch(apiOp, schema, apiOp.Namespace, buffer) client, err := s.clientGetter.TableClientForWatch(apiOp, schema, apiOp.Namespace, buffer)
if err != nil { if err != nil {
return nil, err return nil, err
@ -423,8 +420,8 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, params
gvk := attributes.GVK(schema) gvk := attributes.GVK(schema)
input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind() input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind()
buffer := &warningBuffer{} buffer := WarningBuffer{}
k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, ns, buffer)) k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, ns, &buffer))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -436,7 +433,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, params
resp, err = k8sClient.Create(apiOp, &unstructured.Unstructured{Object: input}, opts) resp, err = k8sClient.Create(apiOp, &unstructured.Unstructured{Object: input}, opts)
rowToObject(resp) rowToObject(resp)
return resp, buffer.Warnings, err return resp, buffer, err
} }
// Update updates a single object in the store. // Update updates a single object in the store.
@ -447,8 +444,8 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, params
) )
ns := types.Namespace(input) ns := types.Namespace(input)
buffer := &warningBuffer{} buffer := WarningBuffer{}
k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, ns, buffer)) k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, ns, &buffer))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -486,7 +483,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, params
return nil, nil, err return nil, nil, err
} }
return resp, buffer.Warnings, nil return resp, buffer, nil
} }
resourceVersion := input.String("metadata", "resourceVersion") resourceVersion := input.String("metadata", "resourceVersion")
@ -505,7 +502,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, params
} }
rowToObject(resp) rowToObject(resp)
return resp, buffer.Warnings, nil return resp, buffer, nil
} }
// Delete deletes an object from a store. // Delete deletes an object from a store.
@ -515,8 +512,8 @@ func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id stri
return nil, nil, nil return nil, nil, nil
} }
buffer := &warningBuffer{} buffer := WarningBuffer{}
k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, apiOp.Namespace, buffer)) k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, apiOp.Namespace, &buffer))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -532,5 +529,5 @@ func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id stri
Status: http.StatusNoContent, Status: http.StatusNoContent,
} }
} }
return obj, buffer.Warnings, nil return obj, buffer, nil
} }