Skip to content

[Suggestion] Generate the YAML string where resources are separated with "---" #7011

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

Open
cmoulliard opened this issue Apr 15, 2025 · 1 comment

Comments

@cmoulliard
Copy link
Contributor

cmoulliard commented Apr 15, 2025

Is your enhancement related to a problem? Please describe

Until now if we provide a List of objects to be serialized using the class io.quarkiverse.tekton.common.utils.Serialization and the method: asYaml() we got a wrong yaml result that we cannot install on a kubernetes cluster

---
- apiVersion: "v1"
  kind: "ConfigMap"
  metadata:
    name: "my-quarkus-hello-maven-settings"
  data:
...
- apiVersion: "v1"
  kind: "ConfigMap"
  metadata:
    name: "my-quarkus-hello-maven-settings"
  data:
...

Describe the solution you'd like

Ideally, the serializer should be able to iterate through the list of the objects to trim it and add a CR as such

    public void process(List<HasMetadata> resources) {
        // Iterate through the list of the resources to process them individually to include ---
        // as separator between them instead of a list of items
        StringBuilder yamlOutput = new StringBuilder();
        for (HasMetadata resource : resources) {
            String yaml = Serialization.asYaml(resource).trim();
            yamlOutput.append(yaml).append("\n");
        }

       write(yamlOutput.toString();

By doing that, we get as result

---
apiVersion: "v1"
kind: "ConfigMap"
metadata:
  name: "my-quarkus-hello-maven-settings"
data:
  settings.xml: |
...
---
apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
  name: "my-quarkus-hello-project-pvc"
spec:
  accessModes:
  - "ReadWriteOnce"
  resources:
    requests:
      storage: "2Gi"
  volumeMode: "Filesystem"
---
etc.

Describe alternatives you've considered

No response

Additional context

No response

@cmoulliard
Copy link
Contributor Author

this is what I did in our util class

            /**
             * The array list of the kubernetes resources should be formated as such
             *
             * ---
             * manifest1
             * ---
             * manifest2
             * ...
             *
             * instead of a list as we got as error when deployed: cannot unmarshal array into Go value of type
             * unstructured.detector
             *
             * - manifest1
             * - manifest2
             * ...
             *
             */
            return ((List<HasMetadata>) object).stream()
                    .map(Serialization::writeValueAsYamlSafe)
                    .map(s -> s.replaceAll(TAG_PATTERN, BLANK))
                    .collect(Collectors.joining());

using

    private static YAMLFactory YAML_FACTORY = new YAMLFactory()
            .enable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
            .disable(YAMLGenerator.Feature.SPLIT_LINES)
            .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
            .enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS);
...

    private static <T> String writeValueAsYamlSafe(T object) {
        try {
            return YAML_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant