func SortObjects(decoder runtime.Decoder, objs []runtime.Object, fieldInput string) (*RuntimeSort, error) { parser := jsonpath.New("sorting") field, err := massageJSONPath(fieldInput) if err != nil { return nil, err } if err := parser.Parse(field); err != nil { return nil, err } for ix := range objs { item := objs[ix] switch u := item.(type) { case *runtime.Unknown: var err error if objs[ix], _, err = decoder.Decode(u.Raw, nil, nil); err != nil { return nil, err } } } values, err := parser.FindResults(reflect.ValueOf(objs[0]).Elem().Interface()) if err != nil { return nil, err } if len(values) == 0 { return nil, fmt.Errorf("couldn't find any field with path: %s", field) } sorter := NewRuntimeSort(field, objs) sort.Sort(sorter) return sorter, nil }
// DecodeNestedRawExtensionOrUnknown func DecodeNestedRawExtensionOrUnknown(d runtime.Decoder, ext *runtime.RawExtension) { if ext.Raw == nil || ext.Object != nil { return } obj, gvk, err := d.Decode(ext.Raw, nil, nil) if err != nil { // TODO: record mime-type with the object unk := &runtime.Unknown{Raw: ext.Raw} if gvk != nil { unk.APIVersion = gvk.GroupVersion().String() unk.Kind = gvk.Kind } obj = unk } ext.Object = obj }
// AddObjectsFromPath loads the JSON or YAML file containing Kubernetes API resources // and adds them to the provided ObjectRetriever. func AddObjectsFromPath(path string, o ObjectRetriever, decoder runtime.Decoder) error { data, err := ioutil.ReadFile(path) if err != nil { return err } data, err = yaml.ToJSON(data) if err != nil { return err } obj, err := decoder.Decode(data) if err != nil { return err } if err := o.Add(obj); err != nil { return err } return nil }
func SortObjects(decoder runtime.Decoder, objs []runtime.Object, fieldInput string) (*RuntimeSort, error) { parser := jsonpath.New("sorting") field, err := massageJSONPath(fieldInput) if err != nil { return nil, err } if err := parser.Parse(field); err != nil { return nil, err } for ix := range objs { item := objs[ix] switch u := item.(type) { case *runtime.Unknown: var err error // decode runtime.Unknown to runtime.Unstructured for sorting. // we don't actually want the internal versions of known types. if objs[ix], _, err = decoder.Decode(u.Raw, nil, &unstructured.Unstructured{}); err != nil { return nil, err } } } var values [][]reflect.Value if unstructured, ok := objs[0].(*unstructured.Unstructured); ok { values, err = parser.FindResults(unstructured.Object) } else { values, err = parser.FindResults(reflect.ValueOf(objs[0]).Elem().Interface()) } if err != nil { return nil, err } if len(values) == 0 { return nil, fmt.Errorf("couldn't find any field with path: %s", field) } sorter := NewRuntimeSort(field, objs) sort.Sort(sorter) return sorter, nil }