Openshift/Kubernetes - oc patch all elements of array

12/17/2019

I'm trying to convert an openshift service from LoadBalancer to ClusterIP using the oc patch command, which requires removing a few elements:

oc -n default patch svc router --type json -p '[\
{ "op" : "remove", "path" : "/spec/externalIPs" },\
{ "op" : "remove", "path" : "/spec/ports/0/nodePort" },\
{ "op" : "replace", "path" : "/spec/type", "value" : "ClusterIP" }]'

This works fine for services with a single .spec.ports entry, however when there are multiple, this fails to remove them all, and the patch operation fails.

Specifically, it fails in this part "op" : "remove", "path" : "/spec/ports/0/nodePort" since I'm only specifying index 0.

What would be the proper syntax to patch all elements of the .spec.ports array?

What kind of path language is this? It seems related to yamlpath but it only accepts a limited form of forward slash notation.

I've tried multiple variants, which always ends with an error message like Error from server: jsonpatch remove operation does not apply: doc is missing path: /spec/ports/*/nodePort.

One possible solution would be to explicitly remove each array index, up to some sufficiently high value, but that's awful.

oc -n default patch svc router --type json -p '[\
{ "op" : "remove", "path" : "/spec/externalIPs" },\
{ "op" : "remove", "path" : "/spec/ports/0/nodePort" },\
{ "op" : "remove", "path" : "/spec/ports/1/nodePort" },\
{ "op" : "remove", "path" : "/spec/ports/2/nodePort" },\
{ "op" : "remove", "path" : "/spec/ports/3/nodePort" },\
...
{ "op" : "remove", "path" : "/spec/ports/99/nodePort" },\
{ "op" : "replace", "path" : "/spec/type", "value" : "ClusterIP" }]'

Sample service YAML:

apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.openshift.io/password: OhsPYf5Ae3
    prometheus.openshift.io/username: admin
    service.alpha.openshift.io/serving-cert-generation-error: secret/router-certs
      references serviceUID , which does not match 9ce61b57-9f35-11e9-af68-0050569ec26d
    service.alpha.openshift.io/serving-cert-generation-error-num: "10"
    service.alpha.openshift.io/serving-cert-secret-name: router-certs
  creationTimestamp: 2019-07-05T15:00:22Z
  labels:
    bah: bah
    router: router
  name: router
  namespace: default
  resourceVersion: "56450329"
  selfLink: /api/v1/namespaces/default/services/router
  uid: 9ce61b57-9f35-11e9-af68-0050569ec26d
spec:
  clusterIP: 172.31.122.90
  externalIPs:
  - 10.66.11.44
  - 10.66.11.41
  externalTrafficPolicy: Cluster
  ports:
  - name: 80-tcp
    nodePort: 31380
    port: 80
    protocol: TCP
    targetPort: 80
  - name: 443-tcp
    nodePort: 30816
    port: 443
    protocol: TCP
    targetPort: 443
  - name: 1936-tcp
    nodePort: 32677
    port: 1936
    protocol: TCP
    targetPort: 1936
  selector:
    router: router
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: 10.66.11.41
-- André Fernandes
kubernetes
openshift
patch
yaml

0 Answers