Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(framework): assert loaded workload and resource metadata annotations are coerced #39

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions framework/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) WithWorkload(spec *
} else {
out.Workloads = maps.Clone(s.Workloads)
}

spec.Metadata = coerceAnnotationsType(spec.Metadata)
for resName, resource := range spec.Resources {
resource.Metadata = coerceAnnotationsType(resource.Metadata)
spec.Resources[resName] = resource
}

name, ok := spec.Metadata["name"].(string)
if !ok {
return nil, fmt.Errorf("metadata: name: is missing or is not a string")
Expand Down Expand Up @@ -175,6 +182,19 @@ func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) WithPrimedResources
return &out, nil
}

// The metadata maps (workload metadata and resource metadata) decode in a weird way in yaml.v3 which causes type
// coercions elsewhere in the software to fail. We have to rewrite the type to be clear.
func coerceAnnotationsType(in map[string]interface{}) map[string]interface{} {
if a, ok := in["annotations"].(score.WorkloadMetadata); ok {
in = maps.Clone(in)
in["annotations"] = map[string]interface{}(a)
} else if a, ok := in["annotations"].(score.ResourceMetadata); ok {
in = maps.Clone(in)
in["annotations"] = map[string]interface{}(a)
}
return in
}

func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) getResourceDependencies(workloadName, resName string) (map[ResourceUid]bool, error) {
outMap := make(map[ResourceUid]bool)
res := s.Workloads[workloadName].Spec.Resources[resName]
Expand Down
13 changes: 11 additions & 2 deletions framework/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestWithWorkload(t *testing.T) {
next, err := start.WithWorkload(mustLoadWorkload(t, `
metadata:
name: example
annotations:
acme.org/x: y
containers:
hello-world:
image: hi
Expand All @@ -66,7 +68,12 @@ resources:
assert.Len(t, next.Workloads, 1)
assert.Nil(t, next.Workloads["example"].File, nil)
assert.Equal(t, score.Workload{
Metadata: map[string]interface{}{"name": "example"},
Metadata: map[string]interface{}{
"name": "example",
"annotations": map[string]interface{}{
"acme.org/x": "y",
},
},
Containers: map[string]score.Container{"hello-world": {Image: "hi"}},
Resources: map[string]score.Resource{"foo": {Type: "thing"}},
}, next.Workloads["example"].Spec)
Expand Down Expand Up @@ -156,7 +163,7 @@ resources:
"thing3.apple#dog": {
Guid: "00000000-0000-0000-0000-000000000000",
Type: "thing3", Class: "apple", Id: "dog", State: map[string]interface{}{},
Metadata: map[string]interface{}{"annotations": score.ResourceMetadata{"foo": "bar"}},
Metadata: map[string]interface{}{"annotations": map[string]interface{}{"foo": "bar"}},
Params: map[string]interface{}{"color": "green"},
SourceWorkload: "example",
Outputs: map[string]interface{}{},
Expand All @@ -170,6 +177,8 @@ resources:
Outputs: map[string]interface{}{},
},
}, next.Resources)

assert.NotNil(t, next.Resources["thing3.apple#dog"].Metadata["annotations"].(map[string]interface{}))
})

t.Run("one workload - same resource - same metadata", func(t *testing.T) {
Expand Down
Loading