Skip to content

Commit

Permalink
fix: map[any]any normalize
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 2, 2024
1 parent 54a46bf commit 3912d8a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
8 changes: 6 additions & 2 deletions examples/resources/basic/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ spec:
target: Resources
source: |
{
apiVersion: "example.org/v1"
kind: "Generated"
apiVersion: "ec2.aws.upbound.io/v1beta1"
kind: "Instance"
metadata.name = "instance"
spec.forProvider.ami: "ami-0d9858aa3c6322f73"
spec.forProvider.instanceType: "t2.micro"
spec.forProvider.region: "us-east-2"
}
7 changes: 6 additions & 1 deletion fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Fatal(rsp, errors.Wrap(err, "failed to run kcl function pipelines"))
return rsp, nil
}

log.Debug(fmt.Sprintf("Pipeline output: %v", outputBytes.String()))
data, err := pkgresource.DataResourcesFromYaml(outputBytes.Bytes())
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot parse data resources from the pipeline output in %T", rsp))
return rsp, nil
}
log.Debug(fmt.Sprintf("Pipeline data: %v", data))

var resources pkgresource.ResourceList
for _, r := range in.Spec.Resources {
Expand All @@ -137,6 +138,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
Base: *base,
})
}
log.Debug(fmt.Sprintf("Input resources: %v", resources))
result, err := pkgresource.ProcessResources(dxr, oxr, desired, observed, in.Spec.Target, resources, &pkgresource.AddResourcesOptions{
Basename: in.Name,
Data: data,
Expand All @@ -153,6 +155,9 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Fatal(rsp, errors.Wrapf(err, "cannot set desired composite resource in %T", rsp))
return rsp, nil
}
for n, d := range desired {
log.Debug(fmt.Sprintf("Setting DesiredComposed state to %+v named %s", d.Resource, n))
}
if err := response.SetDesiredComposedResources(rsp, desired); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set desired composed resources in %T", rsp))
return rsp, nil
Expand Down
42 changes: 39 additions & 3 deletions pkg/resource/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,57 @@ func ObjToRawExtension(obj interface{}) (runtime.RawExtension, error) {
return runtime.RawExtension{Raw: raw}, nil
}

func normalizeMap(input interface{}) (interface{}, error) {
switch in := input.(type) {
case map[interface{}]interface{}:
normalized := make(map[string]interface{})
for key, value := range in {
strKey, ok := key.(string)
if !ok {
return nil, fmt.Errorf("found non-string key in the map")
}
normalizedValue, err := normalizeMap(value)
if err != nil {
return nil, err
}
normalized[strKey] = normalizedValue
}
return normalized, nil
case []interface{}:
for i, v := range in {
normalizedValue, err := normalizeMap(v)
if err != nil {
return nil, err
}
in[i] = normalizedValue
}
return in, nil
default:
return input, nil
}
}

// DataResourcesFromYaml returns the manifests list from the YAML stream data.
func DataResourcesFromYaml(in []byte) (result []unstructured.Unstructured, err error) {
bytes, err := krmyaml.SplitDocuments(string(in))
if err != nil {
return
}
for _, b := range bytes {
var data map[string]interface{}
var data interface{}
err = yaml.Unmarshal([]byte(b), &data)
if err != nil {
return
}

// Convert map[any]any to map[string]any
normalizedData, err := normalizeMap(data)
if err != nil {
return nil, err
}

result = append(result, unstructured.Unstructured{
Object: data,
Object: normalizedData.(map[string]interface{}),
})
}
return
Expand Down Expand Up @@ -190,7 +227,6 @@ func AddResourcesTo(o any, opts *AddResourcesOptions) error {
desired := val
for _, d := range opts.Data {
name := resource.Name(d.GetName())

// Add the resource name as a suffix to the Basename
// if there are multiple resources to add
if len(opts.Data) > 1 {
Expand Down

0 comments on commit 3912d8a

Please sign in to comment.