<p>I'm trying to create a k8s node struct and assign default value to it.</p>
<p>The following code complains &quot;<em>unknown field Name in struct literal</em>&quot;</p>
<pre><code>node = &amp;corev1.Node{Name: pod.Spec.NodeName}
</code></pre>
<p>However, this code is fine:</p>
<pre><code>node = &amp;corev1.Node{}
node.Name = pod.Spec.NodeName
</code></pre>
<p>why?</p>

Mike
<p>If the second code works, I'd assume there is an embedded struct, and looking at the code (assuming it's <a href="https://github.com/kubernetes/api/blob/master/core/v1/types.go#L5558-L5576" rel="nofollow noreferrer">this</a>) that appears to be the case:</p>
<pre><code>type Node struct {
    metav1.TypeMeta `json:&quot;,inline&quot;`
    // Standard object's metadata.
    // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
    // +optional
    metav1.ObjectMeta `json:&quot;metadata,omitempty&quot; protobuf:&quot;bytes,1,opt,name=metadata&quot;`

    // Spec defines the behavior of a node.
    // https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
    // +optional
    Spec NodeSpec `json:&quot;spec,omitempty&quot; protobuf:&quot;bytes,2,opt,name=spec&quot;`

    // Most recently observed status of the node.
    // Populated by the system.
    // Read-only.
    // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
    // +optional
    Status NodeStatus `json:&quot;status,omitempty&quot; protobuf:&quot;bytes,3,opt,name=status&quot;`
}
</code></pre>
<p>So the name field is likely either part of <code>metav1.TypeMeta</code>, or <code>metav1.ObjectMeta</code>, digging <a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#ObjectMeta" rel="nofollow noreferrer">deeper</a> it's on ObjectMeta, which means to define inline, you'll need to do something like:</p>
<p>package main</p>
<pre><code>import (
    corev1 &quot;k8s.io/api/core/v1&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
)

// ...

node := &amp;corev1.Node{ObjectMeta: metav1.ObjectMeta{ Name: pod.Spec.NodeName}}
</code></pre>
