<p>I want to define a CRD struct and generate Custom resource that has a field that can take up any value.</p>
<p>If the struct was to be something like:</p>
<pre><code>type MyStruct struct{
    MyField interface{} `json:&quot;myfield&quot;`
}
</code></pre>
<p>I would like <code>MyField</code> to store a number or a string in the CRD:</p>
<pre><code>myfield:2
</code></pre>
<p>or</p>
<pre><code>myfield:&quot;somestring&quot;
</code></pre>
<p>However I get an error:</p>
<pre><code>DeepCopy of &quot;interface{}&quot; is unsupported. Instead, use named interfaces with DeepCopy&lt;named-interface&gt; as one of the methods.
</code></pre>
<p>How do I work around this problem?</p>

Aishwarya Nagaraj
<p>The error you're encountering, &quot;DeepCopy of 'interface{}' is unsupported,&quot; is due to the fact that Kubernetes's client library (client-go) uses code generation to create DeepCopy functions for your custom resources, and it cannot generate a DeepCopy function for an interface{} field because it doesn't know what specific types may be stored in that field at runtime.</p>
<p>To work around this problem and achieve your goal of having a custom resource field that can store either a number or a string, you can use a union type approach with named interfaces. Here's how you can do it:</p>
<p>Define named interfaces for the types you want to support:</p>
<pre><code>type MyStructIntValue interface {
    IsMyStructIntValue() bool
}

type MyStructStringValue interface {
    IsMyStructStringValue() bool
}
</code></pre>
<p>Implement these interfaces for the types you want to use (int and string in your case):</p>
<pre><code>type MyIntValue int

func (i MyIntValue) IsMyStructIntValue() bool {
    return true
}

type MyStringValue string

func (s MyStringValue) IsMyStructStringValue() bool {
    return true
}
</code></pre>
<p>Modify your MyStruct definition to use the named interfaces:</p>
<pre><code>type MyStruct struct {
    MyField MyStructIntValue    `json:&quot;myfield&quot;`
    MyStringField MyStructStringValue `json:&quot;mystringfield&quot;`
}
</code></pre>
<p>When creating a custom resource, you can use either MyIntValue or MyStringValue to set the field:</p>
<pre><code>cr := MyCustomResource{
    MyField: MyIntValue(2),
    MyStringField: MyStringValue(&quot;somestring&quot;),
}
</code></pre>
<p>By using named interfaces with specific types, you provide enough information for Kubernetes's client-go to generate DeepCopy functions for your custom resource. This approach allows you to have a custom resource field that can store either a number or a string while working within the constraints of Kubernetes's code generation.</p>
