<p>I uses a Kubernetes application with a Service that has a single Endpoint. If using <code>curl</code> I retrieve it, I got something like this:</p>
<pre><code>{
  &quot;kind&quot;: &quot;Endpoints&quot;,
  &quot;apiVersion&quot;: &quot;v1&quot;,
  &quot;metadata&quot;: {
  ...
  &quot;subsets&quot;: [
    {
      &quot;addresses&quot;: [
        {
          &quot;ip&quot;: &quot;172.16.235.204&quot;
        }
      ],
      ...
    }
  ]
</code></pre>
<p>Now under given circumstances, my application needs to change its IP with a curl PATCH. I am using the following code:</p>
<pre><code>KUBE_SERVICE_DIR=&quot;/var/run/secrets/kubernetes.io/serviceaccount&quot;
KUBE_TOKEN_FILENAME=&quot;${KUBE_SERVICE_DIR}/token&quot;
KUBE_CA_CERT=&quot;${KUBE_SERVICE_DIR}/ca.crt&quot;
KUBE_TOKEN=$(cat ${KUBE_TOKEN_FILENAME})
BODY='[{&quot;op&quot;: &quot;replace&quot;, &quot;path&quot;: &quot;subsets/0/addresses/0/ip&quot;, &quot;value&quot;: &quot;172.16.140.18&quot;}]'
curl -XPATCH --cacert ${KUBE_CA_CERT} -H &quot;Content-Type: application/json-patch+json&quot; -H &quot;Authorization: Bearer ${KUBE_TOKEN}&quot; --data &quot;${BODY}&quot; &quot;https://${KUBERNETES_API_HOST}:${KUBERNETES_API_PORT}/api/v1/namespaces/cf-db/endpoints/cfdb-ccdb&quot;
</code></pre>
<p>As you can see my patch JSON is trying to apply a replace operation on this path <code>subsets/0/addresses/0/ip</code>setting the new IP.
But when I run it I got:</p>
<pre><code>{
  &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;the server rejected our request due to an error in our request&quot;,
  &quot;reason&quot;: &quot;Invalid&quot;,
  &quot;details&quot;: {
    
  },
  &quot;code&quot;: 422
</code></pre>
<p>Can someone help me to figure out what's wrong with my code? I tried a lot of variations using also examples on StackOverflow but no luck.</p>

Salvatore D'angelo
<p>After a lot of tries and errors, I found the solution below.</p>
<p>A solution like this is used in Patroni (but there is written in Python) where the Service has one Endpoint that always references the master node. When a failover o switchover occurs the Patroni code update the Service Endpoint. A code like this can be used whenever you have a StatefulSet and you always want to have the Service reference the master node even in a failover scenario.</p>
<pre><code>KUBE_SERVICE_DIR=&quot;/var/run/secrets/kubernetes.io/serviceaccount&quot;
KUBE_TOKEN_FILENAME=&quot;${KUBE_SERVICE_DIR}/token&quot;
KUBE_CA_CERT=&quot;${KUBE_SERVICE_DIR}/ca.crt&quot;
KUBE_TOKEN=$(cat ${KUBE_TOKEN_FILENAME})
KUBERNETES_API_HOST=${KUBERNETES_SERVICE_HOST}
KUBERNETES_API_PORT=${KUBERNETES_SERVICE_PORT}

generatePatchData()
{
  local MASTER_IP=$1
  local MASTER_PORT=$2

  cat &lt;&lt;EOF
{&quot;subsets&quot;: [{&quot;addresses&quot;: [{&quot;ip&quot;: &quot;$MASTER_IP&quot;}], &quot;ports&quot;: [{&quot;name&quot;: &quot;postgresql&quot;, &quot;port&quot;: $MASTER_PORT, &quot;protocol&quot;: &quot;TCP&quot;}]}]}
EOF
}

patchEndpointIP() {
    local MASTER_IP=$1
    local MASTER_PORT=$2
    curl -XPATCH --cacert ${KUBE_CA_CERT} -H &quot;Content-Type: application/merge-patch+json&quot; -H &quot;Authorization: Bearer ${KUBE_TOKEN}&quot; &quot;https://${KUBERNETES_API_HOST}:${K
UBERNETES_API_PORT}/api/v1/namespaces/cf-db/endpoints/cfdb-ccdb&quot; --data &quot;$(generatePatchData $MASTER_IP $MASTER_PORT)&quot;
}

getEndpointIP() {
    curl -sSk -H &quot;Authorization: Bearer ${KUBE_TOKEN}&quot; &quot;https://${KUBERNETES_API_HOST}:${KUBERNETES_API_PORT}/api/v1/namespaces/cf-db/endpoints/cfdb-ccdb&quot;
}

patchEndpointIP &quot;172.16.140.13&quot; &quot;5431&quot;
getEndpointIP
</code></pre>
