pyrra
pyrra copied to clipboard
Support for multiple SLOs in local filesystem
In the context of running locally, storing the SLO definitions in a file it seems I currently have to create one file for each SLO that I want to define. IOW: I'd love to see support for multiple ServiceLevelObjective
resources separated by ---
(like the case in the Kubernetes API) for the local filesystem implementation.
I've looked into this. It seems we need to use the other YAML libs to unmarshal. However, with that one, I couldn't make the multi stream files work yet. I'll look into it again, when time permits.
Had to do this exact thing, hacky patch I came up with involved unmarshalling and marshalling each YAML document before calling yaml.UnmarshalStrict
.
func objectivesFromFile(file string) ([]v1alpha1.ServiceLevelObjective, error) {
b, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", file, err)
}
var objectives []v1alpha1.ServiceLevelObjective
dec := yaml2.NewDecoder(bytes.NewReader(b))
dec.SetStrict(true)
for {
var yamlObj any
if err := dec.Decode(&yamlObj); err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("failed to unmarshal objective for roundtrip %q: %w", file, err)
}
buf, err := yaml2.Marshal(yamlObj)
if err != nil {
return nil, fmt.Errorf("failed to marshal objective for roundtrip %q: %w", file, err)
}
var objective v1alpha1.ServiceLevelObjective
if err := yaml.UnmarshalStrict(buf, &objective); err != nil {
return nil, fmt.Errorf("failed to unmarshal objective %q: %w", file, err)
}
objectives = append(objectives, objective)
}
return objectives, nil
}
yaml2
is just sigs.k8s.io/yaml/goyaml.v2