<p>I successfully snapshot my volume using python k8s client.</p>
<p>However, I got the below message.</p>
<p>I didn't have same volumesnapshot in the cluster.</p>
<p>Why this happen?</p>
<p>Code:</p>
<p>def create_snapshot(namespace, pvc_name):</p>
<pre><code>snapshot_class = &quot;snapshotclass&quot;
snapshot_name = f&quot;snapshot-{pvc_name}&quot;

snapshot_resource = {
&quot;apiVersion&quot;: &quot;snapshot.storage.k8s.io/v1beta1&quot;,
&quot;kind&quot;: &quot;VolumeSnapshot&quot;,
&quot;metadata&quot;: {&quot;name&quot;: snapshot_name},
&quot;spec&quot;: {
    &quot;volumeSnapshotClassName&quot;: snapshot_class,
    &quot;source&quot;: {&quot;persistentVolumeClaimName&quot;: pvc_name}
    }
}

res = custom_api.create_namespaced_custom_object(
    group=&quot;snapshot.storage.k8s.io&quot;,
    version=&quot;v1beta1&quot;,
    namespace= namespace,
    plural=&quot;volumesnapshots&quot;,
    body=snapshot_resource,
)

print(res)

create_snapshot(&quot;test&quot;, &quot;test-pvc&quot;)
</code></pre>
<p>The volumesnapshot is created successfully, but I got a message:</p>
<pre><code>  File &quot;/home/new/my/test/rescheduler/utils/k8s_controller.py&quot;, line 72, in create_snapshot
    body=snapshot_resource,
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/api/custom_objects_api.py&quot;, line 225, in create_namespaced_custom_object
    return self.create_namespaced_custom_object_with_http_info(group, version, namespace, plural, body, **kwargs)  # noqa: E501
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/api/custom_objects_api.py&quot;, line 358, in create_namespaced_custom_object_with_http_info
    collection_formats=collection_formats)
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/api_client.py&quot;, line 353, in call_api
    _preload_content, _request_timeout, _host)
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/api_client.py&quot;, line 184, in __call_api
    _request_timeout=_request_timeout)
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/api_client.py&quot;, line 397, in request
    body=body)
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/rest.py&quot;, line 280, in POST
    body=body)
  File &quot;/home/new/my/test/venv/lib/python3.6/site-packages/kubernetes/client/rest.py&quot;, line 233, in request
    raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (409)
Reason: Conflict
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'dec3c73a-e5fc-4c63-8d1a-6e2e6c6600e1', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'my, 25 Apr 2021 10:50:52 GMT', 'Content-Length': '346'})
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;volumesnapshots.snapshot.storage.k8s.io \&quot;snapshot-test-pvc\&quot; already exists&quot;,&quot;reason&quot;:&quot;AlreadyExists&quot;,&quot;details&quot;:{&quot;name&quot;:&quot;snapshot-test-pvc,&quot;group&quot;:&quot;snapshot.storage.k8s.io&quot;,&quot;kind&quot;:&quot;volumesnapshots&quot;},&quot;code&quot;:409}
</code></pre>

sun
<p>Posting this answer as a community wiki to give one of the possible reasons why you can encounter error <code>409</code> when trying to create the resources with above snippet of code.</p>
<p>Feel free to expand it.</p>
<hr />
<p>The error encountered in the question:</p>
<pre><code>Reason: Conflict
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'dec3c73a-e5fc-4c63-8d1a-6e2e6c6600e1', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'my, 25 Apr 2021 10:50:52 GMT', 'Content-Length': '346'})
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;volumesnapshots.snapshot.storage.k8s.io \&quot;snapshot-test-pvc\&quot; already exists&quot;,&quot;reason&quot;:&quot;AlreadyExists&quot;,&quot;details&quot;:{&quot;name&quot;:&quot;snapshot-test-pvc,&quot;group&quot;:&quot;snapshot.storage.k8s.io&quot;,&quot;kind&quot;:&quot;volumesnapshots&quot;},&quot;code&quot;:409}
</code></pre>
<blockquote>
<p><code>&quot;snapshot-test-pvc\&quot; already exists&quot;,&quot;reason&quot;:&quot;AlreadyExists&quot;</code></p>
</blockquote>
<p>states that the resource already exists in the cluster. To check if the resource exists in the cluster you can run following commands:</p>
<ul>
<li><code>$ kubectl get volumesnapshots -A</code></li>
<li><code>$ kubectl describe volumesnapshots RESOURCE_NAME -A</code></li>
</ul>
<p>I've used the code that was in the question and had no issues with it. The course of actions was following:</p>
<ul>
<li>first run - <code>VolumeSnapshot</code> created successfully</li>
<li>second run - code returned the 409 error stating that resource already exists.</li>
</ul>
<pre class="lang-sh prettyprint-override"><code>kubernetes.client.exceptions.ApiException: (409)
Reason: Conflict
&lt;-- REDACTED --&gt; 
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;volumesnapshots.snapshot.storage.k8s.io \&quot;snapshot-example-pvc\&quot; already exists&quot;,&quot;reason&quot;:&quot;AlreadyExists&quot;,&quot;details&quot;:{&quot;name&quot;:&quot;snapshot-example-pvc&quot;,&quot;group&quot;:&quot;snapshot.storage.k8s.io&quot;,&quot;kind&quot;:&quot;volumesnapshots&quot;},&quot;code&quot;:409}
</code></pre>
<blockquote>
<p>A side note!</p>
<p>Above error was returned with modified code from the question (mainly values).</p>
</blockquote>
<p>You can also see this error when trying to run <code>$ kubectl create -f resource.yaml -v=4</code> on already created resource.</p>
<hr />
<p>For anyone interested here is the <a href="https://stackoverflow.com/help/minimal-reproducible-example">minimal, reproducible example</a> of a code used in the question (it was missing <code>import</code> and the <code>def</code> was misplaced):</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config

def create_snapshot(namespace, pvc_name):

    config.load_kube_config()

    custom_api = client.CustomObjectsApi()

    snapshot_class = &quot;snapshotclass&quot;
    snapshot_name = f&quot;snapshot-{pvc_name}&quot;

    snapshot_resource = {
    &quot;apiVersion&quot;: &quot;snapshot.storage.k8s.io/v1beta1&quot;,
    &quot;kind&quot;: &quot;VolumeSnapshot&quot;,
    &quot;metadata&quot;: {&quot;name&quot;: snapshot_name},
    &quot;spec&quot;: {
        &quot;volumeSnapshotClassName&quot;: snapshot_class,
        &quot;source&quot;: {&quot;persistentVolumeClaimName&quot;: pvc_name}
        }
    }

    res = custom_api.create_namespaced_custom_object(
        group=&quot;snapshot.storage.k8s.io&quot;,
        version=&quot;v1beta1&quot;,
        namespace= namespace,
        plural=&quot;volumesnapshots&quot;,
        body=snapshot_resource,
    )

    print(res)

create_snapshot(&quot;default&quot;, &quot;test-pvc&quot;)
</code></pre>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://github.com/fabric8io/kubernetes-client/issues/544" rel="nofollow noreferrer">Github.com: Fabric8io: Kubernetes client: Issues: 409 conflict status code when create pod</a></em></li>
<li><em><a href="https://www.oreilly.com/library/view/managing-kubernetes/9781492033905/ch04.html" rel="nofollow noreferrer">Oreilly.com: Library: View: Managing Kubernetes: Chapter 04</a></em></li>
</ul>
