<p>I'm using the following code to get k8s config map data.</p>
<p>This code is working however I’m not sure regarding the unmarshal, is it a bit verbose, is there a robust way to achieve this?</p>
<pre><code>cm, e := c.Kubernetes.CoreV1().ConfigMaps(“ns1”).Get(“vs-manifest”, metav1.GetOptions{})
if e != nil {
    return errors.New(“error “occurred”)

}

//here I want to get the data
var cmData map[string]string
e = yaml.Unmarshal([]byte(cm.Data[&quot;cm1.yaml&quot;]), &amp;cmData)
if err != nil{
  return errors.New(“error “occurred”)
}

//here i need to read rzr field
appVersion := strings.ReplaceAll(cmData[“rzr”], &quot;.&quot;, &quot;-&quot;)
</code></pre>
<p>This is the config map <code>vs-manifest</code></p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: vs-manifest
  namespace: ns1
data:
  cm1.yaml: |
    version: 1.5
    repo: milestones
    rzr: 1.0005.044

</code></pre>

PeterSO
<p>Some modification suggestions:</p>
<pre class="lang-golang prettyprint-override"><code>// Once you have the configMap, check whether cm1.yaml file exist or not?

var cmFile string 
if value, ok:= cm.Data[&quot;cm1.yaml&quot;]; ok {
  cmFile = value
}

// while unmarshal-ing yaml files, use map[string]interface
// otherwise error may occur.
// Case:
//| a:
//|   b: value
//|   c: 
//|     d: value2

cmData := make(map[string]interface{})
err := yaml.Unmarshal([]byte(cmFile), cmData)
if err != nil {
  return errors.New(&quot;error occurred&quot;)
}

var apiVersion string
// Check whether the &quot;rzr&quot; exist or not
if value, ok := cmData[&quot;rzr&quot;]; ok {
  // convert the value from interface to string
  // using type assertion.
  stringValue, valid := value.(string)
  // if successfully converted to string
  if valid {
    apiVersion = strings.ReplaceAll(stringValue, &quot;.&quot;, &quot;-&quot;)
  } else {
    return errors.New(&quot;failed to convert&quot;)
  }

}


</code></pre>
