| 106 | } |
| 107 | |
| 108 | func ParseObject(object runtime.Object, defaultNamespace string, excludedHooks ...string) (*MappingResult, string, error) { |
| 109 | json, _ := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(object) |
| 110 | var objectMap map[string]interface{} |
| 111 | err := jsoniter.Unmarshal(json, &objectMap) |
| 112 | if err != nil { |
| 113 | return nil, "", fmt.Errorf("could not unmarshal byte sequence: %w", err) |
| 114 | } |
| 115 | |
| 116 | metadata := objectMap["metadata"].(map[string]interface{}) |
| 117 | var oldRelease string |
| 118 | if a := metadata["annotations"]; a != nil { |
| 119 | annotations := a.(map[string]interface{}) |
| 120 | if releaseNs, ok := annotations["meta.helm.sh/release-namespace"].(string); ok { |
| 121 | oldRelease += releaseNs + "/" |
| 122 | } |
| 123 | if releaseName, ok := annotations["meta.helm.sh/release-name"].(string); ok { |
| 124 | oldRelease += releaseName |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Clean namespace metadata as it exists in Kubernetes but not in Helm manifest |
| 129 | purgedObj, _ := deleteStatusAndTidyMetadata(json) |
| 130 | |
| 131 | content, err := yaml.Marshal(purgedObj) |
| 132 | if err != nil { |
| 133 | return nil, "", err |
| 134 | } |
| 135 | |
| 136 | result, err := parseContent(content, defaultNamespace, true, excludedHooks...) |
| 137 | if err != nil { |
| 138 | return nil, "", err |
| 139 | } |
| 140 | |
| 141 | if len(result) != 1 { |
| 142 | return nil, "", fmt.Errorf("failed to parse content of Kubernetes resource %s", metadata["name"]) |
| 143 | } |
| 144 | |
| 145 | result[0].Content = strings.TrimSuffix(result[0].Content, "\n") |
| 146 | |
| 147 | return result[0], oldRelease, nil |
| 148 | } |
| 149 | |
| 150 | func parseContent(content []byte, defaultNamespace string, normalizeManifests bool, excludedHooks ...string) ([]*MappingResult, error) { |
| 151 | var parsedMetadata metadata |