<p>Using the following code:</p>

<pre class="lang-golang prettyprint-override"><code>func GetPods(clientset *kubernetes.Clientset, name, namespace string) ([]corev1.Pod, error) {
    list, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{
        LabelSelector: fmt.Sprintf("app=%s", name),
    })
    if err != nil {
        return nil, err
    }
return list.Items, nil
}
</code></pre>

<p>And then dump the results into yaml using <code>gopkg.in/yaml.v2</code>, and here's the yaml clause that describes container resource:</p>

<pre><code>resources:
  limits:
    cpu:
      format: DecimalSI
    memory:
      format: BinarySI
  requests:
    cpu:
      format: DecimalSI
    memory:
      format: BinarySI
</code></pre>

<p>Which includes none of the actual resource amount that I'm actually interested in, which should look like this using <code>kubectl get pod xxx -o yaml</code>:</p>

<pre><code>resources:
  limits:
    cpu: "4"
    memory: 8Gi
  requests:
    cpu: 200m
    memory: 100Mi
</code></pre>

<p>So how can I properly get the pod spec yaml, that includes all the resource info, using the golang library? What did I do wrong in the above process?</p>

<h2>Update</h2>

<p>I noticed the <a href="https://godoc.org/k8s.io/api/core/v1#PodSpec.String" rel="nofollow noreferrer"><code>Pod.String</code></a> and <a href="https://godoc.org/k8s.io/api/core/v1#PodSpec.Marshal" rel="nofollow noreferrer"><code>Pod.Marshal</code></a> methods.</p>

<p>The <code>pod.String()</code> output seems to be a formatted string of the <code>core.v1.Pod</code> instance, this isn't much use to me since it's not serialized.</p>

<p><code>Pod.Marshal()</code> gives a byte array, contains lots of gibberish when printed. The method itself is one of those undocumented methods inside <code>k8s.io/api/core/v1/generated.pb.go</code>, I really don't know what to do with its output:</p>

<pre class="lang-golang prettyprint-override"><code>func (p *PodResolver) SpecYaml() (string, error) {
    bs, err := p.pod.Marshal()
    fmt.Println(string(bs))
    return string(bs), err
}
// prints a whole lot of gibberish like cpu\x12\x03\n\x014\n\x0f\n\x06memory\x12\x05\n\x038Gi\x12\r\n\x03cpu\x12\x06\n\x04200m\x12\x11\n\x06memory\x12\a\n\x05100MiJ-\n\n
</code></pre>

timfeirg
<p>Try this out:</p>

<pre><code>func GetPods(clientset *kubernetes.Clientset, name, namespace string) ([]corev1.Pod, error) {
list, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{
    LabelSelector: fmt.Sprintf("app=%s", name),
})
if err != nil {
    return nil, err
}
for _, l := range list.Items {
    fmt.Println("Request CPU ==&gt; ", l.Spec.Containers[0].Resources.Requests.Cpu(), " Request Memory ==&gt; ", l.Spec.Containers[0].Resources.Requests.Memory())
    fmt.Println("Limit CPU ==&gt; ", l.Spec.Containers[0].Resources.Limits.Cpu(), " Limit Memory ==&gt; ", l.Spec.Containers[0].Resources.Limits.Memory()) } return list.Items, nil }
</code></pre>

<blockquote>
  <p>Keep in mind that every time something becomes complex it's time to
  choose another path.</p>
</blockquote>

<p>k8s APIs are not so well documented as could be, in this case my suggestion is open up the debug console and navigate through component trees which will certainly indicate which interface use and it's structure.</p>
