extractResourceData builds a resource map with metadata and spec fields.
(r resource.Resource)
| 2067 | |
| 2068 | // extractResourceData builds a resource map with metadata and spec fields. |
| 2069 | func extractResourceData(r resource.Resource) (map[string]any, error) { |
| 2070 | res := make(map[string]any) |
| 2071 | |
| 2072 | // Extract metadata directly from resource methods |
| 2073 | rmd := r.Metadata() |
| 2074 | metadata := map[string]any{ |
| 2075 | cosiMetaKeyNamespace: rmd.Namespace(), |
| 2076 | cosiMetaKeyType: rmd.Type(), |
| 2077 | cosiMetaKeyID: rmd.ID(), |
| 2078 | cosiMetaKeyVersion: rmd.Version().String(), |
| 2079 | cosiMetaKeyPhase: rmd.Phase().String(), |
| 2080 | cosiMetaKeyOwner: rmd.Owner(), |
| 2081 | } |
| 2082 | |
| 2083 | res["metadata"] = metadata |
| 2084 | |
| 2085 | // extract spec |
| 2086 | val := reflect.ValueOf(r.Spec()) |
| 2087 | if val.Kind() == reflect.Pointer { |
| 2088 | val = val.Elem() |
| 2089 | } |
| 2090 | |
| 2091 | if val.Kind() != reflect.Struct { |
| 2092 | return res, nil |
| 2093 | } |
| 2094 | |
| 2095 | yamlField := val.FieldByName("yaml") |
| 2096 | if !yamlField.IsValid() { |
| 2097 | return res, errors.New("field 'yaml' not found") |
| 2098 | } |
| 2099 | |
| 2100 | yamlValue := readUnexportedField(yamlField) |
| 2101 | |
| 2102 | yamlString, ok := yamlValue.(string) |
| 2103 | if !ok { |
| 2104 | //nolint:wrapcheck // cockroachdb/errors.Newf produces a stable typed error; wrapcheck's default ignore-sigs cover .New() but not .Newf(). |
| 2105 | return res, errors.Newf("field 'yaml' is not a string (got %T)", yamlValue) |
| 2106 | } |
| 2107 | |
| 2108 | var unmarshalledData any |
| 2109 | |
| 2110 | err := yaml.Unmarshal([]byte(yamlString), &unmarshalledData) |
| 2111 | if err != nil { |
| 2112 | return res, errors.Wrap(err, "unmarshaling yaml") |
| 2113 | } |
| 2114 | |
| 2115 | res["spec"] = unmarshalledData |
| 2116 | |
| 2117 | return res, nil |
| 2118 | } |
| 2119 | |
| 2120 | // newLookupFunction returns the implementation of the chart `lookup` |
| 2121 | // template function, dispatching across COSI resource kinds and emitting |