pyrra icon indicating copy to clipboard operation
pyrra copied to clipboard

Support for multiple SLOs in local filesystem

Open mhausenblas opened this issue 1 year ago • 2 comments

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.

mhausenblas avatar Feb 27 '23 11:02 mhausenblas

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.

metalmatze avatar Mar 14 '23 22:03 metalmatze

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

johnduhart avatar Jul 02 '24 23:07 johnduhart