Partially marshal a yaml file

Assuming I have a yaml file more or less like this:

apiVersion: flux.weave.works/v1beta1
kind: HelmRelease
metadata:
  annotations:
    flux.weave.works/ignore: 'false'
    flux.weave.works/automated: 'false'
    flux.weave.works/tag.redash: glob:*
  name: redash
  namespace: default
spec:
  chart:
    git: git@github.com:pkaramol/somerepo.git
    path: charts/myapp
    ref: mybranch
  releaseName: redash
  values:
    namespace: default
    redash-service:
      istio:
        enabled: true
      namespace: default
      image: redash/redash
      tag: 8.0.0.
      imagePullPolicy: Always
      port: 5000
      serviceType: ClusterIP
      env:
        config:
          PYTHONUNBUFFERED: '0'
          REDASH_LOG_LEVEL: 'INFO'
          REDASH_REDIS_URL: 'redis://redis:6379/0'
    redis:
      namespace: default
      istio:
        enabled: true
      replicas: 1
      image: redis
      tag: 5.0-alpine
      port: 6379
      imagePullPolicy: Always
      serviceType: ClusterIP
      resources:
        requests:
          cpu: 200m
          memory: 512Mi
        limits:
          cpu: 1000M
          memory: 1500Mi
    someNode:
    sealedsecrets:
      releaseName_hash: 12345
      anotherNode:
        item1: 
        item2: 
        item3: 
        item4: 

Is there a way to partially marshal it? i.e. assuming I am only interested in spec.values.sealedsecrets.anotherNode (which I assume I should be able to get back as an array or slice)

Is this possible?

Yes, using the yaml pacakge you don’t have to unmarshal the complete YAML. This works:

package main

import (
    "fmt"
    "gopkg.in/yaml.v2"
    "log"
)

type StructA struct {
    A string `yaml:"a"`
}

type StructB struct {
    StructA `yaml:"a"`
}

var data = `
b: some b
a:
  a: the A
  ignore: me
  c:
    nested: ignored
`

func main() {
    var b StructB

    err := yaml.Unmarshal([]byte(data), &b)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Println(b.A)
}

Output:

the A
2 Likes

Τhanks that’s what I was looking for.

For some reason can’t seem to find the check box that will allow me to accept your answer.

1 Like