-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moves conversion to unstr to dedicated package (#1113)
We rely on conversion to Unstructured resources in quite a few places in the code base. With the introduction of a dedicated pkg, we can keep those functions together and reuse them across the code base. > [!IMPORTANT] > While converting Kustomize `ResMap` we do not use `MustYaml` func anymore, as this one panics. We attempt to convert to YAML and handle the error instead.
- Loading branch information
1 parent
eee87c0
commit 447f5ad
Showing
4 changed files
with
58 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package conversion | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/kustomize/api/resmap" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
resourceSeparator = "(?m)^---[ \t]*$" | ||
) | ||
|
||
// StrToUnstructured converts a string containing multiple resources in YAML format to a slice of Unstructured objects. | ||
// The input string is split by "---" separator and each part is unmarshalled into an Unstructured object. | ||
func StrToUnstructured(resources string) ([]*unstructured.Unstructured, error) { | ||
splitter := regexp.MustCompile(resourceSeparator) | ||
objectStrings := splitter.Split(resources, -1) | ||
objs := make([]*unstructured.Unstructured, 0, len(objectStrings)) | ||
for _, str := range objectStrings { | ||
if strings.TrimSpace(str) == "" { | ||
continue | ||
} | ||
u := &unstructured.Unstructured{} | ||
if err := yaml.Unmarshal([]byte(str), u); err != nil { | ||
return nil, err | ||
} | ||
|
||
objs = append(objs, u) | ||
} | ||
return objs, nil | ||
} | ||
|
||
// ResMapToUnstructured converts a ResMap to a slice of Unstructured objects. | ||
func ResMapToUnstructured(resMap resmap.ResMap) ([]*unstructured.Unstructured, error) { | ||
resources := make([]*unstructured.Unstructured, 0, resMap.Size()) | ||
for _, res := range resMap.Resources() { | ||
u := &unstructured.Unstructured{} | ||
asYAML, errToYAML := res.AsYAML() | ||
if errToYAML != nil { | ||
return nil, errToYAML | ||
} | ||
if errUnmarshal := yaml.Unmarshal(asYAML, u); errUnmarshal != nil { | ||
return nil, errUnmarshal | ||
} | ||
resources = append(resources, u) | ||
} | ||
|
||
return resources, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters