<p>I'm looking for the go equivalent of:</p>

<pre><code>kubectl get some-custom-resource-kind some-custom-resource -o yaml &gt; file.yaml
Modify the yaml file...
kubectl apply -f file.yaml
</code></pre>

<p>Kubernetes has a client go library for <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">standard resource</a> kinds.</p>

<p>And various vendors have client go libraries for their <a href="https://github.com/openshift/client-go" rel="nofollow noreferrer">custom resources</a>.</p>

<p>But I need to get/update a resource kind that doesn't have a publicly available client go library. The logic is implemented in bash script today and I'm trying to move that function to a go controller. </p>

<p>Seems like it there should be a straightforward way in go to do the equivalent of kubectl.</p>

<p>Thanks, Paul</p>

Paul Bennett
<p>For any type, including your CRDs, use <a href="https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#Client" rel="noreferrer"><code>client.Client</code></a>. </p>

<p>From the documentation:</p>

<pre class="lang-golang prettyprint-override"><code>// Using a typed object.
pod := &amp;corev1.Pod{}
// c is a created client.
_ = c.Get(context.Background(), client.ObjectKey{
    Namespace: "namespace",
    Name:      "name",
}, pod)
pod.SetFinalizers(append(pod.GetFinalizers(), "new-finalizer"))
_ = c.Update(context.Background(), pod)

// Using a unstructured object.
u := &amp;unstructured.Unstructured{}
u.SetGroupVersionKind(schema.GroupVersionKind{
    Group:   "apps",
    Kind:    "Deployment",
    Version: "v1",
})
_ = c.Get(context.Background(), client.ObjectKey{
    Namespace: "namespace",
    Name:      "name",
}, u)
u.SetFinalizers(append(u.GetFinalizers(), "new-finalizer"))
_ = c.Update(context.Background(), u)
</code></pre>

<p>You could just as easily swap in <code>SomeCustomResourceKind</code>:</p>

<pre><code>myCR := &amp;v1alpha1.SomeCustomResourceKind{}

// c is a created client.Client
_ = c.Get(context.TODO(), client.ObjectKey{
  Namespace: "namespace",
  Name:      "some-custom-resource", }, myCR)

myCR.MyProperty = "NewValue"

_ = c.Update(context.TODO(), myCR)
</code></pre>

<p>You mentioned you're trying to move this functionality from a bash script to a Go controller, so it would be worth checking out the <a href="https://github.com/kubernetes-sigs/kubebuilder" rel="noreferrer">Kubebuilder</a> project, which can scaffold out a controller for you (and any additional APIs you might need). It creates fully functional controllers with the controller-runtime Client and wires up all the reconciliation logic to manage your CRDs. </p>
